Skip to content

Commit 5588b7a

Browse files
committed
Merge branch 'main' into internal_code
2 parents 464fc8d + d2646e3 commit 5588b7a

File tree

403 files changed

+10588
-5956
lines changed

Some content is hidden

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

403 files changed

+10588
-5956
lines changed

.github/CODEOWNERS

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Python/bytecodes.c @markshannon
4242
Python/optimizer*.c @markshannon
4343
Python/optimizer_analysis.c @Fidget-Spinner
4444
Python/optimizer_bytecodes.c @Fidget-Spinner
45-
Python/symtable.c @JelleZijlstra @carljm
45+
Python/symtable.c @JelleZijlstra @carljm
4646
Lib/_pyrepl/* @pablogsal @lysnikolaou @ambv
4747
Lib/test/test_patma.py @brandtbucher
4848
Lib/test/test_type_*.py @JelleZijlstra
@@ -201,7 +201,6 @@ Doc/c-api/stable.rst @encukou
201201
**/*itertools* @rhettinger
202202
**/*collections* @rhettinger
203203
**/*random* @rhettinger
204-
**/*queue* @rhettinger
205204
**/*bisect* @rhettinger
206205
**/*heapq* @rhettinger
207206
**/*functools* @rhettinger

.github/workflows/jit.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,15 @@ jobs:
133133
make all --jobs 4
134134
./python.exe -m test --multiprocess 0 --timeout 4500 --verbose2 --verbose3
135135
136-
# --with-lto has been removed temporarily as a result of an open issue in LLVM 18 (see https://github.com/llvm/llvm-project/issues/87553)
137136
- name: Native Linux
138137
if: runner.os == 'Linux' && matrix.architecture == 'x86_64'
139138
run: |
140139
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" ./llvm.sh ${{ matrix.llvm }}
141140
export PATH="$(llvm-config-${{ matrix.llvm }} --bindir):$PATH"
142-
./configure --enable-experimental-jit ${{ matrix.debug && '--with-pydebug' || '--enable-optimizations' }}
141+
./configure --enable-experimental-jit ${{ matrix.debug && '--with-pydebug' || '--enable-optimizations --with-lto' }}
143142
make all --jobs 4
144143
./python -m test --multiprocess 0 --timeout 4500 --verbose2 --verbose3
145144
146-
# --with-lto has been removed temporarily as a result of an open issue in LLVM 18 (see https://github.com/llvm/llvm-project/issues/87553)
147145
- name: Emulated Linux
148146
if: runner.os == 'Linux' && matrix.architecture != 'x86_64'
149147
# The --ignorefile on ./python -m test is used to exclude tests known to fail when running on an emulated Linux.
@@ -161,7 +159,7 @@ jobs:
161159
CC="${{ matrix.compiler == 'clang' && 'clang --target=$HOST' || '$HOST-gcc' }}" \
162160
CPP="$CC --preprocess" \
163161
HOSTRUNNER=qemu-${{ matrix.architecture }} \
164-
./configure --enable-experimental-jit ${{ matrix.debug && '--with-pydebug' || '--enable-optimizations ' }} --build=x86_64-linux-gnu --host="$HOST" --with-build-python=../build/bin/python3 --with-pkg-config=no ac_cv_buggy_getaddrinfo=no ac_cv_file__dev_ptc=no ac_cv_file__dev_ptmx=yes
162+
./configure --enable-experimental-jit ${{ matrix.debug && '--with-pydebug' || '--enable-optimizations --with-lto' }} --build=x86_64-linux-gnu --host="$HOST" --with-build-python=../build/bin/python3 --with-pkg-config=no ac_cv_buggy_getaddrinfo=no ac_cv_file__dev_ptc=no ac_cv_file__dev_ptmx=yes
165163
make all --jobs 4
166164
./python -m test --ignorefile=Tools/jit/ignore-tests-emulated-linux.txt --multiprocess 0 --timeout 4500 --verbose2 --verbose3
167165

.readthedocs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ build:
2626
exit 183;
2727
fi
2828
29+
- asdf plugin add uv
30+
- asdf install uv latest
31+
- asdf global uv latest
2932
- make -C Doc venv html
3033
- mkdir _readthedocs
3134
- mv Doc/build/html _readthedocs/html

Doc/Makefile

+19-6
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ htmlview: html
152152

153153
.PHONY: ensure-sphinx-autobuild
154154
ensure-sphinx-autobuild: venv
155-
$(VENVDIR)/bin/sphinx-autobuild --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install sphinx-autobuild
155+
$(call ensure_package,sphinx-autobuild)
156156

157157
.PHONY: htmllive
158158
htmllive: SPHINXBUILD = $(VENVDIR)/bin/sphinx-autobuild
@@ -174,10 +174,15 @@ venv:
174174
echo "To recreate it, remove it first with \`make clean-venv'."; \
175175
else \
176176
echo "Creating venv in $(VENVDIR)"; \
177-
$(PYTHON) -m venv $(VENVDIR); \
178-
$(VENVDIR)/bin/python3 -m pip install --upgrade pip; \
179-
$(VENVDIR)/bin/python3 -m pip install -r $(REQUIREMENTS); \
180-
echo "The venv has been created in the $(VENVDIR) directory"; \
177+
if uv --version > /dev/null; then \
178+
uv venv $(VENVDIR); \
179+
VIRTUAL_ENV=$(VENVDIR) uv pip install -r $(REQUIREMENTS); \
180+
else \
181+
$(PYTHON) -m venv $(VENVDIR); \
182+
$(VENVDIR)/bin/python3 -m pip install --upgrade pip; \
183+
$(VENVDIR)/bin/python3 -m pip install -r $(REQUIREMENTS); \
184+
echo "The venv has been created in the $(VENVDIR) directory"; \
185+
fi; \
181186
fi
182187

183188
.PHONY: dist
@@ -235,9 +240,17 @@ dist:
235240
rm -r dist/python-$(DISTVERSION)-docs-texinfo
236241
rm dist/python-$(DISTVERSION)-docs-texinfo.tar
237242

243+
define ensure_package
244+
if uv --version > /dev/null; then \
245+
$(VENVDIR)/bin/python3 -m $(1) --version > /dev/null || VIRTUAL_ENV=$(VENVDIR) uv pip install $(1); \
246+
else \
247+
$(VENVDIR)/bin/python3 -m $(1) --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install $(1); \
248+
fi
249+
endef
250+
238251
.PHONY: check
239252
check: venv
240-
$(VENVDIR)/bin/python3 -m pre_commit --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install pre-commit
253+
$(call ensure_package,pre_commit)
241254
$(VENVDIR)/bin/python3 -m pre_commit run --all-files
242255

243256
.PHONY: serve

Doc/README.rst

+10-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ install the tools into there.
2828
Using make
2929
----------
3030

31-
To get started on UNIX, you can create a virtual environment and build
31+
To get started on Unix, you can create a virtual environment and build
3232
documentation with the commands::
3333

3434
make venv
@@ -40,13 +40,13 @@ If you'd like to create the virtual environment in a different location,
4040
you can specify it using the ``VENVDIR`` variable.
4141

4242
You can also skip creating the virtual environment altogether, in which case
43-
the Makefile will look for instances of ``sphinx-build`` and ``blurb``
43+
the ``Makefile`` will look for instances of ``sphinx-build`` and ``blurb``
4444
installed on your process ``PATH`` (configurable with the ``SPHINXBUILD`` and
4545
``BLURB`` variables).
4646

47-
On Windows, we try to emulate the Makefile as closely as possible with a
47+
On Windows, we try to emulate the ``Makefile`` as closely as possible with a
4848
``make.bat`` file. If you need to specify the Python interpreter to use,
49-
set the PYTHON environment variable.
49+
set the ``PYTHON`` environment variable.
5050

5151
Available make targets are:
5252

@@ -62,15 +62,19 @@ Available make targets are:
6262
* "htmlview", which re-uses the "html" builder, but then opens the main page
6363
in your default web browser.
6464

65+
* "htmllive", which re-uses the "html" builder, rebuilds the docs,
66+
starts a local server, and automatically reloads the page in your browser
67+
when you make changes to reST files (Unix only).
68+
6569
* "htmlhelp", which builds HTML files and a HTML Help project file usable to
6670
convert them into a single Compiled HTML (.chm) file -- these are popular
6771
under Microsoft Windows, but very handy on every platform.
6872

6973
To create the CHM file, you need to run the Microsoft HTML Help Workshop
70-
over the generated project (.hhp) file. The make.bat script does this for
74+
over the generated project (.hhp) file. The ``make.bat`` script does this for
7175
you on Windows.
7276

73-
* "latex", which builds LaTeX source files as input to "pdflatex" to produce
77+
* "latex", which builds LaTeX source files as input to ``pdflatex`` to produce
7478
PDF documents.
7579

7680
* "text", which builds a plain text file for each source file.
@@ -95,8 +99,6 @@ Available make targets are:
9599

96100
* "check", which checks for frequent markup errors.
97101

98-
* "serve", which serves the build/html directory on port 8000.
99-
100102
* "dist", (Unix only) which creates distributable archives of HTML, text,
101103
PDF, and EPUB builds.
102104

Doc/c-api/dict.rst

+11
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,17 @@ Dictionary Objects
290290
Py_DECREF(o);
291291
}
292292
293+
The function is not thread-safe in the :term:`free-threaded <free threading>`
294+
build without external synchronization. You can use
295+
:c:macro:`Py_BEGIN_CRITICAL_SECTION` to lock the dictionary while iterating
296+
over it::
297+
298+
Py_BEGIN_CRITICAL_SECTION(self->dict);
299+
while (PyDict_Next(self->dict, &pos, &key, &value)) {
300+
...
301+
}
302+
Py_END_CRITICAL_SECTION();
303+
293304
294305
.. c:function:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
295306

Doc/c-api/init.rst

+157-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ The following functions can be safely called before Python is initialized:
5555
* :c:func:`PyMem_RawCalloc`
5656
* :c:func:`PyMem_RawFree`
5757

58+
* Synchronization:
59+
60+
* :c:func:`PyMutex_Lock`
61+
* :c:func:`PyMutex_Unlock`
62+
5863
.. note::
5964

6065
The following functions **should not be called** before
@@ -391,9 +396,16 @@ Initializing and finalizing the interpreter
391396
:c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since
392397
the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory
393398
allocated by the Python interpreter. This is a no-op when called for a second
394-
time (without calling :c:func:`Py_Initialize` again first). Normally the
395-
return value is ``0``. If there were errors during finalization
396-
(flushing buffered data), ``-1`` is returned.
399+
time (without calling :c:func:`Py_Initialize` again first).
400+
401+
Since this is the reverse of :c:func:`Py_Initialize`, it should be called
402+
in the same thread with the same interpreter active. That means
403+
the main thread and the main interpreter.
404+
This should never be called while :c:func:`Py_RunMain` is running.
405+
406+
Normally the return value is ``0``.
407+
If there were errors during finalization (flushing buffered data),
408+
``-1`` is returned.
397409
398410
This function is provided for a number of reasons. An embedding application
399411
might want to restart Python without having to restart the application itself.
@@ -2152,3 +2164,145 @@ be used in new code.
21522164
.. c:function:: void PyThread_delete_key_value(int key)
21532165
.. c:function:: void PyThread_ReInitTLS()
21542166
2167+
Synchronization Primitives
2168+
==========================
2169+
2170+
The C-API provides a basic mutual exclusion lock.
2171+
2172+
.. c:type:: PyMutex
2173+
2174+
A mutual exclusion lock. The :c:type:`!PyMutex` should be initialized to
2175+
zero to represent the unlocked state. For example::
2176+
2177+
PyMutex mutex = {0};
2178+
2179+
Instances of :c:type:`!PyMutex` should not be copied or moved. Both the
2180+
contents and address of a :c:type:`!PyMutex` are meaningful, and it must
2181+
remain at a fixed, writable location in memory.
2182+
2183+
.. note::
2184+
2185+
A :c:type:`!PyMutex` currently occupies one byte, but the size should be
2186+
considered unstable. The size may change in future Python releases
2187+
without a deprecation period.
2188+
2189+
.. versionadded:: 3.13
2190+
2191+
.. c:function:: void PyMutex_Lock(PyMutex *m)
2192+
2193+
Lock mutex *m*. If another thread has already locked it, the calling
2194+
thread will block until the mutex is unlocked. While blocked, the thread
2195+
will temporarily release the :term:`GIL` if it is held.
2196+
2197+
.. versionadded:: 3.13
2198+
2199+
.. c:function:: void PyMutex_Unlock(PyMutex *m)
2200+
2201+
Unlock mutex *m*. The mutex must be locked --- otherwise, the function will
2202+
issue a fatal error.
2203+
2204+
.. versionadded:: 3.13
2205+
2206+
.. _python-critical-section-api:
2207+
2208+
Python Critical Section API
2209+
---------------------------
2210+
2211+
The critical section API provides a deadlock avoidance layer on top of
2212+
per-object locks for :term:`free-threaded <free threading>` CPython. They are
2213+
intended to replace reliance on the :term:`global interpreter lock`, and are
2214+
no-ops in versions of Python with the global interpreter lock.
2215+
2216+
Critical sections avoid deadlocks by implicitly suspending active critical
2217+
sections and releasing the locks during calls to :c:func:`PyEval_SaveThread`.
2218+
When :c:func:`PyEval_RestoreThread` is called, the most recent critical section
2219+
is resumed, and its locks reacquired. This means the critical section API
2220+
provides weaker guarantees than traditional locks -- they are useful because
2221+
their behavior is similar to the :term:`GIL`.
2222+
2223+
The functions and structs used by the macros are exposed for cases
2224+
where C macros are not available. They should only be used as in the
2225+
given macro expansions. Note that the sizes and contents of the structures may
2226+
change in future Python versions.
2227+
2228+
.. note::
2229+
2230+
Operations that need to lock two objects at once must use
2231+
:c:macro:`Py_BEGIN_CRITICAL_SECTION2`. You *cannot* use nested critical
2232+
sections to lock more than one object at once, because the inner critical
2233+
section may suspend the outer critical sections. This API does not provide
2234+
a way to lock more than two objects at once.
2235+
2236+
Example usage::
2237+
2238+
static PyObject *
2239+
set_field(MyObject *self, PyObject *value)
2240+
{
2241+
Py_BEGIN_CRITICAL_SECTION(self);
2242+
Py_SETREF(self->field, Py_XNewRef(value));
2243+
Py_END_CRITICAL_SECTION();
2244+
Py_RETURN_NONE;
2245+
}
2246+
2247+
In the above example, :c:macro:`Py_SETREF` calls :c:macro:`Py_DECREF`, which
2248+
can call arbitrary code through an object's deallocation function. The critical
2249+
section API avoids potentital deadlocks due to reentrancy and lock ordering
2250+
by allowing the runtime to temporarily suspend the critical section if the
2251+
code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
2252+
2253+
.. c:macro:: Py_BEGIN_CRITICAL_SECTION(op)
2254+
2255+
Acquires the per-object lock for the object *op* and begins a
2256+
critical section.
2257+
2258+
In the free-threaded build, this macro expands to::
2259+
2260+
{
2261+
PyCriticalSection _py_cs;
2262+
PyCriticalSection_Begin(&_py_cs, (PyObject*)(op))
2263+
2264+
In the default build, this macro expands to ``{``.
2265+
2266+
.. versionadded:: 3.13
2267+
2268+
.. c:macro:: Py_END_CRITICAL_SECTION()
2269+
2270+
Ends the critical section and releases the per-object lock.
2271+
2272+
In the free-threaded build, this macro expands to::
2273+
2274+
PyCriticalSection_End(&_py_cs);
2275+
}
2276+
2277+
In the default build, this macro expands to ``}``.
2278+
2279+
.. versionadded:: 3.13
2280+
2281+
.. c:macro:: Py_BEGIN_CRITICAL_SECTION2(a, b)
2282+
2283+
Acquires the per-objects locks for the objects *a* and *b* and begins a
2284+
critical section. The locks are acquired in a consistent order (lowest
2285+
address first) to avoid lock ordering deadlocks.
2286+
2287+
In the free-threaded build, this macro expands to::
2288+
2289+
{
2290+
PyCriticalSection2 _py_cs2;
2291+
PyCriticalSection_Begin2(&_py_cs2, (PyObject*)(a), (PyObject*)(b))
2292+
2293+
In the default build, this macro expands to ``{``.
2294+
2295+
.. versionadded:: 3.13
2296+
2297+
.. c:macro:: Py_END_CRITICAL_SECTION2()
2298+
2299+
Ends the critical section and releases the per-object locks.
2300+
2301+
In the free-threaded build, this macro expands to::
2302+
2303+
PyCriticalSection_End2(&_py_cs2);
2304+
}
2305+
2306+
In the default build, this macro expands to ``}``.
2307+
2308+
.. versionadded:: 3.13

Doc/c-api/typeobj.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1328,8 +1328,8 @@ and :c:data:`PyType_Type` effectively act as defaults.)
13281328
To indicate that a class has changed call :c:func:`PyType_Modified`
13291329

13301330
.. warning::
1331-
This flag is present in header files, but is an internal feature and should
1332-
not be used. It will be removed in a future version of CPython
1331+
This flag is present in header files, but is not be used.
1332+
It will be removed in a future version of CPython
13331333

13341334

13351335
.. c:member:: const char* PyTypeObject.tp_doc

0 commit comments

Comments
 (0)