Skip to content

Commit 5366fee

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/fix-issue-133672' into fix-issue-133672
2 parents 71f72e5 + 53f92d4 commit 5366fee

File tree

288 files changed

+8549
-4075
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

288 files changed

+8549
-4075
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*.ico binary
1111
*.jpg binary
1212
*.pck binary
13+
*.pdf binary
1314
*.png binary
1415
*.psd binary
1516
*.tar binary
@@ -67,6 +68,7 @@ PCbuild/readme.txt dos
6768
**/clinic/*.cpp.h generated
6869
**/clinic/*.h.h generated
6970
*_db.h generated
71+
Doc/c-api/lifecycle.dot.svg generated
7072
Doc/data/stable_abi.dat generated
7173
Doc/library/token-list.inc generated
7274
Include/internal/pycore_ast.h generated

.github/workflows/mypy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ on:
1414
- "Lib/tomllib/**"
1515
- "Misc/mypy/**"
1616
- "Tools/build/compute-changes.py"
17+
- "Tools/build/deepfreeze.py"
1718
- "Tools/build/generate_sbom.py"
1819
- "Tools/build/generate-build-details.py"
1920
- "Tools/build/verify_ensurepip_wheels.py"
2021
- "Tools/build/update_file.py"
22+
- "Tools/build/umarshal.py"
2123
- "Tools/cases_generator/**"
2224
- "Tools/clinic/**"
2325
- "Tools/jit/**"

.github/workflows/reusable-context.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
run: python Tools/build/compute-changes.py
9898
env:
9999
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
100+
GITHUB_EVENT_NAME: ${{ github.event_name }}
100101
CCF_TARGET_REF: ${{ github.base_ref || github.event.repository.default_branch }}
101102
CCF_HEAD_REF: ${{ github.event.pull_request.head.sha || github.sha }}
102103

Doc/c-api/allocation.rst

Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,128 @@ Allocating Objects on the Heap
1616
1717
Initialize a newly allocated object *op* with its type and initial
1818
reference. Returns the initialized object. Other fields of the object are
19-
not affected.
19+
not initialized. Despite its name, this function is unrelated to the
20+
object's :meth:`~object.__init__` method (:c:member:`~PyTypeObject.tp_init`
21+
slot). Specifically, this function does **not** call the object's
22+
:meth:`!__init__` method.
23+
24+
In general, consider this function to be a low-level routine. Use
25+
:c:member:`~PyTypeObject.tp_alloc` where possible.
26+
For implementing :c:member:`!tp_alloc` for your type, prefer
27+
:c:func:`PyType_GenericAlloc` or :c:func:`PyObject_New`.
28+
29+
.. note::
30+
31+
This function only initializes the object's memory corresponding to the
32+
initial :c:type:`PyObject` structure. It does not zero the rest.
2033
2134
2235
.. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
2336
2437
This does everything :c:func:`PyObject_Init` does, and also initializes the
2538
length information for a variable-size object.
2639
40+
.. note::
41+
42+
This function only initializes some of the object's memory. It does not
43+
zero the rest.
44+
2745
2846
.. c:macro:: PyObject_New(TYPE, typeobj)
2947
30-
Allocate a new Python object using the C structure type *TYPE*
31-
and the Python type object *typeobj* (``PyTypeObject*``).
32-
Fields not defined by the Python object header are not initialized.
33-
The caller will own the only reference to the object
34-
(i.e. its reference count will be one).
35-
The size of the memory allocation is determined from the
36-
:c:member:`~PyTypeObject.tp_basicsize` field of the type object.
48+
Allocates a new Python object using the C structure type *TYPE* and the
49+
Python type object *typeobj* (``PyTypeObject*``) by calling
50+
:c:func:`PyObject_Malloc` to allocate memory and initializing it like
51+
:c:func:`PyObject_Init`. The caller will own the only reference to the
52+
object (i.e. its reference count will be one).
53+
54+
Avoid calling this directly to allocate memory for an object; call the type's
55+
:c:member:`~PyTypeObject.tp_alloc` slot instead.
56+
57+
When populating a type's :c:member:`~PyTypeObject.tp_alloc` slot,
58+
:c:func:`PyType_GenericAlloc` is preferred over a custom function that
59+
simply calls this macro.
60+
61+
This macro does not call :c:member:`~PyTypeObject.tp_alloc`,
62+
:c:member:`~PyTypeObject.tp_new` (:meth:`~object.__new__`), or
63+
:c:member:`~PyTypeObject.tp_init` (:meth:`~object.__init__`).
64+
65+
This cannot be used for objects with :c:macro:`Py_TPFLAGS_HAVE_GC` set in
66+
:c:member:`~PyTypeObject.tp_flags`; use :c:macro:`PyObject_GC_New` instead.
67+
68+
Memory allocated by this macro must be freed with :c:func:`PyObject_Free`
69+
(usually called via the object's :c:member:`~PyTypeObject.tp_free` slot).
70+
71+
.. note::
72+
73+
The returned memory is not guaranteed to have been completely zeroed
74+
before it was initialized.
75+
76+
.. note::
77+
78+
This macro does not construct a fully initialized object of the given
79+
type; it merely allocates memory and prepares it for further
80+
initialization by :c:member:`~PyTypeObject.tp_init`. To construct a
81+
fully initialized object, call *typeobj* instead. For example::
82+
83+
PyObject *foo = PyObject_CallNoArgs((PyObject *)&PyFoo_Type);
3784
38-
Note that this function is unsuitable if *typeobj* has
39-
:c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
40-
use :c:func:`PyObject_GC_New` instead.
85+
.. seealso::
86+
87+
* :c:func:`PyObject_Free`
88+
* :c:macro:`PyObject_GC_New`
89+
* :c:func:`PyType_GenericAlloc`
90+
* :c:member:`~PyTypeObject.tp_alloc`
4191

4292

4393
.. c:macro:: PyObject_NewVar(TYPE, typeobj, size)
4494
45-
Allocate a new Python object using the C structure type *TYPE* and the
46-
Python type object *typeobj* (``PyTypeObject*``).
47-
Fields not defined by the Python object header
48-
are not initialized. The allocated memory allows for the *TYPE* structure
49-
plus *size* (``Py_ssize_t``) fields of the size
50-
given by the :c:member:`~PyTypeObject.tp_itemsize` field of
51-
*typeobj*. This is useful for implementing objects like tuples, which are
52-
able to determine their size at construction time. Embedding the array of
53-
fields into the same allocation decreases the number of allocations,
54-
improving the memory management efficiency.
55-
56-
Note that this function is unsuitable if *typeobj* has
57-
:c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
58-
use :c:func:`PyObject_GC_NewVar` instead.
95+
Like :c:macro:`PyObject_New` except:
96+
97+
* It allocates enough memory for the *TYPE* structure plus *size*
98+
(``Py_ssize_t``) fields of the size given by the
99+
:c:member:`~PyTypeObject.tp_itemsize` field of *typeobj*.
100+
* The memory is initialized like :c:func:`PyObject_InitVar`.
101+
102+
This is useful for implementing objects like tuples, which are able to
103+
determine their size at construction time. Embedding the array of fields
104+
into the same allocation decreases the number of allocations, improving the
105+
memory management efficiency.
106+
107+
Avoid calling this directly to allocate memory for an object; call the type's
108+
:c:member:`~PyTypeObject.tp_alloc` slot instead.
109+
110+
When populating a type's :c:member:`~PyTypeObject.tp_alloc` slot,
111+
:c:func:`PyType_GenericAlloc` is preferred over a custom function that
112+
simply calls this macro.
113+
114+
This cannot be used for objects with :c:macro:`Py_TPFLAGS_HAVE_GC` set in
115+
:c:member:`~PyTypeObject.tp_flags`; use :c:macro:`PyObject_GC_NewVar`
116+
instead.
117+
118+
Memory allocated by this function must be freed with :c:func:`PyObject_Free`
119+
(usually called via the object's :c:member:`~PyTypeObject.tp_free` slot).
120+
121+
.. note::
122+
123+
The returned memory is not guaranteed to have been completely zeroed
124+
before it was initialized.
125+
126+
.. note::
127+
128+
This macro does not construct a fully initialized object of the given
129+
type; it merely allocates memory and prepares it for further
130+
initialization by :c:member:`~PyTypeObject.tp_init`. To construct a
131+
fully initialized object, call *typeobj* instead. For example::
132+
133+
PyObject *list_instance = PyObject_CallNoArgs((PyObject *)&PyList_Type);
134+
135+
.. seealso::
136+
137+
* :c:func:`PyObject_Free`
138+
* :c:macro:`PyObject_GC_NewVar`
139+
* :c:func:`PyType_GenericAlloc`
140+
* :c:member:`~PyTypeObject.tp_alloc`
59141

60142

61143
.. c:function:: void PyObject_Del(void *op)

Doc/c-api/gcsupport.rst

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,49 @@ rules:
5757
Analogous to :c:macro:`PyObject_New` but for container objects with the
5858
:c:macro:`Py_TPFLAGS_HAVE_GC` flag set.
5959

60+
Do not call this directly to allocate memory for an object; call the type's
61+
:c:member:`~PyTypeObject.tp_alloc` slot instead.
62+
63+
When populating a type's :c:member:`~PyTypeObject.tp_alloc` slot,
64+
:c:func:`PyType_GenericAlloc` is preferred over a custom function that
65+
simply calls this macro.
66+
67+
Memory allocated by this macro must be freed with
68+
:c:func:`PyObject_GC_Del` (usually called via the object's
69+
:c:member:`~PyTypeObject.tp_free` slot).
70+
71+
.. seealso::
72+
73+
* :c:func:`PyObject_GC_Del`
74+
* :c:macro:`PyObject_New`
75+
* :c:func:`PyType_GenericAlloc`
76+
* :c:member:`~PyTypeObject.tp_alloc`
77+
78+
6079
.. c:macro:: PyObject_GC_NewVar(TYPE, typeobj, size)
6180
6281
Analogous to :c:macro:`PyObject_NewVar` but for container objects with the
6382
:c:macro:`Py_TPFLAGS_HAVE_GC` flag set.
6483

84+
Do not call this directly to allocate memory for an object; call the type's
85+
:c:member:`~PyTypeObject.tp_alloc` slot instead.
86+
87+
When populating a type's :c:member:`~PyTypeObject.tp_alloc` slot,
88+
:c:func:`PyType_GenericAlloc` is preferred over a custom function that
89+
simply calls this macro.
90+
91+
Memory allocated by this macro must be freed with
92+
:c:func:`PyObject_GC_Del` (usually called via the object's
93+
:c:member:`~PyTypeObject.tp_free` slot).
94+
95+
.. seealso::
96+
97+
* :c:func:`PyObject_GC_Del`
98+
* :c:macro:`PyObject_NewVar`
99+
* :c:func:`PyType_GenericAlloc`
100+
* :c:member:`~PyTypeObject.tp_alloc`
101+
102+
65103
.. c:function:: PyObject* PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *type, size_t extra_size)
66104
67105
Analogous to :c:macro:`PyObject_GC_New` but allocates *extra_size*
@@ -73,6 +111,10 @@ rules:
73111
The extra data will be deallocated with the object, but otherwise it is
74112
not managed by Python.
75113
114+
Memory allocated by this function must be freed with
115+
:c:func:`PyObject_GC_Del` (usually called via the object's
116+
:c:member:`~PyTypeObject.tp_free` slot).
117+
76118
.. warning::
77119
The function is marked as unstable because the final mechanism
78120
for reserving extra data after an instance is not yet decided.
@@ -136,6 +178,21 @@ rules:
136178
Releases memory allocated to an object using :c:macro:`PyObject_GC_New` or
137179
:c:macro:`PyObject_GC_NewVar`.
138180
181+
Do not call this directly to free an object's memory; call the type's
182+
:c:member:`~PyTypeObject.tp_free` slot instead.
183+
184+
Do not use this for memory allocated by :c:macro:`PyObject_New`,
185+
:c:macro:`PyObject_NewVar`, or related allocation functions; use
186+
:c:func:`PyObject_Free` instead.
187+
188+
.. seealso::
189+
190+
* :c:func:`PyObject_Free` is the non-GC equivalent of this function.
191+
* :c:macro:`PyObject_GC_New`
192+
* :c:macro:`PyObject_GC_NewVar`
193+
* :c:func:`PyType_GenericAlloc`
194+
* :c:member:`~PyTypeObject.tp_free`
195+
139196
140197
.. c:function:: void PyObject_GC_UnTrack(void *op)
141198
@@ -180,9 +237,9 @@ provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse`
180237
must name its arguments exactly *visit* and *arg*:
181238
182239
183-
.. c:function:: void Py_VISIT(PyObject *o)
240+
.. c:macro:: Py_VISIT(o)
184241
185-
If *o* is not ``NULL``, call the *visit* callback, with arguments *o*
242+
If the :c:expr:`PyObject *` *o* is not ``NULL``, call the *visit* callback, with arguments *o*
186243
and *arg*. If *visit* returns a non-zero value, then return it.
187244
Using this macro, :c:member:`~PyTypeObject.tp_traverse` handlers
188245
look like::

Doc/c-api/init.rst

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,36 @@ Note that the ``PyGILState_*`` functions assume there is only one global
919919
interpreter (created automatically by :c:func:`Py_Initialize`). Python
920920
supports the creation of additional interpreters (using
921921
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
922-
``PyGILState_*`` API is unsupported.
922+
``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
923+
and similar functions default to :term:`attaching <attached thread state>` a
924+
:term:`thread state` for the main interpreter, meaning that the thread can't safely
925+
interact with the calling subinterpreter.
926+
927+
Supporting subinterpreters in non-Python threads
928+
------------------------------------------------
929+
930+
If you would like to support subinterpreters with non-Python created threads, you
931+
must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
932+
API.
933+
934+
In particular, you must store the interpreter state from the calling
935+
function and pass it to :c:func:`PyThreadState_New`, which will ensure that
936+
the :term:`thread state` is targeting the correct interpreter::
937+
938+
/* The return value of PyInterpreterState_Get() from the
939+
function that created this thread. */
940+
PyInterpreterState *interp = ThreadData->interp;
941+
PyThreadState *tstate = PyThreadState_New(interp);
942+
PyThreadState_Swap(tstate);
943+
944+
/* GIL of the subinterpreter is now held.
945+
Perform Python actions here. */
946+
result = CallSomeFunction();
947+
/* evaluate result or handle exception */
923948
949+
/* Destroy the thread state. No Python API allowed beyond this point. */
950+
PyThreadState_Clear(tstate);
951+
PyThreadState_DeleteCurrent();
924952
925953
.. _fork-and-threads:
926954
@@ -1097,6 +1125,10 @@ code, or when embedding the Python interpreter:
10971125
.. seealso:
10981126
:c:func:`PyEval_ReleaseThread`
10991127
1128+
.. note::
1129+
Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1130+
thread if the runtime is finalizing.
1131+
11001132
11011133
The following functions use thread-local storage, and are not compatible
11021134
with sub-interpreters:
@@ -1123,10 +1155,10 @@ with sub-interpreters:
11231155
When the function returns, there will be an :term:`attached thread state`
11241156
and the thread will be able to call arbitrary Python code. Failure is a fatal error.
11251157
1126-
.. note::
1127-
Calling this function from a thread when the runtime is finalizing will
1128-
hang the thread until the program exits, even if the thread was not
1129-
created by Python. Refer to
1158+
.. warning::
1159+
Calling this function when the runtime is finalizing is unsafe. Doing
1160+
so will either hang the thread until the program ends, or fully crash
1161+
the interpreter in rare cases. Refer to
11301162
:ref:`cautions-regarding-runtime-finalization` for more details.
11311163
11321164
.. versionchanged:: 3.14
@@ -1143,28 +1175,37 @@ with sub-interpreters:
11431175
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
11441176
:c:func:`PyGILState_Release` on the same thread.
11451177
1146-
11471178
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
11481179
11491180
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
11501181
GILState API has been used on the current thread. Note that the main thread
11511182
always has such a thread-state, even if no auto-thread-state call has been
11521183
made on the main thread. This is mainly a helper/diagnostic function.
11531184
1154-
.. seealso: :c:func:`PyThreadState_Get``
1185+
.. note::
1186+
This function does not account for :term:`thread states <thread state>` created
1187+
by something other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1188+
Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1189+
for most cases.
11551190
1191+
.. seealso: :c:func:`PyThreadState_Get``
11561192
11571193
.. c:function:: int PyGILState_Check()
11581194
11591195
Return ``1`` if the current thread is holding the :term:`GIL` and ``0`` otherwise.
11601196
This function can be called from any thread at any time.
1161-
Only if it has had its Python thread state initialized and currently is
1162-
holding the :term:`GIL` will it return ``1``.
1197+
Only if it has had its :term:`thread state <attached thread state>` initialized
1198+
via :c:func:`PyGILState_Ensure` will it return ``1``.
11631199
This is mainly a helper/diagnostic function. It can be useful
11641200
for example in callback contexts or memory allocation functions when
11651201
knowing that the :term:`GIL` is locked can allow the caller to perform sensitive
11661202
actions or otherwise behave differently.
11671203
1204+
.. note::
1205+
If the current Python process has ever created a subinterpreter, this
1206+
function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1207+
for most cases.
1208+
11681209
.. versionadded:: 3.4
11691210
11701211

0 commit comments

Comments
 (0)