private-attribute-cpp 2.2.7__tar.gz → 2.2.8__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.
Files changed (20) hide show
  1. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/PKG-INFO +2 -2
  2. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/README.md +1 -1
  3. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/private_attribute.cpp +277 -171
  4. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/pyproject.toml +1 -1
  5. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/CMakeLists.txt +0 -0
  6. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/LICENSE +0 -0
  7. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/picosha2.h +0 -0
  8. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/private_attribute.pyi +0 -0
  9. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_attr_classify_owner.py +0 -0
  10. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_class_private_attrs.py +0 -0
  11. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_gc_collect.py +0 -0
  12. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_init_subclass.py +0 -0
  13. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_private_abc.py +0 -0
  14. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_private_attrs.py +0 -0
  15. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_private_func.py +0 -0
  16. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_private_inheritance.py +0 -0
  17. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_private_method.py +0 -0
  18. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_private_wrap.py +0 -0
  19. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/tests/test_slots_cycle.py +0 -0
  20. {private_attribute_cpp-2.2.7 → private_attribute_cpp-2.2.8}/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.7
3
+ Version: 2.2.8
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`** — the tuple of original (pre-decoration) functions.
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`** — the tuple of original (pre-decoration) functions.
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
@@ -255,6 +269,7 @@ namespace {
255
269
  static std::shared_mutex all_register_new_metaclass_mutex;
256
270
  static std::unordered_map<uintptr_t, PyObjectStorage> all_register_type_weak_ref;
257
271
  static std::unordered_map<uintptr_t, uintptr_t> weakref_type;
272
+ static std::unordered_set<uintptr_t> all_register_type;
258
273
  // Single dict-backed strong-ref cache shared by the module itself (keyed by
259
274
  // kModuleSelfKey) and by PrivateAttrBase (keyed by kPrivateAttrBaseKey),
260
275
  // so clean_all_storages can release everything at interpreter shutdown.
@@ -264,6 +279,181 @@ namespace {
264
279
  };
265
280
  };
266
281
 
282
+ struct PrivateAttrCreationData
283
+ {
284
+ std::string class_name;
285
+ PyObjectStorage attrs_copy = (PyObject*)nullptr;
286
+ std::unordered_set<TwoStringTuple> private_attrs_set;
287
+ std::unordered_map<uintptr_t, PyObjectStorage> code_map;
288
+ std::unordered_set<std::string> private_attrs_vector_string;
289
+ std::vector<uintptr_t> all_need_analyse_base;
290
+ std::unordered_map<std::string, PyObjectStorage> need_remove_itself;
291
+ PyObjectStorage private_func = (PyObject*)nullptr;
292
+ PyObjectStorage base_kwds = (PyObject*)nullptr;
293
+ PyObjectStorage name = (PyObject*)nullptr;
294
+ PyObjectStorage bases = (PyObject*)nullptr;
295
+ bool use_parents = false;
296
+ bool cleared = false;
297
+ std::unique_lock<std::mutex> lock_;
298
+ bool locked = false;
299
+ bool installed = false;
300
+ PrivateAttrCreationData() noexcept : lock_(::AllData::type_creation_lock_mutex, std::defer_lock) {}
301
+
302
+ void lock() noexcept {
303
+ if (!locked) {
304
+ lock_.lock();
305
+ AllData::type_creation_locked = true;
306
+ locked = true;
307
+ AllData::tmp_type_allowed_code_map = code_map;
308
+ AllData::tmp_type_attr_set = private_attrs_set;
309
+ AllData::tmp_type_attr_dict = need_remove_itself;
310
+ AllData::tmp_creation_data = this;
311
+ if (private_func) {
312
+ ::AllData::tmp_type_need_call.push_back(private_func);
313
+ }
314
+ }
315
+ }
316
+
317
+ void unlock() noexcept {
318
+ if (locked) {
319
+ AllData::tmp_type_allowed_code_map.clear();
320
+ AllData::tmp_type_attr_set.clear();
321
+ ::AllData::tmp_type_need_call.clear();
322
+ AllData::tmp_type_attr_dict.clear();
323
+ AllData::tmp_creation_data = nullptr;
324
+ AllData::type_creation_locked = false;
325
+ locked = false;
326
+ lock_.unlock();
327
+ }
328
+ }
329
+
330
+ void clear() noexcept {
331
+ if (cleared) {
332
+ return;
333
+ }
334
+ name = (PyObject*)nullptr;
335
+ bases = (PyObject*)nullptr;
336
+ attrs_copy = (PyObject*)nullptr;
337
+ private_func = (PyObject*)nullptr;
338
+ base_kwds = (PyObject*)nullptr;
339
+
340
+ need_remove_itself.clear();
341
+ code_map.clear();
342
+ if (locked) {
343
+ unlock();
344
+ }
345
+ cleared = true;
346
+ }
347
+
348
+ bool install(PyObject* new_type) noexcept {
349
+ if (installed) return true;
350
+ if (!new_type) {
351
+ return false;
352
+ }
353
+ if (!PyType_Check(new_type)) {
354
+ PyErr_SetString(PyExc_TypeError, "created object is not a type");
355
+ return false;
356
+ }
357
+
358
+ PyTypeObject* type_instance = (PyTypeObject*)new_type;
359
+ uintptr_t type_id = (uintptr_t)(type_instance);
360
+ int tp_dict_has_static_attr = PyDict_ContainsString(type_instance->tp_dict, "__static_attributes__");
361
+ if (tp_dict_has_static_attr < 0) {
362
+ return false;
363
+ }
364
+ if (tp_dict_has_static_attr) {
365
+ PyErr_Format(PyExc_SystemError,
366
+ "%s (%d): '__static_attributes__' will expose private attribute names and cannot be used in private attribute types", __FILE__, __LINE__);
367
+ return false;
368
+ }
369
+ ::AllData::all_type_attr_set[type_id] = private_attrs_set;
370
+ ensure_tp(type_instance);
371
+ if (::AllData::type_attr_dict.find(type_id) == ::AllData::type_attr_dict.end()) {
372
+ ::AllData::type_attr_dict[type_id] = {};
373
+ }
374
+ // iter mro and put in all_type_parent_id
375
+ PyObject* mro = type_instance->tp_mro;
376
+ Py_ssize_t mro_size = PyTuple_GET_SIZE(mro);
377
+ std::vector<uintptr_t> mro_vector;
378
+ for (Py_ssize_t i = 1; i < mro_size; i++) {
379
+ PyObject* item = PyTuple_GET_ITEM(mro, i);
380
+ if (!item || !PyType_Check(item) || !need_analyse_type(item)) {
381
+ continue;
382
+ }
383
+ mro_vector.push_back((uintptr_t)item);
384
+ }
385
+ ::AllData::all_type_parent_id[type_id] = mro_vector;
386
+ ::AllData::all_object_mutex[type_id].clear();
387
+ ::AllData::all_type_mutex[type_id] = std::make_unique<std::shared_mutex>();
388
+ ::AllData::all_object_attr[type_id] = {};
389
+ ::AllData::all_type_subclass_attr[type_id] = {};
390
+ ::AllData::all_type_subclass_mutex[type_id].clear();
391
+ ::AllData::type_allowed_code_map[type_id] = code_map;
392
+
393
+ for (uintptr_t i: mro_vector) {
394
+ if (::AllData::all_type_subclass_attr.find(i) == ::AllData::all_type_subclass_attr.end()) {
395
+ ::AllData::all_type_subclass_attr[i] = {};
396
+ }
397
+ if (::AllData::all_type_subclass_mutex.find(i) == ::AllData::all_type_subclass_mutex.end()) {
398
+ ::AllData::all_type_subclass_mutex[i].clear();
399
+ }
400
+ // Do not wipe a parent's shadow already written for this class during
401
+ // __init_subclass__ (e.g. `cls._value = ...` inside the parent's
402
+ // __init_subclass__ runs inside tp_new, before postprocess).
403
+ if (::AllData::all_type_subclass_attr[i].find(type_id) == ::AllData::all_type_subclass_attr[i].end()) {
404
+ ::AllData::all_type_subclass_attr[i][type_id] = {};
405
+ }
406
+ if (::AllData::all_type_subclass_mutex[i].find(type_id) == ::AllData::all_type_subclass_mutex[i].end()) {
407
+ ::AllData::all_type_subclass_mutex[i][type_id] = std::make_unique<std::shared_mutex>();
408
+ }
409
+ }
410
+
411
+ for (auto& [key, value]: need_remove_itself) {
412
+ std::string final_key;
413
+ if (private_func) {
414
+ auto private_name_result = custom_random_string(type_id, key, private_func);
415
+ if (!private_name_result.ok) {
416
+ return false;
417
+ }
418
+ final_key = private_name_result.value;
419
+ } else {
420
+ final_key = default_random_string(type_id, key);
421
+ }
422
+ ::AllData::type_attr_dict[type_id][final_key] = value;
423
+ }
424
+ if (private_func) {
425
+ ::AllData::type_need_call[type_id] = private_func;
426
+ }
427
+ unlock();
428
+ installed = true;
429
+ return true;
430
+ }
431
+
432
+ ~PrivateAttrCreationData() noexcept {
433
+ clear();
434
+ }
435
+ };
436
+
437
+ static bool
438
+ try_install_pending_creation(PyObject* new_type) noexcept
439
+ {
440
+ // Finish a pending private-attribute creation if one is still
441
+ // outstanding. prepare()/PrivateAttrType_new leave tmp_creation_data
442
+ // locked (type_creation_locked == true) until install() runs; if
443
+ // creation is interrupted (e.g. an exception in __init_subclass__) the
444
+ // type exists but is not yet registered in all_type_attr_set. The
445
+ // metaclass getattr/setattr/call slots check this first and promote the
446
+ // half-created type before proceeding.
447
+ if (!new_type || !::AllData::tmp_creation_data) {
448
+ return true;;
449
+ }
450
+ if (::AllData::type_creation_locked &&
451
+ ::AllData::all_type_attr_set.find((uintptr_t)new_type) == ::AllData::all_type_attr_set.end()) {
452
+ return ::AllData::tmp_creation_data->install(new_type);
453
+ }
454
+ return true;
455
+ }
456
+
267
457
  static inline void
268
458
  object_create_mutex(uintptr_t final_id, uintptr_t obj_id) noexcept
269
459
  {
@@ -322,6 +512,7 @@ clean_all_storages() noexcept
322
512
  AllData::tmp_type_need_call.clear();
323
513
  AllData::tmp_type_attr_dict.clear();
324
514
  AllData::type_creation_locked = false;
515
+ AllData::tmp_creation_data = nullptr;
325
516
  AllData::all_type_mutex.clear();
326
517
  AllData::type_need_call.clear();
327
518
  AllData::all_type_attr_set.clear();
@@ -332,10 +523,12 @@ clean_all_storages() noexcept
332
523
  AllData::all_type_parent_id.clear();
333
524
  AllData::all_type_getattro.clear();
334
525
  AllData::all_type_setattro.clear();
526
+ AllData::all_type_call.clear();
335
527
  AllData::all_type_traverse.clear();
336
528
  AllData::all_type_clear.clear();
337
529
  AllData::all_type_del.clear();
338
530
  AllData::all_register_type_weak_ref.clear();
531
+ AllData::all_register_type.clear();
339
532
  AllData::store_module_self.clear();
340
533
  }
341
534
 
@@ -351,7 +544,6 @@ struct FinalObject
351
544
 
352
545
  static TwoStringTuple get_string_hash_tuple2(const std::string& name) noexcept;
353
546
  static PyCodeObject* get_now_code() noexcept;
354
- static bool need_analyse_type(PyObject* type) noexcept;
355
547
  static const std::vector<uintptr_t>& type_get_parent_ids(uintptr_t type_id) noexcept;
356
548
  static bool type_private_attr(uintptr_t type, const std::string& name) noexcept;
357
549
  static FinalObject type_get_final_attr(uintptr_t main_type, uintptr_t target_type, const std::string& name) noexcept;
@@ -557,11 +749,6 @@ default_random_string(uintptr_t obj_id, const std::string& attr_name) noexcept
557
749
  return result;
558
750
  }
559
751
 
560
- struct RestorePythonExceptionResult {
561
- std::string value;
562
- bool ok = true;
563
- };
564
-
565
752
  static RestorePythonExceptionResult
566
753
  custom_random_string(uintptr_t obj_id, const std::string& attr_name, PyObject* func) noexcept
567
754
  {
@@ -647,7 +834,6 @@ get_name_from_tp_name(PyTypeObject* typ) noexcept
647
834
  return final_name;
648
835
  }
649
836
 
650
- static void ensure_tp(PyTypeObject* type_instance) noexcept;
651
837
  static void ensure_subclass_tp(PyTypeObject* type_instance) noexcept;
652
838
  static traverseproc get_need_tp_traverse(PyTypeObject* cls) noexcept;
653
839
  static inquiry get_need_tp_clear(PyTypeObject* cls) noexcept;
@@ -2227,6 +2413,7 @@ static getattrofunc get_type_need_tp_getattro(PyTypeObject* cls) noexcept;
2227
2413
  static setattrofunc get_type_need_tp_setattro(PyTypeObject* cls) noexcept;
2228
2414
  static traverseproc get_type_need_tp_traverse(PyTypeObject* cls) noexcept;
2229
2415
  static inquiry get_type_need_tp_clear(PyTypeObject* cls) noexcept;
2416
+ static ternaryfunc get_type_need_tp_call(PyTypeObject* cls) noexcept;
2230
2417
 
2231
2418
  // metaclass-level (type object) traverse/clear: call original if any, and handle AllData-stored type attributes
2232
2419
  static int
@@ -2417,6 +2604,7 @@ PrivateAttr_object_init_private_dict(uintptr_t obj_id, uintptr_t type_id) noexce
2417
2604
  static PyObject* PrivateAttrType_new(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept;
2418
2605
  static PyObject* PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept;
2419
2606
  static int PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept;
2607
+ static PyObject* PrivateAttrType_call(PyObject* cls, PyObject* args, PyObject* kwds) noexcept;
2420
2608
  static void PrivateAttrType_del(PyObject* cls) noexcept;
2421
2609
 
2422
2610
  static PyTypeObject PrivateAttrType = {
@@ -2434,7 +2622,7 @@ static PyTypeObject PrivateAttrType = {
2434
2622
  0, // tp_as_sequence
2435
2623
  0, // tp_as_mapping
2436
2624
  0, // tp_hash
2437
- 0, // tp_call
2625
+ (ternaryfunc)PrivateAttrType_call, // tp_call
2438
2626
  0, // tp_str
2439
2627
  (getattrofunc)PrivateAttrType_getattr, // tp_getattro
2440
2628
  (setattrofunc)PrivateAttrType_setattr, // tp_setattro
@@ -2721,74 +2909,6 @@ real_class_name(const std::string& name, const std::string& class_name) noexcept
2721
2909
  return name;
2722
2910
  }
2723
2911
 
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
2912
  static bool
2793
2913
  need_analyse_type(PyObject* type) noexcept
2794
2914
  {
@@ -3292,98 +3412,6 @@ ensure_subclass_tp(PyTypeObject* type_instance) noexcept
3292
3412
  }
3293
3413
  }
3294
3414
 
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
3415
  // RAII flag: while active, PrivateAttrType_new captures the pristine
3388
3416
  // tp_traverse/tp_clear of the freshly created heap type. type_new assigns
3389
3417
  // subtype_traverse/subtype_clear to every heap type unconditionally, so the
@@ -3438,7 +3466,7 @@ PrivateAttrType_new(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept
3438
3466
  ::AllData::captured_subtype_clear = created->tp_clear;
3439
3467
  }
3440
3468
 
3441
- if (!PrivateAttrType_postprocess(new_type, data)) {
3469
+ if (!data.install(new_type)) {
3442
3470
  Py_DECREF(new_type);
3443
3471
  return nullptr;
3444
3472
  }
@@ -3446,9 +3474,32 @@ PrivateAttrType_new(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept
3446
3474
  return new_type;
3447
3475
  }
3448
3476
 
3477
+ static PyObject*
3478
+ PrivateAttrType_call(PyObject* cls, PyObject* args, PyObject* kwds) noexcept
3479
+ {
3480
+ // Finish any pending private-attribute creation before instantiating the
3481
+ // class (the creation may have been interrupted before install() ran).
3482
+ if (!try_install_pending_creation(cls)) return NULL;
3483
+ auto it = ::AllData::all_type_call.find((uintptr_t)Py_TYPE(cls));
3484
+ if (it != ::AllData::all_type_call.end()) {
3485
+ if (it->second && it->second != PrivateAttrType_call) {
3486
+ return it->second(cls, args, kwds);
3487
+ }
3488
+ }
3489
+ PyTypeObject* base = Py_TYPE(cls)->tp_base;
3490
+ while (base && base->tp_base && base->tp_call == PrivateAttrType_call) {
3491
+ base = base->tp_base;
3492
+ }
3493
+ if (!base) {
3494
+ return PyType_Type.tp_call(cls, args, kwds);
3495
+ }
3496
+ return base->tp_call(cls, args, kwds);
3497
+ }
3498
+
3449
3499
  static PyObject*
3450
3500
  PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
3451
3501
  {
3502
+ if (!try_install_pending_creation(cls)) return NULL;
3452
3503
  if (!PyType_Check(cls)) {
3453
3504
  PyErr_SetString(PyExc_TypeError, "cls must be a type");
3454
3505
  return NULL;
@@ -3472,6 +3523,12 @@ PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
3472
3523
  }
3473
3524
  }
3474
3525
  }
3526
+ auto it = ::AllData::all_type_getattro.find((uintptr_t)Py_TYPE(cls));
3527
+ if (it != ::AllData::all_type_getattro.end()) {
3528
+ if (it->second && it->second != PrivateAttrType_getattr) {
3529
+ return it->second(cls, name);
3530
+ }
3531
+ }
3475
3532
  PyTypeObject* base = Py_TYPE(cls)->tp_base;
3476
3533
  while (base && base->tp_base && base->tp_getattro == PrivateAttrType_getattr) {
3477
3534
  base = base->tp_base;
@@ -3490,7 +3547,7 @@ static bool
3490
3547
  is_registed_type(PyObject* type) noexcept
3491
3548
  {
3492
3549
  uintptr_t typ_id = (uintptr_t)type;
3493
- return (AllData::all_register_type_weak_ref.find(typ_id) != AllData::all_register_type_weak_ref.end());
3550
+ return (AllData::all_register_type.find(typ_id) != AllData::all_register_type.end());
3494
3551
  }
3495
3552
 
3496
3553
  static PyObject* ensure_metaclass_tp(PyObject* /*self*/, PyObject* metaclass) noexcept;
@@ -3664,6 +3721,7 @@ get_need_tp_clear(PyTypeObject* cls) noexcept
3664
3721
  // tp_setattro -> PrivateAttrType_setattr
3665
3722
  // tp_traverse -> PrivateAttrType_tp_traverse
3666
3723
  // tp_clear -> PrivateAttrType_tp_clear
3724
+ // tp_call -> PrivateAttrType_call
3667
3725
  //
3668
3726
  // These helpers walk the metaclass hierarchy (cls->tp_base chain) and skip
3669
3727
  // those wrappers, returning the nearest real implementation (either read
@@ -3711,6 +3769,25 @@ get_type_need_tp_setattro(PyTypeObject* cls) noexcept
3711
3769
  return NULL;
3712
3770
  }
3713
3771
 
3772
+ static ternaryfunc
3773
+ get_type_need_tp_call(PyTypeObject* cls) noexcept
3774
+ {
3775
+ PyTypeObject* base = cls->tp_base;
3776
+ while (base) {
3777
+ if (base->tp_call != PrivateAttrType_call) {
3778
+ return base->tp_call;
3779
+ } else if (::AllData::all_type_call.find((uintptr_t)base) != ::AllData::all_type_call.end()) {
3780
+ if (::AllData::all_type_call[(uintptr_t)base]) {
3781
+ return ::AllData::all_type_call[(uintptr_t)base];
3782
+ }
3783
+ base = base->tp_base;
3784
+ } else {
3785
+ base = base->tp_base;
3786
+ }
3787
+ }
3788
+ return NULL;
3789
+ }
3790
+
3714
3791
  static traverseproc
3715
3792
  get_type_need_tp_traverse(PyTypeObject* cls) noexcept
3716
3793
  {
@@ -3791,6 +3868,7 @@ get_type_need_tp_clear(PyTypeObject* cls) noexcept
3791
3868
  static int
3792
3869
  PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept
3793
3870
  {
3871
+ if (!try_install_pending_creation(cls)) return -1;
3794
3872
  if (!PyType_Check(cls)) {
3795
3873
  PyErr_SetString(PyExc_TypeError, "cls must be a type");
3796
3874
  return -1;
@@ -3821,6 +3899,12 @@ PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept
3821
3899
  // owner is 0: not private for this access context -> normal attribute
3822
3900
  }
3823
3901
  }
3902
+ auto it = ::AllData::all_type_setattro.find((uintptr_t)Py_TYPE(cls));
3903
+ if (it != ::AllData::all_type_setattro.end()) {
3904
+ if (it->second && it->second != PrivateAttrType_setattr) {
3905
+ return it->second(cls, name, value);
3906
+ }
3907
+ }
3824
3908
  PyTypeObject* base = Py_TYPE(cls)->tp_base;
3825
3909
  while (base && base->tp_base && base->tp_setattro == PrivateAttrType_setattr) {
3826
3910
  base = base->tp_base;
@@ -3838,6 +3922,13 @@ static void
3838
3922
  PrivateAttrType_finalize(PyObject* cls) noexcept
3839
3923
  {
3840
3924
  uintptr_t typ_id = (uintptr_t) cls;
3925
+ if (::AllData::all_type_del.find((uintptr_t)Py_TYPE(cls)) != ::AllData::all_type_del.end()) {
3926
+ destructor orig = ::AllData::all_type_del[(uintptr_t)Py_TYPE(cls)];
3927
+ if (orig && orig != PrivateAttrType_finalize) {
3928
+ orig(cls);
3929
+ return;
3930
+ }
3931
+ }
3841
3932
  if (::AllData::all_type_attr_set.find(typ_id) != ::AllData::all_type_attr_set.end()) {
3842
3933
  ::AllData::all_type_attr_set.erase(typ_id);
3843
3934
  }
@@ -4027,7 +4118,7 @@ PrivateTempObject_postprocess(PyObject* self, PyObject* type) noexcept
4027
4118
  PyErr_SetString(PyExc_RuntimeError, "temp not init or have been used");
4028
4119
  return NULL;
4029
4120
  }
4030
- if (!PrivateAttrType_postprocess(type, *(((PrivateTempObject*)self)->tmp))) {
4121
+ if (!(((PrivateTempObject*)self)->tmp->install(type))) {
4031
4122
  return NULL;
4032
4123
  }
4033
4124
  ((PrivateTempObject*)self)->tmp->clear();
@@ -4168,6 +4259,7 @@ metaclass_weakref_callback(PyObject* /*self*/, PyObject* weakref) noexcept
4168
4259
  if (::AllData::weakref_type.find(weakref_id) == ::AllData::weakref_type.end()) Py_RETURN_NONE;
4169
4260
  uintptr_t id = ::AllData::weakref_type[weakref_id];
4170
4261
  ::AllData::weakref_type.erase(weakref_id);
4262
+ ::AllData::all_register_type.erase(id);
4171
4263
 
4172
4264
  auto it = ::AllData::all_register_type_weak_ref.find(id);
4173
4265
  if (it != ::AllData::all_register_type_weak_ref.end()) {
@@ -4180,6 +4272,7 @@ metaclass_weakref_callback(PyObject* /*self*/, PyObject* weakref) noexcept
4180
4272
  METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(traverse);
4181
4273
  METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(clear);
4182
4274
  METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(del);
4275
+ METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(call);
4183
4276
 
4184
4277
  Py_RETURN_NONE;
4185
4278
  }
@@ -4299,6 +4392,13 @@ register_metaclass_head(PyObject* metaclass) noexcept
4299
4392
  }
4300
4393
  mt->tp_clear = PrivateAttrType_tp_clear;
4301
4394
  }
4395
+ // save and replace tp_call
4396
+ if (mt->tp_call != PrivateAttrType_call) {
4397
+ if (::AllData::all_type_call.find(id) == ::AllData::all_type_call.end()) {
4398
+ ::AllData::all_type_call[id] = get_type_need_tp_call(mt);
4399
+ }
4400
+ mt->tp_call = PrivateAttrType_call;
4401
+ }
4302
4402
  Py_RETURN_NONE;
4303
4403
  }
4304
4404
 
@@ -4373,6 +4473,12 @@ ensure_metaclass_tp(PyObject* /*self*/, PyObject* metaclass) noexcept
4373
4473
  }
4374
4474
  mt->tp_clear = PrivateAttrType_tp_clear;
4375
4475
  }
4476
+ if (mt->tp_call != PrivateAttrType_call) {
4477
+ if (::AllData::all_type_call.find(id) == ::AllData::all_type_call.end()) {
4478
+ ::AllData::all_type_call[id] = get_type_need_tp_call(mt);
4479
+ }
4480
+ mt->tp_call = PrivateAttrType_call;
4481
+ }
4376
4482
  }
4377
4483
  Py_RETURN_NONE;
4378
4484
  }
@@ -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"
7
+ version = "2.2.8"
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"