private-attribute-cpp 2.1.19__tar.gz → 2.1.21__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 (24) hide show
  1. {private_attribute_cpp-2.1.19/private_attribute_cpp.egg-info → private_attribute_cpp-2.1.21}/PKG-INFO +1 -1
  2. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/private_attribute.cpp +96 -86
  3. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/private_attribute.pyi +2 -2
  4. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21/private_attribute_cpp.egg-info}/PKG-INFO +1 -1
  5. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/setup.py +1 -1
  6. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/LICENSE +0 -0
  7. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/MANIFEST.in +0 -0
  8. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/README.md +0 -0
  9. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/picosha2.h +0 -0
  10. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/private_attribute_cpp.egg-info/SOURCES.txt +0 -0
  11. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/private_attribute_cpp.egg-info/dependency_links.txt +0 -0
  12. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/private_attribute_cpp.egg-info/not-zip-safe +0 -0
  13. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/private_attribute_cpp.egg-info/top_level.txt +0 -0
  14. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/setup.cfg +0 -0
  15. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_attr_classify_owner.py +0 -0
  16. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_class_private_attrs.py +0 -0
  17. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_gc_collect.py +0 -0
  18. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_private_abc.py +0 -0
  19. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_private_attrs.py +0 -0
  20. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_private_func.py +0 -0
  21. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_private_inheritance.py +0 -0
  22. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_private_method.py +0 -0
  23. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_private_wrap.py +0 -0
  24. {private_attribute_cpp-2.1.19 → private_attribute_cpp-2.1.21}/tests/test_slots_cycle.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: private_attribute_cpp
3
- Version: 2.1.19
3
+ Version: 2.1.21
4
4
  Summary: A Python package that provides a way to define private attributes in C++ implementation.
5
5
  Home-page: https://github.com/Locked-chess-official/private_attribute_cpp
6
6
  Author: HuangHaoHua
@@ -508,8 +508,8 @@ custom_random_string(uintptr_t obj_id, const std::string& attr_name, PyObject* f
508
508
  lock.unlock();
509
509
  PyObject* python_result = PyObject_CallFunction(
510
510
  func,
511
- "ns",
512
- static_cast<Py_ssize_t>(obj_id),
511
+ "Ks",
512
+ static_cast<unsigned long long>(obj_id),
513
513
  attr_name.c_str()
514
514
  );
515
515
  if (python_result) {
@@ -974,7 +974,7 @@ listproxy_count(PyObject* self, PyObject* value) noexcept
974
974
  if (m < 0) {
975
975
  return NULL;
976
976
  }
977
- PyObject* result = PyLong_FromLong(m);
977
+ PyObject* result = PyLong_FromSsize_t(m);
978
978
  return result;
979
979
  }
980
980
 
@@ -1154,7 +1154,7 @@ listproxy_clear(PyObject* self) noexcept
1154
1154
 
1155
1155
  static PyTypeObject listproxyType = {
1156
1156
  PyVarObject_HEAD_INIT(NULL, 0)
1157
- "private_attribute.listproxy", // tp_name
1157
+ "private_attribute._listproxy", // tp_name
1158
1158
  sizeof(listproxy), // tp_basicsize
1159
1159
  0, // tp_itemsize
1160
1160
  (destructor)listproxy_dealloc, // tp_dealloc
@@ -1172,7 +1172,7 @@ static PyTypeObject listproxyType = {
1172
1172
  0, // tp_getattro
1173
1173
  0, // tp_setattro
1174
1174
  0, // tp_as_buffer
1175
- Py_TPFLAGS_DEFAULT| Py_TPFLAGS_HAVE_GC, // tp_flags
1175
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
1176
1176
  "A read-only proxy of a list.", // tp_doc
1177
1177
  listproxy_traverse, // tp_traverse
1178
1178
  listproxy_clear, // tp_clear
@@ -2364,17 +2364,12 @@ PrivateAttrType_tp_clear(PyObject* self) noexcept
2364
2364
  static void
2365
2365
  PrivateAttr_object_init_private_dict(uintptr_t obj_id, uintptr_t type_id) noexcept
2366
2366
  {
2367
- if (::AllData::all_object_mutex.find(type_id) == ::AllData::all_object_mutex.end()) {
2368
- ::AllData::all_object_mutex[type_id].clear();
2369
- }
2370
- if (::AllData::all_object_attr.find(type_id) == ::AllData::all_object_attr.end()) {
2371
- ::AllData::all_object_attr[type_id] = {};
2372
- }
2373
- if (::AllData::all_object_mutex[type_id].find(obj_id) == ::AllData::all_object_mutex[type_id].end()) {
2374
- ::AllData::all_object_mutex[type_id][obj_id] = std::make_unique<std::shared_mutex>();
2375
- }
2376
- if (::AllData::all_object_attr[type_id].find(obj_id) == ::AllData::all_object_attr[type_id].end()) {
2377
- ::AllData::all_object_attr[type_id][obj_id] = {};
2367
+ object_create_mutex(type_id, obj_id);
2368
+ {
2369
+ std::unique_lock<std::shared_mutex> lock(*::AllData::all_object_mutex[type_id][obj_id]);
2370
+ if (::AllData::all_object_attr[type_id].find(obj_id) == ::AllData::all_object_attr[type_id].end()) {
2371
+ ::AllData::all_object_attr[type_id][obj_id] = {};
2372
+ }
2378
2373
  }
2379
2374
  if (::AllData::all_type_parent_id.find(type_id) != ::AllData::all_type_parent_id.end()) {
2380
2375
  for (auto parent_id : ::AllData::all_type_parent_id[type_id]) {
@@ -2441,7 +2436,7 @@ get_string_hash_tuple(const std::string& name) noexcept
2441
2436
  std::string name1hash, name2hash;
2442
2437
  picosha2::hash256_hex_string(name1, name1hash);
2443
2438
  picosha2::hash256_hex_string(name2, name2hash);
2444
- return PyTuple_Pack(2, PyUnicode_FromString(name1hash.c_str()), PyUnicode_FromString(name2hash.c_str()));
2439
+ return Py_BuildValue("(ss)", name1hash.c_str(), name2hash.c_str());
2445
2440
  }
2446
2441
 
2447
2442
  static TwoStringTuple
@@ -2730,6 +2725,14 @@ real_class_name(const std::string& name, const std::string& class_name) noexcept
2730
2725
  return name;
2731
2726
  }
2732
2727
 
2728
+ #define PRIVATEATTR_CREATION_DATA_CLEAR(name) do \
2729
+ { \
2730
+ if (name) { \
2731
+ Py_DECREF(name); \
2732
+ name = nullptr; \
2733
+ } \
2734
+ } while (0)
2735
+
2733
2736
  struct PrivateAttrCreationData
2734
2737
  {
2735
2738
  std::string class_name;
@@ -2753,34 +2756,13 @@ struct PrivateAttrCreationData
2753
2756
  if (cleared) {
2754
2757
  return;
2755
2758
  }
2756
- if (name) {
2757
- Py_DECREF(name);
2758
- name = nullptr;
2759
- }
2760
- if (bases) {
2761
- Py_DECREF(bases);
2762
- bases = nullptr;
2763
- }
2764
- if (attrs) {
2765
- Py_DECREF(attrs);
2766
- attrs = nullptr;
2767
- }
2768
- if (attrs_copy) {
2769
- Py_DECREF(attrs_copy);
2770
- attrs_copy = nullptr;
2771
- }
2772
- if (new_hash_private_attrs) {
2773
- Py_DECREF(new_hash_private_attrs);
2774
- new_hash_private_attrs = nullptr;
2775
- }
2776
- if (private_func) {
2777
- Py_DECREF(private_func);
2778
- private_func = nullptr;
2779
- }
2780
- if (base_kwds) {
2781
- Py_DECREF(base_kwds);
2782
- base_kwds = nullptr;
2783
- }
2759
+ PRIVATEATTR_CREATION_DATA_CLEAR(name);
2760
+ PRIVATEATTR_CREATION_DATA_CLEAR(bases);
2761
+ PRIVATEATTR_CREATION_DATA_CLEAR(attrs);
2762
+ PRIVATEATTR_CREATION_DATA_CLEAR(attrs_copy);
2763
+ PRIVATEATTR_CREATION_DATA_CLEAR(new_hash_private_attrs);
2764
+ PRIVATEATTR_CREATION_DATA_CLEAR(private_func);
2765
+ PRIVATEATTR_CREATION_DATA_CLEAR(base_kwds);
2784
2766
 
2785
2767
  need_remove_itself.clear();
2786
2768
  need_remove_subclass.clear();
@@ -3011,7 +2993,7 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
3011
2993
 
3012
2994
  // forbidden to have "__static_attributes__" in attrs, because it exposes the private attributes name.
3013
2995
  if (PyDict_ContainsString(data.attrs_copy, "__static_attributes__")) {
3014
- PyDict_DelItemString(data.attrs_copy, "__static_attributes__");
2996
+ if (PyDict_DelItemString(data.attrs_copy, "__static_attributes__") < 0) return false;
3015
2997
  }
3016
2998
 
3017
2999
  {
@@ -3036,7 +3018,7 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
3036
3018
  // (PrivateAttrCreationData::clear). No manual INCREF here,
3037
3019
  // otherwise the value leaks (+1) when the class is destroyed.
3038
3020
  data.need_remove_itself[attr_name] = need_value;
3039
- PyDict_DelItem(data.attrs_copy, key);
3021
+ if (PyDict_DelItem(data.attrs_copy, key)) return false;
3040
3022
  continue;
3041
3023
  }
3042
3024
  uintptr_t need_remove_subclass_id = need_remove_to_subclass(attr_name);
@@ -3049,10 +3031,12 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
3049
3031
  }
3050
3032
  // PyObjectStorage assignment INCREFs (RAII); no manual INCREF.
3051
3033
  data.need_remove_subclass[need_remove_subclass_id][attr_name] = need_value;
3052
- PyDict_DelItem(data.attrs_copy, key);
3034
+ if (PyDict_DelItem(data.attrs_copy, key)) return false;
3053
3035
  continue;
3054
3036
  }
3055
- if (value != need_value) PyDict_SetItem(data.attrs_copy, key, need_value);
3037
+ if (value != need_value) {
3038
+ if (PyDict_SetItem(data.attrs_copy, key, need_value) < 0) return false;
3039
+ }
3056
3040
  }
3057
3041
  Py_DECREF(forward_analyse);
3058
3042
  }
@@ -3068,7 +3052,9 @@ PrivateAttrType_preprocess(PyObject* args, PyObject* kwds, PrivateAttrCreationDa
3068
3052
  if (!data.base_kwds) {
3069
3053
  return false;
3070
3054
  }
3071
- if (has_private_func) PyDict_DelItemString(data.base_kwds, "private_func");
3055
+ if (has_private_func) {
3056
+ if (PyDict_DelItemString(data.base_kwds, "private_func") < 0) return false;
3057
+ }
3072
3058
  }
3073
3059
 
3074
3060
  return true;
@@ -3874,54 +3860,78 @@ static const char* PrivateAttrBase_doc = "The class to help to create private at
3874
3860
  static PyObject*
3875
3861
  create_private_attr_base_simple(void) noexcept
3876
3862
  {
3877
- PyObject* name = PyUnicode_InternFromString("PrivateAttrBase");
3878
- if (!name) return NULL;
3879
- PyObject* bases = PyTuple_New(0);
3880
- if (!bases) {
3881
- Py_DECREF(name);
3882
- return NULL;
3883
- }
3884
- PyObject* dict = PyDict_New();
3885
- if (!dict) {
3886
- Py_DECREF(name);
3887
- Py_DECREF(bases);
3888
- return NULL;
3889
- }
3890
- PyObject *private_attrs = PyTuple_New(0);
3891
- if (!private_attrs) {
3892
- Py_DECREF(name);
3893
- Py_DECREF(bases);
3894
- Py_DECREF(dict);
3895
- return NULL;
3896
- }
3897
- PyDict_SetItemString(dict, "__private_attrs__", private_attrs);
3898
- PyDict_SetItemString(dict, "__slots__", private_attrs);
3899
- PyDict_SetItemString(dict, "__doc__", PyUnicode_InternFromString(PrivateAttrBase_doc));
3900
- PyDict_SetItemString(dict, "__module__", PyUnicode_InternFromString("private_attribute"));
3901
- PyObject *args = PyTuple_Pack(3, name, bases, dict);
3902
- PyObject* base_type;
3903
- if (args) {
3863
+ PyObject* name = NULL;
3864
+ PyObject* bases = NULL;
3865
+ PyObject* dict = NULL;
3866
+ PyObject* private_attrs = NULL;
3867
+ PyObject* doc = NULL;
3868
+ PyObject* module_name = NULL;
3869
+ PyObject* args = NULL;
3870
+ PyObject* base_type = NULL;
3871
+
3872
+ name = PyUnicode_InternFromString("PrivateAttrBase");
3873
+ if (!name) goto fail;
3874
+
3875
+ bases = PyTuple_New(0);
3876
+ if (!bases) goto fail;
3877
+
3878
+ dict = PyDict_New();
3879
+ if (!dict) goto fail;
3880
+
3881
+ private_attrs = PyTuple_New(0);
3882
+ if (!private_attrs) goto fail;
3883
+
3884
+ doc = PyUnicode_InternFromString(PrivateAttrBase_doc);
3885
+ if (!doc) goto fail;
3886
+
3887
+ module_name = PyUnicode_InternFromString("private_attribute");
3888
+ if (!module_name) goto fail;
3889
+
3890
+ if (PyDict_SetItemString(dict, "__private_attrs__", private_attrs) < 0) goto fail;
3891
+ if (PyDict_SetItemString(dict, "__slots__", private_attrs) < 0) goto fail;
3892
+ if (PyDict_SetItemString(dict, "__doc__", doc) < 0) goto fail;
3893
+ if (PyDict_SetItemString(dict, "__module__", module_name) < 0) goto fail;
3894
+
3895
+ Py_DECREF(doc);
3896
+ doc = NULL;
3897
+ Py_DECREF(module_name);
3898
+ module_name = NULL;
3899
+
3900
+ args = PyTuple_Pack(3, name, bases, dict);
3901
+ if (!args) goto fail;
3902
+
3903
+ {
3904
3904
  // RAII: the first heap type created through tp_new (PrivateAttrBase)
3905
3905
  // carries CPython's pristine subtype_traverse/subtype_clear in its
3906
3906
  // tp_traverse/tp_clear; PrivateAttrType_new captures those pointers
3907
3907
  // while this guard is active.
3908
3908
  PrivateAttrCaptureGuard capture_guard;
3909
3909
  base_type = PrivateAttrType_new((PyTypeObject*)&PrivateAttrType, args, NULL);
3910
- Py_DECREF(args);
3911
- } else {
3912
- Py_DECREF(name);
3913
- Py_DECREF(bases);
3914
- Py_DECREF(dict);
3915
- return NULL;
3916
3910
  }
3911
+ Py_DECREF(args);
3912
+ args = NULL;
3913
+
3917
3914
  Py_DECREF(name);
3915
+ name = NULL;
3918
3916
  Py_DECREF(bases);
3917
+ bases = NULL;
3919
3918
  Py_DECREF(dict);
3920
- if (!base_type) {
3921
- return NULL;
3922
- }
3919
+ dict = NULL;
3920
+
3921
+ if (!base_type) goto fail;
3922
+
3923
3923
  ((PyTypeObject*)base_type)->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
3924
3924
  return base_type;
3925
+
3926
+ fail:
3927
+ Py_XDECREF(name);
3928
+ Py_XDECREF(bases);
3929
+ Py_XDECREF(dict);
3930
+ Py_XDECREF(private_attrs);
3931
+ Py_XDECREF(doc);
3932
+ Py_XDECREF(module_name);
3933
+ Py_XDECREF(args);
3934
+ return NULL;
3925
3935
  }
3926
3936
 
3927
3937
  typedef struct {
@@ -25,7 +25,7 @@ class _PrivateAttrDict(TypedDict):
25
25
 
26
26
  _T = TypeVar('_T')
27
27
 
28
- class listproxy(Generic[_T]):
28
+ class _listproxy(Generic[_T]):
29
29
  "a read-only proxy of a list"
30
30
  def copy(self) -> list[_T]: ...
31
31
  def count(self, value: Any, /) -> int: ...
@@ -42,7 +42,7 @@ class _PrivateWrap(Generic[_T]):
42
42
  ...
43
43
 
44
44
  @property
45
- def funcs(self) -> listproxy[FunctionType]:
45
+ def funcs(self) -> _listproxy[FunctionType]:
46
46
  "the original functions"
47
47
  ...
48
48
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: private_attribute_cpp
3
- Version: 2.1.19
3
+ Version: 2.1.21
4
4
  Summary: A Python package that provides a way to define private attributes in C++ implementation.
5
5
  Home-page: https://github.com/Locked-chess-official/private_attribute_cpp
6
6
  Author: HuangHaoHua
@@ -24,7 +24,7 @@ readme = open('README.md').read()
24
24
 
25
25
  setup(
26
26
  name='private_attribute_cpp',
27
- version='2.1.19',
27
+ version='2.1.21',
28
28
  author="HuangHaoHua",
29
29
  author_email="13140752715@example.com",
30
30
  description='A Python package that provides a way to define private attributes in C++ implementation.',