private-attribute-cpp 3.0.3__tar.gz → 3.0.4__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.
- private_attribute_cpp-3.0.3/README.md → private_attribute_cpp-3.0.4/PKG-INFO +10 -11
- private_attribute_cpp-3.0.3/PKG-INFO → private_attribute_cpp-3.0.4/README.md +0 -21
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/private_attribute.cpp +109 -383
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/private_attribute.pyi +1 -24
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/pyproject.toml +1 -1
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/CMakeLists.txt +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/LICENSE +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/picosha2.h +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_attr_classify_owner.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_class_private_attrs.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_gc_collect.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_init_subclass.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_abc.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_attrs.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_func.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_inheritance.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_method.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_super.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_wrap.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_slots_cycle.py +0 -0
- {private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_use_parents.py +0 -0
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: private_attribute_cpp
|
|
3
|
+
Version: 3.0.4
|
|
4
|
+
Summary: A Python package that provides a way to define private attributes in C++ implementation.
|
|
5
|
+
Author-Email: HuangHaoHua <13140752715@example.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Locked-chess-official/private_attribute_cpp
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
1
11
|
# private_attribute_cpp
|
|
2
12
|
|
|
3
13
|
[](https://pypi.org/project/private-attribute-cpp/)
|
|
@@ -388,10 +398,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
388
398
|
__setattr__ = private_attribute.create_slot("__setattr__")()
|
|
389
399
|
```
|
|
390
400
|
|
|
391
|
-
> The old `prepare` / `postprocess` pair and `register_metaclass` are
|
|
392
|
-
> deprecated (they emit a warning) and kept only for backward compatibility;
|
|
393
|
-
> new code should use `create_new`.
|
|
394
|
-
|
|
395
401
|
---
|
|
396
402
|
|
|
397
403
|
## Behavioral notes & limitations
|
|
@@ -418,11 +424,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
418
424
|
- **v2.2.0** — a subclass must not define, in its own class body, a name that is
|
|
419
425
|
private to a parent, unless it declares the name in its own
|
|
420
426
|
`__private_attrs__` or opts in with `use_parents=True`.
|
|
421
|
-
- **v2.2.1** — `prepare` locks private-attribute-type creation so only one type
|
|
422
|
-
is being built (`type.__new__`) at a time; `postprocess` unlocks it. The lock
|
|
423
|
-
is released automatically on failure. (Deprecated; see `create_new`.)
|
|
424
|
-
- **v2.2.3** — prefer the `with` form of `prepare`/`postprocess` to guarantee
|
|
425
|
-
the lock is released. (Deprecated; see `create_new`.)
|
|
426
427
|
- CPython may reset the special-method slot descriptors (e.g. when you assign
|
|
427
428
|
`__getattribute__` / `__setattr__`). Use `ensure_type` (or
|
|
428
429
|
`ensure_metaclass`) to re-inject them, and avoid setting those methods on
|
|
@@ -435,8 +436,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
435
436
|
id records attribute names already confirmed non-private, so they go
|
|
436
437
|
straight to the user callback without re-classification. Registered types and
|
|
437
438
|
created types must be heap types (static types are rejected).
|
|
438
|
-
- `register_metaclass` requires a metaclass that supports weak references.
|
|
439
|
-
(Deprecated; use `create_new`, which registers the metaclass automatically.)
|
|
440
439
|
- Do **not** set `__static_attributes__` on a private-attribute class; it is
|
|
441
440
|
removed automatically.
|
|
442
441
|
|
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: private_attribute_cpp
|
|
3
|
-
Version: 3.0.3
|
|
4
|
-
Summary: A Python package that provides a way to define private attributes in C++ implementation.
|
|
5
|
-
Author-Email: HuangHaoHua <13140752715@example.com>
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/Locked-chess-official/private_attribute_cpp
|
|
8
|
-
Requires-Python: >=3.10
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
|
|
11
1
|
# private_attribute_cpp
|
|
12
2
|
|
|
13
3
|
[](https://pypi.org/project/private-attribute-cpp/)
|
|
@@ -398,10 +388,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
398
388
|
__setattr__ = private_attribute.create_slot("__setattr__")()
|
|
399
389
|
```
|
|
400
390
|
|
|
401
|
-
> The old `prepare` / `postprocess` pair and `register_metaclass` are
|
|
402
|
-
> deprecated (they emit a warning) and kept only for backward compatibility;
|
|
403
|
-
> new code should use `create_new`.
|
|
404
|
-
|
|
405
391
|
---
|
|
406
392
|
|
|
407
393
|
## Behavioral notes & limitations
|
|
@@ -428,11 +414,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
428
414
|
- **v2.2.0** — a subclass must not define, in its own class body, a name that is
|
|
429
415
|
private to a parent, unless it declares the name in its own
|
|
430
416
|
`__private_attrs__` or opts in with `use_parents=True`.
|
|
431
|
-
- **v2.2.1** — `prepare` locks private-attribute-type creation so only one type
|
|
432
|
-
is being built (`type.__new__`) at a time; `postprocess` unlocks it. The lock
|
|
433
|
-
is released automatically on failure. (Deprecated; see `create_new`.)
|
|
434
|
-
- **v2.2.3** — prefer the `with` form of `prepare`/`postprocess` to guarantee
|
|
435
|
-
the lock is released. (Deprecated; see `create_new`.)
|
|
436
417
|
- CPython may reset the special-method slot descriptors (e.g. when you assign
|
|
437
418
|
`__getattribute__` / `__setattr__`). Use `ensure_type` (or
|
|
438
419
|
`ensure_metaclass`) to re-inject them, and avoid setting those methods on
|
|
@@ -445,8 +426,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
445
426
|
id records attribute names already confirmed non-private, so they go
|
|
446
427
|
straight to the user callback without re-classification. Registered types and
|
|
447
428
|
created types must be heap types (static types are rejected).
|
|
448
|
-
- `register_metaclass` requires a metaclass that supports weak references.
|
|
449
|
-
(Deprecated; use `create_new`, which registers the metaclass automatically.)
|
|
450
429
|
- Do **not** set `__static_attributes__` on a private-attribute class; it is
|
|
451
430
|
removed automatically.
|
|
452
431
|
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
#include "picosha2.h"
|
|
19
19
|
#include <functional>
|
|
20
20
|
#include <memory>
|
|
21
|
-
#include <new>
|
|
22
21
|
|
|
23
22
|
// python >= 3.15 has PyAnyDict_Check. Under 3.15 defined it as PyDict_Check
|
|
24
23
|
#if PY_VERSION_HEX < 0x030F0000
|
|
@@ -218,7 +217,6 @@ static PrivateAttrCreationData* get_creation_data(uintptr_t typ_id) noexcept;
|
|
|
218
217
|
// get_creation_data() returning NULL (no pending install) and the type being
|
|
219
218
|
// absent from all_type_attr_set, a non-zero count identifies a half-installed
|
|
220
219
|
// type whose install() never completed.
|
|
221
|
-
static int count_type_slot_objects(PyTypeObject* tp) noexcept;
|
|
222
220
|
static std::string default_random_string(uintptr_t obj_id, const std::string& attr_name) noexcept;
|
|
223
221
|
|
|
224
222
|
struct RestorePythonExceptionResult {
|
|
@@ -412,8 +410,10 @@ struct PrivateAttrCreationData
|
|
|
412
410
|
bool use_parents = false;
|
|
413
411
|
bool cleared = false;
|
|
414
412
|
bool installed = false;
|
|
413
|
+
std::mutex clear_lock, install_lock;
|
|
415
414
|
|
|
416
415
|
void clear() noexcept {
|
|
416
|
+
std::unique_lock<std::mutex> lock(clear_lock);
|
|
417
417
|
if (cleared) {
|
|
418
418
|
return;
|
|
419
419
|
}
|
|
@@ -436,6 +436,7 @@ struct PrivateAttrCreationData
|
|
|
436
436
|
}
|
|
437
437
|
|
|
438
438
|
bool install(PyObject* new_type) noexcept {
|
|
439
|
+
std::unique_lock<std::mutex> lock(install_lock);
|
|
439
440
|
if (installed) return true;
|
|
440
441
|
if (!new_type) {
|
|
441
442
|
return false;
|
|
@@ -468,7 +469,6 @@ struct PrivateAttrCreationData
|
|
|
468
469
|
"%s (%d): '__static_attributes__' will expose private attribute names and cannot be used in private attribute types", __FILE__, __LINE__);
|
|
469
470
|
return false;
|
|
470
471
|
}
|
|
471
|
-
::AllData::all_type_attr_set[type_id] = private_attrs_set;
|
|
472
472
|
ensure_tp(type_instance);
|
|
473
473
|
if (::AllData::type_attr_dict.find(type_id) == ::AllData::type_attr_dict.end()) {
|
|
474
474
|
::AllData::type_attr_dict[type_id] = {};
|
|
@@ -508,7 +508,7 @@ struct PrivateAttrCreationData
|
|
|
508
508
|
}
|
|
509
509
|
// Do not wipe a parent's shadow already written for this class during
|
|
510
510
|
// __init_subclass__ (e.g. `cls._value = ...` inside the parent's
|
|
511
|
-
// __init_subclass__ runs inside tp_new, before
|
|
511
|
+
// __init_subclass__ runs inside tp_new, before install).
|
|
512
512
|
if (::AllData::all_type_subclass_attr[i].find(type_id) == ::AllData::all_type_subclass_attr[i].end()) {
|
|
513
513
|
::AllData::all_type_subclass_attr[i][type_id] = {};
|
|
514
514
|
}
|
|
@@ -533,6 +533,7 @@ struct PrivateAttrCreationData
|
|
|
533
533
|
if (private_func) {
|
|
534
534
|
::AllData::type_need_call[type_id] = private_func;
|
|
535
535
|
}
|
|
536
|
+
::AllData::all_type_attr_set[type_id] = private_attrs_set;
|
|
536
537
|
installed = true;
|
|
537
538
|
return true;
|
|
538
539
|
}
|
|
@@ -547,7 +548,7 @@ static bool
|
|
|
547
548
|
try_install_pending_creation(PyObject* new_type) noexcept
|
|
548
549
|
{
|
|
549
550
|
// Finish a pending private-attribute creation if one is still
|
|
550
|
-
// outstanding.
|
|
551
|
+
// outstanding. PrivateAttrType_new leaves the five slot
|
|
551
552
|
// objects carrying a pointer to the in-flight PrivateAttrCreationData
|
|
552
553
|
// until install() runs; if creation is interrupted (e.g. an exception in
|
|
553
554
|
// __init_subclass__) the type exists but is not yet registered in
|
|
@@ -561,16 +562,7 @@ try_install_pending_creation(PyObject* new_type) noexcept
|
|
|
561
562
|
}
|
|
562
563
|
PrivateAttrCreationData* data = get_creation_data((uintptr_t)new_type);
|
|
563
564
|
if (!data) {
|
|
564
|
-
|
|
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.
|
|
565
|
+
if (::AllData::all_type_attr_set.find((uintptr_t)new_type) == ::AllData::all_type_attr_set.end()) {
|
|
574
566
|
PyErr_Format(PyExc_RuntimeError,
|
|
575
567
|
"private-attribute type '%s' is not fully installed and cannot be used",
|
|
576
568
|
((PyTypeObject*)new_type)->tp_name);
|
|
@@ -593,16 +585,14 @@ is_uninstallable_private_type(PyObject* base) noexcept
|
|
|
593
585
|
if (!base || !PyType_Check(base)) {
|
|
594
586
|
return false;
|
|
595
587
|
}
|
|
596
|
-
|
|
597
|
-
if (::AllData::all_type_attr_set.find((uintptr_t)base) != ::AllData::all_type_attr_set.end()) {
|
|
588
|
+
if (get_creation_data((uintptr_t)base)) {
|
|
598
589
|
return false;
|
|
599
590
|
}
|
|
600
|
-
//
|
|
601
|
-
if (
|
|
591
|
+
// Already truly installed -> fine.
|
|
592
|
+
if (::AllData::all_type_attr_set.find((uintptr_t)base) != ::AllData::all_type_attr_set.end()) {
|
|
602
593
|
return false;
|
|
603
594
|
}
|
|
604
|
-
|
|
605
|
-
return count_type_slot_objects((PyTypeObject*)base) > 0;
|
|
595
|
+
return true;
|
|
606
596
|
}
|
|
607
597
|
|
|
608
598
|
static inline void
|
|
@@ -2551,7 +2541,7 @@ PrivateSuper_New(PyTypeObject* type, PyObject* args, PyObject* kwds) noexcept
|
|
|
2551
2541
|
// `type` fields are populated via __set_name__.
|
|
2552
2542
|
// - PrivateAttrMetaSlotType : for *registered metaclasses*. The `name`,
|
|
2553
2543
|
// `type` and `callback` fields are passed in
|
|
2554
|
-
// explicitly during
|
|
2544
|
+
// explicitly during metaclass registration.
|
|
2555
2545
|
typedef struct PrivateAttrSlot {
|
|
2556
2546
|
PyObject_HEAD
|
|
2557
2547
|
PyObject* slot_name; // special-method name ("__getattribute__", ...)
|
|
@@ -2824,6 +2814,9 @@ static PyObject* slot_setattr_impl(PrivateAttrSlot* self, PyObject* obj, PyObjec
|
|
|
2824
2814
|
static PyObject* slot_delattr_impl(PrivateAttrSlot* self, PyObject* obj, PyObject* name) noexcept;
|
|
2825
2815
|
static PyObject* slot_del_impl(PrivateAttrSlot* self, PyObject* obj) noexcept;
|
|
2826
2816
|
static PyObject* slot_call_impl(PrivateAttrSlot* self, PyObject* args, PyObject* kwds) noexcept;
|
|
2817
|
+
// true iff `obj` is a PrivateAttrMetaNewSlotType descriptor (i.e. a create_new
|
|
2818
|
+
// slot); defined after PrivateAttrMetaNewSlotType.
|
|
2819
|
+
static bool is_meta_new_slot(PyObject* obj) noexcept;
|
|
2827
2820
|
|
|
2828
2821
|
// Resolve the type id for an access target. For a metaclass slot the target
|
|
2829
2822
|
// (obj) IS the class object; for a type slot the target is an instance and its
|
|
@@ -2884,6 +2877,47 @@ slot_new_impl(PrivateAttrSlot* self, PyObject* cls, PyObject* args, PyObject* kw
|
|
|
2884
2877
|
PyErr_SetString(PyExc_TypeError, "__new__ must be called with a type");
|
|
2885
2878
|
return NULL;
|
|
2886
2879
|
}
|
|
2880
|
+
// A create_new slot must be bound to a metaclass via __set_name__ before it
|
|
2881
|
+
// can be invoked. An unbound slot (owner_type == NULL) has no parent type to
|
|
2882
|
+
// delegate to, so running the class-creation machinery would be unsafe.
|
|
2883
|
+
if (self->owner_type == NULL) {
|
|
2884
|
+
PyErr_SetString(PyExc_TypeError,
|
|
2885
|
+
"create_new slot is not bound to a metaclass; assign it in a metaclass body (e.g. __new__ = private_attribute.create_new())");
|
|
2886
|
+
return NULL;
|
|
2887
|
+
}
|
|
2888
|
+
// Resolve the parent metaclass's __new__ exactly once and hold a strong
|
|
2889
|
+
// reference (free-threading safe: a concurrent reassignment of the parent's
|
|
2890
|
+
// __new__ cannot change what we dispatch on). The metaclass MRO always ends
|
|
2891
|
+
// at `type`, whose __new__ always exists, so a missing __new__ here is a
|
|
2892
|
+
// genuine failure — propagate the error rather than guessing.
|
|
2893
|
+
PyObjectStorage super_obj = PyObjectStorage::from_new_ref(
|
|
2894
|
+
PyObject_CallFunction((PyObject*)&PySuper_Type, "OO", self->owner_type, self->owner_type));
|
|
2895
|
+
if (!super_obj) return NULL;
|
|
2896
|
+
PyObjectStorage parent_new = PyObjectStorage::from_new_ref(
|
|
2897
|
+
PyObject_GetAttrString(super_obj, "__new__"));
|
|
2898
|
+
if (!parent_new) return NULL;
|
|
2899
|
+
|
|
2900
|
+
// If the parent metaclass's __new__ is itself a create_new slot, do NOT
|
|
2901
|
+
// preprocess here — the parent slot performs the single preprocess +
|
|
2902
|
+
// install. Instead:
|
|
2903
|
+
// - if this slot has a callback, call it directly with the raw
|
|
2904
|
+
// (cls, name, bases, attrs) + kwds; the callback's own super().__new__
|
|
2905
|
+
// resolves to the parent slot, which preprocesses and installs exactly
|
|
2906
|
+
// once.
|
|
2907
|
+
// - otherwise forward the whole class-creation flow to the parent slot
|
|
2908
|
+
// (the same `parent_new` object we already resolved).
|
|
2909
|
+
if (is_meta_new_slot(parent_new)) {
|
|
2910
|
+
if (self->callback) {
|
|
2911
|
+
PyObjectStorage type_args = PyObjectStorage::from_new_ref(PyTuple_Pack(
|
|
2912
|
+
4, cls,
|
|
2913
|
+
PyTuple_GET_ITEM(args, 0),
|
|
2914
|
+
PyTuple_GET_ITEM(args, 1),
|
|
2915
|
+
PyTuple_GET_ITEM(args, 2)));
|
|
2916
|
+
if (!type_args) return NULL;
|
|
2917
|
+
return PyObject_Call(self->callback, type_args, kwds);
|
|
2918
|
+
}
|
|
2919
|
+
return slot_new_impl((PrivateAttrSlot*)parent_new.get(), cls, args, kwds);
|
|
2920
|
+
}
|
|
2887
2921
|
PrivateAttrCreationData data;
|
|
2888
2922
|
PyTypeObject* typ = (PyTypeObject*)cls;
|
|
2889
2923
|
const char* type_name = get_name_from_tp_name(typ);
|
|
@@ -3445,6 +3479,19 @@ static PyTypeObject PrivateAttrMetaNewSlotType = {
|
|
|
3445
3479
|
PrivateAttrSlot_getsetters, // tp_getset
|
|
3446
3480
|
};
|
|
3447
3481
|
|
|
3482
|
+
// true iff `obj` is a PrivateAttrMetaNewSlotType descriptor (a create_new
|
|
3483
|
+
// slot). Pure type check — the caller resolves the object once and holds a
|
|
3484
|
+
// strong reference, so there is no re-resolution (and hence no TOCTOU window)
|
|
3485
|
+
// here.
|
|
3486
|
+
static bool
|
|
3487
|
+
is_meta_new_slot(PyObject* obj) noexcept
|
|
3488
|
+
{
|
|
3489
|
+
if (!obj) {
|
|
3490
|
+
return false;
|
|
3491
|
+
}
|
|
3492
|
+
return PyObject_IsInstance(obj, (PyObject*)&PrivateAttrMetaNewSlotType) == 1;
|
|
3493
|
+
}
|
|
3494
|
+
|
|
3448
3495
|
// v4: clear the pending-creation back-pointer on `count` slot objects. If
|
|
3449
3496
|
// `expected` is non-NULL, only slots whose data equals `expected` are cleared.
|
|
3450
3497
|
static void
|
|
@@ -3705,34 +3752,6 @@ clear_creation_data_from_slots(PyTypeObject* tp, PrivateAttrCreationData* expect
|
|
|
3705
3752
|
return found;
|
|
3706
3753
|
}
|
|
3707
3754
|
|
|
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
|
-
|
|
3736
3755
|
// Install the special-method structs into a registered metaclass's dict.
|
|
3737
3756
|
// The owner is passed explicitly. If the metaclass already defines one of the
|
|
3738
3757
|
// names, its value becomes the callback. Must use the ORIGINAL type_setattro
|
|
@@ -3777,27 +3796,40 @@ install_metaclass_slots(PyTypeObject* metaclass) noexcept
|
|
|
3777
3796
|
const char* new_name = "__new__";
|
|
3778
3797
|
PyObject* existing = PyDict_GetItemString(dict, new_name);
|
|
3779
3798
|
if (existing) {
|
|
3780
|
-
|
|
3799
|
+
// An already-installed slot of ours is left untouched.
|
|
3800
|
+
if (PyObject_IsInstance(existing, (PyObject*)&PrivateAttrMetaNewSlotType) && (
|
|
3781
3801
|
((PrivateAttrSlot*)existing)->owner_type == NULL || ((PrivateAttrSlot*)existing)->owner_type == (PyObject*)metaclass
|
|
3782
|
-
))
|
|
3783
|
-
|
|
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;
|
|
3802
|
+
)) {
|
|
3803
|
+
return true;
|
|
3795
3804
|
}
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3805
|
+
}
|
|
3806
|
+
// Wrap the original __new__ (if any) directly as the callback of the
|
|
3807
|
+
// created slot, exactly like the six-slot loop above: the slot then
|
|
3808
|
+
// dispatches class creation through the original function instead of
|
|
3809
|
+
// dropping it.
|
|
3810
|
+
PyObject* callback = NULL;
|
|
3811
|
+
if (existing) {
|
|
3812
|
+
callback = existing;
|
|
3813
|
+
while (callback && (
|
|
3814
|
+
PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaSlotType) ||
|
|
3815
|
+
PyObject_IsInstance(callback, (PyObject*)&PrivateAttrMetaNewSlotType) ||
|
|
3816
|
+
PyObject_IsInstance(callback, (PyObject*)&PrivateAttrSlotType))) {
|
|
3817
|
+
callback = ((PrivateAttrSlot*)callback) -> callback;
|
|
3799
3818
|
}
|
|
3800
3819
|
}
|
|
3820
|
+
PrivateAttrSlot* new_slot = create_type_new_slot((PyObject*)metaclass, callback);
|
|
3821
|
+
PyObjectStorage new_slot_storage = PyObjectStorage::from_new_ref((PyObject*)new_slot);
|
|
3822
|
+
if (!new_slot_storage) {
|
|
3823
|
+
return false;
|
|
3824
|
+
}
|
|
3825
|
+
PyObjectStorage name_obj = PyObjectStorage::from_new_ref(PyUnicode_FromString(new_name));
|
|
3826
|
+
if (!name_obj) {
|
|
3827
|
+
return false;
|
|
3828
|
+
}
|
|
3829
|
+
int r = real_setattro((PyObject*)metaclass, name_obj, new_slot_storage);
|
|
3830
|
+
if (r < 0) {
|
|
3831
|
+
return false;
|
|
3832
|
+
}
|
|
3801
3833
|
return true;
|
|
3802
3834
|
}
|
|
3803
3835
|
|
|
@@ -5546,7 +5578,7 @@ new_type_tp_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
|
|
|
5546
5578
|
}
|
|
5547
5579
|
// Only rewrite into the callback field for a *user* value. If the
|
|
5548
5580
|
// value is already one of our slot structs (the initial install
|
|
5549
|
-
// done inside type.__new__ /
|
|
5581
|
+
// done inside type.__new__ / metaclass registration), let the normal
|
|
5550
5582
|
// type_setattro path install it so update_one_slot wires the slots.
|
|
5551
5583
|
if (slot_names) {
|
|
5552
5584
|
for (int i = 0; slot_names[i]; i++) {
|
|
@@ -5811,7 +5843,7 @@ PrivateAttrType_finalize(PyObject* cls) noexcept
|
|
|
5811
5843
|
}
|
|
5812
5844
|
::AllData::all_type_mutex.erase(typ_id);
|
|
5813
5845
|
// Clear the per-type buckets of the object-attr tables (created in
|
|
5814
|
-
//
|
|
5846
|
+
// PrivateAttr_object_init_private_dict);
|
|
5815
5847
|
// otherwise every class that ever got instances leaves an empty bucket
|
|
5816
5848
|
// behind and all_object_attr / all_object_mutex grow forever.
|
|
5817
5849
|
if (::AllData::all_object_attr.find(typ_id) != ::AllData::all_object_attr.end()) {
|
|
@@ -5879,211 +5911,6 @@ create_private_attr_base_simple(void) noexcept
|
|
|
5879
5911
|
return base_type;
|
|
5880
5912
|
}
|
|
5881
5913
|
|
|
5882
|
-
typedef struct {
|
|
5883
|
-
PyObject_HEAD
|
|
5884
|
-
PrivateAttrCreationData* tmp;
|
|
5885
|
-
} PrivateTempObject;
|
|
5886
|
-
|
|
5887
|
-
static PyObject*
|
|
5888
|
-
PrivateTempObject_name(PyObject* self, void* /*closure*/) noexcept
|
|
5889
|
-
{
|
|
5890
|
-
PyObject* name = ((PrivateTempObject*)self)->tmp->name;
|
|
5891
|
-
if (!name) {
|
|
5892
|
-
PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
|
|
5893
|
-
return nullptr;
|
|
5894
|
-
}
|
|
5895
|
-
Py_INCREF(name);
|
|
5896
|
-
return name;
|
|
5897
|
-
}
|
|
5898
|
-
|
|
5899
|
-
static PyObject*
|
|
5900
|
-
PrivateTempObject_base(PyObject* self, void* /*closure*/) noexcept
|
|
5901
|
-
{
|
|
5902
|
-
PyObject* base = ((PrivateTempObject*)self)->tmp->bases;
|
|
5903
|
-
if (!base) {
|
|
5904
|
-
PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
|
|
5905
|
-
return nullptr;
|
|
5906
|
-
}
|
|
5907
|
-
Py_INCREF(base);
|
|
5908
|
-
return base;
|
|
5909
|
-
}
|
|
5910
|
-
|
|
5911
|
-
static PyObject*
|
|
5912
|
-
PrivateTempObject_attrs(PyObject* self, void* /*closure*/) noexcept
|
|
5913
|
-
{
|
|
5914
|
-
PyObject* attrs = ((PrivateTempObject*)self)->tmp->attrs_copy;
|
|
5915
|
-
if (!attrs) {
|
|
5916
|
-
PyErr_SetString(PyExc_RuntimeError, "object not init or have been used");
|
|
5917
|
-
return nullptr;
|
|
5918
|
-
}
|
|
5919
|
-
Py_INCREF(attrs);
|
|
5920
|
-
return attrs;
|
|
5921
|
-
}
|
|
5922
|
-
|
|
5923
|
-
static PyObject*
|
|
5924
|
-
PrivateTempObject_kwds(PyObject* self, void* /*closure*/) noexcept
|
|
5925
|
-
{
|
|
5926
|
-
PyObject* kwds = ((PrivateTempObject*)self)->tmp->base_kwds;
|
|
5927
|
-
if (!kwds) {
|
|
5928
|
-
return PyDict_New();
|
|
5929
|
-
}
|
|
5930
|
-
Py_INCREF(kwds);
|
|
5931
|
-
return kwds;
|
|
5932
|
-
}
|
|
5933
|
-
|
|
5934
|
-
static PyGetSetDef PrivateTempObject_getsets[] = {
|
|
5935
|
-
{"name", (getter)PrivateTempObject_name, NULL, "The name for submetaclass.__new__ argument 1", NULL},
|
|
5936
|
-
{"bases", (getter)PrivateTempObject_base, NULL, "The base classes for submetaclass.__new__ argument 2", NULL},
|
|
5937
|
-
{"attrs", (getter)PrivateTempObject_attrs, NULL, "The attributes for submetaclass.__new__ argument 3", NULL},
|
|
5938
|
-
{"kwds", (getter)PrivateTempObject_kwds, NULL, "The keyword arguments for submetaclass.__new__", NULL},
|
|
5939
|
-
{NULL}
|
|
5940
|
-
};
|
|
5941
|
-
|
|
5942
|
-
static void
|
|
5943
|
-
PrivateTempObject_dealloc(PyObject* self) noexcept
|
|
5944
|
-
{
|
|
5945
|
-
((PrivateTempObject*)self)->tmp->clear();
|
|
5946
|
-
delete ((PrivateTempObject*)self)->tmp;
|
|
5947
|
-
Py_TYPE(self)->tp_free(self);
|
|
5948
|
-
}
|
|
5949
|
-
|
|
5950
|
-
static PyObject*
|
|
5951
|
-
PrivateTempObject_postprocess(PyObject* self, PyObject* type) noexcept
|
|
5952
|
-
{
|
|
5953
|
-
if (!PyType_Check(type)) {
|
|
5954
|
-
PyErr_SetString(PyExc_TypeError, "type must be a type");
|
|
5955
|
-
return NULL;
|
|
5956
|
-
}
|
|
5957
|
-
if ((((PrivateTempObject*)self)->tmp->cleared)) {
|
|
5958
|
-
PyErr_SetString(PyExc_RuntimeError, "temp not init or have been used");
|
|
5959
|
-
return NULL;
|
|
5960
|
-
}
|
|
5961
|
-
if (!(((PrivateTempObject*)self)->tmp->install(type))) {
|
|
5962
|
-
return NULL;
|
|
5963
|
-
}
|
|
5964
|
-
((PrivateTempObject*)self)->tmp->clear();
|
|
5965
|
-
Py_RETURN_NONE;
|
|
5966
|
-
}
|
|
5967
|
-
|
|
5968
|
-
static PyObject*
|
|
5969
|
-
PrivateTempObject_enter(PyObject* self, PyObject* /*args*/) noexcept
|
|
5970
|
-
{
|
|
5971
|
-
Py_INCREF(self);
|
|
5972
|
-
return self;
|
|
5973
|
-
}
|
|
5974
|
-
|
|
5975
|
-
|
|
5976
|
-
static PyObject*
|
|
5977
|
-
PrivateTempObject_exit(PyObject* self, PyObject* /*args*/) noexcept
|
|
5978
|
-
{
|
|
5979
|
-
PrivateTempObject* obj = (PrivateTempObject*)self;
|
|
5980
|
-
if (!obj->tmp->cleared) {
|
|
5981
|
-
obj->tmp->clear();
|
|
5982
|
-
}
|
|
5983
|
-
Py_RETURN_NONE;
|
|
5984
|
-
}
|
|
5985
|
-
|
|
5986
|
-
static PyMethodDef PrivateTempObject_methods[] = {
|
|
5987
|
-
{"postprocess", (PyCFunction)PrivateTempObject_postprocess, METH_O, "postprocess the temp object"},
|
|
5988
|
-
{"__enter__", (PyCFunction)PrivateTempObject_enter, METH_NOARGS, NULL},
|
|
5989
|
-
{"__exit__", (PyCFunction)PrivateTempObject_exit, METH_VARARGS, NULL},
|
|
5990
|
-
{NULL}
|
|
5991
|
-
};
|
|
5992
|
-
|
|
5993
|
-
static int
|
|
5994
|
-
PrivateTempObject_traverse(PyObject* self, visitproc visit, void* arg) noexcept
|
|
5995
|
-
{
|
|
5996
|
-
PrivateTempObject* tmp = (PrivateTempObject*)self;
|
|
5997
|
-
if (tmp->tmp) {
|
|
5998
|
-
PrivateAttrCreationData* data = tmp->tmp;
|
|
5999
|
-
if (!data->cleared) {
|
|
6000
|
-
Py_VISIT(data->attrs_copy);
|
|
6001
|
-
Py_VISIT(data->base_kwds);
|
|
6002
|
-
}
|
|
6003
|
-
}
|
|
6004
|
-
return 0;
|
|
6005
|
-
}
|
|
6006
|
-
|
|
6007
|
-
static int
|
|
6008
|
-
PrivateTempObject_clear(PyObject* self) noexcept
|
|
6009
|
-
{
|
|
6010
|
-
PrivateTempObject* tmp = (PrivateTempObject*)self;
|
|
6011
|
-
if (tmp->tmp && !tmp->tmp->cleared) {
|
|
6012
|
-
tmp->tmp->clear();
|
|
6013
|
-
}
|
|
6014
|
-
return 0;
|
|
6015
|
-
}
|
|
6016
|
-
|
|
6017
|
-
static PyTypeObject PrivateTempType = {
|
|
6018
|
-
PyVarObject_HEAD_INIT(NULL, 0)
|
|
6019
|
-
"private_attribute._PrivateTemp", // tp_name
|
|
6020
|
-
sizeof(PrivateTempObject), // tp_basicsize
|
|
6021
|
-
0, //tp_itemsize
|
|
6022
|
-
PrivateTempObject_dealloc, //tp_dealloc
|
|
6023
|
-
0, //tp_print
|
|
6024
|
-
0, //tp_getattr
|
|
6025
|
-
0, //tp_setattr
|
|
6026
|
-
0, //tp_compare
|
|
6027
|
-
0, //tp_repr
|
|
6028
|
-
0, //tp_as_number
|
|
6029
|
-
0, //tp_as_sequence
|
|
6030
|
-
0, //tp_as_mapping
|
|
6031
|
-
0, //tp_hash
|
|
6032
|
-
0, //tp_call
|
|
6033
|
-
0, //tp_str
|
|
6034
|
-
0, //tp_getattro
|
|
6035
|
-
0, //tp_setattro
|
|
6036
|
-
0, //tp_as_buffer
|
|
6037
|
-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, //tp_flags
|
|
6038
|
-
0, //tp_doc
|
|
6039
|
-
PrivateTempObject_traverse, //tp_traverse
|
|
6040
|
-
PrivateTempObject_clear, //tp_clear
|
|
6041
|
-
0, //tp_richcompare
|
|
6042
|
-
0, //tp_weaklistoffset
|
|
6043
|
-
0, //tp_iter
|
|
6044
|
-
0, //tp_iternext
|
|
6045
|
-
PrivateTempObject_methods, //tp_methods
|
|
6046
|
-
0, //tp_members
|
|
6047
|
-
PrivateTempObject_getsets //tp_getset
|
|
6048
|
-
};
|
|
6049
|
-
|
|
6050
|
-
static PyObject*
|
|
6051
|
-
prepare_for_PrivateAttr(PyObject* /*self*/, PyObject* args, PyObject* kwargs) noexcept
|
|
6052
|
-
{
|
|
6053
|
-
PrivateAttrCreationData* tmp_data = new(std::nothrow) PrivateAttrCreationData();
|
|
6054
|
-
if (!tmp_data) {
|
|
6055
|
-
return PyErr_NoMemory();
|
|
6056
|
-
}
|
|
6057
|
-
if (!PrivateAttrType_preprocess(args, kwargs, *tmp_data, "private_attribute.prepare")) {
|
|
6058
|
-
tmp_data->clear();
|
|
6059
|
-
delete tmp_data;
|
|
6060
|
-
return NULL;
|
|
6061
|
-
}
|
|
6062
|
-
PrivateTempObject* tmp = PyObject_GC_New(PrivateTempObject, &PrivateTempType);
|
|
6063
|
-
if (!tmp) {
|
|
6064
|
-
tmp_data->clear();
|
|
6065
|
-
delete tmp_data;
|
|
6066
|
-
return NULL;
|
|
6067
|
-
}
|
|
6068
|
-
tmp->tmp = tmp_data;
|
|
6069
|
-
PyObject_GC_Track(tmp);
|
|
6070
|
-
return (PyObject*)tmp;
|
|
6071
|
-
}
|
|
6072
|
-
|
|
6073
|
-
static PyObject*
|
|
6074
|
-
postprocess_for_PrivateAttr(PyObject* /*self*/, PyObject* args) noexcept
|
|
6075
|
-
{
|
|
6076
|
-
PyObject* type;
|
|
6077
|
-
PyObject* tmp;
|
|
6078
|
-
if (!PyArg_ParseTuple(args, "OO:private_attribute.postprocess", &type, &tmp)) {
|
|
6079
|
-
return NULL;
|
|
6080
|
-
}
|
|
6081
|
-
if (!PyObject_IsInstance(tmp, (PyObject*)&PrivateTempType)) {
|
|
6082
|
-
PyErr_SetString(PyExc_TypeError, "tmp must be a PrivateTempObject");
|
|
6083
|
-
return NULL;
|
|
6084
|
-
}
|
|
6085
|
-
return PrivateTempObject_postprocess(tmp, type);
|
|
6086
|
-
}
|
|
6087
5914
|
|
|
6088
5915
|
#define METACLASS_WEAKREF_CALLBACK_REMOVE_ITEM(name) do {\
|
|
6089
5916
|
if (::AllData::all_type_##name.find(id) != ::AllData::all_type_##name.end()) {\
|
|
@@ -6237,28 +6064,6 @@ register_metaclass_head(PyObject* metaclass) noexcept
|
|
|
6237
6064
|
Py_RETURN_NONE;
|
|
6238
6065
|
}
|
|
6239
6066
|
|
|
6240
|
-
static PyObject*
|
|
6241
|
-
register_metaclass(PyObject* /*self*/, PyObject* metaclass) noexcept
|
|
6242
|
-
{
|
|
6243
|
-
if (!register_metaclass_head(metaclass)) return NULL;
|
|
6244
|
-
PyObjectStorage subclasses = PyObjectStorage::from_new_ref(PyObject_CallMethod((PyObject*)&PyType_Type, "__subclasses__", "O", metaclass));
|
|
6245
|
-
if (!subclasses) {
|
|
6246
|
-
PyErr_Clear();
|
|
6247
|
-
Py_RETURN_NONE;
|
|
6248
|
-
}
|
|
6249
|
-
if (!PyList_Check(subclasses.get())) {
|
|
6250
|
-
Py_RETURN_NONE;
|
|
6251
|
-
}
|
|
6252
|
-
Py_ssize_t list_len = PyList_GET_SIZE(subclasses.get());
|
|
6253
|
-
for (Py_ssize_t i = 0; i < list_len; i++) {
|
|
6254
|
-
PyObject* subclass = PyList_GET_ITEM(subclasses.get(), i);
|
|
6255
|
-
if (!register_metaclass(0, subclass)) {
|
|
6256
|
-
return NULL;
|
|
6257
|
-
}
|
|
6258
|
-
}
|
|
6259
|
-
Py_RETURN_NONE;
|
|
6260
|
-
}
|
|
6261
|
-
|
|
6262
6067
|
static PyObject*
|
|
6263
6068
|
ensure_type_tp(PyObject* /*self*/, PyObject* type) noexcept
|
|
6264
6069
|
{
|
|
@@ -6375,15 +6180,12 @@ PrivateModule_dir(PyObject* self, PyObject* /*args*/) noexcept
|
|
|
6375
6180
|
{
|
|
6376
6181
|
PyObjectStorage parent_dir = PyObjectStorage::from_new_ref(PyObject_CallMethod((PyObject*)&PyModule_Type, "__dir__", "O", self));
|
|
6377
6182
|
if (!parent_dir) return NULL;
|
|
6378
|
-
PyObjectStorage attr_list = PyObjectStorage::from_new_ref(Py_BuildValue("[
|
|
6183
|
+
PyObjectStorage attr_list = PyObjectStorage::from_new_ref(Py_BuildValue("[ssssssssss]",
|
|
6379
6184
|
"PrivateWrapProxy",
|
|
6380
6185
|
"PrivateAttrType",
|
|
6381
6186
|
"PrivateAttrBase",
|
|
6382
6187
|
"PrivateSuper",
|
|
6383
6188
|
"PrivateClassSuper",
|
|
6384
|
-
"prepare",
|
|
6385
|
-
"postprocess",
|
|
6386
|
-
"register_metaclass",
|
|
6387
6189
|
"ensure_type",
|
|
6388
6190
|
"ensure_metaclass",
|
|
6389
6191
|
"create_new",
|
|
@@ -6420,9 +6222,6 @@ PrivateModule_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
|
|
|
6420
6222
|
"PrivateAttrBase",
|
|
6421
6223
|
"PrivateSuper",
|
|
6422
6224
|
"PrivateClassSuper",
|
|
6423
|
-
"prepare",
|
|
6424
|
-
"postprocess",
|
|
6425
|
-
"register_metaclass",
|
|
6426
6225
|
"ensure_type",
|
|
6427
6226
|
"ensure_metaclass",
|
|
6428
6227
|
"create_new",
|
|
@@ -6438,36 +6237,6 @@ PrivateModule_setattro(PyObject* self, PyObject* name, PyObject* value) noexcept
|
|
|
6438
6237
|
return PyModule_Type.tp_setattro(self, name, value);
|
|
6439
6238
|
}
|
|
6440
6239
|
|
|
6441
|
-
static const char* prepare_and_postprocess_doc = R"(function for custom metaclass to create private attributes class.
|
|
6442
|
-
|
|
6443
|
-
def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
|
|
6444
|
-
the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
|
|
6445
|
-
|
|
6446
|
-
def postprocess(type: type, tmp: tempobject) -> None:
|
|
6447
|
-
the function to postprocess for creating private attributes class. The custom metaclass can call this
|
|
6448
|
-
|
|
6449
|
-
def register_metaclass(metaclass: type) -> None:
|
|
6450
|
-
the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
|
|
6451
|
-
otherwise the private attributes class created by this custom metaclass will not work.
|
|
6452
|
-
|
|
6453
|
-
All usage of this module should be like:
|
|
6454
|
-
```
|
|
6455
|
-
from abc import ABCMeta
|
|
6456
|
-
import private_attribute
|
|
6457
|
-
|
|
6458
|
-
class PrivateAbcMeta(ABCMeta):
|
|
6459
|
-
def __new__(cls, *args, **kwargs):
|
|
6460
|
-
with private_attribute.prepare(*args, **kwargs) as temp:
|
|
6461
|
-
typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
|
|
6462
|
-
private_attribute.postprocess(typ, temp) # or temp.postprocess(typ)
|
|
6463
|
-
return typ
|
|
6464
|
-
|
|
6465
|
-
private_attribute.register_metaclass(PrivateAbcMeta)
|
|
6466
|
-
```
|
|
6467
|
-
|
|
6468
|
-
**NOTE**:
|
|
6469
|
-
Deprecated. See "create_new".
|
|
6470
|
-
)";
|
|
6471
6240
|
|
|
6472
6241
|
static const char* ensure_type_doc = R"(function for custom metaclass to ensure the type is a private attributes class.
|
|
6473
6242
|
def ensure_type(type: type) -> None:
|
|
@@ -6556,9 +6325,6 @@ class MyClass(private_attribute.PrivateAttrBase):
|
|
|
6556
6325
|
)";
|
|
6557
6326
|
|
|
6558
6327
|
static PyMethodDef PrivateModule_methods[] = {
|
|
6559
|
-
{"prepare", (PyCFunction)prepare_for_PrivateAttr, METH_VARARGS | METH_KEYWORDS | METH_STATIC, prepare_and_postprocess_doc},
|
|
6560
|
-
{"postprocess", (PyCFunction)postprocess_for_PrivateAttr, METH_VARARGS | METH_STATIC, prepare_and_postprocess_doc},
|
|
6561
|
-
{"register_metaclass", (PyCFunction)register_metaclass, METH_O | METH_STATIC, prepare_and_postprocess_doc},
|
|
6562
6328
|
{"ensure_type", (PyCFunction)ensure_type_tp, METH_O | METH_STATIC, ensure_type_doc},
|
|
6563
6329
|
{"ensure_metaclass", (PyCFunction)ensure_metaclass_tp, METH_O | METH_STATIC, ensure_metaclass_doc},
|
|
6564
6330
|
{"create_new", (PyCFunction)register_type_create_new, METH_VARARGS, create_new_doc},
|
|
@@ -6567,64 +6333,34 @@ static PyMethodDef PrivateModule_methods[] = {
|
|
|
6567
6333
|
{NULL} // Sentinel
|
|
6568
6334
|
};
|
|
6569
6335
|
|
|
6570
|
-
static PyObject*
|
|
6571
|
-
PrivateModule_get_prepare(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6572
|
-
{
|
|
6573
|
-
// warning that it is deprecated. See "create_new"
|
|
6574
|
-
if (PyErr_WarnEx(NULL, "private_attribute.prepare is deprecated. See 'private_attribute.create_new'.", 2) < 0) {
|
|
6575
|
-
return NULL;
|
|
6576
|
-
}
|
|
6577
|
-
return PyCFunction_NewEx(&PrivateModule_methods[0], NULL, NULL);
|
|
6578
|
-
}
|
|
6579
|
-
|
|
6580
|
-
static PyObject*
|
|
6581
|
-
PrivateModule_get_postprocess(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6582
|
-
{
|
|
6583
|
-
// warning that it is deprecated. See "create_new"
|
|
6584
|
-
if (PyErr_WarnEx(NULL, "private_attribute.postprocess is deprecated. See 'private_attribute.create_new'.", 2) < 0) {
|
|
6585
|
-
return NULL;
|
|
6586
|
-
}
|
|
6587
|
-
return PyCFunction_NewEx(&PrivateModule_methods[1], NULL, NULL);
|
|
6588
|
-
}
|
|
6589
|
-
|
|
6590
|
-
static PyObject*
|
|
6591
|
-
PrivateModule_get_register_metaclass(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6592
|
-
{
|
|
6593
|
-
// warning that it is deprecated. See "create_new"
|
|
6594
|
-
if (PyErr_WarnEx(NULL, "private_attribute.register_metaclass is deprecated. See 'private_attribute.create_new'.", 2) < 0) {
|
|
6595
|
-
return NULL;
|
|
6596
|
-
}
|
|
6597
|
-
return PyCFunction_NewEx(&PrivateModule_methods[2], NULL, NULL);
|
|
6598
|
-
}
|
|
6599
|
-
|
|
6600
6336
|
static PyObject*
|
|
6601
6337
|
PrivateModule_get_ensure_type(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6602
6338
|
{
|
|
6603
|
-
return PyCFunction_NewEx(&PrivateModule_methods[
|
|
6339
|
+
return PyCFunction_NewEx(&PrivateModule_methods[0], NULL, NULL);
|
|
6604
6340
|
}
|
|
6605
6341
|
|
|
6606
6342
|
static PyObject*
|
|
6607
6343
|
PrivateModule_get_ensure_metaclass(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6608
6344
|
{
|
|
6609
|
-
return PyCFunction_NewEx(&PrivateModule_methods[
|
|
6345
|
+
return PyCFunction_NewEx(&PrivateModule_methods[1], NULL, NULL);
|
|
6610
6346
|
}
|
|
6611
6347
|
|
|
6612
6348
|
static PyObject*
|
|
6613
6349
|
PrivateModule_get_create_new(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6614
6350
|
{
|
|
6615
|
-
return PyCFunction_NewEx(&PrivateModule_methods[
|
|
6351
|
+
return PyCFunction_NewEx(&PrivateModule_methods[2], NULL, NULL);
|
|
6616
6352
|
}
|
|
6617
6353
|
|
|
6618
6354
|
static PyObject*
|
|
6619
6355
|
PrivateModule_get_create_meta_slot(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6620
6356
|
{
|
|
6621
|
-
return PyCFunction_NewEx(&PrivateModule_methods[
|
|
6357
|
+
return PyCFunction_NewEx(&PrivateModule_methods[3], NULL, NULL);
|
|
6622
6358
|
}
|
|
6623
6359
|
|
|
6624
6360
|
static PyObject*
|
|
6625
6361
|
PrivateModule_get_create_slot(PyObject* /*self*/, void* /*closure*/) noexcept
|
|
6626
6362
|
{
|
|
6627
|
-
return PyCFunction_NewEx(&PrivateModule_methods[
|
|
6363
|
+
return PyCFunction_NewEx(&PrivateModule_methods[4], NULL, NULL);
|
|
6628
6364
|
}
|
|
6629
6365
|
|
|
6630
6366
|
static PyGetSetDef PrivateModule_getsetters[] = {
|
|
@@ -6633,9 +6369,6 @@ static PyGetSetDef PrivateModule_getsetters[] = {
|
|
|
6633
6369
|
{"PrivateAttrBase", (getter)PrivateModule_get_PrivateAttrBase, NULL, NULL, NULL},
|
|
6634
6370
|
{"PrivateSuper", (getter)PrivateModule_get_PrivateSuper, NULL, NULL, NULL},
|
|
6635
6371
|
{"PrivateClassSuper", (getter)PrivateModule_get_PrivateClassSuper, NULL, NULL, NULL},
|
|
6636
|
-
{"prepare", (getter)PrivateModule_get_prepare, NULL, NULL, NULL},
|
|
6637
|
-
{"postprocess", (getter)PrivateModule_get_postprocess, NULL, NULL, NULL},
|
|
6638
|
-
{"register_metaclass", (getter)PrivateModule_get_register_metaclass, NULL, NULL, NULL},
|
|
6639
6372
|
{"ensure_type", (getter)PrivateModule_get_ensure_type, NULL, NULL, NULL},
|
|
6640
6373
|
{"ensure_metaclass", (getter)PrivateModule_get_ensure_metaclass, NULL, NULL, NULL},
|
|
6641
6374
|
{"create_new", (getter)PrivateModule_get_create_new, NULL, NULL, NULL},
|
|
@@ -6665,9 +6398,6 @@ PrivateModule_getattro(PyObject* self, PyObject* name) noexcept
|
|
|
6665
6398
|
PrivateModule_GETATTRO_CASE(PrivateAttrBase);
|
|
6666
6399
|
PrivateModule_GETATTRO_CASE(PrivateSuper);
|
|
6667
6400
|
PrivateModule_GETATTRO_CASE(PrivateClassSuper);
|
|
6668
|
-
PrivateModule_GETATTRO_CASE(prepare);
|
|
6669
|
-
PrivateModule_GETATTRO_CASE(postprocess);
|
|
6670
|
-
PrivateModule_GETATTRO_CASE(register_metaclass);
|
|
6671
6401
|
PrivateModule_GETATTRO_CASE(ensure_type);
|
|
6672
6402
|
PrivateModule_GETATTRO_CASE(ensure_metaclass);
|
|
6673
6403
|
PrivateModule_GETATTRO_CASE(create_new);
|
|
@@ -6825,15 +6555,12 @@ PrivateModule_exec(PyObject* m) noexcept
|
|
|
6825
6555
|
if (PyModule_AddType(m, &PrivateWrapType) < 0) {
|
|
6826
6556
|
return -1;
|
|
6827
6557
|
}
|
|
6828
|
-
PyObject* all = Py_BuildValue("[
|
|
6558
|
+
PyObject* all = Py_BuildValue("[ssssssssss]",
|
|
6829
6559
|
"PrivateWrapProxy",
|
|
6830
6560
|
"PrivateAttrType",
|
|
6831
6561
|
"PrivateAttrBase",
|
|
6832
6562
|
"PrivateSuper",
|
|
6833
6563
|
"PrivateClassSuper",
|
|
6834
|
-
"prepare",
|
|
6835
|
-
"postprocess",
|
|
6836
|
-
"register_metaclass",
|
|
6837
6564
|
"ensure_type",
|
|
6838
6565
|
"ensure_metaclass",
|
|
6839
6566
|
"create_new",
|
|
@@ -6874,8 +6601,7 @@ PyInit_private_attribute(void) noexcept
|
|
|
6874
6601
|
PyType_Ready(&PrivateSuperType) < 0 ||
|
|
6875
6602
|
PyType_Ready(&PrivateClassSuperType) < 0 ||
|
|
6876
6603
|
PyType_Ready(&PrivateAttrType) < 0 ||
|
|
6877
|
-
PyType_Ready(&PrivateModuleType) < 0
|
|
6878
|
-
PyType_Ready(&PrivateTempType) < 0) {
|
|
6604
|
+
PyType_Ready(&PrivateModuleType) < 0) {
|
|
6879
6605
|
return NULL;
|
|
6880
6606
|
}
|
|
6881
6607
|
return PyModuleDef_Init(&def);
|
|
@@ -15,7 +15,7 @@ class MyClass(PrivateAttrBase):
|
|
|
15
15
|
return self._private_attr1
|
|
16
16
|
```
|
|
17
17
|
"""
|
|
18
|
-
from typing_extensions import Any, TypeVar, Callable, TypedDict, Sequence, Generic, Optional, cast, Iterator
|
|
18
|
+
from typing_extensions import Any, TypeVar, Callable, TypedDict, Sequence, Generic, Optional, cast, Iterator
|
|
19
19
|
from types import FunctionType
|
|
20
20
|
import sys
|
|
21
21
|
|
|
@@ -141,29 +141,6 @@ class PrivateClassSuper:
|
|
|
141
141
|
def __getattr__(self, name: str) -> Any: ...
|
|
142
142
|
|
|
143
143
|
|
|
144
|
-
class _PrivateTemp:
|
|
145
|
-
@property
|
|
146
|
-
def name(self) -> str: ...
|
|
147
|
-
@property
|
|
148
|
-
def bases(self) -> tuple[type]: ...
|
|
149
|
-
@property
|
|
150
|
-
def attrs(self) -> dict[str, Any]: ...
|
|
151
|
-
@property
|
|
152
|
-
def kwds(self) -> dict[str, Any]: ...
|
|
153
|
-
def postprocess(self, typ: type, /) -> None: ... # add in 2.1.17
|
|
154
|
-
|
|
155
|
-
def __enter__(self) -> "_PrivateTemp": ...
|
|
156
|
-
def __exit__(self, exc_type: type, exc_val: Exception, exc_tb: Any) -> None: ...
|
|
157
|
-
|
|
158
|
-
@deprecated("Use `create_new` instead.")
|
|
159
|
-
def prepare(name: str, bases: tuple, attrs: _PrivateAttrDict, /, **kwds) -> _PrivateTemp: ...
|
|
160
|
-
|
|
161
|
-
@deprecated("Use `create_new` instead.")
|
|
162
|
-
def postprocess(typ: type, temp: _PrivateTemp, /) -> None: ...
|
|
163
|
-
|
|
164
|
-
@deprecated("Use `create_new` instead.")
|
|
165
|
-
def register_metaclass(typ: type, /) -> None: ...
|
|
166
|
-
|
|
167
144
|
def ensure_type(typ: type, /) -> None:
|
|
168
145
|
"""function for custom metaclass to ensure the type is a private attributes class.
|
|
169
146
|
def ensure_type(type: type) -> None:
|
|
@@ -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.
|
|
7
|
+
version = "3.0.4"
|
|
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"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_attr_classify_owner.py
RENAMED
|
File without changes
|
{private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_class_private_attrs.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{private_attribute_cpp-3.0.3 → private_attribute_cpp-3.0.4}/tests/test_private_inheritance.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|