8000 gh-105227: Add PyType_GetDict() by ericsnowcurrently · Pull Request #105747 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 10 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add docs.
  • Loading branch information
ericsnowcurrently committed Jun 13, 2023
commit 60d611e257b895ab6e2e654603ae9604c17ac83a
16 changes: 16 additions & 0 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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?

Copy link
Member

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.

specific embgedding 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.

.. versionadded:: 3.12


.. c:function:: void PyType_Modified(PyTypeObject *type)

Invalidate the internal lookup cache for the type and all of its
Expand Down
17 changes: 15 additions & 2 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only marking up tp_dict implies it's the only one in this category. At least tp_bases & tp_mro should be marked too.

**[]**: Names in square brackets are for internal use only.
**<R>** (as a prefix) means the field is required (must be non-``NULL``).
Expand Down Expand Up @@ -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:**

Expand Down
0