-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-105227: Add PyType_GetDict() #105747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-105227: Add PyType_GetDict() #105747
Changes from 4 commits
49f5e75
60d611e
718bbb6
269738d
ac9a071
8368595
23b3a41
7ed5d86
8d9b9ff
97e1d9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,22 @@ Type Objects | |
The return type is now ``unsigned long`` rather than ``long``. | ||
|
||
|
||
.. c:function:: PyObject* PyType_GetDict(PyTypeObject* type) | ||
|
||
Return the type object's internal namespace, which is otherwise only | ||
exposed via a read-only proxy (``cls.__dict__``). This is a | ||
replacement for accessing :c:member:`~PyTypeObject.tp_dict` directly. | ||
The returned dictionary must be treated as read-only. | ||
|
||
This function isn't intended for general use. It's meant for | ||
specific embedding and language-binding cases, where direct access | ||
to the dict is necessary and indirect access (e.g. via the proxy) | ||
isn't adequate. Extension modules may continue to use ``tp_dict``, | ||
directly or indirectly, when setting up their own types. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe elevate this paragraph to a |
||
|
||
.. versionadded:: 3.12 | ||
|
||
|
||
.. c:function:: void PyType_Modified(PyTypeObject *type) | ||
|
||
Invalidate the internal lookup cache for the type and all of its | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,7 +110,7 @@ Quick Reference | |
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| :c:member:`~PyTypeObject.tp_base` | :c:type:`PyTypeObject` * | __base__ | | | X | | | ||
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| :c:member:`~PyTypeObject.tp_dict` | :c:type:`PyObject` * | __dict__ | | | ? | | | ||
| <<:c:member:`~PyTypeObject.tp_dict`>> | :c:type:`PyObject` * | __dict__ | | | ? | | | ||
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
| :c:member:`~PyTypeObject.tp_descr_get` | :c:type:`descrgetfunc` | __get__ | | | | X | | ||
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | ||
|
@@ -157,6 +157,9 @@ Quick Reference | |
**<>**: Names in angle brackets should be initially set to ``NULL`` and | ||
treated as read-only. | ||
**<<>>**: Names in double angle brackets should be initially set to | ||
``NULL`` and treated as read-only after initialization. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only marking up |
||
**[]**: Names in square brackets are for internal use only. | ||
**<R>** (as a prefix) means the field is required (must be non-``NULL``). | ||
|
@@ -1717,7 +1720,17 @@ and :c:type:`PyType_Type` effectively act as defaults.) | |
called; it may also be initialized to a dictionary containing initial attributes | ||
for the type. Once :c:func:`PyType_Ready` has initialized the type, extra | ||
attributes for the type may be added to this dictionary only if they don't | ||
correspond to overloaded operations (like :meth:`__add__`). | ||
correspond to overloaded operations (like :meth:`__add__`). Once | ||
initialization for the type has finished, this field should be | ||
treated as read-only. | ||
|
||
.. versionchanged:: 3.12 | ||
|
||
Internals detail: For the static builtin types this is always ``NULL``. | ||
Instead, the dict for each is stored on ``PyInterpreterState``. | ||
If needed, use :c:func:`PyType_GetDict` to get the corresponding | ||
dict for those types. This is not normally necessary, | ||
and certainly not for user-defined type objects. | ||
|
||
**Inheritance:** | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,5 @@ | ||||||
The new :c:func:`PyType_GetDict` provides the dictionary for the given type | ||||||
object that is normally exposed by ``cls.__dict__``. Normally it's | ||||||
sufficient to use :c:member:`~PyTypeObject.tp_dict`, but for the static | ||||||
builtin types ``tp_dict`` is now always ``NULL``. ``PyType_GetDict()`` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting nit:
Suggested change
|
||||||
provides the correct dict object instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not meant for general use, would it be better to add it as unstable API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I think it makes more sense to say this is for general use.