private-attribute-cpp 3.0.2__tar.gz → 3.0.3__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 (21) hide show
  1. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/PKG-INFO +1 -1
  2. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/private_attribute.cpp +522 -177
  3. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/pyproject.toml +1 -1
  4. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/CMakeLists.txt +0 -0
  5. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/LICENSE +0 -0
  6. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/README.md +0 -0
  7. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/picosha2.h +0 -0
  8. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/private_attribute.pyi +0 -0
  9. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_attr_classify_owner.py +0 -0
  10. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_class_private_attrs.py +0 -0
  11. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_gc_collect.py +0 -0
  12. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_init_subclass.py +0 -0
  13. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_abc.py +0 -0
  14. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_attrs.py +0 -0
  15. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_func.py +0 -0
  16. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_inheritance.py +0 -0
  17. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_method.py +0 -0
  18. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_super.py +0 -0
  19. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_private_wrap.py +0 -0
  20. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/tests/test_slots_cycle.py +0 -0
  21. {private_attribute_cpp-3.0.2 → private_attribute_cpp-3.0.3}/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: 3.0.2
3
+ Version: 3.0.3
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
@@ -202,6 +202,23 @@ PyDict_ContainsString(PyObject *op, const char *key) noexcept
202
202
  #endif
203
203
 
204
204
  typedef struct PrivateAttrCreationData PrivateAttrCreationData;
205
+
206
+ // v4: clear the pending-creation back-pointer on the five per-type slot objects
207
+ // held by a PrivateAttrCreationData. Forward-declared here (the concrete
208
+ // PrivateAttrSlotHasData layout lives in the "v3 slot structs" section below);
209
+ // the implementation is placed after that layout is defined.
210
+ static void clear_slot_back_pointers(PyObjectStorage* slots, int count, PrivateAttrCreationData* expected) noexcept;
211
+ static bool clear_creation_data_from_slots(PyTypeObject* tp, PrivateAttrCreationData* expected) noexcept;
212
+ static PrivateAttrCreationData* get_creation_data(uintptr_t typ_id) noexcept;
213
+ // Count the slot objects in a type's tp_dict that are instances of
214
+ // PrivateAttrSlotType (the five structs injected by inject_type_slots),
215
+ // regardless of whether they still carry a pending PrivateAttrCreationData
216
+ // pointer. 0 means no per-type slot objects are present (the type either never
217
+ // went through our machinery, or its slots were removed). Combined with
218
+ // get_creation_data() returning NULL (no pending install) and the type being
219
+ // absent from all_type_attr_set, a non-zero count identifies a half-installed
220
+ // type whose install() never completed.
221
+ static int count_type_slot_objects(PyTypeObject* tp) noexcept;
205
222
  static std::string default_random_string(uintptr_t obj_id, const std::string& attr_name) noexcept;
206
223
 
207
224
  struct RestorePythonExceptionResult {
@@ -231,7 +248,7 @@ static setattrofunc original_type_tp_setattro = 0;
231
248
  static PyObject* make_type_slot(const char* name, PyObject* callback, PyObject* owner=NULL) noexcept;
232
249
  static PyObject* make_metaclass_slot(const char* name, PyObject* owner, PyObject* callback) noexcept;
233
250
  // inject the five special-method structs into a class namespace (attrs dict)
234
- static bool inject_type_slots(PyObject* attrs) noexcept;
251
+ static bool inject_type_slots(PyObject* attrs, PrivateAttrCreationData* data) noexcept;
235
252
  // install the six special-method structs into a registered (heap) metaclass dict
236
253
  static bool install_metaclass_slots(PyTypeObject* metaclass) noexcept;
237
254
  static PyObject* register_metaclass_head(PyObject* metaclass) noexcept;
@@ -241,6 +258,8 @@ static bool update_slot_callback(PyTypeObject* owner, const char* name, PyObject
241
258
  static bool is_confirmed_public(uintptr_t obj_id, const std::string& name) noexcept;
242
259
  static void mark_confirmed_public(uintptr_t obj_id, const std::string& name) noexcept;
243
260
  static void clear_public_obj(uintptr_t obj_id) noexcept;
261
+ static bool is_registered_type(PyObject* type) noexcept;
262
+ static bool is_private_meta_type(PyObject* typ) noexcept;
244
263
 
245
264
  struct is_private_class_control {
246
265
  PyObjectStorage weakref; // weakref to the type object
@@ -257,15 +276,13 @@ namespace {
257
276
  static std::unordered_map<uintptr_t, std::unordered_map<std::string, PyObjectStorage>> type_attr_dict;
258
277
  };
259
278
  static std::unordered_map<uintptr_t, std::unordered_map<uintptr_t, PyObjectStorage>> type_allowed_code_map;
260
- // 2.2.1: while a type is still being created (its id is not yet written
261
- // into all_type_attr_set), tmp_creation_data points at the in-flight
262
- // PrivateAttrCreationData. Accesses that happen inside type.tp_new (e.g.
263
- // a parent's __init_subclass__) read the private-attribute set and the
264
- // code-object map directly from tmp_creation_data until postprocess
265
- // promotes the data into the real AllData tables.
266
- static std::mutex type_creation_lock_mutex;
267
- static bool type_creation_locked = false;
268
- static PrivateAttrCreationData *tmp_creation_data = nullptr;
279
+ // v4: there is no global in-flight creation pointer or lock. The five
280
+ // special-method slot objects injected into a class namespace each carry
281
+ // a pointer to the stack-allocated PrivateAttrCreationData (see
282
+ // PrivateAttrSlotHasData). Accesses that happen inside type.tp_new (e.g.
283
+ // a parent's __init_subclass__) recover that pointer from the type's own
284
+ // tp_dict via get_creation_data() until install() promotes the data into
285
+ // the real AllData tables and clears the slot pointers.
269
286
 
270
287
  static std::unordered_map<uintptr_t, std::unique_ptr<std::shared_mutex>> all_type_mutex;
271
288
  static std::unordered_map<uintptr_t, PyObjectStorage> type_need_call;
@@ -384,35 +401,29 @@ struct PrivateAttrCreationData
384
401
  PyObjectStorage base_kwds = (PyObject*)nullptr;
385
402
  PyObjectStorage name = (PyObject*)nullptr;
386
403
  PyObjectStorage bases = (PyObject*)nullptr;
404
+ // v4: strong references to the five per-type slot objects injected into
405
+ // attrs_copy during preprocess. Held here so the destructor can clear their
406
+ // `data` back-pointer directly without re-reading the attrs dict (which may
407
+ // already be gone on some error paths).
408
+ PyObjectStorage slot_objs[5] = {
409
+ (PyObject*)nullptr, (PyObject*)nullptr, (PyObject*)nullptr,
410
+ (PyObject*)nullptr, (PyObject*)nullptr
411
+ };
387
412
  bool use_parents = false;
388
413
  bool cleared = false;
389
- std::unique_lock<std::mutex> lock_;
390
- bool locked = false;
391
414
  bool installed = false;
392
- PrivateAttrCreationData() noexcept : lock_(::AllData::type_creation_lock_mutex, std::defer_lock) {}
393
-
394
- void lock() noexcept {
395
- if (!locked) {
396
- lock_.lock();
397
- AllData::type_creation_locked = true;
398
- locked = true;
399
- AllData::tmp_creation_data = this;
400
- }
401
- }
402
-
403
- void unlock() noexcept {
404
- if (locked) {
405
- AllData::tmp_creation_data = nullptr;
406
- AllData::type_creation_locked = false;
407
- locked = false;
408
- lock_.unlock();
409
- }
410
- }
411
415
 
412
416
  void clear() noexcept {
413
417
  if (cleared) {
414
418
  return;
415
419
  }
420
+ // v4: clear the back-pointer on the five slot objects we injected (we
421
+ // hold strong refs to them directly, so no need to re-read the attrs
422
+ // dict). These are always PrivateAttrSlotType per-type slots created by
423
+ // inject_type_slots. This prevents the stack-allocated
424
+ // PrivateAttrCreationData from dangling if creation was interrupted
425
+ // before install() ran.
426
+ clear_slot_back_pointers(slot_objs, 5, this);
416
427
  name = (PyObject*)nullptr;
417
428
  bases = (PyObject*)nullptr;
418
429
  attrs_copy = (PyObject*)nullptr;
@@ -421,9 +432,6 @@ struct PrivateAttrCreationData
421
432
 
422
433
  need_remove_itself.clear();
423
434
  code_map.clear();
424
- if (locked) {
425
- unlock();
426
- }
427
435
  cleared = true;
428
436
  }
429
437
 
@@ -438,6 +446,18 @@ struct PrivateAttrCreationData
438
446
  }
439
447
 
440
448
  PyTypeObject* type_instance = (PyTypeObject*)new_type;
449
+
450
+ // v4: the slot objects (not a global pointer/lock) carry the pending
451
+ // install state. Detect a pending install by scanning the five slot
452
+ // objects in the type's tp_dict; if none has a non-null pointer there
453
+ // is nothing to do. Then clear all slot pointers unconditionally so the
454
+ // stack-allocated PrivateAttrCreationData can never dangle even if the
455
+ // install below fails partway.
456
+ if (!get_creation_data((uintptr_t)type_instance)) {
457
+ return true; // no pending install
458
+ }
459
+ clear_creation_data_from_slots(type_instance, nullptr);
460
+
441
461
  uintptr_t type_id = (uintptr_t)(type_instance);
442
462
  int tp_dict_has_static_attr = PyDict_ContainsString(type_instance->tp_dict, "__static_attributes__");
443
463
  if (tp_dict_has_static_attr < 0) {
@@ -513,7 +533,6 @@ struct PrivateAttrCreationData
513
533
  if (private_func) {
514
534
  ::AllData::type_need_call[type_id] = private_func;
515
535
  }
516
- unlock();
517
536
  installed = true;
518
537
  return true;
519
538
  }
@@ -528,20 +547,62 @@ static bool
528
547
  try_install_pending_creation(PyObject* new_type) noexcept
529
548
  {
530
549
  // Finish a pending private-attribute creation if one is still
531
- // outstanding. prepare()/PrivateAttrType_new leave tmp_creation_data
532
- // locked (type_creation_locked == true) until install() runs; if
533
- // creation is interrupted (e.g. an exception in __init_subclass__) the
534
- // type exists but is not yet registered in all_type_attr_set. The
535
- // metaclass getattr/setattr/call slots check this first and promote the
536
- // half-created type before proceeding.
537
- if (!new_type || !::AllData::tmp_creation_data) {
538
- return true;;
539
- }
540
- if (::AllData::type_creation_locked &&
541
- ::AllData::all_type_attr_set.find((uintptr_t)new_type) == ::AllData::all_type_attr_set.end()) {
542
- return ::AllData::tmp_creation_data->install(new_type);
550
+ // outstanding. prepare()/PrivateAttrType_new leave the five slot
551
+ // objects carrying a pointer to the in-flight PrivateAttrCreationData
552
+ // until install() runs; if creation is interrupted (e.g. an exception in
553
+ // __init_subclass__) the type exists but is not yet registered in
554
+ // all_type_attr_set. The metaclass getattr/setattr/call slots check this
555
+ // first and promote the half-created type before proceeding.
556
+ if (!new_type || !PyType_Check(new_type)) {
557
+ return true;
543
558
  }
544
- return true;
559
+ if (::AllData::all_type_attr_set.find((uintptr_t)new_type) != ::AllData::all_type_attr_set.end()) {
560
+ return true; // already installed
561
+ }
562
+ PrivateAttrCreationData* data = get_creation_data((uintptr_t)new_type);
563
+ if (!data) {
564
+ // No in-flight creation data is reachable from the type's slot
565
+ // objects. If the slot objects are nevertheless present (their data
566
+ // pointers have all been cleared) the type was left half-created:
567
+ // the slot structs were injected into its namespace but install()
568
+ // never completed, so it must NOT be allowed to proceed (no
569
+ // half-finished type may be created from or controlled). If there
570
+ // are no slot objects either, this is simply not one of our types.
571
+ if (count_type_slot_objects((PyTypeObject*)new_type) > 0) {
572
+ // Slot objects present but no pending pointer -> unrecoverable
573
+ // half-installed type.
574
+ PyErr_Format(PyExc_RuntimeError,
575
+ "private-attribute type '%s' is not fully installed and cannot be used",
576
+ ((PyTypeObject*)new_type)->tp_name);
577
+ return false;
578
+ }
579
+ return true;
580
+ }
581
+ return data->install(new_type);
582
+ }
583
+
584
+ // Check whether a single base type is a half-installed, unusable private type:
585
+ // it is NOT registered in all_type_attr_set (not truly installed), it has no
586
+ // pending install left (get_creation_data returns NULL), yet its per-type slot
587
+ // objects are still present in its tp_dict. Such a type can never be completed
588
+ // (its creation data is gone), so it must not be subclassed. Returns true only
589
+ // for that unrecoverable case; pending exceptions are left untouched.
590
+ static bool
591
+ is_uninstallable_private_type(PyObject* base) noexcept
592
+ {
593
+ if (!base || !PyType_Check(base)) {
594
+ return false;
595
+ }
596
+ // Already truly installed -> fine.
597
+ if (::AllData::all_type_attr_set.find((uintptr_t)base) != ::AllData::all_type_attr_set.end()) {
598
+ return false;
599
+ }
600
+ // Still has a pending install -> recoverable, not uninstallable.
601
+ if (get_creation_data((uintptr_t)base)) {
602
+ return false;
603
+ }
604
+ // Slot objects present but no pending data -> half-installed, unrecoverable.
605
+ return count_type_slot_objects((PyTypeObject*)base) > 0;
545
606
  }
546
607
 
547
608
  static inline void
@@ -597,8 +658,6 @@ clean_all_storages() noexcept
597
658
  AllData::obj_attr_keys.clear();
598
659
  AllData::type_attr_dict.clear();
599
660
  AllData::type_allowed_code_map.clear();
600
- AllData::type_creation_locked = false;
601
- AllData::tmp_creation_data = nullptr;
602
661
  AllData::all_type_mutex.clear();
603
662
  AllData::type_need_call.clear();
604
663
  AllData::all_type_attr_set.clear();
@@ -679,13 +738,14 @@ is_class_code(uintptr_t typ_id, PyCodeObject* code) noexcept
679
738
  return true;
680
739
  }
681
740
  }
682
- // 2.2.1: fall back to the in-creation code-object map while a type has not
741
+ // v4: fall back to the in-creation code-object map while a type has not
683
742
  // been fully registered yet (e.g. inside type.tp_new / a parent's
684
- // __init_subclass__).
685
- if (::AllData::type_creation_locked && ::AllData::all_type_attr_set.find(typ_id) == ::AllData::all_type_attr_set.end()){
743
+ // __init_subclass__). The in-flight data is recovered from the slot objects
744
+ // in the type's own tp_dict.
745
+ if (::AllData::all_type_attr_set.find(typ_id) == ::AllData::all_type_attr_set.end()){
746
+ PrivateAttrCreationData* data = get_creation_data(typ_id);
686
747
  uintptr_t code_id = (uintptr_t)code;
687
- if (::AllData::tmp_creation_data &&
688
- ::AllData::tmp_creation_data->code_map.find(code_id) != ::AllData::tmp_creation_data->code_map.end()) {
748
+ if (data && data->code_map.find(code_id) != data->code_map.end()) {
689
749
  return true;
690
750
  }
691
751
  }
@@ -698,9 +758,10 @@ get_type_need_call(uintptr_t typ_id) noexcept
698
758
  if (::AllData::type_need_call.find(typ_id) != ::AllData::type_need_call.end()){
699
759
  return ::AllData::type_need_call[typ_id];
700
760
  }
701
- if (::AllData::type_creation_locked && ::AllData::all_type_attr_set.find(typ_id) == ::AllData::all_type_attr_set.end()) {
702
- if (::AllData::tmp_creation_data && ::AllData::tmp_creation_data->private_func) {
703
- return ::AllData::tmp_creation_data->private_func;
761
+ if (::AllData::all_type_attr_set.find(typ_id) == ::AllData::all_type_attr_set.end()) {
762
+ PrivateAttrCreationData* data = get_creation_data(typ_id);
763
+ if (data && data->private_func) {
764
+ return data->private_func;
704
765
  }
705
766
  }
706
767
  return nullptr;
@@ -716,12 +777,13 @@ is_type_private(uintptr_t typ_id, const std::string& name) noexcept
716
777
  return true;
717
778
  }
718
779
  }
719
- // 2.2.1: if a type has not been registered yet (its id is absent from
720
- // all_type_attr_set), read from the in-creation storage instead.
721
- else if (::AllData::type_creation_locked && ::AllData::all_type_attr_set.find(typ_id) == ::AllData::all_type_attr_set.end()) {
780
+ // v4: if a type has not been registered yet (its id is absent from
781
+ // all_type_attr_set), read from the in-creation storage instead (recovered
782
+ // from the type's slot objects).
783
+ else if (::AllData::all_type_attr_set.find(typ_id) == ::AllData::all_type_attr_set.end()) {
784
+ PrivateAttrCreationData* data = get_creation_data(typ_id);
722
785
  TwoStringTuple key = get_string_hash_tuple2(name);
723
- if (::AllData::tmp_creation_data &&
724
- ::AllData::tmp_creation_data->private_attrs_set.find(key) != ::AllData::tmp_creation_data->private_attrs_set.end()){
786
+ if (data && data->private_attrs_set.find(key) != data->private_attrs_set.end()){
725
787
  return true;
726
788
  }
727
789
  }
@@ -2186,12 +2248,12 @@ static PyTypeObject PrivateClassSuperType = {
2186
2248
 
2187
2249
  // Index of `id` in `mro` (a private-attribute ancestor list in MRO order,
2188
2250
  // nearest first), or -1 if absent.
2189
- static int
2251
+ static Py_ssize_t
2190
2252
  mro_index_of(const std::vector<uintptr_t>& mro, uintptr_t id) noexcept
2191
2253
  {
2192
- for (size_t i = 0; i < mro.size(); ++i) {
2254
+ for (Py_ssize_t i = 0; i < mro.size(); ++i) {
2193
2255
  if (mro[i] == id) {
2194
- return static_cast<int>(i);
2256
+ return i;
2195
2257
  }
2196
2258
  }
2197
2259
  return -1;
@@ -2232,7 +2294,7 @@ private_super_validate(PrivateSuperObject* self, bool is_class) noexcept
2232
2294
  if (!a_parents.ok) {
2233
2295
  return false;
2234
2296
  }
2235
- int b_idx = mro_index_of(*a_parents.ids, b_id);
2297
+ Py_ssize_t b_idx = mro_index_of(*a_parents.ids, b_id);
2236
2298
  if (b_idx < 0) {
2237
2299
  PyErr_SetString(PyExc_ValueError, "B must be a parent type of A");
2238
2300
  return false;
@@ -2306,11 +2368,11 @@ private_super_getattro(PyObject* self, PyObject* name, bool is_class) noexcept
2306
2368
  // The running code must belong to A.
2307
2369
  PyObjectStorage now_code = PyObjectStorage::from_new_ref(get_now_code());
2308
2370
  if (!now_code) {
2309
- PyErr_SetString(PyExc_AttributeError, "private attribute");
2371
+ PyErr_SetString(PyExc_AttributeError, "cannot use 'PrivateSuper' or 'PrivateClassSuper' outside the class");
2310
2372
  return NULL;
2311
2373
  }
2312
2374
  if (!is_class_code((uintptr_t)s->A, (PyCodeObject*)now_code.get())) {
2313
- PyErr_SetString(PyExc_AttributeError, "private attribute");
2375
+ PyErr_SetString(PyExc_AttributeError, "cannot use 'PrivateSuper' or 'PrivateClassSuper' outside the class");
2314
2376
  return NULL;
2315
2377
  }
2316
2378
 
@@ -2347,7 +2409,7 @@ private_super_getattro(PyObject* self, PyObject* name, bool is_class) noexcept
2347
2409
  key = default_random_string(target_type, attr_name);
2348
2410
  }
2349
2411
 
2350
- PyObject* value = NULL;
2412
+ PyObjectStorage value;
2351
2413
  if (main_type == target_type) {
2352
2414
  type_create_mutex(target_type);
2353
2415
  std::shared_lock<std::shared_mutex> lock(*::AllData::all_type_mutex[target_type]);
@@ -2361,8 +2423,7 @@ private_super_getattro(PyObject* self, PyObject* name, bool is_class) noexcept
2361
2423
  PyErr_Format(PyExc_AttributeError, "'%s' has no class attribute '%s'", tn, attr_name.c_str());
2362
2424
  return NULL;
2363
2425
  }
2364
- value = it->second.get();
2365
- Py_XINCREF(value);
2426
+ value = it->second;
2366
2427
  } else {
2367
2428
  type_parenttype_create_mutex(main_type, target_type);
2368
2429
  std::shared_lock<std::shared_mutex> lock(*::AllData::all_type_subclass_mutex[main_type][target_type]);
@@ -2376,8 +2437,11 @@ private_super_getattro(PyObject* self, PyObject* name, bool is_class) noexcept
2376
2437
  PyErr_Format(PyExc_AttributeError, "'%s' has no class attribute '%s'", tn, attr_name.c_str());
2377
2438
  return NULL;
2378
2439
  }
2379
- value = it->second.get();
2380
- Py_XINCREF(value);
2440
+ value = it->second;
2441
+ }
2442
+ if (!value) {
2443
+ PyErr_Format(PyExc_SystemError, "%s (%d): attribute is NULL", __FILE__, __LINE__);
2444
+ return NULL;
2381
2445
  }
2382
2446
 
2383
2447
  // Invoke the descriptor's __get__: PrivateSuper -> __get__(obj, A);
@@ -2387,9 +2451,9 @@ private_super_getattro(PyObject* self, PyObject* name, bool is_class) noexcept
2387
2451
  descrgetfunc get = Py_TYPE(value)->tp_descr_get;
2388
2452
  if (get) {
2389
2453
  PyObject* r = get(value, inst, owner);
2390
- Py_DECREF(value);
2391
2454
  return r;
2392
2455
  }
2456
+ Py_INCREF(value);
2393
2457
  return value;
2394
2458
  }
2395
2459
 
@@ -2496,13 +2560,23 @@ typedef struct PrivateAttrSlot {
2496
2560
  int is_metaclass; // 1 for metaclass slots, 0 for type slots
2497
2561
  } PrivateAttrSlot;
2498
2562
 
2563
+ // v4: the three type objects (PrivateAttrSlotType / PrivateAttrMetaSlotType /
2564
+ // PrivateAttrMetaNewSlotType) share the PrivateAttrSlot layout. Only the
2565
+ // plain per-type slot objects (PrivateAttrSlotType) carry a pending creation
2566
+ // data pointer; the metaclass slot types never have a pending install, so they
2567
+ // keep the base size and `data` is simply never written for them.
2568
+ typedef struct {
2569
+ PrivateAttrSlot pa;
2570
+ PrivateAttrCreationData* data;
2571
+ } PrivateAttrSlotHasData;
2572
+
2499
2573
  static void PrivateAttrType_finalize(PyObject* cls) noexcept;
2500
2574
  static void clear_object_related(uintptr_t id_self, uintptr_t typ_id) noexcept;
2501
2575
  static void PrivateAttr_object_init_private_dict(uintptr_t obj_id, uintptr_t type_id) noexcept;
2502
2576
  static bool try_install_pending_creation(PyObject* new_type) noexcept;
2503
2577
 
2504
2578
  static PyMemberDef PrivateAttrSlot_members[] = {
2505
- {"__wrapt__", T_OBJECT_EX, offsetof(PrivateAttrSlot, callback), 0, NULL},
2579
+ {"__wrapped__", T_OBJECT_EX, offsetof(PrivateAttrSlot, callback), 0, NULL},
2506
2580
  {NULL}
2507
2581
  };
2508
2582
 
@@ -2526,6 +2600,10 @@ PrivateAttrSlot_get_name(PrivateAttrSlot* self, void * /*closure*/) noexcept
2526
2600
  if (result) {return result;}
2527
2601
  else PyErr_Clear();
2528
2602
  }
2603
+ if (self->slot_name) {
2604
+ Py_INCREF(self->slot_name);
2605
+ return self->slot_name;
2606
+ }
2529
2607
  return PyUnicode_FromString("");
2530
2608
  }
2531
2609
 
@@ -2537,6 +2615,14 @@ PrivateAttrSlot_get_qualname(PrivateAttrSlot* self, void * /*closure*/) noexcept
2537
2615
  if (result) {return result;}
2538
2616
  else PyErr_Clear();
2539
2617
  }
2618
+ if (self->slot_name) {
2619
+ const char* func_name = PyUnicode_AsUTF8(self->slot_name);
2620
+ const char* type_name = "<unknown>";
2621
+ if (self->owner_type && PyType_Check(self->owner_type)) {
2622
+ type_name = get_name_from_tp_name((PyTypeObject*)self->owner_type);
2623
+ }
2624
+ return PyUnicode_FromFormat("%s.%s", type_name, func_name);
2625
+ }
2540
2626
  return PyUnicode_FromString("");
2541
2627
  }
2542
2628
 
@@ -2604,6 +2690,19 @@ static PyGetSetDef PrivateAttrSlot_getsetters[] = {
2604
2690
  {NULL}
2605
2691
  };
2606
2692
 
2693
+ static PyObject*
2694
+ PrivateAttrSlot_getattro(PyObject* obj, PyObject* name) noexcept
2695
+ {
2696
+ PrivateAttrSlot* self = (PrivateAttrSlot*)obj;
2697
+ PyObject* first_result = PyObject_GenericGetAttr(obj, name);
2698
+ if (first_result) return first_result;
2699
+ else if (self->callback) {
2700
+ PyErr_Clear();
2701
+ return PyObject_GetAttr((PyObject*)self->callback, name);
2702
+ }
2703
+ return NULL;
2704
+ }
2705
+
2607
2706
  // --- public non-private-name cache ---
2608
2707
  static bool
2609
2708
  is_confirmed_public(uintptr_t obj_id, const std::string& name) noexcept
@@ -2803,7 +2902,7 @@ slot_new_impl(PrivateAttrSlot* self, PyObject* cls, PyObject* args, PyObject* kw
2803
2902
  if (!final_result) return NULL;
2804
2903
  if (!PyType_Check(final_result)) {
2805
2904
  Py_DECREF(final_result);
2806
- PyErr_SetString(PyExc_TypeError, "__new__ must return a type");
2905
+ PyErr_SetString(PyExc_TypeError, "metaclass.__new__ must return a type");
2807
2906
  return NULL;
2808
2907
  }
2809
2908
  if (!data.install(final_result)) {
@@ -2823,8 +2922,20 @@ slot_getattribute_impl(PrivateAttrSlot* self, PyObject* obj, PyObject* name) noe
2823
2922
  uintptr_t type_id = slot_obj_type_id(obj, self->is_metaclass);
2824
2923
 
2825
2924
  if (self->is_metaclass) {
2925
+ if (!is_private_meta_type((PyObject*)Py_TYPE(obj))) {
2926
+ PyErr_SetString(PyExc_TypeError,
2927
+ "The metaclass is not a register type or a subclass of the PrivateAttrType! You may put it on a wrong place");
2928
+ return NULL;
2929
+ }
2826
2930
  if (!try_install_pending_creation(obj)) return NULL;
2827
2931
  } else {
2932
+ auto check_need_analyse = need_analyse_type((PyObject*)Py_TYPE(obj));
2933
+ if (check_need_analyse == AnalyseStatus::Error) {return NULL;}
2934
+ if (check_need_analyse == AnalyseStatus::No) {
2935
+ PyErr_SetString(PyExc_TypeError,
2936
+ "The class is not an instance of register type or the PrivateAttrType! You may put it on a wrong place");
2937
+ return NULL;
2938
+ }
2828
2939
  PrivateAttr_object_init_private_dict(obj_id, type_id);
2829
2940
  }
2830
2941
 
@@ -2868,6 +2979,22 @@ slot_getattr_impl(PrivateAttrSlot* self, PyObject* obj, PyObject* name) noexcept
2868
2979
  if (!c) return NULL;
2869
2980
  std::string name_str(c);
2870
2981
 
2982
+ if (self->is_metaclass) {
2983
+ if (!is_private_meta_type((PyObject*)Py_TYPE(obj))) {
2984
+ PyErr_SetString(PyExc_TypeError,
2985
+ "The metaclass is not a register type or a subclass of the PrivateAttrType! You may put it on a wrong place");
2986
+ return NULL;
2987
+ }
2988
+ } else {
2989
+ auto check_need_analyse = need_analyse_type((PyObject*)Py_TYPE(obj));
2990
+ if (check_need_analyse == AnalyseStatus::Error) {return NULL;}
2991
+ if (check_need_analyse == AnalyseStatus::No) {
2992
+ PyErr_SetString(PyExc_TypeError,
2993
+ "The class is not an instance of register type or the PrivateAttrType! You may put it on a wrong place");
2994
+ return NULL;
2995
+ }
2996
+ }
2997
+
2871
2998
  // Decide the error message: if the name is private, __getattr__ must NOT
2872
2999
  // walk the subsequent flow (callback / super) — go straight to an error.
2873
3000
  {
@@ -2945,8 +3072,20 @@ slot_setattr_impl(PrivateAttrSlot* self, PyObject* obj, PyObject* name, PyObject
2945
3072
  uintptr_t type_id = slot_obj_type_id(obj, self->is_metaclass);
2946
3073
 
2947
3074
  if (self->is_metaclass) {
3075
+ if (!is_private_meta_type((PyObject*)Py_TYPE(obj))) {
3076
+ PyErr_SetString(PyExc_TypeError,
3077
+ "The metaclass is not a register type or a subclass of the PrivateAttrType! You may put it on a wrong place");
3078
+ return NULL;
3079
+ }
2948
3080
  if (!try_install_pending_creation(obj)) return NULL;
2949
3081
  } else {
3082
+ auto check_need_analyse = need_analyse_type((PyObject*)Py_TYPE(obj));
3083
+ if (check_need_analyse == AnalyseStatus::Error) {return NULL;}
3084
+ if (check_need_analyse == AnalyseStatus::No) {
3085
+ PyErr_SetString(PyExc_TypeError,
3086
+ "The class is not an instance of register type or the PrivateAttrType! You may put it on a wrong place");
3087
+ return NULL;
3088
+ }
2950
3089
  PrivateAttr_object_init_private_dict(obj_id, type_id);
2951
3090
  }
2952
3091
 
@@ -2998,8 +3137,20 @@ slot_delattr_impl(PrivateAttrSlot* self, PyObject* obj, PyObject* name) noexcept
2998
3137
  uintptr_t type_id = slot_obj_type_id(obj, self->is_metaclass);
2999
3138
 
3000
3139
  if (self->is_metaclass) {
3140
+ if (!is_private_meta_type((PyObject*)Py_TYPE(obj))) {
3141
+ PyErr_SetString(PyExc_TypeError,
3142
+ "The metaclass is not a register type or a subclass of the PrivateAttrType! You may put it on a wrong place");
3143
+ return NULL;
3144
+ }
3001
3145
  if (!try_install_pending_creation(obj)) return NULL;
3002
3146
  } else {
3147
+ auto check_need_analyse = need_analyse_type((PyObject*)Py_TYPE(obj));
3148
+ if (check_need_analyse == AnalyseStatus::Error) {return NULL;}
3149
+ if (check_need_analyse == AnalyseStatus::No) {
3150
+ PyErr_SetString(PyExc_TypeError,
3151
+ "The class is not an instance of register type or the PrivateAttrType! You may put it on a wrong place");
3152
+ return NULL;
3153
+ }
3003
3154
  PrivateAttr_object_init_private_dict(obj_id, type_id);
3004
3155
  }
3005
3156
 
@@ -3046,6 +3197,21 @@ slot_delattr_impl(PrivateAttrSlot* self, PyObject* obj, PyObject* name) noexcept
3046
3197
  static PyObject*
3047
3198
  slot_del_impl(PrivateAttrSlot* self, PyObject* obj) noexcept
3048
3199
  {
3200
+ if (self->is_metaclass) {
3201
+ if (!is_private_meta_type((PyObject*)Py_TYPE(obj))) {
3202
+ PyErr_SetString(PyExc_TypeError,
3203
+ "The metaclass is not a register type or a subclass of the PrivateAttrType! You may put it on a wrong place");
3204
+ return NULL;
3205
+ }
3206
+ } else {
3207
+ auto check_need_analyse = need_analyse_type((PyObject*)Py_TYPE(obj));
3208
+ if (check_need_analyse == AnalyseStatus::Error) {return NULL;}
3209
+ if (check_need_analyse == AnalyseStatus::No) {
3210
+ PyErr_SetString(PyExc_TypeError,
3211
+ "The class is not an instance of register type or the PrivateAttrType! You may put it on a wrong place");
3212
+ return NULL;
3213
+ }
3214
+ }
3049
3215
  bool should_return_null = false;
3050
3216
  auto original_refcnt = Py_REFCNT(obj);
3051
3217
  if (self->callback) {
@@ -3175,7 +3341,7 @@ static PyMethodDef PrivateAttrSlot_methods[] = {
3175
3341
  static PyTypeObject PrivateAttrSlotType = {
3176
3342
  PyVarObject_HEAD_INIT(NULL, 0)
3177
3343
  "private_attribute._PrivateAttrSlot", // tp_name
3178
- sizeof(PrivateAttrSlot), // tp_basicsize
3344
+ sizeof(PrivateAttrSlotHasData), // tp_basicsize
3179
3345
  0, // tp_itemsize
3180
3346
  (destructor)slot_dealloc, // tp_dealloc
3181
3347
  0, // tp_print
@@ -3189,7 +3355,7 @@ static PyTypeObject PrivateAttrSlotType = {
3189
3355
  0, // tp_hash
3190
3356
  (ternaryfunc)slot_call, // tp_call
3191
3357
  0, // tp_str
3192
- 0, // tp_getattro
3358
+ PrivateAttrSlot_getattro, // tp_getattro
3193
3359
  0, // tp_setattro
3194
3360
  0, // tp_as_buffer
3195
3361
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
@@ -3226,7 +3392,7 @@ static PyTypeObject PrivateAttrMetaSlotType = {
3226
3392
  0, // tp_hash
3227
3393
  (ternaryfunc)slot_call, // tp_call
3228
3394
  0, // tp_str
3229
- 0, // tp_getattro
3395
+ PrivateAttrSlot_getattro, // tp_getattro
3230
3396
  0, // tp_setattro
3231
3397
  0, // tp_as_buffer
3232
3398
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
@@ -3263,7 +3429,7 @@ static PyTypeObject PrivateAttrMetaNewSlotType = {
3263
3429
  0, // tp_hash
3264
3430
  (ternaryfunc)slot_new_call, // tp_call
3265
3431
  0, // tp_str
3266
- 0, // tp_getattro
3432
+ PrivateAttrSlot_getattro, // tp_getattro
3267
3433
  0, // tp_setattro
3268
3434
  0, // tp_as_buffer
3269
3435
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
@@ -3279,6 +3445,23 @@ static PyTypeObject PrivateAttrMetaNewSlotType = {
3279
3445
  PrivateAttrSlot_getsetters, // tp_getset
3280
3446
  };
3281
3447
 
3448
+ // v4: clear the pending-creation back-pointer on `count` slot objects. If
3449
+ // `expected` is non-NULL, only slots whose data equals `expected` are cleared.
3450
+ static void
3451
+ clear_slot_back_pointers(PyObjectStorage* slots, int count, PrivateAttrCreationData* expected) noexcept
3452
+ {
3453
+ for (int i = 0; i < count; i++) {
3454
+ PyObject* slot = slots[i].get();
3455
+ if (slot && PyObject_IsInstance(slot, (PyObject*)&PrivateAttrSlotType)) {
3456
+ PrivateAttrSlotHasData* s = (PrivateAttrSlotHasData*)slot;
3457
+ if (!expected || s->data == expected) {
3458
+ s->data = nullptr;
3459
+ }
3460
+ }
3461
+ slots[i] = (PyObject*)nullptr;
3462
+ }
3463
+ }
3464
+
3282
3465
  static const char* type_slot_names[] = {
3283
3466
  "__getattribute__", "__getattr__", "__setattr__", "__delattr__", "__del__", NULL
3284
3467
  };
@@ -3297,6 +3480,11 @@ slot_new_common(const char* name, PyObject* owner, PyObject* callback, int is_me
3297
3480
  s->owner_type = owner ? (PyObject*)Py_NewRef(owner) : NULL;
3298
3481
  s->callback = callback ? (PyObject*)Py_NewRef(callback) : NULL;
3299
3482
  s->is_metaclass = is_metaclass;
3483
+ // v4: the extended layout only exists on per-type slots; the pending
3484
+ // creation pointer starts NULL and is written during inject_type_slots.
3485
+ if (!is_metaclass) {
3486
+ ((PrivateAttrSlotHasData*)s)->data = nullptr;
3487
+ }
3300
3488
  if (!s->slot_name) {
3301
3489
  Py_DECREF(s);
3302
3490
  return NULL;
@@ -3426,29 +3614,32 @@ make_metaclass_slot(const char* name, PyObject* owner, PyObject* callback) noexc
3426
3614
  // Inject the special-method structs into a class namespace (attrs dict). For
3427
3615
  // each special name present in attrs, the user's value becomes the callback;
3428
3616
  // the struct replaces it in the dict so type_new wires the slots via
3429
- // update_one_slot and invokes __set_name__.
3617
+ // update_one_slot and invokes __set_name__. v4: each injected per-type slot
3618
+ // also stores a pointer to `data` so install() can be triggered later from the
3619
+ // type's own tp_dict without any global pointer.
3430
3620
  static bool
3431
- inject_type_slots(PyObject* attrs) noexcept
3621
+ inject_type_slots(PyObject* attrs, PrivateAttrCreationData* data) noexcept
3432
3622
  {
3433
3623
  for (int i = 0; type_slot_names[i]; i++) {
3434
3624
  const char* nm = type_slot_names[i];
3435
- int has = PyDict_ContainsString(attrs, nm);
3436
- if (has < 0) return false;
3437
- PyObject* callback = NULL;
3438
- if (has) {
3439
- callback = PyDict_GetItemString(attrs, nm);
3440
- }
3625
+ PyObject* callback = PyDict_GetItemString(attrs, nm);
3441
3626
  while (callback && (
3442
3627
  PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaSlotType) ||
3443
- PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaNewSlotType)
3628
+ PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaNewSlotType) ||
3629
+ (PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType) && ((PrivateAttrSlot*)callback)->owner_type != NULL)
3444
3630
  )) {
3445
3631
  callback = ((PrivateAttrSlot*)callback) -> callback;
3446
3632
  }
3447
- if (callback && PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType)) {
3633
+ if (callback && PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType) && ((PrivateAttrSlot*)callback)->owner_type == NULL) {
3448
3634
  continue;
3449
3635
  }
3450
3636
  PyObject* slot = make_type_slot(nm, callback, NULL);
3451
3637
  if (!slot) return false;
3638
+ // v4: record the pending creation data on the freshly created slot.
3639
+ ((PrivateAttrSlotHasData*)slot)->data = data;
3640
+ // Hold a strong reference in the creation data so the destructor can
3641
+ // clear the back-pointer directly (no attrs-dict re-read needed).
3642
+ data->slot_objs[i] = slot;
3452
3643
  int r = PyDict_SetItemString(attrs, nm, slot);
3453
3644
  Py_DECREF(slot);
3454
3645
  if (r < 0) return false;
@@ -3456,6 +3647,92 @@ inject_type_slots(PyObject* attrs) noexcept
3456
3647
  return true;
3457
3648
  }
3458
3649
 
3650
+ // Return the pending PrivateAttrCreationData for a (possibly still-being-
3651
+ // created) type by scanning the five per-type slot objects stored in the type's
3652
+ // tp_dict. Returns NULL when none of them points at a pending data object.
3653
+ static PrivateAttrCreationData*
3654
+ get_creation_data(uintptr_t typ_id) noexcept
3655
+ {
3656
+ PyObject* obj = (PyObject*)typ_id;
3657
+ if (!PyType_Check(obj)) {
3658
+ return nullptr;
3659
+ }
3660
+ PyTypeObject* tp = (PyTypeObject*)obj;
3661
+ PyObject* dict = tp->tp_dict;
3662
+ if (!dict) {
3663
+ return nullptr;
3664
+ }
3665
+ for (int i = 0; type_slot_names[i]; i++) {
3666
+ PyObject* slot = PyDict_GetItemString(dict, type_slot_names[i]);
3667
+ if (slot && PyObject_IsInstance(slot, (PyObject*)&PrivateAttrSlotType)) {
3668
+ PrivateAttrCreationData* data = ((PrivateAttrSlotHasData*)slot)->data;
3669
+ if (data) {
3670
+ return data;
3671
+ }
3672
+ }
3673
+ }
3674
+ return nullptr;
3675
+ }
3676
+
3677
+ // Clear the pending creation pointer from every per-type slot object stored in
3678
+ // the type's tp_dict. If `expected` is non-NULL, only clears slots whose data
3679
+ // pointer equals `expected`; returns true iff at least one such slot was found
3680
+ // (i.e. this type still had a pending install for `expected`). When `expected`
3681
+ // is NULL, every slot pointer is cleared unconditionally and the return value
3682
+ // is always true.
3683
+ static bool
3684
+ clear_creation_data_from_slots(PyTypeObject* tp, PrivateAttrCreationData* expected) noexcept
3685
+ {
3686
+ if (!tp) {
3687
+ return false;
3688
+ }
3689
+ PyObject* dict = tp->tp_dict;
3690
+ if (!dict) {
3691
+ return false;
3692
+ }
3693
+ bool found = false;
3694
+ for (int i = 0; type_slot_names[i]; i++) {
3695
+ PyObject* slot = PyDict_GetItemString(dict, type_slot_names[i]);
3696
+ if (!slot || !PyObject_IsInstance(slot, (PyObject*)&PrivateAttrSlotType)) {
3697
+ continue;
3698
+ }
3699
+ PrivateAttrSlotHasData* s = (PrivateAttrSlotHasData*)slot;
3700
+ if (!expected || s->data == expected) {
3701
+ s->data = nullptr;
3702
+ found = true;
3703
+ }
3704
+ }
3705
+ return found;
3706
+ }
3707
+
3708
+ // Count the per-type slot objects in `tp`'s tp_dict (the five structs injected
3709
+ // by inject_type_slots(), each an instance of PrivateAttrSlotType), regardless
3710
+ // of whether they still carry a pending PrivateAttrCreationData pointer. A
3711
+ // non-zero count means the type went through our slot-injection machinery. When
3712
+ // combined with get_creation_data() returning NULL (no pending install left)
3713
+ // AND the type not being present in all_type_attr_set, this identifies a
3714
+ // half-installed type whose install() never completed.
3715
+ static int count_type_slot_objects(PyTypeObject* tp) noexcept
3716
+ {
3717
+ if (!tp) {
3718
+ return 0;
3719
+ }
3720
+ PyObject* dict = tp->tp_dict;
3721
+ if (!dict) {
3722
+ return 0;
3723
+ }
3724
+ int count = 0;
3725
+ for (int i = 0; type_slot_names[i]; i++) {
3726
+ PyObject* slot = PyDict_GetItemString(dict, type_slot_names[i]);
3727
+ if (slot && PyObject_IsInstance(slot, (PyObject*)&PrivateAttrSlotType)) {
3728
+ count++;
3729
+ }
3730
+ }
3731
+ return count;
3732
+ }
3733
+
3734
+
3735
+
3459
3736
  // Install the special-method structs into a registered metaclass's dict.
3460
3737
  // The owner is passed explicitly. If the metaclass already defines one of the
3461
3738
  // names, its value becomes the callback. Must use the ORIGINAL type_setattro
@@ -3469,17 +3746,21 @@ install_metaclass_slots(PyTypeObject* metaclass) noexcept
3469
3746
  setattrofunc real_setattro = original_type_tp_setattro ? original_type_tp_setattro : PyType_Type.tp_setattro;
3470
3747
  for (int i = 0; metaclass_slot_names[i]; i++) {
3471
3748
  const char* nm = metaclass_slot_names[i];
3472
- int has = PyDict_ContainsString(dict, nm);
3473
- if (has < 0) return false;
3474
3749
  PyObject* callback = NULL;
3475
- if (has) {
3476
- PyObject* existing = PyDict_GetItemString(dict, nm);
3477
- if (!existing) return false;
3750
+ PyObject* existing = PyDict_GetItemString(dict, nm);
3751
+ if (existing) {
3478
3752
  // skip over an already-installed struct of ours
3479
- if (PyObject_IsInstance(existing, (PyObject*)&PrivateAttrMetaSlotType)) {
3753
+ if (PyObject_IsInstance(existing, (PyObject*)&PrivateAttrMetaSlotType) &&
3754
+ (!((PrivateAttrSlot*)existing)->owner_type || ((PrivateAttrSlot*)existing)->owner_type == (PyObject*)metaclass)) {
3480
3755
  continue;
3481
3756
  } else {
3482
3757
  callback = existing;
3758
+ while (callback && (
3759
+ PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaSlotType) ||
3760
+ PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaNewSlotType) ||
3761
+ PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType))) {
3762
+ callback = ((PrivateAttrSlot*)callback) -> callback;
3763
+ }
3483
3764
  }
3484
3765
  }
3485
3766
  PyObjectStorage slot = PyObjectStorage::from_new_ref(make_metaclass_slot(nm, (PyObject*)metaclass, callback));
@@ -3494,25 +3775,27 @@ install_metaclass_slots(PyTypeObject* metaclass) noexcept
3494
3775
  }
3495
3776
  }
3496
3777
  const char* new_name = "__new__";
3497
- int has = PyDict_ContainsString(dict, new_name);
3498
- if (has < 0) return false;
3499
- if (has) {
3500
- PyObject* existing = PyDict_GetItemString(dict, new_name);
3501
- if (!existing) {return false;}
3502
- if (!PyObject_IsInstance(existing, (PyObject*)&PrivateAttrMetaNewSlotType)) {
3503
- auto* new_slot = create_type_new_slot((PyObject*)metaclass, NULL);
3504
- PyObjectStorage new_slot_storage = PyObjectStorage::from_new_ref((PyObject*)new_slot);
3505
- if (!new_slot_storage) {
3506
- return false;
3507
- }
3508
- PyObjectStorage name_obj = PyObjectStorage::from_new_ref(PyUnicode_FromString(new_name));
3509
- if (!name_obj) {
3510
- return false;
3511
- }
3512
- int r = real_setattro((PyObject*)metaclass, name_obj, new_slot_storage);
3513
- if (r < 0) {
3514
- return false;
3515
- }
3778
+ PyObject* existing = PyDict_GetItemString(dict, new_name);
3779
+ if (existing) {
3780
+ if (!(PyObject_IsInstance(existing, (PyObject*)&PrivateAttrMetaNewSlotType) && (
3781
+ ((PrivateAttrSlot*)existing)->owner_type == NULL || ((PrivateAttrSlot*)existing)->owner_type == (PyObject*)metaclass
3782
+ ))) {
3783
+ goto make_slot;
3784
+ }
3785
+ } else {
3786
+ make_slot:
3787
+ PrivateAttrSlot* new_slot = create_type_new_slot((PyObject*)metaclass, NULL);
3788
+ PyObjectStorage new_slot_storage = PyObjectStorage::from_new_ref((PyObject*)new_slot);
3789
+ if (!new_slot_storage) {
3790
+ return false;
3791
+ }
3792
+ PyObjectStorage name_obj = PyObjectStorage::from_new_ref(PyUnicode_FromString(new_name));
3793
+ if (!name_obj) {
3794
+ return false;
3795
+ }
3796
+ int r = real_setattro((PyObject*)metaclass, name_obj, new_slot_storage);
3797
+ if (r < 0) {
3798
+ return false;
3516
3799
  }
3517
3800
  }
3518
3801
  return true;
@@ -3721,6 +4004,20 @@ PrivateAttr_get_tp_members(PyTypeObject* type) noexcept
3721
4004
  #endif
3722
4005
  }
3723
4006
 
4007
+ static void *
4008
+ PrivateAttr_Member_GetOffset(PyObject *obj, PyMemberDef *mp) noexcept
4009
+ {
4010
+ unsigned char *addr = (unsigned char *)obj + mp->offset;
4011
+ // only python >= 3.15 has _Py_AFTER_ITEMS
4012
+ #if PY_VERSION_HEX >= 0x030F0000
4013
+ if (mp->flags & _Py_AFTER_ITEMS) {
4014
+ PyTypeObject *type = Py_TYPE(obj);
4015
+ addr += _Py_SIZE_ROUND_UP(Py_SIZE(obj) * type->tp_itemsize, SIZEOF_VOID_P);
4016
+ }
4017
+ #endif
4018
+ return addr;
4019
+ }
4020
+
3724
4021
  static int
3725
4022
  PrivateAttr_traverse_slots(PyTypeObject* type, PyObject* self,
3726
4023
  visitproc visit, void* arg) noexcept
@@ -3735,7 +4032,7 @@ PrivateAttr_traverse_slots(PyTypeObject* type, PyObject* self,
3735
4032
  Py_ssize_t n = Py_SIZE(type);
3736
4033
  for (Py_ssize_t i = 0; i < n; i++, mp++) {
3737
4034
  if (mp->type == T_OBJECT_EX) {
3738
- char* addr = (char*)self + mp->offset;
4035
+ void* addr = PrivateAttr_Member_GetOffset(self, mp);
3739
4036
  PyObject* obj = *(PyObject**)addr;
3740
4037
  if (obj != NULL) {
3741
4038
  int err = visit(obj, arg);
@@ -3761,7 +4058,7 @@ PrivateAttr_clear_slots(PyTypeObject* type, PyObject* self) noexcept
3761
4058
  Py_ssize_t n = Py_SIZE(type);
3762
4059
  for (Py_ssize_t i = 0; i < n; i++, mp++) {
3763
4060
  if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
3764
- char* addr = (char*)self + mp->offset;
4061
+ void* addr = PrivateAttr_Member_GetOffset(self, mp);
3765
4062
  PyObject* obj = *(PyObject**)addr;
3766
4063
  if (obj != NULL) {
3767
4064
  *(PyObject**)addr = NULL;
@@ -4196,6 +4493,47 @@ static PyObject* PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
4196
4493
  static int PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept;
4197
4494
  static PyObject* PrivateAttrType_call(PyObject* cls, PyObject* args, PyObject* kwds) noexcept;
4198
4495
  static void PrivateAttrType_del(PyObject* cls) noexcept;
4496
+ static PyObject* PrivateAttrType_init_subclass(PyObject* cls, PyObject* noargs) noexcept;
4497
+
4498
+ // Requirement: subclasses of PrivateAttrType (i.e. metaclasses that derive from
4499
+ // it) must NOT override __getattribute__ / __getattr__ / __setattr__ /
4500
+ // __delattr__. Those names rewire tp_getattro / tp_setattro away from our
4501
+ // interception slots (PrivateAttrType_getattr / PrivateAttrType_setattr) via
4502
+ // CPython's fixup_slot_dispatchers, which would let a metaclass subclass bypass
4503
+ // the private-attribute machinery. This __init_subclass__ hook runs for every
4504
+ // subclass of PrivateAttrType and rejects any such override by comparing the
4505
+ // freshly-created subclass's live slots against the canonical ones.
4506
+ static PyObject*
4507
+ PrivateAttrType_init_subclass(PyObject* cls, PyObject* /*noargs*/) noexcept
4508
+ {
4509
+ if (!PyType_Check(cls)) {
4510
+ PyErr_SetString(PyExc_TypeError, "__init_subclass__ requires a type");
4511
+ return NULL;
4512
+ }
4513
+ PyTypeObject* tp = (PyTypeObject*)cls;
4514
+ // __getattribute__ / __getattr__ override -> tp_getattro is no longer our
4515
+ // PrivateAttrType_getattr slot.
4516
+ if (tp->tp_getattro != (getattrofunc)PrivateAttrType_getattr) {
4517
+ PyErr_Format(PyExc_TypeError,
4518
+ "cannot override '__getattribute__' or '__getattr__' in a subclass of PrivateAttrType ('%s')",
4519
+ tp->tp_name);
4520
+ return NULL;
4521
+ }
4522
+ // __setattr__ / __delattr__ override -> tp_setattro is no longer our
4523
+ // PrivateAttrType_setattr slot.
4524
+ if (tp->tp_setattro != (setattrofunc)PrivateAttrType_setattr) {
4525
+ PyErr_Format(PyExc_TypeError,
4526
+ "cannot override '__setattr__' or '__delattr__' in a subclass of PrivateAttrType ('%s')",
4527
+ tp->tp_name);
4528
+ return NULL;
4529
+ }
4530
+ Py_RETURN_NONE;
4531
+ }
4532
+
4533
+ static PyMethodDef PrivateAttrType_methods[] = {
4534
+ {"__init_subclass__", (PyCFunction)PrivateAttrType_init_subclass, METH_CLASS | METH_NOARGS, NULL},
4535
+ {NULL}
4536
+ };
4199
4537
 
4200
4538
  static PyTypeObject PrivateAttrType = {
4201
4539
  PyVarObject_HEAD_INIT(NULL, 0)
@@ -4226,7 +4564,7 @@ static PyTypeObject PrivateAttrType = {
4226
4564
  0, // tp_weaklistoffset
4227
4565
  0, // tp_iter
4228
4566
  0, // tp_iternext
4229
- 0, // tp_methods
4567
+ PrivateAttrType_methods, // tp_methods
4230
4568
  0, // tp_members
4231
4569
  0, // tp_getset
4232
4570
  &PyType_Type, // tp_base
@@ -4239,6 +4577,12 @@ static PyTypeObject PrivateAttrType = {
4239
4577
  (newfunc)PrivateAttrType_new, // tp_new
4240
4578
  };
4241
4579
 
4580
+ static bool
4581
+ is_private_meta_type(PyObject* typ) noexcept
4582
+ {
4583
+ return PyObject_IsSubclass(typ, (PyObject*)&PrivateAttrType) || is_registered_type(typ);
4584
+ }
4585
+
4242
4586
  static PyObject*
4243
4587
  get_string_hash_tuple(const std::string& name) noexcept
4244
4588
  {
@@ -4299,11 +4643,11 @@ type_get_final_attr(uintptr_t main_type, uintptr_t target_type, const std::strin
4299
4643
  if (item_set.find(key) != item_set.end()) {
4300
4644
  PyObject* obj = item_set[key];
4301
4645
  return obj;
4302
- } else if (::AllData::type_creation_locked && ::AllData::all_type_attr_set.find(main_type) == ::AllData::all_type_attr_set.end()) {
4303
- // now we can only use name instead of key on tmp_creation_data->need_remove_itself[name]
4304
- if (::AllData::tmp_creation_data &&
4305
- ::AllData::tmp_creation_data->need_remove_itself.find(name) != ::AllData::tmp_creation_data->need_remove_itself.end()) {
4306
- PyObject* obj = ::AllData::tmp_creation_data->need_remove_itself[name];
4646
+ } else if (::AllData::all_type_attr_set.find(main_type) == ::AllData::all_type_attr_set.end()) {
4647
+ // now we can only use name instead of key on need_remove_itself[name]
4648
+ PrivateAttrCreationData* data = get_creation_data(main_type);
4649
+ if (data && data->need_remove_itself.find(name) != data->need_remove_itself.end()) {
4650
+ PyObject* obj = data->need_remove_itself[name];
4307
4651
  return obj;
4308
4652
  }
4309
4653
  }
@@ -4368,9 +4712,9 @@ type_private_attr(uintptr_t type_id, const std::string& name) noexcept
4368
4712
  if (item_set.find(hash_tuple) != item_set.end()) {
4369
4713
  return {true, true};
4370
4714
  }
4371
- } else if (::AllData::type_creation_locked && ::AllData::all_type_attr_set.find(type_id) == ::AllData::all_type_attr_set.end()) {
4372
- if (::AllData::tmp_creation_data &&
4373
- ::AllData::tmp_creation_data->private_attrs_set.find(hash_tuple) != ::AllData::tmp_creation_data->private_attrs_set.end()) {
4715
+ } else if (::AllData::all_type_attr_set.find(type_id) == ::AllData::all_type_attr_set.end()) {
4716
+ PrivateAttrCreationData* data = get_creation_data(type_id);
4717
+ if (data && data->private_attrs_set.find(hash_tuple) != data->private_attrs_set.end()) {
4374
4718
  return {true, true};
4375
4719
  }
4376
4720
  }
@@ -4633,30 +4977,24 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
4633
4977
  return false;
4634
4978
  }
4635
4979
 
4980
+ bool need_del_use_parent = false;
4981
+ bool kwds_is_avaliable = false;
4636
4982
  if (kwds && PyDict_Check(kwds)) {
4637
- int has_use_parents = PyDict_ContainsString(kwds, "use_parents");
4638
- if (has_use_parents < 0) {
4639
- return false;
4640
- }
4641
- if (has_use_parents) {
4642
- PyObject* use_parents_obj = PyDict_GetItemString(kwds, "use_parents");
4983
+ kwds_is_avaliable = true;
4984
+ PyObject* use_parents_obj = PyDict_GetItemString(kwds, "use_parents");
4985
+ if (use_parents_obj) {
4643
4986
  int truth = PyObject_IsTrue(use_parents_obj);
4644
4987
  if (truth < 0) {
4645
4988
  return false;
4646
4989
  }
4647
4990
  data.use_parents = (truth == 1);
4991
+ need_del_use_parent = true;
4648
4992
  }
4649
4993
  }
4650
4994
 
4651
4995
  PyObjectStorage __private_attrs__;
4652
- int contains_private_attrs = PyDict_ContainsString(attrs, "__private_attrs__");
4653
- if (contains_private_attrs < 0) {
4654
- return false;
4655
- }
4656
- if (contains_private_attrs) {
4657
- __private_attrs__ = PyDict_GetItemString(attrs, "__private_attrs__");
4658
- }
4659
- else {
4996
+ __private_attrs__ = PyDict_GetItemString(attrs, "__private_attrs__");
4997
+ if (!__private_attrs__) {
4660
4998
  __private_attrs__ = PyObjectStorage::from_new_ref(PyTuple_New(0));
4661
4999
  }
4662
5000
 
@@ -4731,14 +5069,8 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
4731
5069
  return false;
4732
5070
  }
4733
5071
 
4734
- int has_slots = PyDict_ContainsString(data.attrs_copy, "__slots__");
4735
- if (has_slots < 0) {
4736
- return false;
4737
- }
4738
-
4739
- if (has_slots) {
4740
- PyObject* all_slots = PyDict_GetItemString(data.attrs_copy, "__slots__");
4741
- if (!all_slots) {return false;}
5072
+ PyObject* all_slots = PyDict_GetItemString(data.attrs_copy, "__slots__");
5073
+ if (all_slots) {
4742
5074
  if (PyUnicode_Check(all_slots)) {
4743
5075
  const char* slot_cstr = PyUnicode_AsUTF8(all_slots);
4744
5076
  if (data.private_attrs_vector_string.find((std::string)slot_cstr) != data.private_attrs_vector_string.end()){
@@ -4783,6 +5115,10 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
4783
5115
  if (!base || !PyType_Check(base)) {
4784
5116
  continue;
4785
5117
  }
5118
+ if (is_registered_type(base) || PyObject_IsSubclass(base, (PyObject*)&PrivateAttrType)) {
5119
+ PyErr_SetString(PyExc_TypeError, "private-attribute type cannot inherit from a private-attribute metaclass");
5120
+ return false;
5121
+ }
4786
5122
  AnalyseStatus analyse_status = need_analyse_type(base);
4787
5123
  if (analyse_status == AnalyseStatus::Error) {
4788
5124
  return false;
@@ -4790,6 +5126,16 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
4790
5126
  if (analyse_status == AnalyseStatus::No) {
4791
5127
  continue;
4792
5128
  }
5129
+ // A private-attribute base that was left half-installed (its slot
5130
+ // objects are present but its install never completed) can never be
5131
+ // completed, so subclassing it must fail immediately rather than
5132
+ // propagate a broken half-finished type.
5133
+ if (is_uninstallable_private_type(base)) {
5134
+ PyErr_Format(PyExc_RuntimeError,
5135
+ "private-attribute base '%s' is not fully installed and cannot be used",
5136
+ ((PyTypeObject*)base)->tp_name);
5137
+ return false;
5138
+ }
4793
5139
  data.all_need_analyse_base.push_back((uintptr_t)base);
4794
5140
  }
4795
5141
 
@@ -4890,26 +5236,16 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
4890
5236
  }
4891
5237
 
4892
5238
  // get "private_func" from kwds
4893
- if (kwds && PyDict_Check(kwds)) {
4894
- int has_private_func = PyDict_ContainsString(kwds, "private_func");
4895
- if (has_private_func < 0) {
4896
- return false;
4897
- }
4898
- if (has_private_func) {
4899
- data.private_func = PyDict_GetItemString(kwds, "private_func");
4900
- }
5239
+ if (kwds_is_avaliable) {
5240
+ data.private_func = PyDict_GetItemString(kwds, "private_func");
4901
5241
  data.base_kwds = PyObjectStorage::from_new_ref(PyDict_Copy(kwds));
4902
5242
  if (!data.base_kwds) {
4903
5243
  return false;
4904
5244
  }
4905
- if (has_private_func) {
5245
+ if (data.private_func) {
4906
5246
  if (PyDict_DelItemString(data.base_kwds, "private_func") < 0) return false;
4907
5247
  }
4908
- int kwds_has_use_parents = PyDict_ContainsString(kwds, "use_parents");
4909
- if (kwds_has_use_parents < 0) {
4910
- return false;
4911
- }
4912
- if (kwds_has_use_parents) {
5248
+ if (need_del_use_parent) {
4913
5249
  if (PyDict_DelItemString(data.base_kwds, "use_parents") < 0) return false;
4914
5250
  }
4915
5251
  }
@@ -4926,11 +5262,12 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
4926
5262
  // __setattr__ / __delattr__ / __del__) into the class namespace so CPython
4927
5263
  // wires tp_getattro / tp_setattro / tp_finalize through them instead of us
4928
5264
  // setting those slots directly. Any user-defined value for one of these
4929
- // names becomes the struct's callback.
4930
- if (!inject_type_slots(data.attrs_copy)) {
5265
+ // names becomes the struct's callback. v4: each injected slot also carries
5266
+ // a pointer to `data` so the pending install can be recovered without a
5267
+ // global pointer.
5268
+ if (!inject_type_slots(data.attrs_copy, &data)) {
4931
5269
  return false;
4932
5270
  }
4933
- data.lock();
4934
5271
 
4935
5272
  return true;
4936
5273
  }
@@ -5152,7 +5489,7 @@ PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
5152
5489
  }
5153
5490
 
5154
5491
  static bool
5155
- is_registed_type(PyObject* type) noexcept
5492
+ is_registered_type(PyObject* type) noexcept
5156
5493
  {
5157
5494
  uintptr_t typ_id = (uintptr_t)type;
5158
5495
  return (AllData::all_register_type_weak_ref.find(typ_id) != AllData::all_register_type_weak_ref.end());
@@ -5182,16 +5519,16 @@ new_type_tp_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
5182
5519
  if (name_cstr) {
5183
5520
  const char** slot_names = NULL;
5184
5521
  int is_metaclass = 0;
5185
- if (is_registed_type(self)) {
5522
+ if (is_registered_type(self)) {
5186
5523
  // if name_cstr is __new__, call update_metaclass_new
5187
5524
  if (strcmp(name_cstr, "__new__") == 0) {
5188
5525
  PyObject* callback = value;
5189
- if (value && (PyObject_IsInstance(value, (PyObject*)&PrivateAttrSlotType)
5190
- || PyObject_IsInstance(value, (PyObject*)&PrivateAttrMetaSlotType)
5191
- || PyObject_IsInstance(value, (PyObject*)&PrivateAttrMetaNewSlotType))) {
5192
- callback = ((PrivateAttrSlot*)value) -> callback;
5526
+ while (callback && (PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType)
5527
+ || PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaSlotType)
5528
+ || PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaNewSlotType))) {
5529
+ callback = ((PrivateAttrSlot*)callback) -> callback;
5193
5530
  }
5194
- if (!update_metaclass_new((PyTypeObject*)self, value)) {
5531
+ if (!update_metaclass_new((PyTypeObject*)self, callback)) {
5195
5532
  return -1;
5196
5533
  }
5197
5534
  return 0;
@@ -5215,10 +5552,10 @@ new_type_tp_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
5215
5552
  for (int i = 0; slot_names[i]; i++) {
5216
5553
  if (strcmp(name_cstr, slot_names[i]) == 0) {
5217
5554
  PyObject* callback = value;
5218
- if (value && (PyObject_IsInstance(value, (PyObject*)&PrivateAttrSlotType)
5219
- || PyObject_IsInstance(value, (PyObject*)&PrivateAttrMetaSlotType)
5220
- || PyObject_IsInstance(value, (PyObject*)&PrivateAttrMetaNewSlotType))) {
5221
- callback = ((PrivateAttrSlot*)value) -> callback;
5555
+ while (callback && (PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType)
5556
+ || PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaSlotType)
5557
+ || PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaNewSlotType))) {
5558
+ callback = ((PrivateAttrSlot*)callback) -> callback;
5222
5559
  }
5223
5560
  if (!update_slot_callback((PyTypeObject*)self, name_cstr, callback, is_metaclass)) {
5224
5561
  return -1;
@@ -5823,6 +6160,14 @@ register_metaclass_head(PyObject* metaclass) noexcept
5823
6160
  PyErr_SetString(PyExc_TypeError, "metaclass must be a metatype");
5824
6161
  return NULL;
5825
6162
  }
6163
+ // Requirement: PrivateAttrType (and any metaclass subclassing it) must not
6164
+ // be registered. PrivateAttrType already provides private-attribute
6165
+ // creation; registering it (or a subclass of it) as a registered metaclass
6166
+ // would double-interpret the same classes and corrupt the control tables.
6167
+ if (PyObject_IsSubclass(metaclass, (PyObject*)&PrivateAttrType)) {
6168
+ PyErr_SetString(PyExc_ValueError, "cannot register PrivateAttrType or a subclass of it");
6169
+ return NULL;
6170
+ }
5826
6171
  if (metaclass == (PyObject*)&PyType_Type) {
5827
6172
  PyErr_SetString(PyExc_ValueError, "cannot register type itself");
5828
6173
  return NULL;
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "private_attribute_cpp"
7
- version = "3.0.2"
7
+ version = "3.0.3"
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"