Skip to content

Commit 4a9cdb2

Browse files
generatedunixname1734921407115435meta-codesync[bot]
authored andcommitted
Sync pre-release CPython main branch from GitHub (2026-09-02)
Summary: Imported python/cpython `3.16.0a0` from upstream rev [`3daa7f8`](https://www.github.com/python/cpython/commit/3daa7f8258fa21931e7655de66160b05afcfc8c9) (committed 2026-09-02 00:32:39+00:00). # Commit Info - Base: (`3.16.0a0`) - [`b38e073`](https://www.github.com/python/cpython/commit/b38e073f1be8fe40af991c044a791cefff098f0d) (commit date: 2026-08-31 21:54:35+00:00) - Imported: (`3.16.0a0`) - [`3daa7f8`](https://www.github.com/python/cpython/commit/3daa7f8258fa21931e7655de66160b05afcfc8c9) (commit date: 2026-09-02 00:32:39+00:00) # Noteworthy file changes - Native files (1 added): ``` + Include/internal/pycore_iterobject.h ``` - Low-signal files (6 added) (NEWS.d, docs, .github) Complete list of added/removed files: https://www.internalfb.com/intern/everpaste/?color=0&handle=GB8JJi8bv1TBgrUFAB17IwR5y08Hbr0LAAAz Differential Revision: D118409796 fbshipit-source-id: a72069e21a163f7807631119ca3dcc7363811beb
1 parent 5c6735c commit 4a9cdb2

49 files changed

Lines changed: 1622 additions & 133 deletions

Some content is hidden

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

Doc/library/concurrent.interpreters.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ objects are either directly shared or copied efficiently. For example:
191191
* :class:`float`
192192
* :class:`tuple` (of similarly supported objects)
193193

194-
There is a small number of Python types that actually share mutable
194+
There are a small number of Python types that actually share mutable
195195
data between interpreters:
196196

197197
* :class:`memoryview`
@@ -274,7 +274,7 @@ Interpreter objects
274274

275275
.. method:: call(callable, /, *args, **kwargs)
276276

277-
Return the result of calling running the given function in the
277+
Return the result of running the given function in the
278278
interpreter (in the current thread).
279279

280280
.. _interp-call-in-thread:

Doc/library/functions.rst

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,54 @@ are always available. They are listed here in alphabetical order.
6565

6666

6767
.. function:: aiter(async_iterable, /)
68+
aiter(callable, /, stop_value, *, stop_exception=StopAsyncIteration)
69+
aiter(callable, /, *, stop_exception)
70+
71+
Return an :term:`asynchronous iterator` object.
72+
The first argument is interpreted very differently
73+
depending on the presence of the other arguments.
74+
Without other arguments,
75+
the single argument must be an :term:`asynchronous iterable`,
76+
and the result is equivalent to calling ``x.__aiter__()``.
77+
78+
If *stop_value* or *stop_exception* is given,
79+
then the first argument must be a callable object.
80+
The asynchronous iterator created in this case
81+
calls *callable* with no arguments and awaits the result
82+
for each call to its :meth:`~object.__anext__` method;
83+
if the awaited value is equal to *stop_value*,
84+
or if the call raises an exception matching *stop_exception*,
85+
:exc:`StopAsyncIteration` will be raised,
86+
otherwise the value will be returned.
87+
The callable is only called when the result of :meth:`~object.__anext__`
88+
is awaited.
89+
90+
*stop_exception* is an exception class or a tuple of exception classes.
91+
If *stop_value* is not specified,
92+
the iteration stops only when the callable raises an exception.
93+
If the callable raises :exc:`StopAsyncIteration`
94+
which does not match *stop_exception*,
95+
it is replaced with a :exc:`RuntimeError`,
96+
as for asynchronous generators (see :pep:`525`).
97+
98+
For example, reading fixed-size chunks from an asynchronous stream
99+
until the end of file is reached::
68100

69-
Return an :term:`asynchronous iterator` for an :term:`asynchronous iterable`.
70-
Equivalent to calling ``x.__aiter__()``.
101+
from functools import partial
102+
async for chunk in aiter(partial(reader.read, 1024), b''):
103+
process_chunk(chunk)
104+
105+
Or consuming an :class:`asyncio.Queue` until it is shut down::
71106

72-
Note: Unlike :func:`iter`, :func:`aiter` has no 2-argument variant.
107+
from asyncio import QueueShutDown
108+
async for item in aiter(queue.get, stop_exception=QueueShutDown):
109+
process_item(item)
73110

74111
.. versionadded:: 3.10
75112

113+
.. versionchanged:: next
114+
Added the *stop_value* and *stop_exception* parameters.
115+
76116
.. function:: all(iterable, /)
77117

78118
Return ``True`` if all elements of the *iterable* are true (or if the iterable
@@ -1143,22 +1183,34 @@ are always available. They are listed here in alphabetical order.
11431183

11441184

11451185
.. function:: iter(iterable, /)
1146-
iter(callable, sentinel, /)
1186+
iter(callable, /, stop_value, *, stop_exception=StopIteration)
1187+
iter(callable, /, *, stop_exception)
11471188
11481189
Return an :term:`iterator` object. The first argument is interpreted very
1149-
differently depending on the presence of the second argument. Without a
1150-
second argument, the single argument must be a collection object which supports the
1190+
differently depending on the presence of the other arguments. Without other
1191+
arguments, the single argument must be a collection object which supports the
11511192
:term:`iterable` protocol (the :meth:`~object.__iter__` method),
11521193
or it must support
11531194
the sequence protocol (the :meth:`~object.__getitem__` method with integer arguments
11541195
starting at ``0``). If it does not support either of those protocols,
1155-
:exc:`TypeError` is raised. If the second argument, *sentinel*, is given,
1196+
:exc:`TypeError` is raised.
1197+
1198+
If *stop_value* or *stop_exception* is given,
11561199
then the first argument must be a callable object. The iterator created in this case
11571200
will call *callable* with no arguments for each call to its
11581201
:meth:`~iterator.__next__` method; if the value returned is equal to
1159-
*sentinel*, :exc:`StopIteration` will be raised, otherwise the value will
1202+
*stop_value*, or if the call raises an exception matching *stop_exception*,
1203+
:exc:`StopIteration` will be raised, otherwise the value will
11601204
be returned.
11611205

1206+
*stop_exception* is an exception class or a tuple of exception classes.
1207+
If *stop_value* is not specified,
1208+
the iteration stops only when the callable raises an exception.
1209+
If the callable raises :exc:`StopIteration`
1210+
which does not match *stop_exception*,
1211+
it is replaced with a :exc:`RuntimeError`,
1212+
as for generators (see :pep:`479`).
1213+
11621214
See also :ref:`typeiter`.
11631215

11641216
One useful application of the second form of :func:`iter` is to build a
@@ -1170,6 +1222,19 @@ are always available. They are listed here in alphabetical order.
11701222
for block in iter(partial(f.read, 64), b''):
11711223
process_block(block)
11721224

1225+
*stop_exception* is useful for callables
1226+
which report exhaustion by raising an exception
1227+
instead of returning a special value.
1228+
For example, draining a queue::
1229+
1230+
import queue
1231+
for item in iter(input_queue.get_nowait, stop_exception=queue.Empty):
1232+
process_item(item)
1233+
1234+
.. versionchanged:: next
1235+
Added the *stop_exception* parameter
1236+
and allowed passing *stop_value* by keyword.
1237+
11731238

11741239
.. function:: len(object, /)
11751240

Doc/library/xml.etree.elementtree.rst

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -711,16 +711,16 @@ Functions
711711

712712
.. function:: tostring(element, encoding="us-ascii", method="xml", *, \
713713
xml_declaration=None, default_namespace=None, \
714-
short_empty_elements=True)
714+
short_empty_elements=True, standalone=None)
715715

716716
Generates a string representation of an XML element, including all
717717
subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is
718718
the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to
719719
generate a Unicode string (otherwise, a bytestring is generated). *method*
720720
is either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``).
721-
*xml_declaration*, *default_namespace* and *short_empty_elements* has the same
722-
meaning as in :meth:`ElementTree.write`. Returns an (optionally) encoded string
723-
containing the XML data.
721+
*xml_declaration*, *default_namespace*, *short_empty_elements* and
722+
*standalone* has the same meaning as in :meth:`ElementTree.write`.
723+
Returns an (optionally) encoded string containing the XML data.
724724

725725
.. versionchanged:: 3.4
726726
Added the *short_empty_elements* parameter.
@@ -732,19 +732,23 @@ Functions
732732
The :func:`tostring` function now preserves the attribute order
733733
specified by the user.
734734

735+
.. versionchanged:: next
736+
Added the *standalone* parameter.
737+
735738

736739
.. function:: tostringlist(element, encoding="us-ascii", method="xml", *, \
737740
xml_declaration=None, default_namespace=None, \
738-
short_empty_elements=True)
741+
short_empty_elements=True, standalone=None)
739742

740743
Generates a string representation of an XML element, including all
741744
subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is
742745
the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to
743746
generate a Unicode string (otherwise, a bytestring is generated). *method*
744747
is either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``).
745-
*xml_declaration*, *default_namespace* and *short_empty_elements* has the same
746-
meaning as in :meth:`ElementTree.write`. Returns a list of (optionally) encoded
747-
strings containing the XML data. It does not guarantee any specific sequence,
748+
*xml_declaration*, *default_namespace*, *short_empty_elements* and
749+
*standalone* has the same meaning as in :meth:`ElementTree.write`.
750+
Returns a list of (optionally) encoded strings containing the XML data.
751+
It does not guarantee any specific sequence,
748752
except that ``b"".join(tostringlist(element)) == tostring(element)``.
749753

750754
.. versionadded:: 3.2
@@ -759,6 +763,9 @@ Functions
759763
The :func:`tostringlist` function now preserves the attribute order
760764
specified by the user.
761765

766+
.. versionchanged:: next
767+
Added the *standalone* parameter.
768+
762769

763770
.. function:: XML(text, parser=None)
764771

@@ -1186,7 +1193,7 @@ ElementTree Objects
11861193

11871194
.. method:: write(file, encoding="us-ascii", xml_declaration=None, \
11881195
default_namespace=None, method="xml", *, \
1189-
short_empty_elements=True)
1196+
short_empty_elements=True, standalone=None)
11901197
11911198
Writes the element tree to a file, as XML. *file* is a file name, or a
11921199
:term:`file object` opened for writing. *encoding* [1]_ is the output
@@ -1202,6 +1209,13 @@ ElementTree Objects
12021209
emitted as a single self-closed tag, otherwise they are emitted as a pair
12031210
of start/end tags.
12041211

1212+
The keyword-only *standalone* parameter is the value of the standalone
1213+
document declaration in the XML declaration.
1214+
Use ``True`` for ``standalone="yes"``, ``False`` for ``standalone="no"``,
1215+
and ``None`` (the default) to omit it.
1216+
An XML declaration is written if *standalone* is not ``None``;
1217+
combining it with ``xml_declaration=False`` raises a :exc:`ValueError`.
1218+
12051219
The output is either a string (:class:`str`) or binary (:class:`bytes`).
12061220
This is controlled by the *encoding* argument. If *encoding* is
12071221
``"unicode"``, the output is a string; otherwise, it's binary. Note that
@@ -1216,6 +1230,9 @@ ElementTree Objects
12161230
The :meth:`write` method now preserves the attribute order specified
12171231
by the user.
12181232

1233+
.. versionchanged:: next
1234+
Added the *standalone* parameter.
1235+
12191236

12201237
This is the XML file that is going to be manipulated::
12211238

Doc/whatsnew/3.16.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ New features
7575
Other language changes
7676
======================
7777

78+
* The :func:`iter` function now accepts the *stop_exception* parameter.
79+
The created iterator stops when the callable raises the specified exception.
80+
The second parameter is now named *stop_value* and can be passed by keyword.
81+
:func:`aiter` now accepts the same *stop_value* and *stop_exception*
82+
parameters, calling an asynchronous callable and awaiting the result.
83+
(Contributed by Serhiy Storchaka in :gh:`64862`.)
84+
7885
* :meth:`memoryview.cast` now allows casting a multidimensional
7986
F-contiguous view to a one-dimensional view.
8087
(Contributed by Jaemin Park in :gh:`91484`.)

Include/internal/pycore_genobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
2929

3030
// Export for '_asyncio' shared extension
3131
PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
32+
// Set the exception passed to throw(typ[, val[, tb]]).
33+
// Return 0 on success, -1 on failure.
34+
extern int _PyGen_SetException(PyObject *typ, PyObject *val, PyObject *tb);
3235

3336
PyAPI_FUNC(PyObject *)_PyCoro_GetAwaitableIter(PyObject *o);
3437
PyAPI_FUNC(PyObject *)_PyAsyncGenValueWrapperNew(PyThreadState *state, PyObject *);

Include/internal/pycore_global_objects_fini_generated.h

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

Include/internal/pycore_global_strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,8 @@ struct _Py_global_strings {
831831
STRUCT_FOR_ID(stdout)
832832
STRUCT_FOR_ID(step)
833833
STRUCT_FOR_ID(steps)
834+
STRUCT_FOR_ID(stop_exception)
835+
STRUCT_FOR_ID(stop_value)
834836
STRUCT_FOR_ID(store_name)
835837
STRUCT_FOR_ID(strategy)
836838
STRUCT_FOR_ID(strftime)

Include/internal/pycore_interp_structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ struct _py_func_state {
538538
If you add a new static type to the standard library, you may have to
539539
update one of these numbers.
540540
*/
541-
#define _Py_NUM_MANAGED_PREINITIALIZED_TYPES 120
541+
#define _Py_NUM_MANAGED_PREINITIALIZED_TYPES 122
542542
#define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES \
543543
(_Py_NUM_MANAGED_PREINITIALIZED_TYPES + 83)
544544
#define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef Py_INTERNAL_ITEROBJECT_H
2+
#define Py_INTERNAL_ITEROBJECT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
extern PyTypeObject _PyACallIter_Type;
12+
extern PyTypeObject _PyACallIterAwaitable_Type;
13+
14+
// Like PyCallIter_New(), but the iteration also stops when *callable* raises
15+
// an exception matching *stop_exc* (an exception class or a tuple of exception
16+
// classes). *sentinel* can be NULL; NULL *stop_exc* means StopIteration.
17+
extern PyObject *_PyCallIter_NewEx(PyObject *callable, PyObject *sentinel,
18+
PyObject *stop_exc);
19+
20+
// The asynchronous counterpart of _PyCallIter_NewEx(): the result of
21+
// *callable* is awaited, and NULL *stop_exc* means StopAsyncIteration.
22+
extern PyObject *_PyACallIter_New(PyObject *callable, PyObject *sentinel,
23+
PyObject *stop_exc);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
#endif /* !Py_INTERNAL_ITEROBJECT_H */

Include/internal/pycore_runtime_init_generated.h

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

0 commit comments

Comments
 (0)