Skip to content

Commit 7be0dab

Browse files
authored
Merge branch 'main' into 81719-zipfile-refactor-for-subclass
2 parents 7ac534a + d7e12a3 commit 7be0dab

File tree

82 files changed

+1725
-371
lines changed

Some content is hidden

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

82 files changed

+1725
-371
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,12 @@ Opening network connections
611611
to bind the socket locally. The *local_host* and *local_port*
612612
are looked up using :meth:`getaddrinfo`.
613613

614+
.. note::
615+
616+
On Windows, when using the proactor event loop with ``local_addr=None``,
617+
an :exc:`OSError` with :attr:`!errno.WSAEINVAL` will be raised
618+
when running it.
619+
614620
* *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
615621
to connect the socket to a remote address. The *remote_host* and
616622
*remote_port* are looked up using :meth:`getaddrinfo`.

Doc/library/asyncio-queue.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ Queue
120120
raise :exc:`QueueShutDown`.
121121

122122
If *immediate* is true, the queue is terminated immediately.
123-
The queue is drained to be completely empty. All callers of
124-
:meth:`~Queue.join` are unblocked regardless of the number
125-
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
123+
The queue is drained to be completely empty and the count
124+
of unfinished tasks is reduced by the number of tasks drained.
125+
If unfinished tasks is zero, callers of :meth:`~Queue.join`
126+
are unblocked. Also, blocked callers of :meth:`~Queue.get`
126127
are unblocked and will raise :exc:`QueueShutDown` because the
127128
queue is empty.
128129

Doc/library/codecs.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,36 @@ to :class:`bytes` mappings. They are not supported by :meth:`bytes.decode`
14831483
Restoration of the aliases for the binary transforms.
14841484

14851485

1486+
.. _standalone-codec-functions:
1487+
1488+
Standalone Codec Functions
1489+
^^^^^^^^^^^^^^^^^^^^^^^^^^
1490+
1491+
The following functions provide encoding and decoding functionality similar to codecs,
1492+
but are not available as named codecs through :func:`codecs.encode` or :func:`codecs.decode`.
1493+
They are used internally (for example, by :mod:`pickle`) and behave similarly to the
1494+
``string_escape`` codec that was removed in Python 3.
1495+
1496+
.. function:: codecs.escape_encode(input, errors=None)
1497+
1498+
Encode *input* using escape sequences. Similar to how :func:`repr` on bytes
1499+
produces escaped byte values.
1500+
1501+
*input* must be a :class:`bytes` object.
1502+
1503+
Returns a tuple ``(output, length)`` where *output* is a :class:`bytes`
1504+
object and *length* is the number of bytes consumed.
1505+
1506+
.. function:: codecs.escape_decode(input, errors=None)
1507+
1508+
Decode *input* from escape sequences back to the original bytes.
1509+
1510+
*input* must be a :term:`bytes-like object`.
1511+
1512+
Returns a tuple ``(output, length)`` where *output* is a :class:`bytes`
1513+
object and *length* is the number of bytes consumed.
1514+
1515+
14861516
.. _text-transforms:
14871517

14881518
Text Transforms

Doc/library/concurrent.futures.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ that :class:`ProcessPoolExecutor` will not work in the interactive interpreter.
342342
Calling :class:`Executor` or :class:`Future` methods from a callable submitted
343343
to a :class:`ProcessPoolExecutor` will result in deadlock.
344344

345+
Note that the restrictions on functions and arguments needing to picklable as
346+
per :class:`multiprocessing.Process` apply when using :meth:`~Executor.submit`
347+
and :meth:`~Executor.map` on a :class:`ProcessPoolExecutor`. A function defined
348+
in a REPL or a lambda should not be expected to work.
349+
345350
.. class:: ProcessPoolExecutor(max_workers=None, mp_context=None, initializer=None, initargs=(), max_tasks_per_child=None)
346351

347352
An :class:`Executor` subclass that executes calls asynchronously using a pool

Doc/library/concurrent.interpreters.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ makes them similar to processes, but they still enjoy in-process
134134
efficiency, like threads.
135135

136136
All that said, interpreters do naturally support certain flavors of
137-
concurrency, as a powerful side effect of that isolation.
137+
concurrency.
138138
There's a powerful side effect of that isolation. It enables a
139139
different approach to concurrency than you can take with async or
140140
threads. It's a similar concurrency model to CSP or the actor model,

Doc/library/http.cookies.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ Morsel Objects
148148
in HTTP requests, and is not accessible through JavaScript. This is intended
149149
to mitigate some forms of cross-site scripting.
150150

151-
The attribute :attr:`samesite` specifies that the browser is not allowed to
152-
send the cookie along with cross-site requests. This helps to mitigate CSRF
153-
attacks. Valid values for this attribute are "Strict" and "Lax".
151+
The attribute :attr:`samesite` controls when the browser sends the cookie with
152+
cross-site requests. This helps to mitigate CSRF attacks. Valid values are
153+
"Strict" (only sent with same-site requests), "Lax" (sent with same-site
154+
requests and top-level navigations), and "None" (sent with same-site and
155+
cross-site requests). When using "None", the "secure" attribute must also
156+
be set, as required by modern browsers.
154157

155158
The attribute :attr:`partitioned` indicates to user agents that these
156159
cross-site cookies *should* only be available in the same top-level context

Doc/library/multiprocessing.rst

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ To show the individual process IDs involved, here is an expanded example::
9797
For an explanation of why the ``if __name__ == '__main__'`` part is
9898
necessary, see :ref:`multiprocessing-programming`.
9999

100+
The arguments to :class:`Process` usually need to be unpickleable from within
101+
the child process. If you tried typing the above example directly into a REPL it
102+
could lead to an :exc:`AttributeError` in the child process trying to locate the
103+
*f* function in the ``__main__`` module.
100104

101105

102106
.. _multiprocessing-start-methods:
@@ -233,9 +237,12 @@ processes for a different context. In particular, locks created using
233237
the *fork* context cannot be passed to processes started using the
234238
*spawn* or *forkserver* start methods.
235239

236-
A library which wants to use a particular start method should probably
237-
use :func:`get_context` to avoid interfering with the choice of the
238-
library user.
240+
Libraries using :mod:`multiprocessing` or
241+
:class:`~concurrent.futures.ProcessPoolExecutor` should be designed to allow
242+
their users to provide their own multiprocessing context. Using a specific
243+
context of your own within a library can lead to incompatibilities with the
244+
rest of the library user's application. Always document if your library
245+
requires a specific start method.
239246

240247
.. warning::
241248

@@ -538,9 +545,42 @@ The :mod:`multiprocessing` package mostly replicates the API of the
538545
to pass to *target*.
539546

540547
If a subclass overrides the constructor, it must make sure it invokes the
541-
base class constructor (:meth:`Process.__init__`) before doing anything else
548+
base class constructor (``super().__init__()``) before doing anything else
542549
to the process.
543550

551+
.. note::
552+
553+
In general, all arguments to :class:`Process` must be picklable. This is
554+
frequently observed when trying to create a :class:`Process` or use a
555+
:class:`concurrent.futures.ProcessPoolExecutor` from a REPL with a
556+
locally defined *target* function.
557+
558+
Passing a callable object defined in the current REPL session causes the
559+
child process to die via an uncaught :exc:`AttributeError` exception when
560+
starting as *target* must have been defined within an importable module
561+
in order to be loaded during unpickling.
562+
563+
Example of this uncatchable error from the child::
564+
565+
>>> import multiprocessing as mp
566+
>>> def knigit():
567+
... print("Ni!")
568+
...
569+
>>> process = mp.Process(target=knigit)
570+
>>> process.start()
571+
>>> Traceback (most recent call last):
572+
File ".../multiprocessing/spawn.py", line ..., in spawn_main
573+
File ".../multiprocessing/spawn.py", line ..., in _main
574+
AttributeError: module '__main__' has no attribute 'knigit'
575+
>>> process
576+
<SpawnProcess name='SpawnProcess-1' pid=379473 parent=378707 stopped exitcode=1>
577+
578+
See :ref:`multiprocessing-programming-spawn`. While this restriction is
579+
not true if using the ``"fork"`` start method, as of Python ``3.14`` that
580+
is no longer the default on any platform. See
581+
:ref:`multiprocessing-start-methods`.
582+
See also :gh:`132898`.
583+
544584
.. versionchanged:: 3.3
545585
Added the *daemon* parameter.
546586

@@ -3070,10 +3110,10 @@ start method.
30703110

30713111
More picklability
30723112

3073-
Ensure that all arguments to :meth:`Process.__init__` are picklable.
3074-
Also, if you subclass :class:`~multiprocessing.Process` then make sure that
3075-
instances will be picklable when the :meth:`Process.start
3076-
<multiprocessing.Process.start>` method is called.
3113+
Ensure that all arguments to :class:`~multiprocessing.Process` are
3114+
picklable. Also, if you subclass ``Process.__init__``, you must make sure
3115+
that instances will be picklable when the
3116+
:meth:`Process.start <multiprocessing.Process.start>` method is called.
30773117

30783118
Global variables
30793119

Doc/library/queue.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ until empty or terminated immediately with a hard shutdown.
256256
raise :exc:`ShutDown`.
257257

258258
If *immediate* is true, the queue is terminated immediately.
259-
The queue is drained to be completely empty. All callers of
260-
:meth:`~Queue.join` are unblocked regardless of the number
261-
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
259+
The queue is drained to be completely empty and the count
260+
of unfinished tasks is reduced by the number of tasks drained.
261+
If unfinished tasks is zero, callers of :meth:`~Queue.join`
262+
are unblocked. Also, blocked callers of :meth:`~Queue.get`
262263
are unblocked and will raise :exc:`ShutDown` because the
263264
queue is empty.
264265

Doc/library/ssl.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,13 @@ SSL sockets also have the following additional methods and attributes:
12901290

12911291
.. versionadded:: 3.5
12921292

1293+
.. method:: SSLSocket.group()
1294+
1295+
Return the group used for doing key agreement on this connection. If no
1296+
connection has been established, returns ``None``.
1297+
1298+
.. versionadded:: next
1299+
12931300
.. method:: SSLSocket.compression()
12941301

12951302
Return the compression algorithm being used as a string, or ``None``
@@ -1647,6 +1654,25 @@ to speed up repeated connections from the same clients.
16471654

16481655
.. versionadded:: 3.6
16491656

1657+
.. method:: SSLContext.get_groups(*, include_aliases=False)
1658+
1659+
Get a list of groups implemented for key agreement, taking into
1660+
account the current TLS :attr:`~SSLContext.minimum_version` and
1661+
:attr:`~SSLContext.maximum_version` values. For example::
1662+
1663+
>>> ctx = ssl.create_default_context()
1664+
>>> ctx.minimum_version = ssl.TLSVersion.TLSv1_3
1665+
>>> ctx.maximum_version = ssl.TLSVersion.TLSv1_3
1666+
>>> ctx.get_groups() # doctest: +SKIP
1667+
['secp256r1', 'secp384r1', 'secp521r1', 'x25519', 'x448', ...]
1668+
1669+
By default, this method returns only the preferred IANA names for the
1670+
available groups. However, if the ``include_aliases`` parameter is set to
1671+
:const:`True` this method will also return any associated aliases such as
1672+
the ECDH curve names supported in older versions of OpenSSL.
1673+
1674+
.. versionadded:: next
1675+
16501676
.. method:: SSLContext.set_default_verify_paths()
16511677

16521678
Load a set of default "certification authority" (CA) certificates from
@@ -1672,6 +1698,19 @@ to speed up repeated connections from the same clients.
16721698
TLS 1.3 cipher suites cannot be disabled with
16731699
:meth:`~SSLContext.set_ciphers`.
16741700

1701+
.. method:: SSLContext.set_groups(groups)
1702+
1703+
Set the groups allowed for key agreement for sockets created with this
1704+
context. It should be a string in the `OpenSSL group list format
1705+
<https://docs.openssl.org/master/man3/SSL_CTX_set1_groups_list/>`_.
1706+
1707+
.. note::
1708+
1709+
When connected, the :meth:`SSLSocket.group` method of SSL sockets will
1710+
return the group used for key agreement on that connection.
1711+
1712+
.. versionadded:: next
1713+
16751714
.. method:: SSLContext.set_alpn_protocols(protocols)
16761715

16771716
Specify which protocols the socket should advertise during the SSL/TLS

Doc/reference/compound_stmts.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,9 @@ is equivalent to ::
14211421
class Foo(object):
14221422
pass
14231423

1424+
There may be one or more base classes; see :ref:`multiple-inheritance` below for more
1425+
information.
1426+
14241427
The class's suite is then executed in a new execution frame (see :ref:`naming`),
14251428
using a newly created local namespace and the original global namespace.
14261429
(Usually, the suite contains mostly function definitions.) When the class's
@@ -1490,6 +1493,119 @@ can be used to create instance variables with different implementation details.
14901493
were introduced in :pep:`318`.
14911494

14921495

1496+
.. _multiple-inheritance:
1497+
1498+
Multiple inheritance
1499+
--------------------
1500+
1501+
Python classes may have multiple base classes, a technique known as
1502+
*multiple inheritance*. The base classes are specified in the class definition
1503+
by listing them in parentheses after the class name, separated by commas.
1504+
For example, the following class definition:
1505+
1506+
.. doctest::
1507+
1508+
>>> class A: pass
1509+
>>> class B: pass
1510+
>>> class C(A, B): pass
1511+
1512+
defines a class ``C`` that inherits from classes ``A`` and ``B``.
1513+
1514+
The :term:`method resolution order` (MRO) is the order in which base classes are
1515+
searched when looking up an attribute on a class. See :ref:`python_2.3_mro` for a
1516+
description of how Python determines the MRO for a class.
1517+
1518+
Multiple inheritance is not always allowed. Attempting to define a class with multiple
1519+
inheritance will raise an error if one of the bases does not allow subclassing, if a consistent MRO
1520+
cannot be created, if no valid metaclass can be determined, or if there is an instance
1521+
layout conflict. We'll discuss each of these in turn.
1522+
1523+
First, all base classes must allow subclassing. While most classes allow subclassing,
1524+
some built-in classes do not, such as :class:`bool`:
1525+
1526+
.. doctest::
1527+
1528+
>>> class SubBool(bool): # TypeError
1529+
... pass
1530+
Traceback (most recent call last):
1531+
...
1532+
TypeError: type 'bool' is not an acceptable base type
1533+
1534+
In the resolved MRO of a class, the class's bases appear in the order they were
1535+
specified in the class's bases list. Additionally, the MRO always lists a child
1536+
class before any of its bases. A class definition will fail if it is impossible to
1537+
resolve a consistent MRO that satisfies these rules from the list of bases provided:
1538+
1539+
.. doctest::
1540+
1541+
>>> class Base: pass
1542+
>>> class Child(Base): pass
1543+
>>> class Grandchild(Base, Child): pass # TypeError
1544+
Traceback (most recent call last):
1545+
...
1546+
TypeError: Cannot create a consistent method resolution order (MRO) for bases Base, Child
1547+
1548+
In the MRO of ``Grandchild``, ``Base`` must appear before ``Child`` because it is first
1549+
in the base class list, but it must also appear after ``Child`` because it is a parent of
1550+
``Child``. This is a contradiction, so the class cannot be defined.
1551+
1552+
If some of the bases have a custom :term:`metaclass`, the metaclass of the resulting class
1553+
is chosen among the metaclasses of the bases and the explicitly specified metaclass of the
1554+
child class. It must be a metaclass that is a subclass of
1555+
all other candidate metaclasses. If no such metaclass exists among the candidates,
1556+
the class cannot be created, as explained in :ref:`metaclass-determination`.
1557+
1558+
Finally, the instance layouts of the bases must be compatible. This means that it must be
1559+
possible to compute a *solid base* for the class. Exactly which classes are solid bases
1560+
depends on the Python implementation.
1561+
1562+
.. impl-detail::
1563+
1564+
In CPython, a class is a solid base if it has a
1565+
nonempty :attr:`~object.__slots__` definition.
1566+
Many but not all classes defined in C are also solid bases, including most
1567+
builtins (such as :class:`int` or :class:`BaseException`)
1568+
but excluding most concrete :class:`Exception` classes. Generally, a C class
1569+
is a solid base if its underlying struct is different in size from its base class.
1570+
1571+
Every class has a solid base. :class:`object`, the base class, has itself as its solid base.
1572+
If there is a single base, the child class's solid base is that class if it is a solid base,
1573+
or else the base class's solid base. If there are multiple bases, we first find the solid base
1574+
for each base class to produce a list of candidate solid bases. If there is a unique solid base
1575+
that is a subclass of all others, then that class is the solid base. Otherwise, class creation
1576+
fails.
1577+
1578+
Example:
1579+
1580+
.. doctest::
1581+
1582+
>>> class Solid1:
1583+
... __slots__ = ("solid1",)
1584+
>>>
1585+
>>> class Solid2:
1586+
... __slots__ = ("solid2",)
1587+
>>>
1588+
>>> class SolidChild(Solid1):
1589+
... __slots__ = ("solid_child",)
1590+
>>>
1591+
>>> class C1: # solid base is `object`
1592+
... pass
1593+
>>>
1594+
>>> # OK: solid bases are `Solid1` and `object`, and `Solid1` is a subclass of `object`.
1595+
>>> class C2(Solid1, C1): # solid base is `Solid1`
1596+
... pass
1597+
>>>
1598+
>>> # OK: solid bases are `SolidChild` and `Solid1`, and `SolidChild` is a subclass of `Solid1`.
1599+
>>> class C3(SolidChild, Solid1): # solid base is `SolidChild`
1600+
... pass
1601+
>>>
1602+
>>> # Error: solid bases are `Solid1` and `Solid2`, but neither is a subclass of the other.
1603+
>>> class C4(Solid1, Solid2): # error: no single solid base
1604+
... pass
1605+
Traceback (most recent call last):
1606+
...
1607+
TypeError: multiple bases have instance lay-out conflict
1608+
14931609
.. _async:
14941610

14951611
Coroutines

0 commit comments

Comments
 (0)