Skip to content

Commit 5109a35

Browse files
authored
Merge branch 'main' into check-mro-lookup-not-null
2 parents c305485 + ac75110 commit 5109a35

File tree

233 files changed

+7351
-3354
lines changed

Some content is hidden

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

233 files changed

+7351
-3354
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"image": "ghcr.io/python/devcontainer:2024.09.25.11038928730",
2+
"image": "ghcr.io/python/devcontainer:2025.05.25.15232270922",
33
"onCreateCommand": [
44
// Install common tooling.
55
"dnf",

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,10 @@ Python/frozen_modules/MANIFEST
171171
/python
172172
!/Python/
173173

174+
# People's custom https://docs.anthropic.com/en/docs/claude-code/memory configs.
175+
/.claude/
176+
CLAUDE.local.md
177+
178+
#### main branch only stuff below this line, things to backport go above. ####
174179
# main branch only: ABI files are not checked/maintained.
175180
Doc/data/python*.abi

Doc/c-api/init_config.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ initialization::
21112111
21122112
/* Specify sys.path explicitly */
21132113
/* If you want to modify the default set of paths, finish
2114-
initialization first and then use PySys_GetObject("path") */
2114+
initialization first and then use PySys_GetAttrString("path") */
21152115
config.module_search_paths_set = 1;
21162116
status = PyWideStringList_Append(&config.module_search_paths,
21172117
L"/path/to/stdlib");

Doc/c-api/intro.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,41 @@ after every statement run by the interpreter.)
838838

839839
Please refer to :file:`Misc/SpecialBuilds.txt` in the Python source distribution
840840
for more detailed information.
841+
842+
843+
.. _c-api-tools:
844+
845+
Recommended third party tools
846+
=============================
847+
848+
The following third party tools offer both simpler and more sophisticated
849+
approaches to creating C, C++ and Rust extensions for Python:
850+
851+
* `Cython <https://cython.org/>`_
852+
* `cffi <https://cffi.readthedocs.io>`_
853+
* `HPy <https://hpyproject.org/>`_
854+
* `nanobind <https://github.com/wjakob/nanobind>`_ (C++)
855+
* `Numba <https://numba.pydata.org/>`_
856+
* `pybind11 <https://pybind11.readthedocs.io/>`_ (C++)
857+
* `PyO3 <https://pyo3.rs/>`_ (Rust)
858+
* `SWIG <https://www.swig.org>`_
859+
860+
Using tools such as these can help avoid writing code that is tightly bound to
861+
a particular version of CPython, avoid reference counting errors, and focus
862+
more on your own code than on using the CPython API. In general, new versions
863+
of Python can be supported by updating the tool, and your code will often use
864+
newer and more efficient APIs automatically. Some tools also support compiling
865+
for other implementations of Python from a single set of sources.
866+
867+
These projects are not supported by the same people who maintain Python, and
868+
issues need to be raised with the projects directly. Remember to check that the
869+
project is still maintained and supported, as the list above may become
870+
outdated.
871+
872+
.. seealso::
873+
874+
`Python Packaging User Guide: Binary Extensions <https://packaging.python.org/guides/packaging-binary-extensions/>`_
875+
The Python Packaging User Guide not only covers several available
876+
tools that simplify the creation of binary extensions, but also
877+
discusses the various reasons why creating an extension module may be
878+
desirable in the first place.

Doc/c-api/module.rst

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,22 +288,40 @@ An alternate way to specify extensions is to request "multi-phase initialization
288288
Extension modules created this way behave more like Python modules: the
289289
initialization is split between the *creation phase*, when the module object
290290
is created, and the *execution phase*, when it is populated.
291-
The distinction is similar to the :py:meth:`!__new__` and :py:meth:`!__init__` methods
292-
of classes.
291+
The distinction is similar to the :py:meth:`~object.__new__` and
292+
:py:meth:`~object.__init__` methods of classes.
293293
294294
Unlike modules created using single-phase initialization, these modules are not
295-
singletons: if the *sys.modules* entry is removed and the module is re-imported,
296-
a new module object is created, and the old module is subject to normal garbage
297-
collection -- as with Python modules.
298-
By default, multiple modules created from the same definition should be
299-
independent: changes to one should not affect the others.
300-
This means that all state should be specific to the module object (using e.g.
301-
using :c:func:`PyModule_GetState`), or its contents (such as the module's
302-
:attr:`~object.__dict__` or individual classes created with :c:func:`PyType_FromSpec`).
295+
singletons.
296+
For example, if the :py:attr:`sys.modules` entry is removed and the module
297+
is re-imported, a new module object is created, and typically populated with
298+
fresh method and type objects.
299+
The old module is subject to normal garbage collection.
300+
This mirrors the behavior of pure-Python modules.
301+
302+
Additional module instances may be created in
303+
:ref:`sub-interpreters <sub-interpreter-support>`
304+
or after after Python runtime reinitialization
305+
(:c:func:`Py_Finalize` and :c:func:`Py_Initialize`).
306+
In these cases, sharing Python objects between module instances would likely
307+
cause crashes or undefined behavior.
308+
309+
To avoid such issues, each instance of an extension module should
310+
be *isolated*: changes to one instance should not implicitly affect the others,
311+
and all state, including references to Python objects, should be specific to
312+
a particular module instance.
313+
See :ref:`isolating-extensions-howto` for more details and a practical guide.
314+
315+
A simpler way to avoid these issues is
316+
:ref:`raising an error on repeated initialization <isolating-extensions-optout>`.
303317
304318
All modules created using multi-phase initialization are expected to support
305-
:ref:`sub-interpreters <sub-interpreter-support>`. Making sure multiple modules
306-
are independent is typically enough to achieve this.
319+
:ref:`sub-interpreters <sub-interpreter-support>`, or otherwise explicitly
320+
signal a lack of support.
321+
This is usually achieved by isolation or blocking repeated initialization,
322+
as above.
323+
A module may also be limited to the main interpreter using
324+
the :c:data:`Py_mod_multiple_interpreters` slot.
307325
308326
To request multi-phase initialization, the initialization function
309327
(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty

Doc/c-api/sys.rst

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,57 @@ These are utility functions that make functionality from the :mod:`sys` module
258258
accessible to C code. They all work with the current interpreter thread's
259259
:mod:`sys` module's dict, which is contained in the internal thread state structure.
260260
261+
.. c:function:: PyObject *PySys_GetAttr(PyObject *name)
262+
263+
Get the attribute *name* of the :mod:`sys` module.
264+
Return a :term:`strong reference`.
265+
Raise :exc:`RuntimeError` and return ``NULL`` if it does not exist or
266+
if the :mod:`sys` module cannot be found.
267+
268+
If the non-existing object should not be treated as a failure, you can use
269+
:c:func:`PySys_GetOptionalAttr` instead.
270+
271+
.. versionadded:: next
272+
273+
.. c:function:: PyObject *PySys_GetAttrString(const char *name)
274+
275+
This is the same as :c:func:`PySys_GetAttr`, but *name* is
276+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
277+
rather than a :c:expr:`PyObject*`.
278+
279+
If the non-existing object should not be treated as a failure, you can use
280+
:c:func:`PySys_GetOptionalAttrString` instead.
281+
282+
.. versionadded:: next
283+
284+
.. c:function:: int PySys_GetOptionalAttr(PyObject *name, PyObject **result)
285+
286+
Variant of :c:func:`PySys_GetAttr` which doesn't raise
287+
exception if the object does not exist.
288+
289+
* Set *\*result* to a new :term:`strong reference` to the object and
290+
return ``1`` if the object exists.
291+
* Set *\*result* to ``NULL`` and return ``0`` without setting an exception
292+
if the object does not exist.
293+
* Set an exception, set *\*result* to ``NULL``, and return ``-1``,
294+
if an error occurred.
295+
296+
.. versionadded:: next
297+
298+
.. c:function:: int PySys_GetOptionalAttrString(const char *name, PyObject **result)
299+
300+
This is the same as :c:func:`PySys_GetOptionalAttr`, but *name* is
301+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
302+
rather than a :c:expr:`PyObject*`.
303+
304+
.. versionadded:: next
305+
261306
.. c:function:: PyObject *PySys_GetObject(const char *name)
262307
263-
Return the object *name* from the :mod:`sys` module or ``NULL`` if it does
264-
not exist, without setting an exception.
308+
Similar to :c:func:`PySys_GetAttrString`, but return a :term:`borrowed
309+
reference` and return ``NULL`` *without* setting exception on failure.
310+
311+
Preserves exception that was set before the call.
265312
266313
.. c:function:: int PySys_SetObject(const char *name, PyObject *v)
267314

Doc/c-api/unicode.rst

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,6 @@ the user settings on the machine running the codec.
14611461
.. versionadded:: 3.3
14621462
14631463
1464-
Methods & Slots
1465-
"""""""""""""""
1466-
1467-
14681464
.. _unicodemethodsandslots:
14691465
14701466
Methods and Slot Functions
@@ -1726,10 +1722,6 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
17261722
from user input, prefer calling :c:func:`PyUnicode_FromString` and
17271723
:c:func:`PyUnicode_InternInPlace` directly.
17281724
1729-
.. impl-detail::
1730-
1731-
Strings interned this way are made :term:`immortal`.
1732-
17331725
17341726
.. c:function:: unsigned int PyUnicode_CHECK_INTERNED(PyObject *str)
17351727
@@ -1806,9 +1798,24 @@ object.
18061798
18071799
See also :c:func:`PyUnicodeWriter_DecodeUTF8Stateful`.
18081800
1801+
.. c:function:: int PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)
1802+
1803+
Write the ASCII string *str* into *writer*.
1804+
1805+
*size* is the string length in bytes. If *size* is equal to ``-1``, call
1806+
``strlen(str)`` to get the string length.
1807+
1808+
*str* must only contain ASCII characters. The behavior is undefined if
1809+
*str* contains non-ASCII characters.
1810+
1811+
On success, return ``0``.
1812+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1813+
1814+
.. versionadded:: next
1815+
18091816
.. c:function:: int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size)
18101817
1811-
Writer the wide string *str* into *writer*.
1818+
Write the wide string *str* into *writer*.
18121819
18131820
*size* is a number of wide characters. If *size* is equal to ``-1``, call
18141821
``wcslen(str)`` to get the string length.

Doc/data/stable_abi.dat

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/extending/extending.rst

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,32 @@ value must be in a particular range or must satisfy other conditions,
204204
:c:data:`PyExc_ValueError` is appropriate.
205205

206206
You can also define a new exception that is unique to your module.
207-
For this, you can declare a static global object variable at the beginning
208-
of the file::
207+
The simplest way to do this is to declare a static global object variable at
208+
the beginning of the file::
209209

210-
static PyObject *SpamError;
210+
static PyObject *SpamError = NULL;
211211

212-
and initialize it with an exception object in the module's
212+
and initialize it by calling :c:func:`PyErr_NewException` in the module's
213213
:c:data:`Py_mod_exec` function (:c:func:`!spam_module_exec`)::
214214

215+
SpamError = PyErr_NewException("spam.error", NULL, NULL);
216+
217+
Since :c:data:`!SpamError` is a global variable, it will be overwitten every time
218+
the module is reinitialized, when the :c:data:`Py_mod_exec` function is called.
219+
220+
For now, let's avoid the issue: we will block repeated initialization by raising an
221+
:py:exc:`ImportError`::
222+
223+
static PyObject *SpamError = NULL;
224+
215225
static int
216226
spam_module_exec(PyObject *m)
217227
{
228+
if (SpamError != NULL) {
229+
PyErr_SetString(PyExc_ImportError,
230+
"cannot initialize spam module more than once");
231+
return -1;
232+
}
218233
SpamError = PyErr_NewException("spam.error", NULL, NULL);
219234
if (PyModule_AddObjectRef(m, "SpamError", SpamError) < 0) {
220235
return -1;
@@ -253,6 +268,11 @@ needed to ensure that it will not be discarded, causing :c:data:`!SpamError` to
253268
become a dangling pointer. Should it become a dangling pointer, C code which
254269
raises the exception could cause a core dump or other unintended side effects.
255270

271+
For now, the :c:func:`Py_DECREF` call to remove this reference is missing.
272+
Even when the Python interpreter shuts down, the global :c:data:`!SpamError`
273+
variable will not be garbage-collected. It will "leak".
274+
We did, however, ensure that this will happen at most once per process.
275+
256276
We discuss the use of :c:macro:`PyMODINIT_FUNC` as a function return type later in this
257277
sample.
258278

Doc/extending/index.rst

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@ Recommended third party tools
2626
=============================
2727

2828
This guide only covers the basic tools for creating extensions provided
29-
as part of this version of CPython. Third party tools like
30-
`Cython <https://cython.org/>`_, `cffi <https://cffi.readthedocs.io>`_,
31-
`SWIG <https://www.swig.org>`_ and `Numba <https://numba.pydata.org/>`_
32-
offer both simpler and more sophisticated approaches to creating C and C++
33-
extensions for Python.
34-
35-
.. seealso::
36-
37-
`Python Packaging User Guide: Binary Extensions <https://packaging.python.org/guides/packaging-binary-extensions/>`_
38-
The Python Packaging User Guide not only covers several available
39-
tools that simplify the creation of binary extensions, but also
40-
discusses the various reasons why creating an extension module may be
41-
desirable in the first place.
29+
as part of this version of CPython. Some :ref:`third party tools
30+
<c-api-tools>` offer both simpler and more sophisticated approaches to creating
31+
C and C++ extensions for Python.
4232

4333

4434
Creating extensions without third party tools

0 commit comments

Comments
 (0)