private-attribute-cpp 1.3.4__tar.gz → 1.3.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: private_attribute_cpp
3
- Version: 1.3.4
3
+ Version: 1.3.6
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
@@ -61,7 +61,8 @@ public:
61
61
  }
62
62
  };
63
63
 
64
- class TwoStringTuple {
64
+ class TwoStringTuple
65
+ {
65
66
  private:
66
67
  std::string first;
67
68
  std::string second;
@@ -127,7 +128,8 @@ namespace {
127
128
  };
128
129
  };
129
130
 
130
- struct FinalObject {
131
+ struct FinalObject
132
+ {
131
133
  PyObject* result = NULL;
132
134
  int status = 0;
133
135
  FinalObject(PyObject* result)
@@ -816,11 +818,10 @@ type_delattr(PyObject* typ, std::string attr_name)
816
818
  // ================================================================
817
819
  // _PrivateWrap
818
820
  // ================================================================
819
- typedef struct PrivateWrapObject {
821
+ typedef struct {
820
822
  PyObject_HEAD
821
823
  PyObject *result;
822
824
  PyObject *func_list;
823
- PyObject *decorator;
824
825
  } PrivateWrapObject;
825
826
 
826
827
  static PrivateWrapObject* PrivateWrap_New(PyObject *decorator, PyObject *func, PyObject *list);
@@ -1009,9 +1010,6 @@ PrivateWrap_New(PyObject *decorator, PyObject *func, PyObject *list)
1009
1010
  return NULL;
1010
1011
  }
1011
1012
 
1012
- self->decorator = decorator;
1013
- Py_INCREF(decorator);
1014
-
1015
1013
  self->func_list = list;
1016
1014
  Py_INCREF(list);
1017
1015
 
@@ -1025,7 +1023,6 @@ PrivateWrap_dealloc(PrivateWrapObject *self)
1025
1023
  {
1026
1024
  Py_XDECREF(self->result);
1027
1025
  Py_XDECREF(self->func_list);
1028
- Py_XDECREF(self->decorator);
1029
1026
  Py_TYPE(self)->tp_free((PyObject *)self);
1030
1027
  }
1031
1028
 
@@ -1674,7 +1671,8 @@ real_class_name(std::string name, std::string class_name)
1674
1671
  return name;
1675
1672
  }
1676
1673
 
1677
- struct PrivateAttrCreationData {
1674
+ struct PrivateAttrCreationData
1675
+ {
1678
1676
  std::string class_name;
1679
1677
  PyObject* attrs_copy = nullptr;
1680
1678
  PyObject* new_hash_private_attrs = nullptr;
@@ -2214,8 +2212,14 @@ PrivateAttrType_setattr(PyObject* cls, PyObject* name, PyObject* value)
2214
2212
  PyErr_SetString(PyExc_TypeError, "cls must be a type");
2215
2213
  return -1;
2216
2214
  }
2215
+ // check Py_TPFLAGS_IMMUTABLETYPE
2217
2216
  uintptr_t typ_id = (uintptr_t)(cls);
2218
2217
  std::string name_str = PyUnicode_AsUTF8(name);
2218
+ if (((PyTypeObject*)cls)->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
2219
+ std::string error_message = "cannot set '" + name_str + "' attribute of immutable type '" + ((PyTypeObject*)cls)->tp_name + "'";
2220
+ PyErr_SetString(PyExc_TypeError, error_message.c_str());
2221
+ return -1;
2222
+ }
2219
2223
  PyCodeObject* now_code = get_now_code();
2220
2224
  if (type_private_attr(typ_id, name_str)) {
2221
2225
  if (!now_code || (!is_class_code(typ_id, now_code) && !is_subclass_code(typ_id, now_code))) {
@@ -2315,8 +2319,7 @@ PrivateAttrType_del(PyObject* cls)
2315
2319
  (Py_TYPE(cls))->tp_free(cls);
2316
2320
  }
2317
2321
 
2318
- static const char* PrivateAttrBase_doc = "The class to help to create private attribute. "
2319
- "It does not have any special behavior, but it can be used as a base class for PrivateAttrType to avoid some conflicts with other metaclasses.";
2322
+ static const char* PrivateAttrBase_doc = "The class to help to create private attribute. It does not have any special behavior.";
2320
2323
 
2321
2324
  // PrivateAttrBase
2322
2325
  static PyObject*
@@ -2378,7 +2381,7 @@ PrivateTempObject_name(PyObject* self, void* /*closure*/)
2378
2381
  {
2379
2382
  PyObject* name = ((PrivateTempObject*)self)->tmp->name;
2380
2383
  if (!name) {
2381
- PyErr_SetString(PyExc_RuntimeError, "object not init");
2384
+ PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
2382
2385
  return nullptr;
2383
2386
  }
2384
2387
  Py_INCREF(name);
@@ -2390,7 +2393,7 @@ PrivateTempObject_base(PyObject* self, void* /*closure*/)
2390
2393
  {
2391
2394
  PyObject* base = ((PrivateTempObject*)self)->tmp->bases;
2392
2395
  if (!base) {
2393
- PyErr_SetString(PyExc_RuntimeError, "object not init");
2396
+ PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
2394
2397
  return nullptr;
2395
2398
  }
2396
2399
  Py_INCREF(base);
@@ -2402,7 +2405,7 @@ PrivateTempObject_attrs(PyObject* self, void* /*closure*/)
2402
2405
  {
2403
2406
  PyObject* attrs = ((PrivateTempObject*)self)->tmp->attrs_copy;
2404
2407
  if (!attrs) {
2405
- PyErr_SetString(PyExc_RuntimeError, "object not init");
2408
+ PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
2406
2409
  return nullptr;
2407
2410
  }
2408
2411
  Py_INCREF(attrs);
@@ -2414,7 +2417,7 @@ PrivateTempObject_kwds(PyObject* self, void* /*closure*/)
2414
2417
  {
2415
2418
  PyObject* kwds = ((PrivateTempObject*)self)->tmp->base_kwds;
2416
2419
  if (!kwds) {
2417
- PyErr_SetString(PyExc_RuntimeError, "object not init");
2420
+ PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
2418
2421
  return nullptr;
2419
2422
  }
2420
2423
  Py_INCREF(kwds);
@@ -2473,20 +2476,25 @@ static PyTypeObject PrivateTempType = {
2473
2476
  static PyObject*
2474
2477
  prepare_for_PrivateAttr(PyObject* /*self*/, PyObject* args, PyObject* kwargs)
2475
2478
  {
2476
- PrivateTempObject* tmp = PyObject_New(PrivateTempObject, &PrivateTempType);
2477
- if (!tmp) {
2479
+ PrivateAttrCreationData* tmp_data = new PrivateAttrCreationData();
2480
+ if (!PrivateAttrType_preprocess(args, kwargs, *tmp_data)) {
2481
+ tmp_data->clear();
2482
+ delete tmp_data;
2478
2483
  return NULL;
2479
2484
  }
2480
- tmp->tmp = new PrivateAttrCreationData();
2481
- if (!PrivateAttrType_preprocess(args, kwargs, *(tmp->tmp))) {
2482
- Py_DECREF(tmp);
2485
+ PrivateTempObject* tmp = PyObject_New(PrivateTempObject, &PrivateTempType);
2486
+ if (!tmp) {
2487
+ tmp_data->clear();
2488
+ delete tmp_data;
2483
2489
  return NULL;
2484
2490
  }
2491
+ tmp->tmp = tmp_data;
2485
2492
  return (PyObject*)tmp;
2486
2493
  }
2487
2494
 
2488
2495
  static PyObject*
2489
- postprocess_for_PrivateAttr(PyObject* /*self*/, PyObject* args) {
2496
+ postprocess_for_PrivateAttr(PyObject* /*self*/, PyObject* args)
2497
+ {
2490
2498
  PyObject* type;
2491
2499
  PyObject* tmp;
2492
2500
  if (!PyArg_ParseTuple(args, "OO", &type, &tmp)) {
@@ -2542,7 +2550,6 @@ register_metaclass(PyObject* /*self*/, PyObject* metaclass)
2542
2550
  ((PyTypeObject*)metaclass)->tp_getattro = PrivateAttrType_getattr;
2543
2551
  ((PyTypeObject*)metaclass)->tp_setattro = PrivateAttrType_setattr;
2544
2552
  ((PyTypeObject*)metaclass)->tp_finalize = register_finalize;
2545
- ((PyTypeObject*)metaclass)->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
2546
2553
  Py_RETURN_NONE;
2547
2554
  }
2548
2555
 
@@ -2655,13 +2662,14 @@ static PyGetSetDef PrivateModule_getsetters[] = {
2655
2662
  static const char* prepare_and_postprocess_doc = R"(function for custom metaclass to create private attributes class.
2656
2663
 
2657
2664
  def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
2658
- the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments. The custom metaclass can call this function in its __prepare__ method to get the information for creating private attributes class.
2665
+ the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
2659
2666
 
2660
2667
  def postprocess(type: type, tmp: tempobject) -> None:
2661
2668
  the function to postprocess for creating private attributes class. The custom metaclass can call this
2662
2669
 
2663
2670
  def register_metaclass(metaclass: type) -> None:
2664
- the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class, otherwise the private attributes class created by this custom metaclass will not work.
2671
+ the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
2672
+ otherwise the private attributes class created by this custom metaclass will not work.
2665
2673
 
2666
2674
  All usage of this module should be like:
2667
2675
  ```
@@ -2675,18 +2683,16 @@ class PrivateAbcMeta(ABCMeta):
2675
2683
  private_attribute.postprocess(typ, temp)
2676
2684
  return typ
2677
2685
 
2678
-
2679
2686
  private_attribute.register_metaclass(PrivateAbcMeta)
2680
2687
  ```
2681
2688
  )";
2682
2689
 
2683
2690
  static const char* ensure_type_doc = R"(function for custom metaclass to ensure the type is a private attributes class.
2684
-
2685
2691
  def ensure_type(type: type) -> None:
2686
2692
  the function to ensure the type is a private attributes class `tp_getattro`, `tp_setattro` and `tp_finalizer`.
2687
2693
  )";
2688
2694
 
2689
- static const char* ensure_metaclass_doc = R"(function for custom metaclass to ensure the metaclass is registered.
2695
+ static const char* ensure_metaclass_doc = R"(function for custom metaclass to ensure the metaclass is working.
2690
2696
  def ensure_metaclass(metaclass: type) -> None:
2691
2697
  the function to ensure the metaclass `tp_getattro`, `tp_setattro` and `tp_finalizer`.
2692
2698
  )";
@@ -2740,7 +2746,7 @@ A module that provides a metaclass for creating classes with private attributes.
2740
2746
  Private attributes are defined in the `__private_attrs__` sequence and are only
2741
2747
  You can use the `PrivateAttrBase` metaclass to create classes with private attributes.
2742
2748
  The attributes which are private are not on the instance's `__dict__` and cannot be accessed outside
2743
- but in the classmethods it is reachable.
2749
+ but in the methods defined in class it is reachable.
2744
2750
  Usage example:
2745
2751
  ```python
2746
2752
  class MyClass(PrivateAttrBase):
@@ -2773,7 +2779,7 @@ PyInit_private_attribute(void)
2773
2779
  PyType_Ready(&PrivateWrapProxyType) < 0 ||
2774
2780
  PyType_Ready(&PrivateAttrType) < 0 ||
2775
2781
  PyType_Ready(&PrivateModuleType) < 0 ||
2776
- PyType_Ready(&PrivateTempType)) {
2782
+ PyType_Ready(&PrivateTempType) < 0) {
2777
2783
  return NULL;
2778
2784
  }
2779
2785
 
@@ -0,0 +1,200 @@
1
+ """A module that provides a metaclass for creating classes with private attributes.
2
+ Private attributes are defined in the `__private_attrs__` sequence and are only
3
+ You can use the `PrivateAttrBase` metaclass to create classes with private attributes.
4
+ The attributes which are private are not on the instance's `__dict__` and cannot be accessed outside
5
+ but in the methods defined in class it is reachable.
6
+ Usage example:
7
+ ```python
8
+ class MyClass(PrivateAttrBase):
9
+ __private_attrs__ = ('_private_attr1',)
10
+ def __init__(self):
11
+ self._private_attr1 = 1
12
+
13
+ @property
14
+ def public_attr1(self):
15
+ return self._private_attr1
16
+ ```
17
+ """
18
+ from typing import Any, TypeVar, Callable, TypedDict, Sequence, Generic
19
+ from types import FunctionType
20
+
21
+ # define the dict that must have a key "__private_attrs__" and value must be the sequence of strings
22
+ class PrivateAttrDict(TypedDict):
23
+ __private_attrs__: Sequence[str]
24
+
25
+ T = TypeVar('T')
26
+
27
+ class _PrivateWrap(Generic[T]):
28
+ @property
29
+ def result(self) -> T:
30
+ "the final result of decorating"
31
+ ...
32
+
33
+ @property
34
+ def funcs(self) -> tuple[FunctionType]:
35
+ "the original functions"
36
+ ...
37
+
38
+ def __getattr__(self, name: str) -> Any:
39
+ return getattr(self.result, name)
40
+
41
+ TVar = TypeVar('TVar')
42
+
43
+ class PrivateWrapProxy:
44
+ """
45
+ PrivateWrapProxy is a proxy for private attributes.
46
+ Usage:
47
+ ```
48
+ from private_attribute import PrivateWrapProxy, PrivateAttrBase
49
+
50
+ class MyClass(PrivateAttrBase):
51
+ __private_attrs__ = ()
52
+ @PrivateWrapProxy(decorator)
53
+ def my_method(self): ...
54
+
55
+ @PrivateWrapProxy(decorator)
56
+ def my_method2(self): ...
57
+ ```
58
+ It returned a '_PrivateWrap' object.
59
+
60
+ If you need to decorate more function, use like this:
61
+ ```
62
+ from private_attribute import PrivateWrapProxy, PrivateAttrBase
63
+
64
+ class MyClass(PrivateAttrBase):
65
+ __private_attrs__ = ()
66
+ @PrivateWrapProxy(decorator)
67
+ def my_method(self): ...
68
+
69
+ @PrivateWrapProxy(my_method.some_decorator, my_method)
70
+ def my_method(self): ...
71
+ ```
72
+ """
73
+ def __init__(self, decorator: Callable[[TVar], T], orig: _PrivateWrap[Any]|None = None, /) -> None: ...
74
+ def __call__(self, func: TVar, /) -> _PrivateWrap[T]: ...
75
+
76
+ class PrivateAttrType(type):
77
+ "metaclass for private attributes"
78
+ def __new__(cls, name: str, bases: tuple,
79
+ attrs: PrivateAttrDict, /,
80
+ private_func: Callable[[int, str], str]|None = None) -> PrivateAttrType: ...
81
+
82
+ class PrivateAttrBase(metaclass=PrivateAttrType):
83
+ "The class to help to create private attribute. It does not have any special behavior."
84
+ __slots__ = ()
85
+ __private_attrs__ = ()
86
+
87
+
88
+ class _PrivateTemp:
89
+ @property
90
+ def name(self) -> str: ...
91
+ @property
92
+ def bases(self) -> tuple[type]: ...
93
+ @property
94
+ def attrs(self) -> dict[str, Any]: ...
95
+ @property
96
+ def kwds(self) -> dict[str, Any]: ...
97
+
98
+ def prepare(name: str, bases: tuple, attrs: PrivateAttrDict, /, **kwds) -> _PrivateTemp:
99
+ """
100
+ function for custom metaclass to create private attributes class.
101
+
102
+ def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
103
+ the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
104
+
105
+ def postprocess(type: type, tmp: tempobject) -> None:
106
+ the function to postprocess for creating private attributes class. The custom metaclass can call this
107
+
108
+ def register_metaclass(metaclass: type) -> None:
109
+ the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
110
+ otherwise the private attributes class created by this custom metaclass will not work.
111
+
112
+ All usage of this module should be like:
113
+ ```
114
+ from abc import ABCMeta
115
+ import private_attribute
116
+
117
+ class PrivateAbcMeta(ABCMeta):
118
+ def __new__(cls, *args, **kwargs):
119
+ temp = private_attribute.prepare(*args, **kwargs)
120
+ typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
121
+ private_attribute.postprocess(typ, temp)
122
+ return typ
123
+
124
+ private_attribute.register_metaclass(PrivateAbcMeta)
125
+ ```
126
+ """
127
+ ...
128
+
129
+ def postprocess(typ: type, temp: _PrivateTemp, /) -> None:
130
+ """
131
+ function for custom metaclass to create private attributes class.
132
+
133
+ def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
134
+ the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
135
+
136
+ def postprocess(type: type, tmp: tempobject) -> None:
137
+ the function to postprocess for creating private attributes class. The custom metaclass can call this
138
+
139
+ def register_metaclass(metaclass: type) -> None:
140
+ the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
141
+ otherwise the private attributes class created by this custom metaclass will not work.
142
+
143
+ All usage of this module should be like:
144
+ ```
145
+ from abc import ABCMeta
146
+ import private_attribute
147
+
148
+ class PrivateAbcMeta(ABCMeta):
149
+ def __new__(cls, *args, **kwargs):
150
+ temp = private_attribute.prepare(*args, **kwargs)
151
+ typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
152
+ private_attribute.postprocess(typ, temp)
153
+ return typ
154
+
155
+ private_attribute.register_metaclass(PrivateAbcMeta)
156
+ ```
157
+ """
158
+ ...
159
+ def register_metaclass(typ: type, /) -> None:
160
+ """
161
+ function for custom metaclass to create private attributes class.
162
+
163
+ def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
164
+ the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
165
+
166
+ def postprocess(type: type, tmp: tempobject) -> None:
167
+ the function to postprocess for creating private attributes class. The custom metaclass can call this
168
+
169
+ def register_metaclass(metaclass: type) -> None:
170
+ the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
171
+ otherwise the private attributes class created by this custom metaclass will not work.
172
+
173
+ All usage of this module should be like:
174
+ ```
175
+ from abc import ABCMeta
176
+ import private_attribute
177
+
178
+ class PrivateAbcMeta(ABCMeta):
179
+ def __new__(cls, *args, **kwargs):
180
+ temp = private_attribute.prepare(*args, **kwargs)
181
+ typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
182
+ private_attribute.postprocess(typ, temp)
183
+ return typ
184
+
185
+ private_attribute.register_metaclass(PrivateAbcMeta)
186
+ ```
187
+ """
188
+ ...
189
+ def ensure_type(typ: type, /) -> None:
190
+ """function for custom metaclass to ensure the type is a private attributes class.
191
+ def ensure_type(type: type) -> None:
192
+ the function to ensure the type is a private attributes class `tp_getattro`, `tp_setattro` and `tp_finalizer`.
193
+ """
194
+ ...
195
+ def ensure_metaclass(typ: type, /) -> None:
196
+ """function for custom metaclass to ensure the metaclass is working.
197
+ def ensure_metaclass(metaclass: type) -> None:
198
+ the function to ensure the metaclass `tp_getattro`, `tp_setattro` and `tp_finalizer`.
199
+ """
200
+ ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: private_attribute_cpp
3
- Version: 1.3.4
3
+ Version: 1.3.6
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
@@ -19,7 +19,7 @@ readme = open('README.md').read()
19
19
 
20
20
  setup(
21
21
  name='private_attribute_cpp',
22
- version='1.3.4',
22
+ version='1.3.6',
23
23
  author="HuangHaoHua",
24
24
  author_email="13140752715@example.com",
25
25
  description='A Python package that provides a way to define private attributes in C++ implementation.',
@@ -1,50 +0,0 @@
1
- from typing import Any, TypeVar, Callable, TypedDict, Sequence, Generic
2
- from types import FunctionType
3
-
4
- # define the dict that must have a key "__private_attrs__" and value must be the sequence of strings
5
- class PrivateAttrDict(TypedDict):
6
- __private_attrs__: Sequence[str]
7
-
8
- T = TypeVar('T')
9
-
10
- class _PrivateWrap(Generic[T]):
11
- @property
12
- def result(self) -> T: ...
13
-
14
- @property
15
- def funcs(self) -> tuple[FunctionType]: ...
16
-
17
- def __getattr__(self, name: str) -> Any:
18
- return getattr(self.result, name)
19
-
20
- TVar = TypeVar('TVar')
21
-
22
- class PrivateWrapProxy:
23
- def __init__(self, decorator: Callable[[TVar], T], orig: _PrivateWrap[Any]|None = None, /) -> None: ...
24
- def __call__(self, func: TVar, /) -> _PrivateWrap[T]: ...
25
-
26
- class PrivateAttrType(type):
27
- def __new__(cls, name: str, bases: tuple,
28
- attrs: PrivateAttrDict, /,
29
- private_func: Callable[[int, str], str]|None = None) -> PrivateAttrType: ...
30
-
31
- class PrivateAttrBase(metaclass=PrivateAttrType):
32
- __slots__ = ()
33
- __private_attrs__ = ()
34
-
35
-
36
- class _PrivateTemp:
37
- @property
38
- def name(self) -> str: ...
39
- @property
40
- def bases(self) -> tuple[type]: ...
41
- @property
42
- def attrs(self) -> dict[str, Any]: ...
43
- @property
44
- def kwds(self) -> dict[str, Any]: ...
45
-
46
- def prepare(name: str, bases: tuple, attrs: PrivateAttrDict, /, **kwds) -> _PrivateTemp: ...
47
- def postprocess(typ: type, temp: _PrivateTemp, /) -> None: ...
48
- def register_metaclass(typ: type, /) -> None: ...
49
- def ensure_type(typ: type, /) -> None: ...
50
- def ensure_metaclass(typ: type, /) -> None: ...