private-attribute-cpp 2.2.7__tar.gz → 2.2.8.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/PKG-INFO +2 -2
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/README.md +1 -1
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/private_attribute.cpp +273 -170
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/pyproject.toml +1 -1
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/CMakeLists.txt +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/LICENSE +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/picosha2.h +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/private_attribute.pyi +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_attr_classify_owner.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_class_private_attrs.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_gc_collect.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_init_subclass.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_abc.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_attrs.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_func.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_inheritance.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_method.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_wrap.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_slots_cycle.py +0 -0
- {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_use_parents.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: private_attribute_cpp
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.8.1
|
|
4
4
|
Summary: A Python package that provides a way to define private attributes in C++ implementation.
|
|
5
5
|
Author-Email: HuangHaoHua <13140752715@example.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -197,7 +197,7 @@ class MyClass(PrivateAttrBase):
|
|
|
197
197
|
decorator application. It exposes two public attributes:
|
|
198
198
|
|
|
199
199
|
- **`.result`** — the final decorated result.
|
|
200
|
-
- **`.funcs`** —
|
|
200
|
+
- **`.funcs`** — a read-only list proxy (`_listproxy`) of the original (pre-decoration) functions.
|
|
201
201
|
|
|
202
202
|
To stack decorators, resolve name conflicts, or fix decorator ordering, chain
|
|
203
203
|
`.result.<attr>` onto the wrapped method and pass the method itself as the
|
|
@@ -187,7 +187,7 @@ class MyClass(PrivateAttrBase):
|
|
|
187
187
|
decorator application. It exposes two public attributes:
|
|
188
188
|
|
|
189
189
|
- **`.result`** — the final decorated result.
|
|
190
|
-
- **`.funcs`** —
|
|
190
|
+
- **`.funcs`** — a read-only list proxy (`_listproxy`) of the original (pre-decoration) functions.
|
|
191
191
|
|
|
192
192
|
To stack decorators, resolve name conflicts, or fix decorator ordering, chain
|
|
193
193
|
`.result.<attr>` onto the wrapped method and pass the method itself as the
|
|
@@ -201,6 +201,17 @@ PyDict_ContainsString(PyObject *op, const char *key) noexcept
|
|
|
201
201
|
}
|
|
202
202
|
#endif
|
|
203
203
|
|
|
204
|
+
typedef struct PrivateAttrCreationData PrivateAttrCreationData;
|
|
205
|
+
static std::string default_random_string(uintptr_t obj_id, const std::string& attr_name) noexcept;
|
|
206
|
+
|
|
207
|
+
struct RestorePythonExceptionResult {
|
|
208
|
+
std::string value;
|
|
209
|
+
bool ok = true;
|
|
210
|
+
};
|
|
211
|
+
static RestorePythonExceptionResult custom_random_string(uintptr_t obj_id, const std::string& attr_name, PyObject* func) noexcept;
|
|
212
|
+
static bool need_analyse_type(PyObject* type) noexcept;
|
|
213
|
+
static void ensure_tp(PyTypeObject* type_instance) noexcept;
|
|
214
|
+
|
|
204
215
|
namespace {
|
|
205
216
|
namespace AllData {
|
|
206
217
|
static std::unordered_map<AllPyobjectAttrCacheKey, std::string> cache;
|
|
@@ -223,6 +234,7 @@ namespace {
|
|
|
223
234
|
static std::unordered_map<uintptr_t, PyObjectStorage> tmp_type_allowed_code_map;
|
|
224
235
|
static std::vector<PyObjectStorage> tmp_type_need_call; // use vector to avoid of the dealloc after python end
|
|
225
236
|
static std::unordered_map<std::string, PyObjectStorage> tmp_type_attr_dict;
|
|
237
|
+
static PrivateAttrCreationData *tmp_creation_data = nullptr;
|
|
226
238
|
|
|
227
239
|
static std::unordered_map<uintptr_t, std::unique_ptr<std::shared_mutex>> all_type_mutex;
|
|
228
240
|
static std::unordered_map<uintptr_t, PyObjectStorage> type_need_call;
|
|
@@ -239,6 +251,8 @@ namespace {
|
|
|
239
251
|
static std::unordered_map<uintptr_t, getattrofunc> all_type_getattro;
|
|
240
252
|
// all type tp_setattro map
|
|
241
253
|
static std::unordered_map<uintptr_t, setattrofunc> all_type_setattro;
|
|
254
|
+
// all type tp_call map
|
|
255
|
+
static std::unordered_map<uintptr_t, ternaryfunc> all_type_call;
|
|
242
256
|
// all type tp_traverse map
|
|
243
257
|
static std::unordered_map<uintptr_t, traverseproc> all_type_traverse;
|
|
244
258
|
// all type tp_clear map
|
|
@@ -264,6 +278,181 @@ namespace {
|
|
|
264
278
|
};
|
|
265
279
|
};
|
|
266
280
|
|
|
281
|
+
struct PrivateAttrCreationData
|
|
282
|
+
{
|
|
283
|
+
std::string class_name;
|
|
284
|
+
PyObjectStorage attrs_copy = (PyObject*)nullptr;
|
|
285
|
+
std::unordered_set<TwoStringTuple> private_attrs_set;
|
|
286
|
+
std::unordered_map<uintptr_t, PyObjectStorage> code_map;
|
|
287
|
+
std::unordered_set<std::string> private_attrs_vector_string;
|
|
288
|
+
std::vector<uintptr_t> all_need_analyse_base;
|
|
289
|
+
std::unordered_map<std::string, PyObjectStorage> need_remove_itself;
|
|
290
|
+
PyObjectStorage private_func = (PyObject*)nullptr;
|
|
291
|
+
PyObjectStorage base_kwds = (PyObject*)nullptr;
|
|
292
|
+
PyObjectStorage name = (PyObject*)nullptr;
|
|
293
|
+
PyObjectStorage bases = (PyObject*)nullptr;
|
|
294
|
+
bool use_parents = false;
|
|
295
|
+
bool cleared = false;
|
|
296
|
+
std::unique_lock<std::mutex> lock_;
|
|
297
|
+
bool locked = false;
|
|
298
|
+
bool installed = false;
|
|
299
|
+
PrivateAttrCreationData() noexcept : lock_(::AllData::type_creation_lock_mutex, std::defer_lock) {}
|
|
300
|
+
|
|
301
|
+
void lock() noexcept {
|
|
302
|
+
if (!locked) {
|
|
303
|
+
lock_.lock();
|
|
304
|
+
AllData::type_creation_locked = true;
|
|
305
|
+
locked = true;
|
|
306
|
+
AllData::tmp_type_allowed_code_map = code_map;
|
|
307
|
+
AllData::tmp_type_attr_set = private_attrs_set;
|
|
308
|
+
AllData::tmp_type_attr_dict = need_remove_itself;
|
|
309
|
+
AllData::tmp_creation_data = this;
|
|
310
|
+
if (private_func) {
|
|
311
|
+
::AllData::tmp_type_need_call.push_back(private_func);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
void unlock() noexcept {
|
|
317
|
+
if (locked) {
|
|
318
|
+
AllData::tmp_type_allowed_code_map.clear();
|
|
319
|
+
AllData::tmp_type_attr_set.clear();
|
|
320
|
+
::AllData::tmp_type_need_call.clear();
|
|
321
|
+
AllData::tmp_type_attr_dict.clear();
|
|
322
|
+
AllData::tmp_creation_data = nullptr;
|
|
323
|
+
AllData::type_creation_locked = false;
|
|
324
|
+
locked = false;
|
|
325
|
+
lock_.unlock();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
void clear() noexcept {
|
|
330
|
+
if (cleared) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
name = (PyObject*)nullptr;
|
|
334
|
+
bases = (PyObject*)nullptr;
|
|
335
|
+
attrs_copy = (PyObject*)nullptr;
|
|
336
|
+
private_func = (PyObject*)nullptr;
|
|
337
|
+
base_kwds = (PyObject*)nullptr;
|
|
338
|
+
|
|
339
|
+
need_remove_itself.clear();
|
|
340
|
+
code_map.clear();
|
|
341
|
+
if (locked) {
|
|
342
|
+
unlock();
|
|
343
|
+
}
|
|
344
|
+
cleared = true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
bool install(PyObject* new_type) noexcept {
|
|
348
|
+
if (installed) return true;
|
|
349
|
+
if (!new_type) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
if (!PyType_Check(new_type)) {
|
|
353
|
+
PyErr_SetString(PyExc_TypeError, "created object is not a type");
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
PyTypeObject* type_instance = (PyTypeObject*)new_type;
|
|
358
|
+
uintptr_t type_id = (uintptr_t)(type_instance);
|
|
359
|
+
int tp_dict_has_static_attr = PyDict_ContainsString(type_instance->tp_dict, "__static_attributes__");
|
|
360
|
+
if (tp_dict_has_static_attr < 0) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
if (tp_dict_has_static_attr) {
|
|
364
|
+
PyErr_Format(PyExc_SystemError,
|
|
365
|
+
"%s (%d): '__static_attributes__' will expose private attribute names and cannot be used in private attribute types", __FILE__, __LINE__);
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
::AllData::all_type_attr_set[type_id] = private_attrs_set;
|
|
369
|
+
ensure_tp(type_instance);
|
|
370
|
+
if (::AllData::type_attr_dict.find(type_id) == ::AllData::type_attr_dict.end()) {
|
|
371
|
+
::AllData::type_attr_dict[type_id] = {};
|
|
372
|
+
}
|
|
373
|
+
// iter mro and put in all_type_parent_id
|
|
374
|
+
PyObject* mro = type_instance->tp_mro;
|
|
375
|
+
Py_ssize_t mro_size = PyTuple_GET_SIZE(mro);
|
|
376
|
+
std::vector<uintptr_t> mro_vector;
|
|
377
|
+
for (Py_ssize_t i = 1; i < mro_size; i++) {
|
|
378
|
+
PyObject* item = PyTuple_GET_ITEM(mro, i);
|
|
379
|
+
if (!item || !PyType_Check(item) || !need_analyse_type(item)) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
mro_vector.push_back((uintptr_t)item);
|
|
383
|
+
}
|
|
384
|
+
::AllData::all_type_parent_id[type_id] = mro_vector;
|
|
385
|
+
::AllData::all_object_mutex[type_id].clear();
|
|
386
|
+
::AllData::all_type_mutex[type_id] = std::make_unique<std::shared_mutex>();
|
|
387
|
+
::AllData::all_object_attr[type_id] = {};
|
|
388
|
+
::AllData::all_type_subclass_attr[type_id] = {};
|
|
389
|
+
::AllData::all_type_subclass_mutex[type_id].clear();
|
|
390
|
+
::AllData::type_allowed_code_map[type_id] = code_map;
|
|
391
|
+
|
|
392
|
+
for (uintptr_t i: mro_vector) {
|
|
393
|
+
if (::AllData::all_type_subclass_attr.find(i) == ::AllData::all_type_subclass_attr.end()) {
|
|
394
|
+
::AllData::all_type_subclass_attr[i] = {};
|
|
395
|
+
}
|
|
396
|
+
if (::AllData::all_type_subclass_mutex.find(i) == ::AllData::all_type_subclass_mutex.end()) {
|
|
397
|
+
::AllData::all_type_subclass_mutex[i].clear();
|
|
398
|
+
}
|
|
399
|
+
// Do not wipe a parent's shadow already written for this class during
|
|
400
|
+
// __init_subclass__ (e.g. `cls._value = ...` inside the parent's
|
|
401
|
+
// __init_subclass__ runs inside tp_new, before postprocess).
|
|
402
|
+
if (::AllData::all_type_subclass_attr[i].find(type_id) == ::AllData::all_type_subclass_attr[i].end()) {
|
|
403
|
+
::AllData::all_type_subclass_attr[i][type_id] = {};
|
|
404
|
+
}
|
|
405
|
+
if (::AllData::all_type_subclass_mutex[i].find(type_id) == ::AllData::all_type_subclass_mutex[i].end()) {
|
|
406
|
+
::AllData::all_type_subclass_mutex[i][type_id] = std::make_unique<std::shared_mutex>();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
for (auto& [key, value]: need_remove_itself) {
|
|
411
|
+
std::string final_key;
|
|
412
|
+
if (private_func) {
|
|
413
|
+
auto private_name_result = custom_random_string(type_id, key, private_func);
|
|
414
|
+
if (!private_name_result.ok) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
final_key = private_name_result.value;
|
|
418
|
+
} else {
|
|
419
|
+
final_key = default_random_string(type_id, key);
|
|
420
|
+
}
|
|
421
|
+
::AllData::type_attr_dict[type_id][final_key] = value;
|
|
422
|
+
}
|
|
423
|
+
if (private_func) {
|
|
424
|
+
::AllData::type_need_call[type_id] = private_func;
|
|
425
|
+
}
|
|
426
|
+
unlock();
|
|
427
|
+
installed = true;
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
~PrivateAttrCreationData() noexcept {
|
|
432
|
+
clear();
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
static bool
|
|
437
|
+
try_install_pending_creation(PyObject* new_type) noexcept
|
|
438
|
+
{
|
|
439
|
+
// Finish a pending private-attribute creation if one is still
|
|
440
|
+
// outstanding. prepare()/PrivateAttrType_new leave tmp_creation_data
|
|
441
|
+
// locked (type_creation_locked == true) until install() runs; if
|
|
442
|
+
// creation is interrupted (e.g. an exception in __init_subclass__) the
|
|
443
|
+
// type exists but is not yet registered in all_type_attr_set. The
|
|
444
|
+
// metaclass getattr/setattr/call slots check this first and promote the
|
|
445
|
+
// half-created type before proceeding.
|
|
446
|
+
if (!new_type || !::AllData::tmp_creation_data) {
|
|
447
|
+
return true;;
|
|
448
|
+
}
|
|
449
|
+
if (::AllData::type_creation_locked &&
|
|
450
|
+
::AllData::all_type_attr_set.find((uintptr_t)new_type) == ::AllData::all_type_attr_set.end()) {
|
|
451
|
+
return ::AllData::tmp_creation_data->install(new_type);
|
|
452
|
+
}
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
|
|
267
456
|
static inline void
|
|
268
457
|
object_create_mutex(uintptr_t final_id, uintptr_t obj_id) noexcept
|
|
269
458
|
{
|
|
@@ -322,6 +511,7 @@ clean_all_storages() noexcept
|
|
|
322
511
|
AllData::tmp_type_need_call.clear();
|
|
323
512
|
AllData::tmp_type_attr_dict.clear();
|
|
324
513
|
AllData::type_creation_locked = false;
|
|
514
|
+
AllData::tmp_creation_data = nullptr;
|
|
325
515
|
AllData::all_type_mutex.clear();
|
|
326
516
|
AllData::type_need_call.clear();
|
|
327
517
|
AllData::all_type_attr_set.clear();
|
|
@@ -332,6 +522,7 @@ clean_all_storages() noexcept
|
|
|
332
522
|
AllData::all_type_parent_id.clear();
|
|
333
523
|
AllData::all_type_getattro.clear();
|
|
334
524
|
AllData::all_type_setattro.clear();
|
|
525
|
+
AllData::all_type_call.clear();
|
|
335
526
|
AllData::all_type_traverse.clear();
|
|
336
527
|
AllData::all_type_clear.clear();
|
|
337
528
|
AllData::all_type_del.clear();
|
|
@@ -351,7 +542,6 @@ struct FinalObject
|
|
|
351
542
|
|
|
352
543
|
static TwoStringTuple get_string_hash_tuple2(const std::string& name) noexcept;
|
|
353
544
|
static PyCodeObject* get_now_code() noexcept;
|
|
354
|
-
static bool need_analyse_type(PyObject* type) noexcept;
|
|
355
545
|
static const std::vector<uintptr_t>& type_get_parent_ids(uintptr_t type_id) noexcept;
|
|
356
546
|
static bool type_private_attr(uintptr_t type, const std::string& name) noexcept;
|
|
357
547
|
static FinalObject type_get_final_attr(uintptr_t main_type, uintptr_t target_type, const std::string& name) noexcept;
|
|
@@ -557,11 +747,6 @@ default_random_string(uintptr_t obj_id, const std::string& attr_name) noexcept
|
|
|
557
747
|
return result;
|
|
558
748
|
}
|
|
559
749
|
|
|
560
|
-
struct RestorePythonExceptionResult {
|
|
561
|
-
std::string value;
|
|
562
|
-
bool ok = true;
|
|
563
|
-
};
|
|
564
|
-
|
|
565
750
|
static RestorePythonExceptionResult
|
|
566
751
|
custom_random_string(uintptr_t obj_id, const std::string& attr_name, PyObject* func) noexcept
|
|
567
752
|
{
|
|
@@ -647,7 +832,6 @@ get_name_from_tp_name(PyTypeObject* typ) noexcept
|
|
|
647
832
|
return final_name;
|
|
648
833
|
}
|
|
649
834
|
|
|
650
|
-
static void ensure_tp(PyTypeObject* type_instance) noexcept;
|
|
651
835
|
static void ensure_subclass_tp(PyTypeObject* type_instance) noexcept;
|
|
652
836
|
static traverseproc get_need_tp_traverse(PyTypeObject* cls) noexcept;
|
|
653
837
|
static inquiry get_need_tp_clear(PyTypeObject* cls) noexcept;
|
|
@@ -2227,6 +2411,7 @@ static getattrofunc get_type_need_tp_getattro(PyTypeObject* cls) noexcept;
|
|
|
2227
2411
|
static setattrofunc get_type_need_tp_setattro(PyTypeObject* cls) noexcept;
|
|
2228
2412
|
static traverseproc get_type_need_tp_traverse(PyTypeObject* cls) noexcept;
|
|
2229
2413
|
static inquiry get_type_need_tp_clear(PyTypeObject* cls) noexcept;
|
|
2414
|
+
static ternaryfunc get_type_need_tp_call(PyTypeObject* cls) noexcept;
|
|
2230
2415
|
|
|
2231
2416
|
// metaclass-level (type object) traverse/clear: call original if any, and handle AllData-stored type attributes
|
|
2232
2417
|
static int
|
|
@@ -2417,6 +2602,7 @@ PrivateAttr_object_init_private_dict(uintptr_t obj_id, uintptr_t type_id) noexce
|
|
|
2417
2602
|
static PyObject* PrivateAttrType_new(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept;
|
|
2418
2603
|
static PyObject* PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept;
|
|
2419
2604
|
static int PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept;
|
|
2605
|
+
static PyObject* PrivateAttrType_call(PyObject* cls, PyObject* args, PyObject* kwds) noexcept;
|
|
2420
2606
|
static void PrivateAttrType_del(PyObject* cls) noexcept;
|
|
2421
2607
|
|
|
2422
2608
|
static PyTypeObject PrivateAttrType = {
|
|
@@ -2434,7 +2620,7 @@ static PyTypeObject PrivateAttrType = {
|
|
|
2434
2620
|
0, // tp_as_sequence
|
|
2435
2621
|
0, // tp_as_mapping
|
|
2436
2622
|
0, // tp_hash
|
|
2437
|
-
|
|
2623
|
+
(ternaryfunc)PrivateAttrType_call, // tp_call
|
|
2438
2624
|
0, // tp_str
|
|
2439
2625
|
(getattrofunc)PrivateAttrType_getattr, // tp_getattro
|
|
2440
2626
|
(setattrofunc)PrivateAttrType_setattr, // tp_setattro
|
|
@@ -2721,74 +2907,6 @@ real_class_name(const std::string& name, const std::string& class_name) noexcept
|
|
|
2721
2907
|
return name;
|
|
2722
2908
|
}
|
|
2723
2909
|
|
|
2724
|
-
struct PrivateAttrCreationData
|
|
2725
|
-
{
|
|
2726
|
-
std::string class_name;
|
|
2727
|
-
PyObjectStorage attrs_copy = (PyObject*)nullptr;
|
|
2728
|
-
std::unordered_set<TwoStringTuple> private_attrs_set;
|
|
2729
|
-
std::unordered_map<uintptr_t, PyObjectStorage> code_map;
|
|
2730
|
-
std::unordered_set<std::string> private_attrs_vector_string;
|
|
2731
|
-
std::vector<uintptr_t> all_need_analyse_base;
|
|
2732
|
-
std::unordered_map<std::string, PyObjectStorage> need_remove_itself;
|
|
2733
|
-
PyObjectStorage private_func = (PyObject*)nullptr;
|
|
2734
|
-
PyObjectStorage base_kwds = (PyObject*)nullptr;
|
|
2735
|
-
PyObjectStorage name = (PyObject*)nullptr;
|
|
2736
|
-
PyObjectStorage bases = (PyObject*)nullptr;
|
|
2737
|
-
bool use_parents = false;
|
|
2738
|
-
bool cleared = false;
|
|
2739
|
-
std::unique_lock<std::mutex> lock_;
|
|
2740
|
-
bool locked = false;
|
|
2741
|
-
PrivateAttrCreationData() noexcept : lock_(::AllData::type_creation_lock_mutex, std::defer_lock) {}
|
|
2742
|
-
|
|
2743
|
-
void lock() noexcept {
|
|
2744
|
-
if (!locked) {
|
|
2745
|
-
lock_.lock();
|
|
2746
|
-
AllData::type_creation_locked = true;
|
|
2747
|
-
locked = true;
|
|
2748
|
-
AllData::tmp_type_allowed_code_map = code_map;
|
|
2749
|
-
AllData::tmp_type_attr_set = private_attrs_set;
|
|
2750
|
-
AllData::tmp_type_attr_dict = need_remove_itself;
|
|
2751
|
-
if (private_func) {
|
|
2752
|
-
::AllData::tmp_type_need_call.push_back(private_func);
|
|
2753
|
-
}
|
|
2754
|
-
}
|
|
2755
|
-
}
|
|
2756
|
-
|
|
2757
|
-
void unlock() noexcept {
|
|
2758
|
-
if (locked) {
|
|
2759
|
-
AllData::tmp_type_allowed_code_map.clear();
|
|
2760
|
-
AllData::tmp_type_attr_set.clear();
|
|
2761
|
-
::AllData::tmp_type_need_call.clear();
|
|
2762
|
-
AllData::tmp_type_attr_dict.clear();
|
|
2763
|
-
AllData::type_creation_locked = false;
|
|
2764
|
-
locked = false;
|
|
2765
|
-
lock_.unlock();
|
|
2766
|
-
}
|
|
2767
|
-
}
|
|
2768
|
-
|
|
2769
|
-
void clear() noexcept {
|
|
2770
|
-
if (cleared) {
|
|
2771
|
-
return;
|
|
2772
|
-
}
|
|
2773
|
-
name = (PyObject*)nullptr;
|
|
2774
|
-
bases = (PyObject*)nullptr;
|
|
2775
|
-
attrs_copy = (PyObject*)nullptr;
|
|
2776
|
-
private_func = (PyObject*)nullptr;
|
|
2777
|
-
base_kwds = (PyObject*)nullptr;
|
|
2778
|
-
|
|
2779
|
-
need_remove_itself.clear();
|
|
2780
|
-
code_map.clear();
|
|
2781
|
-
if (locked) {
|
|
2782
|
-
unlock();
|
|
2783
|
-
}
|
|
2784
|
-
cleared = true;
|
|
2785
|
-
}
|
|
2786
|
-
|
|
2787
|
-
~PrivateAttrCreationData() noexcept {
|
|
2788
|
-
clear();
|
|
2789
|
-
}
|
|
2790
|
-
};
|
|
2791
|
-
|
|
2792
2910
|
static bool
|
|
2793
2911
|
need_analyse_type(PyObject* type) noexcept
|
|
2794
2912
|
{
|
|
@@ -3292,98 +3410,6 @@ ensure_subclass_tp(PyTypeObject* type_instance) noexcept
|
|
|
3292
3410
|
}
|
|
3293
3411
|
}
|
|
3294
3412
|
|
|
3295
|
-
static bool
|
|
3296
|
-
PrivateAttrType_postprocess(PyObject* new_type, PrivateAttrCreationData& data) noexcept
|
|
3297
|
-
{
|
|
3298
|
-
if (!new_type) {
|
|
3299
|
-
return false;
|
|
3300
|
-
}
|
|
3301
|
-
if (!PyType_Check(new_type)) {
|
|
3302
|
-
PyErr_SetString(PyExc_TypeError, "created object is not a type");
|
|
3303
|
-
return false;
|
|
3304
|
-
}
|
|
3305
|
-
|
|
3306
|
-
PyTypeObject* type_instance = (PyTypeObject*)new_type;
|
|
3307
|
-
uintptr_t type_id = (uintptr_t)(type_instance);
|
|
3308
|
-
int tp_dict_has_static_attr = PyDict_ContainsString(type_instance->tp_dict, "__static_attributes__");
|
|
3309
|
-
if (tp_dict_has_static_attr < 0) {
|
|
3310
|
-
return false;
|
|
3311
|
-
}
|
|
3312
|
-
if (tp_dict_has_static_attr) {
|
|
3313
|
-
PyErr_Format(PyExc_SystemError,
|
|
3314
|
-
"%s (%d): '__static_attributes__' will expose private attribute names and cannot be used in private attribute types", __FILE__, __LINE__);
|
|
3315
|
-
return false;
|
|
3316
|
-
}
|
|
3317
|
-
::AllData::all_type_attr_set[type_id] = data.private_attrs_set;
|
|
3318
|
-
|
|
3319
|
-
ensure_tp(type_instance);
|
|
3320
|
-
|
|
3321
|
-
if (::AllData::type_attr_dict.find(type_id) == ::AllData::type_attr_dict.end()) {
|
|
3322
|
-
::AllData::type_attr_dict[type_id] = {};
|
|
3323
|
-
}
|
|
3324
|
-
|
|
3325
|
-
// iter mro and put in all_type_parent_id
|
|
3326
|
-
PyObject* mro = type_instance->tp_mro;
|
|
3327
|
-
Py_ssize_t mro_size = PyTuple_GET_SIZE(mro);
|
|
3328
|
-
std::vector<uintptr_t> mro_vector;
|
|
3329
|
-
for (Py_ssize_t i = 1; i < mro_size; i++) {
|
|
3330
|
-
PyObject* item = PyTuple_GET_ITEM(mro, i);
|
|
3331
|
-
if (!item || !PyType_Check(item) || !need_analyse_type(item)) {
|
|
3332
|
-
continue;
|
|
3333
|
-
}
|
|
3334
|
-
mro_vector.push_back((uintptr_t)item);
|
|
3335
|
-
}
|
|
3336
|
-
::AllData::all_type_parent_id[type_id] = mro_vector;
|
|
3337
|
-
|
|
3338
|
-
::AllData::all_object_mutex[type_id].clear();
|
|
3339
|
-
::AllData::all_type_mutex[type_id] = std::make_unique<std::shared_mutex>();
|
|
3340
|
-
::AllData::all_object_attr[type_id] = {};
|
|
3341
|
-
::AllData::all_type_subclass_attr[type_id] = {};
|
|
3342
|
-
::AllData::all_type_subclass_mutex[type_id].clear();
|
|
3343
|
-
::AllData::type_allowed_code_map[type_id] = data.code_map;
|
|
3344
|
-
|
|
3345
|
-
for (uintptr_t i: mro_vector) {
|
|
3346
|
-
if (::AllData::all_type_subclass_attr.find(i) == ::AllData::all_type_subclass_attr.end()) {
|
|
3347
|
-
::AllData::all_type_subclass_attr[i] = {};
|
|
3348
|
-
}
|
|
3349
|
-
if (::AllData::all_type_subclass_mutex.find(i) == ::AllData::all_type_subclass_mutex.end()) {
|
|
3350
|
-
::AllData::all_type_subclass_mutex[i].clear();
|
|
3351
|
-
}
|
|
3352
|
-
// Do not wipe a parent's shadow already written for this class during
|
|
3353
|
-
// __init_subclass__ (e.g. `cls._value = ...` inside the parent's
|
|
3354
|
-
// __init_subclass__ runs inside tp_new, before postprocess).
|
|
3355
|
-
if (::AllData::all_type_subclass_attr[i].find(type_id) == ::AllData::all_type_subclass_attr[i].end()) {
|
|
3356
|
-
::AllData::all_type_subclass_attr[i][type_id] = {};
|
|
3357
|
-
}
|
|
3358
|
-
if (::AllData::all_type_subclass_mutex[i].find(type_id) == ::AllData::all_type_subclass_mutex[i].end()) {
|
|
3359
|
-
::AllData::all_type_subclass_mutex[i][type_id] = std::make_unique<std::shared_mutex>();
|
|
3360
|
-
}
|
|
3361
|
-
}
|
|
3362
|
-
|
|
3363
|
-
for (auto& [key, value]: data.need_remove_itself) {
|
|
3364
|
-
std::string final_key;
|
|
3365
|
-
if (data.private_func) {
|
|
3366
|
-
auto private_name_result = custom_random_string(type_id, key, data.private_func);
|
|
3367
|
-
if (!private_name_result.ok) {
|
|
3368
|
-
return false;
|
|
3369
|
-
}
|
|
3370
|
-
final_key = private_name_result.value;
|
|
3371
|
-
} else {
|
|
3372
|
-
final_key = default_random_string(type_id, key);
|
|
3373
|
-
}
|
|
3374
|
-
::AllData::type_attr_dict[type_id][final_key] = value;
|
|
3375
|
-
}
|
|
3376
|
-
|
|
3377
|
-
if (data.private_func) {
|
|
3378
|
-
// The PyObjectStorage assignment already INCREFs (RAII); the manual
|
|
3379
|
-
// INCREF was removed to avoid a +1 leak.
|
|
3380
|
-
::AllData::type_need_call[type_id] = data.private_func;
|
|
3381
|
-
}
|
|
3382
|
-
data.unlock();
|
|
3383
|
-
|
|
3384
|
-
return true;
|
|
3385
|
-
}
|
|
3386
|
-
|
|
3387
3413
|
// RAII flag: while active, PrivateAttrType_new captures the pristine
|
|
3388
3414
|
// tp_traverse/tp_clear of the freshly created heap type. type_new assigns
|
|
3389
3415
|
// subtype_traverse/subtype_clear to every heap type unconditionally, so the
|
|
@@ -3438,7 +3464,7 @@ PrivateAttrType_new(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept
|
|
|
3438
3464
|
::AllData::captured_subtype_clear = created->tp_clear;
|
|
3439
3465
|
}
|
|
3440
3466
|
|
|
3441
|
-
if (!
|
|
3467
|
+
if (!data.install(new_type)) {
|
|
3442
3468
|
Py_DECREF(new_type);
|
|
3443
3469
|
return nullptr;
|
|
3444
3470
|
}
|
|
@@ -3446,9 +3472,32 @@ PrivateAttrType_new(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept
|
|
|
3446
3472
|
return new_type;
|
|
3447
3473
|
}
|
|
3448
3474
|
|
|
3475
|
+
static PyObject*
|
|
3476
|
+
PrivateAttrType_call(PyObject* cls, PyObject* args, PyObject* kwds) noexcept
|
|
3477
|
+
{
|
|
3478
|
+
// Finish any pending private-attribute creation before instantiating the
|
|
3479
|
+
// class (the creation may have been interrupted before install() ran).
|
|
3480
|
+
if (!try_install_pending_creation(cls)) return NULL;
|
|
3481
|
+
auto it = ::AllData::all_type_call.find((uintptr_t)Py_TYPE(cls));
|
|
3482
|
+
if (it != ::AllData::all_type_call.end()) {
|
|
3483
|
+
if (it->second && it->second != PrivateAttrType_call) {
|
|
3484
|
+
return it->second(cls, args, kwds);
|
|
3485
|
+
}
|
|
3486
|
+
}
|
|
3487
|
+
PyTypeObject* base = Py_TYPE(cls)->tp_base;
|
|
3488
|
+
while (base && base->tp_base && base->tp_call == PrivateAttrType_call) {
|
|
3489
|
+
base = base->tp_base;
|
|
3490
|
+
}
|
|
3491
|
+
if (!base) {
|
|
3492
|
+
return PyType_Type.tp_call(cls, args, kwds);
|
|
3493
|
+
}
|
|
3494
|
+
return base->tp_call(cls, args, kwds);
|
|
3495
|
+
}
|
|
3496
|
+
|
|
3449
3497
|
static PyObject*
|
|
3450
3498
|
PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
|
|
3451
3499
|
{
|
|
3500
|
+
if (!try_install_pending_creation(cls)) return NULL;
|
|
3452
3501
|
if (!PyType_Check(cls)) {
|
|
3453
3502
|
PyErr_SetString(PyExc_TypeError, "cls must be a type");
|
|
3454
3503
|
return NULL;
|
|
@@ -3472,6 +3521,12 @@ PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
|
|
|
3472
3521
|
}
|
|
3473
3522
|
}
|
|
3474
3523
|
}
|
|
3524
|
+
auto it = ::AllData::all_type_getattro.find((uintptr_t)Py_TYPE(cls));
|
|
3525
|
+
if (it != ::AllData::all_type_getattro.end()) {
|
|
3526
|
+
if (it->second && it->second != PrivateAttrType_getattr) {
|
|
3527
|
+
return it->second(cls, name);
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3475
3530
|
PyTypeObject* base = Py_TYPE(cls)->tp_base;
|
|
3476
3531
|
while (base && base->tp_base && base->tp_getattro == PrivateAttrType_getattr) {
|
|
3477
3532
|
base = base->tp_base;
|
|
@@ -3664,6 +3719,7 @@ get_need_tp_clear(PyTypeObject* cls) noexcept
|
|
|
3664
3719
|
// tp_setattro -> PrivateAttrType_setattr
|
|
3665
3720
|
// tp_traverse -> PrivateAttrType_tp_traverse
|
|
3666
3721
|
// tp_clear -> PrivateAttrType_tp_clear
|
|
3722
|
+
// tp_call -> PrivateAttrType_call
|
|
3667
3723
|
//
|
|
3668
3724
|
// These helpers walk the metaclass hierarchy (cls->tp_base chain) and skip
|
|
3669
3725
|
// those wrappers, returning the nearest real implementation (either read
|
|
@@ -3711,6 +3767,25 @@ get_type_need_tp_setattro(PyTypeObject* cls) noexcept
|
|
|
3711
3767
|
return NULL;
|
|
3712
3768
|
}
|
|
3713
3769
|
|
|
3770
|
+
static ternaryfunc
|
|
3771
|
+
get_type_need_tp_call(PyTypeObject* cls) noexcept
|
|
3772
|
+
{
|
|
3773
|
+
PyTypeObject* base = cls->tp_base;
|
|
3774
|
+
while (base) {
|
|
3775
|
+
if (base->tp_call != PrivateAttrType_call) {
|
|
3776
|
+
return base->tp_call;
|
|
3777
|
+
} else if (::AllData::all_type_call.find((uintptr_t)base) != ::AllData::all_type_call.end()) {
|
|
3778
|
+
if (::AllData::all_type_call[(uintptr_t)base]) {
|
|
3779
|
+
return ::AllData::all_type_call[(uintptr_t)base];
|
|
3780
|
+
}
|
|
3781
|
+
base = base->tp_base;
|
|
3782
|
+
} else {
|
|
3783
|
+
base = base->tp_base;
|
|
3784
|
+
}
|
|
3785
|
+
}
|
|
3786
|
+
return NULL;
|
|
3787
|
+
}
|
|
3788
|
+
|
|
3714
3789
|
static traverseproc
|
|
3715
3790
|
get_type_need_tp_traverse(PyTypeObject* cls) noexcept
|
|
3716
3791
|
{
|
|
@@ -3791,6 +3866,7 @@ get_type_need_tp_clear(PyTypeObject* cls) noexcept
|
|
|
3791
3866
|
static int
|
|
3792
3867
|
PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept
|
|
3793
3868
|
{
|
|
3869
|
+
if (!try_install_pending_creation(cls)) return -1;
|
|
3794
3870
|
if (!PyType_Check(cls)) {
|
|
3795
3871
|
PyErr_SetString(PyExc_TypeError, "cls must be a type");
|
|
3796
3872
|
return -1;
|
|
@@ -3821,6 +3897,12 @@ PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept
|
|
|
3821
3897
|
// owner is 0: not private for this access context -> normal attribute
|
|
3822
3898
|
}
|
|
3823
3899
|
}
|
|
3900
|
+
auto it = ::AllData::all_type_setattro.find((uintptr_t)Py_TYPE(cls));
|
|
3901
|
+
if (it != ::AllData::all_type_setattro.end()) {
|
|
3902
|
+
if (it->second && it->second != PrivateAttrType_setattr) {
|
|
3903
|
+
return it->second(cls, name, value);
|
|
3904
|
+
}
|
|
3905
|
+
}
|
|
3824
3906
|
PyTypeObject* base = Py_TYPE(cls)->tp_base;
|
|
3825
3907
|
while (base && base->tp_base && base->tp_setattro == PrivateAttrType_setattr) {
|
|
3826
3908
|
base = base->tp_base;
|
|
@@ -3838,6 +3920,13 @@ static void
|
|
|
3838
3920
|
PrivateAttrType_finalize(PyObject* cls) noexcept
|
|
3839
3921
|
{
|
|
3840
3922
|
uintptr_t typ_id = (uintptr_t) cls;
|
|
3923
|
+
if (::AllData::all_type_del.find((uintptr_t)Py_TYPE(cls)) != ::AllData::all_type_del.end()) {
|
|
3924
|
+
destructor orig = ::AllData::all_type_del[(uintptr_t)Py_TYPE(cls)];
|
|
3925
|
+
if (orig && orig != PrivateAttrType_finalize) {
|
|
3926
|
+
orig(cls);
|
|
3927
|
+
return;
|
|
3928
|
+
}
|
|
3929
|
+
}
|
|
3841
3930
|
if (::AllData::all_type_attr_set.find(typ_id) != ::AllData::all_type_attr_set.end()) {
|
|
3842
3931
|
::AllData::all_type_attr_set.erase(typ_id);
|
|
3843
3932
|
}
|
|
@@ -4027,7 +4116,7 @@ PrivateTempObject_postprocess(PyObject* self, PyObject* type) noexcept
|
|
|
4027
4116
|
PyErr_SetString(PyExc_RuntimeError, "temp not init or have been used");
|
|
4028
4117
|
return NULL;
|
|
4029
4118
|
}
|
|
4030
|
-
if (!
|
|
4119
|
+
if (!(((PrivateTempObject*)self)->tmp->install(type))) {
|
|
4031
4120
|
return NULL;
|
|
4032
4121
|
}
|
|
4033
4122
|
((PrivateTempObject*)self)->tmp->clear();
|
|
@@ -4180,6 +4269,7 @@ metaclass_weakref_callback(PyObject* /*self*/, PyObject* weakref) noexcept
|
|
|
4180
4269
|
METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(traverse);
|
|
4181
4270
|
METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(clear);
|
|
4182
4271
|
METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(del);
|
|
4272
|
+
METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(call);
|
|
4183
4273
|
|
|
4184
4274
|
Py_RETURN_NONE;
|
|
4185
4275
|
}
|
|
@@ -4299,6 +4389,13 @@ register_metaclass_head(PyObject* metaclass) noexcept
|
|
|
4299
4389
|
}
|
|
4300
4390
|
mt->tp_clear = PrivateAttrType_tp_clear;
|
|
4301
4391
|
}
|
|
4392
|
+
// save and replace tp_call
|
|
4393
|
+
if (mt->tp_call != PrivateAttrType_call) {
|
|
4394
|
+
if (::AllData::all_type_call.find(id) == ::AllData::all_type_call.end()) {
|
|
4395
|
+
::AllData::all_type_call[id] = get_type_need_tp_call(mt);
|
|
4396
|
+
}
|
|
4397
|
+
mt->tp_call = PrivateAttrType_call;
|
|
4398
|
+
}
|
|
4302
4399
|
Py_RETURN_NONE;
|
|
4303
4400
|
}
|
|
4304
4401
|
|
|
@@ -4373,6 +4470,12 @@ ensure_metaclass_tp(PyObject* /*self*/, PyObject* metaclass) noexcept
|
|
|
4373
4470
|
}
|
|
4374
4471
|
mt->tp_clear = PrivateAttrType_tp_clear;
|
|
4375
4472
|
}
|
|
4473
|
+
if (mt->tp_call != PrivateAttrType_call) {
|
|
4474
|
+
if (::AllData::all_type_call.find(id) == ::AllData::all_type_call.end()) {
|
|
4475
|
+
::AllData::all_type_call[id] = get_type_need_tp_call(mt);
|
|
4476
|
+
}
|
|
4477
|
+
mt->tp_call = PrivateAttrType_call;
|
|
4478
|
+
}
|
|
4376
4479
|
}
|
|
4377
4480
|
Py_RETURN_NONE;
|
|
4378
4481
|
}
|
|
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "private_attribute_cpp"
|
|
7
|
-
version = "2.2.
|
|
7
|
+
version = "2.2.8.1"
|
|
8
8
|
description = "A Python package that provides a way to define private attributes in C++ implementation."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_attr_classify_owner.py
RENAMED
|
File without changes
|
{private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_class_private_attrs.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8.1}/tests/test_private_inheritance.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|