private-attribute-cpp 3.0.0__tar.gz → 3.0.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.
Files changed (21) hide show
  1. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/PKG-INFO +1 -1
  2. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/private_attribute.cpp +125 -116
  3. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/pyproject.toml +1 -1
  4. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_super.py +92 -0
  5. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/CMakeLists.txt +0 -0
  6. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/LICENSE +0 -0
  7. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/README.md +0 -0
  8. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/picosha2.h +0 -0
  9. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/private_attribute.pyi +0 -0
  10. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_attr_classify_owner.py +0 -0
  11. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_class_private_attrs.py +0 -0
  12. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_gc_collect.py +0 -0
  13. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_init_subclass.py +0 -0
  14. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_abc.py +0 -0
  15. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_attrs.py +0 -0
  16. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_func.py +0 -0
  17. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_inheritance.py +0 -0
  18. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_method.py +0 -0
  19. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_private_wrap.py +0 -0
  20. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.1}/tests/test_slots_cycle.py +0 -0
  21. {private_attribute_cpp-3.0.0 → private_attribute_cpp-3.0.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: 3.0.0
3
+ Version: 3.0.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
@@ -235,7 +235,7 @@ static bool inject_type_slots(PyObject* attrs) noexcept;
235
235
  // install the six special-method structs into a registered (heap) metaclass dict
236
236
  static bool install_metaclass_slots(PyTypeObject* metaclass) noexcept;
237
237
  // write a new user callback into an installed slot struct's callback field
238
- static bool update_slot_callback(PyTypeObject* owner, const char* name, PyObject* callback) noexcept;
238
+ static bool update_slot_callback(PyTypeObject* owner, const char* name, PyObject* callback, int is_metaclass) noexcept;
239
239
  // public non-private-name cache helpers
240
240
  static bool is_confirmed_public(uintptr_t obj_id, const std::string& name) noexcept;
241
241
  static void mark_confirmed_public(uintptr_t obj_id, const std::string& name) noexcept;
@@ -277,12 +277,6 @@ namespace {
277
277
  all_object_mutex, all_type_subclass_mutex;
278
278
  static std::mutex object_creatmutex_mutex, type_createmutex_mutex, type_parenttype_createmutex_mutex;
279
279
  static std::unordered_map<uintptr_t, std::vector<uintptr_t>> all_type_parent_id;
280
- // all type tp_getattro map
281
- static std::unordered_map<uintptr_t, getattrofunc> all_type_getattro;
282
- // all type tp_setattro map
283
- static std::unordered_map<uintptr_t, setattrofunc> all_type_setattro;
284
- // all type tp_call map
285
- static std::unordered_map<uintptr_t, ternaryfunc> all_type_call;
286
280
  // all type tp_traverse map
287
281
  static std::unordered_map<uintptr_t, traverseproc> all_type_traverse;
288
282
  // all type tp_clear map
@@ -294,7 +288,6 @@ namespace {
294
288
  // defaults as "not a real original" and keep walking upward.
295
289
  static traverseproc captured_subtype_traverse = NULL;
296
290
  static inquiry captured_subtype_clear = NULL;
297
- static std::unordered_map<uintptr_t, destructor> all_type_del;
298
291
 
299
292
  static std::shared_mutex all_register_new_metaclass_mutex;
300
293
  static std::unordered_map<uintptr_t, PyObjectStorage> all_register_type_weak_ref;
@@ -612,12 +605,8 @@ clean_all_storages() noexcept
612
605
  AllData::all_object_mutex.clear();
613
606
  AllData::all_type_subclass_mutex.clear();
614
607
  AllData::all_type_parent_id.clear();
615
- AllData::all_type_getattro.clear();
616
- AllData::all_type_setattro.clear();
617
- AllData::all_type_call.clear();
618
608
  AllData::all_type_traverse.clear();
619
609
  AllData::all_type_clear.clear();
620
- AllData::all_type_del.clear();
621
610
  AllData::all_register_type_weak_ref.clear();
622
611
  AllData::store_module_self.clear();
623
612
  AllData::public_attr_cache.clear();
@@ -1009,8 +998,6 @@ id_getattr(uintptr_t final_id, const std::string& attr_name, PyObject* obj, PyOb
1009
998
  }
1010
999
  if (get && has_set) {
1011
1000
  PyObject* final_result = get(result, obj, typ);
1012
- ensure_tp((PyTypeObject*)typ);
1013
- ensure_subclass_tp((PyTypeObject*)typ);
1014
1001
  return final_result;
1015
1002
  }
1016
1003
 
@@ -1038,8 +1025,6 @@ id_getattr(uintptr_t final_id, const std::string& attr_name, PyObject* obj, PyOb
1038
1025
 
1039
1026
  if (get) {
1040
1027
  PyObject* final_result = get(result, obj, typ);
1041
- ensure_tp((PyTypeObject*)typ);
1042
- ensure_subclass_tp((PyTypeObject*)typ);
1043
1028
  return final_result;
1044
1029
  }
1045
1030
  if (result) {
@@ -1071,8 +1056,6 @@ type_getattr(uintptr_t main_type, PyObject* typ, const std::string& attr_name) n
1071
1056
  auto funcptr = type->tp_descr_get;
1072
1057
  if (funcptr) {
1073
1058
  PyObject* final_result = funcptr(result, Py_None, (PyObject*)type);
1074
- ensure_tp((PyTypeObject*)type);
1075
- ensure_subclass_tp((PyTypeObject*)type);
1076
1059
  return final_result;
1077
1060
  } else {
1078
1061
  Py_INCREF(result);
@@ -2147,6 +2130,19 @@ static PyTypeObject PrivateClassSuperType = {
2147
2130
  (newfunc)PrivateSuper_New, // tp_new
2148
2131
  };
2149
2132
 
2133
+ // Index of `id` in `mro` (a private-attribute ancestor list in MRO order,
2134
+ // nearest first), or -1 if absent.
2135
+ static int
2136
+ mro_index_of(const std::vector<uintptr_t>& mro, uintptr_t id) noexcept
2137
+ {
2138
+ for (size_t i = 0; i < mro.size(); ++i) {
2139
+ if (mro[i] == id) {
2140
+ return static_cast<int>(i);
2141
+ }
2142
+ }
2143
+ return -1;
2144
+ }
2145
+
2150
2146
  static bool
2151
2147
  private_super_validate(PrivateSuperObject* self, bool is_class) noexcept
2152
2148
  {
@@ -2168,31 +2164,39 @@ private_super_validate(PrivateSuperObject* self, bool is_class) noexcept
2168
2164
  return false;
2169
2165
  }
2170
2166
  }
2171
- // B must be a parent of A.
2167
+ // B must be a parent of A. Instead of PyObject_IsSubclass(A, B), use A's
2168
+ // stored private-attribute ancestor MRO (all_type_parent_id[A], nearest
2169
+ // first) and check membership by id.
2172
2170
  if (self->A == self->B) {
2173
2171
  PyErr_SetString(PyExc_ValueError, "B must be a parent type of A, not A itself");
2174
2172
  return false;
2175
2173
  }
2176
- int a_sub_b = PyObject_IsSubclass(self->A, self->B);
2177
- if (a_sub_b < 0) {
2174
+ uintptr_t a_id = (uintptr_t)self->A;
2175
+ uintptr_t b_id = (uintptr_t)self->B;
2176
+ uintptr_t c_id = (uintptr_t)self->C;
2177
+ ParentIdsResult a_parents = type_get_parent_ids(a_id);
2178
+ if (!a_parents.ok) {
2178
2179
  return false;
2179
2180
  }
2180
- if (!a_sub_b) {
2181
+ int b_idx = mro_index_of(*a_parents.ids, b_id);
2182
+ if (b_idx < 0) {
2181
2183
  PyErr_SetString(PyExc_ValueError, "B must be a parent type of A");
2182
2184
  return false;
2183
2185
  }
2184
- // C must be B, A, or both a subclass of B and a parent of A.
2185
- bool c_ok = (self->C == self->B) || (self->C == self->A);
2186
+ // C must be B, A, or both a subclass of B and a parent of A. Both
2187
+ // relationships are answered from the stored MRO id lists (no
2188
+ // PyObject_IsSubclass): C is a parent of A iff C is in A's ancestor list,
2189
+ // and C is a subclass of B iff B is in C's ancestor list (B sits after C
2190
+ // on C's inheritance chain).
2191
+ bool c_ok = (c_id == b_id) || (c_id == a_id);
2186
2192
  if (!c_ok) {
2187
- int c_sub_b = PyObject_IsSubclass(self->C, self->B);
2188
- if (c_sub_b < 0) {
2189
- return false;
2190
- }
2191
- int a_sub_c = PyObject_IsSubclass(self->A, self->C);
2192
- if (a_sub_c < 0) {
2193
- return false;
2193
+ if (mro_index_of(*a_parents.ids, c_id) >= 0) {
2194
+ ParentIdsResult c_parents = type_get_parent_ids(c_id);
2195
+ if (!c_parents.ok) {
2196
+ return false;
2197
+ }
2198
+ c_ok = (mro_index_of(*c_parents.ids, b_id) >= 0);
2194
2199
  }
2195
- c_ok = (c_sub_b && a_sub_c);
2196
2200
  }
2197
2201
  if (!c_ok) {
2198
2202
  PyErr_SetString(PyExc_ValueError,
@@ -2205,13 +2209,18 @@ private_super_validate(PrivateSuperObject* self, bool is_class) noexcept
2205
2209
  PyErr_SetString(PyExc_TypeError, "cls must be a type");
2206
2210
  return false;
2207
2211
  }
2208
- int ok = PyObject_IsSubclass(self->obj, self->A);
2209
- if (ok < 0) {
2210
- return false;
2211
- }
2212
- if (!ok) {
2213
- PyErr_SetString(PyExc_TypeError, "cls must be a subclass of A");
2214
- return false;
2212
+ uintptr_t obj_id = (uintptr_t)self->obj;
2213
+ // A type is a subclass of itself; otherwise cls is a subclass of A iff
2214
+ // A appears in cls's private-attribute ancestor MRO.
2215
+ if (obj_id != a_id) {
2216
+ ParentIdsResult obj_parents = type_get_parent_ids(obj_id);
2217
+ if (!obj_parents.ok) {
2218
+ return false;
2219
+ }
2220
+ if (mro_index_of(*obj_parents.ids, a_id) < 0) {
2221
+ PyErr_SetString(PyExc_TypeError, "cls must be a subclass of A");
2222
+ return false;
2223
+ }
2215
2224
  }
2216
2225
  } else {
2217
2226
  int ok = PyObject_IsInstance(self->obj, self->A);
@@ -2251,6 +2260,22 @@ private_super_getattro(PyObject* self, PyObject* name, bool is_class) noexcept
2251
2260
  return NULL;
2252
2261
  }
2253
2262
 
2263
+ // Only private attributes of B (the second type) can be read through
2264
+ // PrivateSuper/PrivateClassSuper; reject any other name early, before any
2265
+ // storage-key work.
2266
+ TypePrivateAttrResult pa = type_private_attr((uintptr_t)s->B, attr_name);
2267
+ if (!pa.ok) {
2268
+ return NULL;
2269
+ }
2270
+ if (!pa.is_private) {
2271
+ const char* tn = get_name_from_tp_name((PyTypeObject*)s->B);
2272
+ if (tn == NULL) {
2273
+ return NULL;
2274
+ }
2275
+ PyErr_Format(PyExc_AttributeError, "'%s' has no private attribute '%s'", tn, attr_name.c_str());
2276
+ return NULL;
2277
+ }
2278
+
2254
2279
  // Resolve the storage key for C and look up the class-level storage.
2255
2280
  // C == B -> B's own class body storage (type_attr_dict[B])
2256
2281
  // C != B -> B's shadow for C (all_type_subclass_attr[B][C])
@@ -2831,12 +2856,16 @@ static PyObject*
2831
2856
  slot_call_impl(PrivateAttrSlot* self, PyObject* args, PyObject* kwds) noexcept
2832
2857
  {
2833
2858
  // metaclass __call__: args = (cls, *call_args)
2859
+ Py_ssize_t n = PyTuple_GET_SIZE(args);
2860
+ if (n == 0) {
2861
+ PyErr_SetString(PyExc_TypeError, "metaclass __call__ requires the type itself");
2862
+ return NULL;
2863
+ }
2834
2864
  PyObject* cls = PyTuple_GET_ITEM(args, 0);
2835
2865
  if (!try_install_pending_creation(cls)) return NULL;
2836
2866
  if (self->callback) {
2837
2867
  return PyObject_Call(self->callback, args, kwds);
2838
2868
  }
2839
- Py_ssize_t n = PyTuple_GET_SIZE(args);
2840
2869
  PyObjectStorage rest = PyObjectStorage::from_new_ref(PyTuple_GetSlice(args, 1, n));
2841
2870
  if (!rest) return NULL;
2842
2871
  return super_slot_call(self->owner_type, cls, "__call__", rest, kwds);
@@ -3069,20 +3098,57 @@ install_metaclass_slots(PyTypeObject* metaclass) noexcept
3069
3098
  }
3070
3099
 
3071
3100
  // Write a new user callback into an installed slot struct's callback field.
3101
+ //
3102
+ // If the slot name already exists and is one of our structs, we try to reuse it
3103
+ // in place: for a PrivateAttrMetaSlotType struct whose owner_type still matches
3104
+ // `owner`, only the callback field is rewritten (no re-creation). Otherwise we
3105
+ // fall through and rebuild the struct in place. If the existing value is NOT one
3106
+ // of our slot structs at all, we also rebuild in place instead of giving up, so
3107
+ // the original attribute is preserved as the new callback.
3072
3108
  static bool
3073
- update_slot_callback(PyTypeObject* owner, const char* name, PyObject* callback) noexcept
3109
+ update_slot_callback(PyTypeObject* owner, const char* name, PyObject* callback, int is_metaclass) noexcept
3074
3110
  {
3075
3111
  PyObject* dict = owner->tp_dict;
3076
3112
  if (!dict) return false;
3077
3113
  PyObject* slot = PyDict_GetItemString(dict, name);
3078
- if (!slot) return false;
3079
- if (!PyObject_IsInstance(slot, (PyObject*)&PrivateAttrMetaSlotType)
3080
- && !PyObject_IsInstance(slot, (PyObject*)&PrivateAttrSlotType)) {
3114
+
3115
+ // Fast path: reuse an existing struct when possible.
3116
+ if (slot && PyObject_IsInstance(slot, (PyObject*)&PrivateAttrMetaSlotType)) {
3117
+ PrivateAttrSlot* s = (PrivateAttrSlot*)slot;
3118
+ if (s->owner_type == (PyObject*)owner) {
3119
+ // Same owner: treat this as a pure callback-field update.
3120
+ Py_XSETREF(s->callback, callback ? (PyObject*)Py_NewRef(callback) : NULL);
3121
+ return true;
3122
+ }
3123
+ // Owner differs: fall through to re-create in place.
3124
+ } else if (slot && PyObject_IsInstance(slot, (PyObject*)&PrivateAttrSlotType)) {
3125
+ PrivateAttrSlot* s = (PrivateAttrSlot*)slot;
3126
+ if (!is_metaclass && s->owner_type == (PyObject*)owner) {
3127
+ Py_XSETREF(s->callback, callback ? (PyObject*)Py_NewRef(callback) : NULL);
3128
+ return true;
3129
+ }
3130
+ // Otherwise fall through to re-create in place.
3131
+ }
3132
+
3133
+ // Rebuild the slot struct in place, preserving the original value (if any)
3134
+ // as the new callback. This covers:
3135
+ // - no existing slot at all,
3136
+ // - an existing struct with a mismatched owner_type,
3137
+ // - an existing value that is not one of our slot structs.
3138
+ PyObject* new_slot = is_metaclass
3139
+ ? make_metaclass_slot(name, (PyObject*)owner, callback)
3140
+ : make_type_slot(name, callback);
3141
+ if (!new_slot) return false;
3142
+
3143
+ setattrofunc real_setattro = original_type_tp_setattro ? original_type_tp_setattro : PyType_Type.tp_setattro;
3144
+ PyObjectStorage name_obj = PyObjectStorage::from_new_ref(PyUnicode_FromString(name));
3145
+ if (!name_obj) {
3146
+ Py_DECREF(new_slot);
3081
3147
  return false;
3082
3148
  }
3083
- PrivateAttrSlot* s = (PrivateAttrSlot*)slot;
3084
- Py_XSETREF(s->callback, callback ? (PyObject*)Py_NewRef(callback) : NULL);
3085
- return true;
3149
+ int r = real_setattro((PyObject*)owner, name_obj, new_slot);
3150
+ Py_DECREF(new_slot);
3151
+ return r >= 0;
3086
3152
  }
3087
3153
 
3088
3154
  // ===============================================================
@@ -4594,20 +4660,7 @@ PrivateAttrType_call(PyObject* cls, PyObject* args, PyObject* kwds) noexcept
4594
4660
  // Finish any pending private-attribute creation before instantiating the
4595
4661
  // class (the creation may have been interrupted before install() ran).
4596
4662
  if (!try_install_pending_creation(cls)) return NULL;
4597
- auto it = ::AllData::all_type_call.find((uintptr_t)Py_TYPE(cls));
4598
- if (it != ::AllData::all_type_call.end()) {
4599
- if (it->second && it->second != PrivateAttrType_call) {
4600
- return it->second(cls, args, kwds);
4601
- }
4602
- }
4603
- PyTypeObject* base = Py_TYPE(cls)->tp_base;
4604
- while (base && base->tp_base && base->tp_call == PrivateAttrType_call) {
4605
- base = base->tp_base;
4606
- }
4607
- if (!base) {
4608
- return PyType_Type.tp_call(cls, args, kwds);
4609
- }
4610
- return base->tp_call(cls, args, kwds);
4663
+ return PyType_Type.tp_call(cls, args, kwds);
4611
4664
  }
4612
4665
 
4613
4666
  static PyObject*
@@ -4620,7 +4673,7 @@ PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
4620
4673
  }
4621
4674
  uintptr_t typ_id = (uintptr_t)(cls);
4622
4675
  std::string name_str = PyUnicode_AsUTF8(name);
4623
- {
4676
+ if (!is_confirmed_public(typ_id, name_str)) {
4624
4677
  PyObjectStorage now_code = PyObjectStorage::from_new_ref(get_now_code());
4625
4678
  TypePrivateAttrResult private_result = type_private_attr(typ_id, name_str);
4626
4679
  if (!private_result.ok) {
@@ -4643,23 +4696,9 @@ PrivateAttrType_getattr(PyObject* cls, PyObject* name) noexcept
4643
4696
  return type_getattr(classify.type_id, cls, name_str);
4644
4697
  }
4645
4698
  }
4699
+ mark_confirmed_public(typ_id, name_str);
4646
4700
  }
4647
- auto it = ::AllData::all_type_getattro.find((uintptr_t)Py_TYPE(cls));
4648
- if (it != ::AllData::all_type_getattro.end()) {
4649
- if (it->second && it->second != PrivateAttrType_getattr) {
4650
- return it->second(cls, name);
4651
- }
4652
- }
4653
- PyTypeObject* base = Py_TYPE(cls)->tp_base;
4654
- while (base && base->tp_base && base->tp_getattro == PrivateAttrType_getattr) {
4655
- base = base->tp_base;
4656
- }
4657
- if (!base) {
4658
- PyObject* result = PyType_Type.tp_getattro(cls, name);
4659
- return result;
4660
- }
4661
- PyObject* result = base->tp_getattro(cls, name);
4662
- return result;
4701
+ return PyType_Type.tp_getattro(cls, name);
4663
4702
  }
4664
4703
 
4665
4704
  static bool
@@ -4685,8 +4724,10 @@ new_type_tp_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
4685
4724
  const char* name_cstr = PyUnicode_AsUTF8(name);
4686
4725
  if (name_cstr) {
4687
4726
  const char** slot_names = NULL;
4727
+ int is_metaclass = 0;
4688
4728
  if (is_registed_type(self)) {
4689
4729
  slot_names = metaclass_slot_names;
4730
+ is_metaclass = 1;
4690
4731
  } else {
4691
4732
  AnalyseStatus analyse_status = need_analyse_type(self);
4692
4733
  if (analyse_status == AnalyseStatus::Error) {
@@ -4706,7 +4747,7 @@ new_type_tp_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
4706
4747
  if (slot_names && !is_our_struct) {
4707
4748
  for (int i = 0; slot_names[i]; i++) {
4708
4749
  if (strcmp(name_cstr, slot_names[i]) == 0) {
4709
- if (!update_slot_callback((PyTypeObject*)self, name_cstr, value)) {
4750
+ if (!update_slot_callback((PyTypeObject*)self, name_cstr, value, is_metaclass)) {
4710
4751
  return -1;
4711
4752
  }
4712
4753
  return 0;
@@ -4887,11 +4928,10 @@ PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept
4887
4928
  uintptr_t typ_id = (uintptr_t)(cls);
4888
4929
  std::string name_str = PyUnicode_AsUTF8(name);
4889
4930
  if (((PyTypeObject*)cls)->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
4890
- std::string error_message = "cannot set '" + name_str + "' attribute of immutable type '" + ((PyTypeObject*)cls)->tp_name + "'";
4891
- PyErr_SetString(PyExc_TypeError, error_message.c_str());
4931
+ PyErr_Format(PyExc_TypeError, "cannot set '%s' attribute of immutable type '%s'", name_str.c_str(), ((PyTypeObject*)cls)->tp_name);
4892
4932
  return -1;
4893
4933
  }
4894
- {
4934
+ if (!is_confirmed_public(typ_id, name_str)) {
4895
4935
  PyObjectStorage now_code = PyObjectStorage::from_new_ref(get_now_code());
4896
4936
  TypePrivateAttrResult private_result = type_private_attr(typ_id, name_str);
4897
4937
  if (!private_result.ok) {
@@ -4915,37 +4955,15 @@ PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value) noexcept
4915
4955
  }
4916
4956
  // owner is 0: not private for this access context -> normal attribute
4917
4957
  }
4958
+ mark_confirmed_public(typ_id, name_str);
4918
4959
  }
4919
- auto it = ::AllData::all_type_setattro.find((uintptr_t)Py_TYPE(cls));
4920
- if (it != ::AllData::all_type_setattro.end()) {
4921
- if (it->second && it->second != PrivateAttrType_setattr) {
4922
- return it->second(cls, name, value);
4923
- }
4924
- }
4925
- PyTypeObject* base = Py_TYPE(cls)->tp_base;
4926
- while (base && base->tp_base && base->tp_setattro == PrivateAttrType_setattr) {
4927
- base = base->tp_base;
4928
- }
4929
- if (!base) {
4930
- return new_type_tp_setattro(cls, name, value);
4931
- }
4932
- int result = base->tp_setattro(cls, name, value);
4933
- ensure_tp((PyTypeObject*)cls);
4934
- ensure_subclass_tp((PyTypeObject*)cls);
4935
- return result;
4960
+ return new_type_tp_setattro(cls, name, value);
4936
4961
  }
4937
4962
 
4938
4963
  static void
4939
4964
  PrivateAttrType_finalize(PyObject* cls) noexcept
4940
4965
  {
4941
4966
  uintptr_t typ_id = (uintptr_t) cls;
4942
- if (::AllData::all_type_del.find((uintptr_t)Py_TYPE(cls)) != ::AllData::all_type_del.end()) {
4943
- destructor orig = ::AllData::all_type_del[(uintptr_t)Py_TYPE(cls)];
4944
- if (orig && orig != PrivateAttrType_finalize) {
4945
- orig(cls);
4946
- return;
4947
- }
4948
- }
4949
4967
  if (::AllData::all_type_attr_set.find(typ_id) != ::AllData::all_type_attr_set.end()) {
4950
4968
  ::AllData::all_type_attr_set.erase(typ_id);
4951
4969
  }
@@ -4981,12 +4999,6 @@ PrivateAttrType_finalize(PyObject* cls) noexcept
4981
4999
  }
4982
5000
  }
4983
5001
  }
4984
- if (::AllData::all_type_getattro.find(typ_id) != ::AllData::all_type_getattro.end()) {
4985
- ::AllData::all_type_getattro.erase(typ_id);
4986
- }
4987
- if (::AllData::all_type_setattro.find(typ_id) != ::AllData::all_type_setattro.end()) {
4988
- ::AllData::all_type_setattro.erase(typ_id);
4989
- }
4990
5002
  ::AllData::all_type_mutex.erase(typ_id);
4991
5003
  // Clear the per-type buckets of the object-attr tables (created in
4992
5004
  // PrivateAttrType_postprocess / PrivateAttr_object_init_private_dict);
@@ -4998,6 +5010,7 @@ PrivateAttrType_finalize(PyObject* cls) noexcept
4998
5010
  if (::AllData::all_object_mutex.find(typ_id) != ::AllData::all_object_mutex.end()) {
4999
5011
  ::AllData::all_object_mutex.erase(typ_id);
5000
5012
  }
5013
+ clear_public_obj(typ_id);
5001
5014
  clear_obj(typ_id);
5002
5015
  }
5003
5016
 
@@ -5283,12 +5296,8 @@ metaclass_weakref_callback(PyObject* /*self*/, PyObject* weakref) noexcept
5283
5296
  // erase; do not DECREF here (double-DECREF).
5284
5297
  ::AllData::all_register_type_weak_ref.erase(it);
5285
5298
  }
5286
- METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(getattro);
5287
- METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(setattro);
5288
5299
  METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(traverse);
5289
5300
  METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(clear);
5290
- METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(del);
5291
- METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(call);
5292
5301
 
5293
5302
  Py_RETURN_NONE;
5294
5303
  }
@@ -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.0"
7
+ version = "3.0.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"
@@ -174,6 +174,98 @@ class TestOwnership(unittest.TestCase):
174
174
  with self.assertRaises(AttributeError):
175
175
  read_missing()
176
176
 
177
+ def test_non_private_attribute_of_b_rejected_early(self):
178
+ # Only private attributes of B (the second type) are reachable through
179
+ # PrivateSuper. A public attribute on B must be rejected before any
180
+ # storage lookup.
181
+ class Owner(private_attribute.PrivateAttrBase):
182
+ __private_attrs__ = ["_p"]
183
+ _p = "private"
184
+ public = "PUBLIC"
185
+
186
+ class Child(Owner, use_parents=True):
187
+ __private_attrs__ = []
188
+
189
+ def read_public(self):
190
+ return private_attribute.PrivateSuper(Child, Owner, Owner, self).public
191
+
192
+ with self.assertRaises(AttributeError):
193
+ Child().read_public()
194
+
195
+ def test_non_private_attribute_of_b_rejected_early_class_super(self):
196
+ # Same early rejection through the classmethod flavour.
197
+ class Owner(private_attribute.PrivateAttrBase):
198
+ __private_attrs__ = ["_p"]
199
+ _p = "private"
200
+ public = "PUBLIC"
201
+
202
+ class Child(Owner, use_parents=True):
203
+ __private_attrs__ = []
204
+
205
+ @classmethod
206
+ def read_public(cls):
207
+ return private_attribute.PrivateClassSuper(Child, Owner, Owner, cls).public
208
+
209
+ with self.assertRaises(AttributeError):
210
+ Child.read_public()
211
+
212
+
213
+ class TestMultipleInheritance(unittest.TestCase):
214
+ """
215
+ The MRO-id-list subclass checks must not mistake a multiple-inheritance
216
+ sibling (which may appear before B in A's MRO) for a subclass of B.
217
+ """
218
+
219
+ def test_sibling_before_b_is_not_a_subclass_of_b(self):
220
+ class Root(private_attribute.PrivateAttrBase):
221
+ __private_attrs__ = []
222
+
223
+ class Sibling(Root):
224
+ __private_attrs__ = []
225
+
226
+ class B(Root):
227
+ __private_attrs__ = ["_g"]
228
+ _g = "g"
229
+
230
+ # A inherits Sibling before B, so Sibling appears before B in A's MRO,
231
+ # yet Sibling is not a subclass of B.
232
+ class A(Sibling, B):
233
+ __private_attrs__ = []
234
+
235
+ def read(self):
236
+ return private_attribute.PrivateSuper(A, B, Sibling, self)._g
237
+
238
+ self.assertEqual(A.__mro__, (A, Sibling, B, Root,
239
+ private_attribute.PrivateAttrBase, object))
240
+ with self.assertRaises(ValueError):
241
+ A().read()
242
+
243
+ def test_true_subclass_of_b_accepted_in_multiple_inheritance(self):
244
+ class Root(private_attribute.PrivateAttrBase):
245
+ __private_attrs__ = []
246
+
247
+ class B(Root):
248
+ __private_attrs__ = ["_g"]
249
+ _g = "g"
250
+
251
+ @classmethod
252
+ def set_g(cls, v):
253
+ cls._g = v
254
+
255
+ class Mid(B):
256
+ __private_attrs__ = []
257
+
258
+ class A(Mid, Root):
259
+ __private_attrs__ = []
260
+
261
+ def read(self):
262
+ return private_attribute.PrivateSuper(A, B, Mid, self)._g
263
+
264
+ # Mid is a genuine subclass of B; the MRO-id check must accept it and
265
+ # read B's shadow for Mid.
266
+ Mid.set_g("written-on-mid")
267
+ self.assertEqual(A().read(), "written-on-mid")
268
+
177
269
 
178
270
  if __name__ == "__main__":
179
271
  unittest.main()