Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/library/concurrent.interpreters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ objects are either directly shared or copied efficiently. For example:
* :class:`float`
* :class:`tuple` (of similarly supported objects)

There is a small number of Python types that actually share mutable
There are a small number of Python types that actually share mutable
data between interpreters:

* :class:`memoryview`
Expand Down Expand Up @@ -274,7 +274,7 @@ Interpreter objects

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

Return the result of calling running the given function in the
Return the result of running the given function in the
interpreter (in the current thread).

.. _interp-call-in-thread:
Expand Down
81 changes: 73 additions & 8 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,54 @@ are always available. They are listed here in alphabetical order.


.. function:: aiter(async_iterable, /)
aiter(callable, /, stop_value, *, stop_exception=StopAsyncIteration)
aiter(callable, /, *, stop_exception)

Return an :term:`asynchronous iterator` object.
The first argument is interpreted very differently
depending on the presence of the other arguments.
Without other arguments,
the single argument must be an :term:`asynchronous iterable`,
and the result is equivalent to calling ``x.__aiter__()``.

If *stop_value* or *stop_exception* is given,
then the first argument must be a callable object.
The asynchronous iterator created in this case
calls *callable* with no arguments and awaits the result
for each call to its :meth:`~object.__anext__` method;
if the awaited value is equal to *stop_value*,
or if the call raises an exception matching *stop_exception*,
:exc:`StopAsyncIteration` will be raised,
otherwise the value will be returned.
The callable is only called when the result of :meth:`~object.__anext__`
is awaited.

*stop_exception* is an exception class or a tuple of exception classes.
If *stop_value* is not specified,
the iteration stops only when the callable raises an exception.
If the callable raises :exc:`StopAsyncIteration`
which does not match *stop_exception*,
it is replaced with a :exc:`RuntimeError`,
as for asynchronous generators (see :pep:`525`).

For example, reading fixed-size chunks from an asynchronous stream
until the end of file is reached::

Return an :term:`asynchronous iterator` for an :term:`asynchronous iterable`.
Equivalent to calling ``x.__aiter__()``.
from functools import partial
async for chunk in aiter(partial(reader.read, 1024), b''):
process_chunk(chunk)

Or consuming an :class:`asyncio.Queue` until it is shut down::

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

.. versionadded:: 3.10

.. versionchanged:: next
Added the *stop_value* and *stop_exception* parameters.

.. function:: all(iterable, /)

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


.. function:: iter(iterable, /)
iter(callable, sentinel, /)
iter(callable, /, stop_value, *, stop_exception=StopIteration)
iter(callable, /, *, stop_exception)

Return an :term:`iterator` object. The first argument is interpreted very
differently depending on the presence of the second argument. Without a
second argument, the single argument must be a collection object which supports the
differently depending on the presence of the other arguments. Without other
arguments, the single argument must be a collection object which supports the
:term:`iterable` protocol (the :meth:`~object.__iter__` method),
or it must support
the sequence protocol (the :meth:`~object.__getitem__` method with integer arguments
starting at ``0``). If it does not support either of those protocols,
:exc:`TypeError` is raised. If the second argument, *sentinel*, is given,
:exc:`TypeError` is raised.

If *stop_value* or *stop_exception* is given,
then the first argument must be a callable object. The iterator created in this case
will call *callable* with no arguments for each call to its
:meth:`~iterator.__next__` method; if the value returned is equal to
*sentinel*, :exc:`StopIteration` will be raised, otherwise the value will
*stop_value*, or if the call raises an exception matching *stop_exception*,
:exc:`StopIteration` will be raised, otherwise the value will
be returned.

*stop_exception* is an exception class or a tuple of exception classes.
If *stop_value* is not specified,
the iteration stops only when the callable raises an exception.
If the callable raises :exc:`StopIteration`
which does not match *stop_exception*,
it is replaced with a :exc:`RuntimeError`,
as for generators (see :pep:`479`).

See also :ref:`typeiter`.

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

*stop_exception* is useful for callables
which report exhaustion by raising an exception
instead of returning a special value.
For example, draining a queue::

import queue
for item in iter(input_queue.get_nowait, stop_exception=queue.Empty):
process_item(item)

.. versionchanged:: next
Added the *stop_exception* parameter
and allowed passing *stop_value* by keyword.


.. function:: len(object, /)

Expand Down
35 changes: 26 additions & 9 deletions Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -711,16 +711,16 @@ Functions

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

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

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

.. versionchanged:: next
Added the *standalone* parameter.


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

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

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

.. versionchanged:: next
Added the *standalone* parameter.


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

Expand Down Expand Up @@ -1186,7 +1193,7 @@ ElementTree Objects

.. method:: write(file, encoding="us-ascii", xml_declaration=None, \
default_namespace=None, method="xml", *, \
short_empty_elements=True)
short_empty_elements=True, standalone=None)

Writes the element tree to a file, as XML. *file* is a file name, or a
:term:`file object` opened for writing. *encoding* [1]_ is the output
Expand All @@ -1202,6 +1209,13 @@ ElementTree Objects
emitted as a single self-closed tag, otherwise they are emitted as a pair
of start/end tags.

The keyword-only *standalone* parameter is the value of the standalone
document declaration in the XML declaration.
Use ``True`` for ``standalone="yes"``, ``False`` for ``standalone="no"``,
and ``None`` (the default) to omit it.
An XML declaration is written if *standalone* is not ``None``;
combining it with ``xml_declaration=False`` raises a :exc:`ValueError`.

The output is either a string (:class:`str`) or binary (:class:`bytes`).
This is controlled by the *encoding* argument. If *encoding* is
``"unicode"``, the output is a string; otherwise, it's binary. Note that
Expand All @@ -1216,6 +1230,9 @@ ElementTree Objects
The :meth:`write` method now preserves the attribute order specified
by the user.

.. versionchanged:: next
Added the *standalone* parameter.


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

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ New features
Other language changes
======================

* The :func:`iter` function now accepts the *stop_exception* parameter.
The created iterator stops when the callable raises the specified exception.
The second parameter is now named *stop_value* and can be passed by keyword.
:func:`aiter` now accepts the same *stop_value* and *stop_exception*
parameters, calling an asynchronous callable and awaiting the result.
(Contributed by Serhiy Storchaka in :gh:`64862`.)

* :meth:`memoryview.cast` now allows casting a multidimensional
F-contiguous view to a one-dimensional view.
(Contributed by Jaemin Park in :gh:`91484`.)
Expand Down
3 changes: 3 additions & 0 deletions Include/internal/pycore_genobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);

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

PyAPI_FUNC(PyObject *)_PyCoro_GetAwaitableIter(PyObject *o);
PyAPI_FUNC(PyObject *)_PyAsyncGenValueWrapperNew(PyThreadState *state, PyObject *);
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ struct _Py_global_strings {
STRUCT_FOR_ID(stdout)
STRUCT_FOR_ID(step)
STRUCT_FOR_ID(steps)
STRUCT_FOR_ID(stop_exception)
STRUCT_FOR_ID(stop_value)
STRUCT_FOR_ID(store_name)
STRUCT_FOR_ID(strategy)
STRUCT_FOR_ID(strftime)
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ struct _py_func_state {
If you add a new static type to the standard library, you may have to
update one of these numbers.
*/
#define _Py_NUM_MANAGED_PREINITIALIZED_TYPES 120
#define _Py_NUM_MANAGED_PREINITIALIZED_TYPES 122
#define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES \
(_Py_NUM_MANAGED_PREINITIALIZED_TYPES + 83)
#define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10
Expand Down
28 changes: 28 additions & 0 deletions Include/internal/pycore_iterobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef Py_INTERNAL_ITEROBJECT_H
#define Py_INTERNAL_ITEROBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

extern PyTypeObject _PyACallIter_Type;
extern PyTypeObject _PyACallIterAwaitable_Type;

// Like PyCallIter_New(), but the iteration also stops when *callable* raises
// an exception matching *stop_exc* (an exception class or a tuple of exception
// classes). *sentinel* can be NULL; NULL *stop_exc* means StopIteration.
extern PyObject *_PyCallIter_NewEx(PyObject *callable, PyObject *sentinel,
PyObject *stop_exc);

// The asynchronous counterpart of _PyCallIter_NewEx(): the result of
// *callable* is awaited, and NULL *stop_exc* means StopAsyncIteration.
extern PyObject *_PyACallIter_New(PyObject *callable, PyObject *sentinel,
PyObject *stop_exc);

#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_ITEROBJECT_H */
2 changes: 2 additions & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading