Skip to content

Commit ce5c60a

Browse files
committed
Merge in the main branch
2 parents 0c99bad + 1e4e59b commit ce5c60a

File tree

80 files changed

+3527
-1232
lines changed

Some content is hidden

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

80 files changed

+3527
-1232
lines changed

.github/workflows/jit.yml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ jobs:
5757
fail-fast: false
5858
matrix:
5959
target:
60-
- i686-pc-windows-msvc/msvc
61-
- x86_64-pc-windows-msvc/msvc
62-
- aarch64-pc-windows-msvc/msvc
60+
# To re-enable later when we support these.
61+
# - i686-pc-windows-msvc/msvc
62+
# - x86_64-pc-windows-msvc/msvc
63+
# - aarch64-pc-windows-msvc/msvc
6364
- x86_64-apple-darwin/clang
6465
- aarch64-apple-darwin/clang
6566
- x86_64-unknown-linux-gnu/gcc
@@ -70,15 +71,16 @@ jobs:
7071
llvm:
7172
- 21
7273
include:
73-
- target: i686-pc-windows-msvc/msvc
74-
architecture: Win32
75-
runner: windows-2022
76-
- target: x86_64-pc-windows-msvc/msvc
77-
architecture: x64
78-
runner: windows-2022
79-
- target: aarch64-pc-windows-msvc/msvc
80-
architecture: ARM64
81-
runner: windows-11-arm
74+
# To re-enable later when we support these.
75+
# - target: i686-pc-windows-msvc/msvc
76+
# architecture: Win32
77+
# runner: windows-2022
78+
# - target: x86_64-pc-windows-msvc/msvc
79+
# architecture: x64
80+
# runner: windows-2022
81+
# - target: aarch64-pc-windows-msvc/msvc
82+
# architecture: ARM64
83+
# runner: windows-11-arm
8284
- target: x86_64-apple-darwin/clang
8385
architecture: x86_64
8486
runner: macos-15-intel

Doc/c-api/code.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ bound into a function.
211211
.. versionadded:: 3.12
212212
213213
214+
.. c:function:: PyObject *PyCode_Optimize(PyObject *code, PyObject *consts, PyObject *names, PyObject *lnotab_obj)
215+
216+
This is a :term:`soft deprecated` function that does nothing.
217+
218+
Prior to Python 3.10, this function would perform basic optimizations to a
219+
code object.
220+
221+
.. versionchanged:: 3.10
222+
This function now does nothing.
223+
224+
214225
.. _c_codeobject_flags:
215226
216227
Code Object Flags

Doc/c-api/dict.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,49 @@ Dictionary Objects
431431
it before returning.
432432
433433
.. versionadded:: 3.12
434+
435+
436+
Dictionary View Objects
437+
^^^^^^^^^^^^^^^^^^^^^^^
438+
439+
.. c:function:: int PyDictViewSet_Check(PyObject *op)
440+
441+
Return true if *op* is a view of a set inside a dictionary. This is currently
442+
equivalent to :c:expr:`PyDictKeys_Check(op) || PyDictItems_Check(op)`. This
443+
function always succeeds.
444+
445+
446+
.. c:var:: PyTypeObject PyDictKeys_Type
447+
448+
Type object for a view of dictionary keys. In Python, this is the type of
449+
the object returned by :meth:`dict.keys`.
450+
451+
452+
.. c:function:: int PyDictKeys_Check(PyObject *op)
453+
454+
Return true if *op* is an instance of a dictionary keys view. This function
455+
always succeeds.
456+
457+
458+
.. c:var:: PyTypeObject PyDictValues_Type
459+
460+
Type object for a view of dictionary values. In Python, this is the type of
461+
the object returned by :meth:`dict.values`.
462+
463+
464+
.. c:function:: int PyDictValues_Check(PyObject *op)
465+
466+
Return true if *op* is an instance of a dictionary values view. This function
467+
always succeeds.
468+
469+
470+
.. c:var:: PyTypeObject PyDictItems_Type
471+
472+
Type object for a view of dictionary items. In Python, this is the type of
473+
the object returned by :meth:`dict.items`.
474+
475+
476+
.. c:function:: int PyDictItems_Check(PyObject *op)
477+
478+
Return true if *op* is an instance of a dictionary items view. This function
479+
always succeeds.

Doc/c-api/exceptions.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ For convenience, some of these functions will always return a
309309
.. versionadded:: 3.4
310310
311311
312+
.. c:function:: void PyErr_RangedSyntaxLocationObject(PyObject *filename, int lineno, int col_offset, int end_lineno, int end_col_offset)
313+
314+
Similar to :c:func:`PyErr_SyntaxLocationObject`, but also sets the
315+
*end_lineno* and *end_col_offset* information for the current exception.
316+
317+
.. versionadded:: 3.10
318+
319+
312320
.. c:function:: void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
313321
314322
Like :c:func:`PyErr_SyntaxLocationObject`, but *filename* is a byte string
@@ -976,6 +984,9 @@ because the :ref:`call protocol <call>` takes care of recursion handling.
976984
be concatenated to the :exc:`RecursionError` message caused by the recursion
977985
depth limit.
978986
987+
.. seealso::
988+
The :c:func:`PyUnstable_ThreadState_SetStackProtection` function.
989+
979990
.. versionchanged:: 3.9
980991
This function is now also available in the :ref:`limited API <limited-c-api>`.
981992

Doc/c-api/import.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,24 @@ Importing Modules
333333
strings instead of Python :class:`str` objects.
334334
335335
.. versionadded:: 3.14
336+
337+
.. c:function:: PyObject* PyImport_CreateModuleFromInitfunc(PyObject *spec, PyObject* (*initfunc)(void))
338+
339+
This function is a building block that enables embedders to implement
340+
the :py:meth:`~importlib.abc.Loader.create_module` step of custom
341+
static extension importers (e.g. of statically-linked extensions).
342+
343+
*spec* must be a :class:`~importlib.machinery.ModuleSpec` object.
344+
345+
*initfunc* must be an :ref:`initialization function <extension-export-hook>`,
346+
the same as for :c:func:`PyImport_AppendInittab`.
347+
348+
On success, create and return a module object.
349+
This module will not be initialized; call :c:func:`!PyModule_Exec`
350+
to initialize it.
351+
(Custom importers should do this in their
352+
:py:meth:`~importlib.abc.Loader.exec_module` method.)
353+
354+
On error, return NULL with an exception set.
355+
356+
.. versionadded:: next

Doc/c-api/init.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,43 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
13661366
.. versionadded:: 3.11
13671367
13681368
1369+
.. c:function:: int PyUnstable_ThreadState_SetStackProtection(PyThreadState *tstate, void *stack_start_addr, size_t stack_size)
1370+
1371+
Set the stack protection start address and stack protection size
1372+
of a Python thread state.
1373+
1374+
On success, return ``0``.
1375+
On failure, set an exception and return ``-1``.
1376+
1377+
CPython implements :ref:`recursion control <recursion>` for C code by raising
1378+
:py:exc:`RecursionError` when it notices that the machine execution stack is close
1379+
to overflow. See for example the :c:func:`Py_EnterRecursiveCall` function.
1380+
For this, it needs to know the location of the current thread's stack, which it
1381+
normally gets from the operating system.
1382+
When the stack is changed, for example using context switching techniques like the
1383+
Boost library's ``boost::context``, you must call
1384+
:c:func:`~PyUnstable_ThreadState_SetStackProtection` to inform CPython of the change.
1385+
1386+
Call :c:func:`~PyUnstable_ThreadState_SetStackProtection` either before
1387+
or after changing the stack.
1388+
Do not call any other Python C API between the call and the stack
1389+
change.
1390+
1391+
See :c:func:`PyUnstable_ThreadState_ResetStackProtection` for undoing this operation.
1392+
1393+
.. versionadded:: next
1394+
1395+
1396+
.. c:function:: void PyUnstable_ThreadState_ResetStackProtection(PyThreadState *tstate)
1397+
1398+
Reset the stack protection start address and stack protection size
1399+
of a Python thread state to the operating system defaults.
1400+
1401+
See :c:func:`PyUnstable_ThreadState_SetStackProtection` for an explanation.
1402+
1403+
.. versionadded:: next
1404+
1405+
13691406
.. c:function:: PyInterpreterState* PyInterpreterState_Get(void)
13701407
13711408
Get the current interpreter.

Doc/library/functools.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ The :mod:`functools` module defines the following functions:
672672
dispatch>` :term:`generic function`.
673673

674674
To define a generic method, decorate it with the ``@singledispatchmethod``
675-
decorator. When defining a function using ``@singledispatchmethod``, note
675+
decorator. When defining a method using ``@singledispatchmethod``, note
676676
that the dispatch happens on the type of the first non-*self* or non-*cls*
677677
argument::
678678

@@ -716,6 +716,9 @@ The :mod:`functools` module defines the following functions:
716716

717717
.. versionadded:: 3.8
718718

719+
.. versionchanged:: next
720+
Added support of non-:term:`descriptor` callables.
721+
719722

720723
.. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
721724

Doc/library/stdtypes.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,6 +3173,30 @@ objects.
31733173

31743174
.. versionadded:: 3.14
31753175

3176+
.. method:: take_bytes(n=None, /)
3177+
3178+
Remove the first *n* bytes from the bytearray and return them as an immutable
3179+
:class:`bytes`.
3180+
By default (if *n* is ``None``), return all bytes and clear the bytearray.
3181+
3182+
If *n* is negative, index from the end and take the first :func:`len`
3183+
plus *n* bytes. If *n* is out of bounds, raise :exc:`IndexError`.
3184+
3185+
Taking less than the full length will leave remaining bytes in the
3186+
:class:`bytearray`, which requires a copy. If the remaining bytes should be
3187+
discarded, use :func:`~bytearray.resize` or :keyword:`del` to truncate
3188+
then :func:`~bytearray.take_bytes` without a size.
3189+
3190+
.. impl-detail::
3191+
3192+
Taking all bytes is a zero-copy operation.
3193+
3194+
.. versionadded:: next
3195+
3196+
See the :ref:`What's New <whatsnew315-bytearray-take-bytes>` entry for
3197+
common code patterns which can be optimized with
3198+
:func:`bytearray.take_bytes`.
3199+
31763200
Since bytearray objects are sequences of integers (akin to a list), for a
31773201
bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be
31783202
a bytearray object of length 1. (This contrasts with text strings, where

Doc/library/xml.etree.elementtree.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ Functions
656656
.. versionchanged:: 3.13
657657
Added the :meth:`!close` method.
658658

659+
.. versionchanged:: next
660+
A :exc:`ResourceWarning` is now emitted if the iterator opened a file
661+
and is not explicitly closed.
662+
659663

660664
.. function:: parse(source, parser=None)
661665

Doc/whatsnew/3.15.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,86 @@ Other language changes
307307
not only integers or floats, although this does not improve precision.
308308
(Contributed by Serhiy Storchaka in :gh:`67795`.)
309309

310+
.. _whatsnew315-bytearray-take-bytes:
311+
312+
* Added :meth:`bytearray.take_bytes(n=None, /) <bytearray.take_bytes>` to take
313+
bytes out of a :class:`bytearray` without copying. This enables optimizing code
314+
which must return :class:`bytes` after working with a mutable buffer of bytes
315+
such as data buffering, network protocol parsing, encoding, decoding,
316+
and compression. Common code patterns which can be optimized with
317+
:func:`~bytearray.take_bytes` are listed below.
318+
319+
(Contributed by Cody Maloney in :gh:`139871`.)
320+
321+
.. list-table:: Suggested Optimizing Refactors
322+
:header-rows: 1
323+
324+
* - Description
325+
- Old
326+
- New
327+
328+
* - Return :class:`bytes` after working with :class:`bytearray`
329+
- .. code:: python
330+
331+
def read() -> bytes:
332+
buffer = bytearray(1024)
333+
...
334+
return bytes(buffer)
335+
336+
- .. code:: python
337+
338+
def read() -> bytes:
339+
buffer = bytearray(1024)
340+
...
341+
return buffer.take_bytes()
342+
343+
* - Empty a buffer getting the bytes
344+
- .. code:: python
345+
346+
buffer = bytearray(1024)
347+
...
348+
data = bytes(buffer)
349+
buffer.clear()
350+
351+
- .. code:: python
352+
353+
buffer = bytearray(1024)
354+
...
355+
data = buffer.take_bytes()
356+
357+
* - Split a buffer at a specific separator
358+
- .. code:: python
359+
360+
buffer = bytearray(b'abc\ndef')
361+
n = buffer.find(b'\n')
362+
data = bytes(buffer[:n + 1])
363+
del buffer[:n + 1]
364+
assert data == b'abc'
365+
assert buffer == bytearray(b'def')
366+
367+
- .. code:: python
368+
369+
buffer = bytearray(b'abc\ndef')
370+
n = buffer.find(b'\n')
371+
data = buffer.take_bytes(n + 1)
372+
373+
* - Split a buffer at a specific separator; discard after the separator
374+
- .. code:: python
375+
376+
buffer = bytearray(b'abc\ndef')
377+
n = buffer.find(b'\n')
378+
data = bytes(buffer[:n])
379+
buffer.clear()
380+
assert data == b'abc'
381+
assert len(buffer) == 0
382+
383+
- .. code:: python
384+
385+
buffer = bytearray(b'abc\ndef')
386+
n = buffer.find(b'\n')
387+
buffer.resize(n)
388+
data = buffer.take_bytes()
389+
310390
* Many functions related to compiling or parsing Python code, such as
311391
:func:`compile`, :func:`ast.parse`, :func:`symtable.symtable`,
312392
and :func:`importlib.abc.InspectLoader.source_to_code`, now allow to pass
@@ -418,6 +498,14 @@ difflib
418498
(Contributed by Jiahao Li in :gh:`134580`.)
419499

420500

501+
functools
502+
---------
503+
504+
* :func:`~functools.singledispatchmethod` now supports non-:term:`descriptor`
505+
callables.
506+
(Contributed by Serhiy Storchaka in :gh:`140873`.)
507+
508+
421509
hashlib
422510
-------
423511

@@ -986,6 +1074,16 @@ New features
9861074
* Add :c:func:`PyTuple_FromArray` to create a :class:`tuple` from an array.
9871075
(Contributed by Victor Stinner in :gh:`111489`.)
9881076

1077+
* Add :c:func:`PyUnstable_ThreadState_SetStackProtection` and
1078+
:c:func:`PyUnstable_ThreadState_ResetStackProtection` functions to set
1079+
the stack protection base address and stack protection size of a Python
1080+
thread state.
1081+
(Contributed by Victor Stinner in :gh:`139653`.)
1082+
1083+
* Add a new :c:func:`PyImport_CreateModuleFromInitfunc` C-API for creating
1084+
a module from a *spec* and *initfunc*.
1085+
(Contributed by Itamar Oren in :gh:`116146`.)
1086+
9891087

9901088
Changed C APIs
9911089
--------------
@@ -1150,3 +1248,9 @@ that may require changes to your code.
11501248

11511249
* :meth:`~mmap.mmap.resize` has been removed on platforms that don't support the
11521250
underlying syscall, instead of raising a :exc:`SystemError`.
1251+
1252+
* Resource warning is now emitted for unclosed
1253+
:func:`xml.etree.ElementTree.iterparse` iterator if it opened a file.
1254+
Use its :meth:`!close` method or the :func:`contextlib.closing` context
1255+
manager to close it.
1256+
(Contributed by Osama Abdelkader and Serhiy Storchaka in :gh:`140601`.)

0 commit comments

Comments
 (0)