Skip to content

Commit e17b229

Browse files
committed
Merge remote-tracking branch 'upstream/main' into defaultdict
2 parents 4536222 + 1cc7551 commit e17b229

Some content is hidden

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

57 files changed

+4419
-4122
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ Tools/jit/ @brandtbucher @savannahostrowski @diegorusso
289289
InternalDocs/jit.md @brandtbucher @savannahostrowski @diegorusso @AA-Turner
290290

291291
# Micro-op / μop / Tier 2 Optimiser
292-
Python/optimizer.c @markshannon
292+
Python/optimizer.c @markshannon @Fidget-Spinner
293293
Python/optimizer_analysis.c @markshannon @tomasr8 @Fidget-Spinner
294294
Python/optimizer_bytecodes.c @markshannon @tomasr8 @Fidget-Spinner
295-
Python/optimizer_symbols.c @markshannon @tomasr8
295+
Python/optimizer_symbols.c @markshannon @tomasr8 @Fidget-Spinner
296296

297297
# Parser, Lexer, and Grammar
298298
Grammar/python.gram @pablogsal @lysnikolaou

Doc/c-api/tuple.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,11 @@ Tuple Objects
148148
Struct Sequence Objects
149149
-----------------------
150150
151-
Struct sequence objects are the C equivalent of :func:`~collections.namedtuple`
152-
objects, i.e. a sequence whose items can also be accessed through attributes.
151+
A struct sequence object is a :term:`named tuple`, that is, a sequence
152+
whose items can also be accessed through attributes.
153+
It is similar to :func:`collections.namedtuple`, but provides a slightly
154+
different interface.
155+
153156
To create a struct sequence, you first have to create a specific struct sequence
154157
type.
155158

Doc/deprecations/pending-removal-in-3.20.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Pending removal in Python 3.20
2323
- :mod:`tabnanny`
2424
- :mod:`tkinter.font`
2525
- :mod:`tkinter.ttk`
26+
- :mod:`wsgiref.simple_server`
2627
- :mod:`zlib`
2728

2829
(Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.)

Doc/glossary.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ Glossary
813813
:func:`itertools.groupby`.
814814

815815
There are several ways to create a key function. For example. the
816-
:meth:`str.lower` method can serve as a key function for case insensitive
816+
:meth:`str.casefold` method can serve as a key function for case insensitive
817817
sorts. Alternatively, a key function can be built from a
818818
:keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also,
819819
:func:`operator.attrgetter`, :func:`operator.itemgetter`, and

Doc/library/decimal.rst

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ The :mod:`decimal` module provides support for fast correctly rounded
3434
decimal floating-point arithmetic. It offers several advantages over the
3535
:class:`float` datatype:
3636

37-
* Decimal "is based on a floating-point model which was designed with people
38-
in mind, and necessarily has a paramount guiding principle -- computers must
39-
provide an arithmetic that works in the same way as the arithmetic that
40-
people learn at school." -- excerpt from the decimal arithmetic specification.
37+
* Decimal "is based on a `floating-point model
38+
<https://speleotrove.com/decimal/damodel.html#refnumber>`__ which was designed
39+
with people in mind, and necessarily has a paramount guiding principle --
40+
computers must provide an arithmetic that works in the same way as the
41+
arithmetic that people learn at school." -- excerpt from the decimal
42+
arithmetic specification.
4143

4244
* Decimal numbers can be represented exactly. In contrast, numbers like
4345
``1.1`` and ``2.2`` do not have exact representations in binary
@@ -238,6 +240,26 @@ floating-point flying circus:
238240
>>> c % a
239241
Decimal('0.77')
240242

243+
Decimals can be formatted (with :func:`format` built-in or :ref:`f-strings`) in
244+
fixed-point or scientific notation, using the same formatting syntax (see
245+
:ref:`formatspec`) as builtin :class:`float` type:
246+
247+
.. doctest::
248+
249+
>>> format(Decimal('2.675'), "f")
250+
'2.675'
251+
>>> format(Decimal('2.675'), ".2f")
252+
'2.68'
253+
>>> f"{Decimal('2.675'):.2f}"
254+
'2.68'
255+
>>> format(Decimal('2.675'), ".2e")
256+
'2.68e+0'
257+
>>> with localcontext() as ctx:
258+
... ctx.rounding = ROUND_DOWN
259+
... print(format(Decimal('2.675'), ".2f"))
260+
...
261+
2.67
262+
241263
And some mathematical functions are also available to Decimal:
242264

243265
>>> getcontext().prec = 28

Doc/library/exceptions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,8 @@ depending on the system error code.
742742

743743
.. attribute:: characters_written
744744

745-
An integer containing the number of characters written to the stream
746-
before it blocked. This attribute is available when using the
745+
An integer containing the number of **bytes** written to the stream
746+
before it blocked. This attribute is available when using the
747747
buffered I/O classes from the :mod:`io` module.
748748

749749
.. exception:: ChildProcessError

Doc/library/locale.rst

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ The :mod:`locale` module defines the following exception and functions:
4848
If *locale* is omitted or ``None``, the current setting for *category* is
4949
returned.
5050

51+
Example::
52+
53+
>>> import locale
54+
>>> loc = locale.setlocale(locale.LC_ALL) # get current locale
55+
# use German locale; name and availability varies with platform
56+
>>> locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
57+
>>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut
58+
>>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
59+
>>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
60+
>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
61+
5162
:func:`setlocale` is not thread-safe on most systems. Applications typically
52-
start with a call of ::
63+
start with a call of::
5364

5465
import locale
5566
locale.setlocale(locale.LC_ALL, '')
@@ -580,18 +591,6 @@ The :mod:`locale` module defines the following exception and functions:
580591
:func:`localeconv`.
581592

582593

583-
Example::
584-
585-
>>> import locale
586-
>>> loc = locale.getlocale() # get current locale
587-
# use German locale; name might vary with platform
588-
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
589-
>>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut
590-
>>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
591-
>>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
592-
>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
593-
594-
595594
Background, details, hints, tips and caveats
596595
--------------------------------------------
597596

Doc/library/multiprocessing.rst

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,21 @@ Reference
521521
The :mod:`multiprocessing` package mostly replicates the API of the
522522
:mod:`threading` module.
523523

524+
.. _global-start-method:
525+
526+
Global start method
527+
^^^^^^^^^^^^^^^^^^^
528+
529+
Python supports several ways to create and initialize a process.
530+
The global start method sets the default mechanism for creating a process.
531+
532+
Several multiprocessing functions and methods that may also instantiate
533+
certain objects will implicitly set the global start method to the system's default,
534+
if it hasn’t been set already. The global start method can only be set once.
535+
If you need to change the start method from the system default, you must
536+
proactively set the global start method before calling functions or methods,
537+
or creating these objects.
538+
524539

525540
:class:`Process` and exceptions
526541
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -910,6 +925,9 @@ For an example of the usage of queues for interprocess communication see
910925
locks/semaphores. When a process first puts an item on the queue a feeder
911926
thread is started which transfers objects from a buffer into the pipe.
912927

928+
Instantiating this class may set the global start method. See
929+
:ref:`global-start-method` for more details.
930+
913931
The usual :exc:`queue.Empty` and :exc:`queue.Full` exceptions from the
914932
standard library's :mod:`queue` module are raised to signal timeouts.
915933

@@ -1025,6 +1043,9 @@ For an example of the usage of queues for interprocess communication see
10251043

10261044
It is a simplified :class:`Queue` type, very close to a locked :class:`Pipe`.
10271045

1046+
Instantiating this class may set the global start method. See
1047+
:ref:`global-start-method` for more details.
1048+
10281049
.. method:: close()
10291050

10301051
Close the queue: release internal resources.
@@ -1055,6 +1076,9 @@ For an example of the usage of queues for interprocess communication see
10551076
:class:`JoinableQueue`, a :class:`Queue` subclass, is a queue which
10561077
additionally has :meth:`task_done` and :meth:`join` methods.
10571078

1079+
Instantiating this class may set the global start method. See
1080+
:ref:`global-start-method` for more details.
1081+
10581082
.. method:: task_done()
10591083

10601084
Indicate that a formerly enqueued task is complete. Used by queue
@@ -1167,8 +1191,8 @@ Miscellaneous
11671191
:mod:`multiprocessing` module.
11681192

11691193
If *method* is ``None`` then the default context is returned. Note that if
1170-
the global start method has not been set, this will set it to the
1171-
default method.
1194+
the global start method has not been set, this will set it to the system default
1195+
See :ref:`global-start-method` for more details.
11721196
Otherwise *method* should be ``'fork'``, ``'spawn'``,
11731197
``'forkserver'``. :exc:`ValueError` is raised if the specified
11741198
start method is not available. See :ref:`multiprocessing-start-methods`.
@@ -1179,10 +1203,9 @@ Miscellaneous
11791203

11801204
Return the name of start method used for starting processes.
11811205

1182-
If the global start method has not been set and *allow_none* is
1183-
``False``, then the start method is set to the default and the name
1184-
is returned. If the start method has not been set and *allow_none* is
1185-
``True`` then ``None`` is returned.
1206+
If the global start method is not set and *allow_none* is ``False``, the global start
1207+
method is set to the default, and its name is returned. See
1208+
:ref:`global-start-method` for more details.
11861209

11871210
The return value can be ``'fork'``, ``'spawn'``, ``'forkserver'``
11881211
or ``None``. See :ref:`multiprocessing-start-methods`.
@@ -1409,13 +1432,19 @@ object -- see :ref:`multiprocessing-managers`.
14091432

14101433
A barrier object: a clone of :class:`threading.Barrier`.
14111434

1435+
Instantiating this class may set the global start method. See
1436+
:ref:`global-start-method` for more details.
1437+
14121438
.. versionadded:: 3.3
14131439

14141440
.. class:: BoundedSemaphore([value])
14151441

14161442
A bounded semaphore object: a close analog of
14171443
:class:`threading.BoundedSemaphore`.
14181444

1445+
Instantiating this class may set the global start method. See
1446+
:ref:`global-start-method` for more details.
1447+
14191448
A solitary difference from its close analog exists: its ``acquire`` method's
14201449
first argument is named *block*, as is consistent with :meth:`Lock.acquire`.
14211450

@@ -1436,13 +1465,18 @@ object -- see :ref:`multiprocessing-managers`.
14361465
If *lock* is specified then it should be a :class:`Lock` or :class:`RLock`
14371466
object from :mod:`multiprocessing`.
14381467

1468+
Instantiating this class may set the global start method. See
1469+
:ref:`global-start-method` for more details.
1470+
14391471
.. versionchanged:: 3.3
14401472
The :meth:`~threading.Condition.wait_for` method was added.
14411473

14421474
.. class:: Event()
14431475

14441476
A clone of :class:`threading.Event`.
14451477

1478+
Instantiating this class may set the global start method. See
1479+
:ref:`global-start-method` for more details.
14461480

14471481
.. class:: Lock()
14481482

@@ -1458,6 +1492,9 @@ object -- see :ref:`multiprocessing-managers`.
14581492
instance of ``multiprocessing.synchronize.Lock`` initialized with a
14591493
default context.
14601494

1495+
Instantiating this class may set the global start method. See
1496+
:ref:`global-start-method` for more details.
1497+
14611498
:class:`Lock` supports the :term:`context manager` protocol and thus may be
14621499
used in :keyword:`with` statements.
14631500

@@ -1515,6 +1552,9 @@ object -- see :ref:`multiprocessing-managers`.
15151552
instance of ``multiprocessing.synchronize.RLock`` initialized with a
15161553
default context.
15171554

1555+
Instantiating this class may set the global start method. See
1556+
:ref:`global-start-method` for more details.
1557+
15181558
:class:`RLock` supports the :term:`context manager` protocol and thus may be
15191559
used in :keyword:`with` statements.
15201560

@@ -1574,6 +1614,9 @@ object -- see :ref:`multiprocessing-managers`.
15741614

15751615
A semaphore object: a close analog of :class:`threading.Semaphore`.
15761616

1617+
Instantiating this class may set the global start method. See
1618+
:ref:`global-start-method` for more details.
1619+
15771620
A solitary difference from its close analog exists: its ``acquire`` method's
15781621
first argument is named *block*, as is consistent with :meth:`Lock.acquire`.
15791622

@@ -1718,7 +1761,7 @@ processes.
17181761
attributes which allow one to use it to store and retrieve strings -- see
17191762
documentation for :mod:`ctypes`.
17201763

1721-
.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True)
1764+
.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True, ctx=None)
17221765

17231766
The same as :func:`RawArray` except that depending on the value of *lock* a
17241767
process-safe synchronization wrapper may be returned instead of a raw ctypes
@@ -1732,9 +1775,13 @@ processes.
17321775
automatically protected by a lock, so it will not necessarily be
17331776
"process-safe".
17341777

1735-
Note that *lock* is a keyword-only argument.
1778+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1779+
calling this may set the global start method. See
1780+
:ref:`global-start-method` for more details.
17361781

1737-
.. function:: Value(typecode_or_type, *args, lock=True)
1782+
Note that *lock* and *ctx* are keyword-only parameters.
1783+
1784+
.. function:: Value(typecode_or_type, *args, lock=True, ctx=None)
17381785

17391786
The same as :func:`RawValue` except that depending on the value of *lock* a
17401787
process-safe synchronization wrapper may be returned instead of a raw ctypes
@@ -1747,19 +1794,27 @@ processes.
17471794
automatically protected by a lock, so it will not necessarily be
17481795
"process-safe".
17491796

1750-
Note that *lock* is a keyword-only argument.
1797+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1798+
calling this may set the global start method. See
1799+
:ref:`global-start-method` for more details.
1800+
1801+
Note that *lock* and *ctx* are keyword-only parameters.
17511802

17521803
.. function:: copy(obj)
17531804

17541805
Return a ctypes object allocated from shared memory which is a copy of the
17551806
ctypes object *obj*.
17561807

1757-
.. function:: synchronized(obj[, lock])
1808+
.. function:: synchronized(obj, lock=None, ctx=None)
17581809

17591810
Return a process-safe wrapper object for a ctypes object which uses *lock* to
17601811
synchronize access. If *lock* is ``None`` (the default) then a
17611812
:class:`multiprocessing.RLock` object is created automatically.
17621813

1814+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1815+
calling this may set the global start method. See
1816+
:ref:`global-start-method` for more details.
1817+
17631818
A synchronized wrapper will have two methods in addition to those of the
17641819
object it wraps: :meth:`get_obj` returns the wrapped object and
17651820
:meth:`get_lock` returns the lock object used for synchronization.
@@ -1877,8 +1932,9 @@ their parent process exits. The manager classes are defined in the
18771932
*serializer* must be ``'pickle'`` (use :mod:`pickle` serialization) or
18781933
``'xmlrpclib'`` (use :mod:`xmlrpc.client` serialization).
18791934

1880-
*ctx* is a context object, or ``None`` (use the current context). See the
1881-
:func:`get_context` function.
1935+
*ctx* is a context object, or ``None`` (use the current context). If ``None``,
1936+
calling this may set the global start method. See
1937+
:ref:`global-start-method` for more details.
18821938

18831939
*shutdown_timeout* is a timeout in seconds used to wait until the process
18841940
used by the manager completes in the :meth:`shutdown` method. If the
@@ -2371,7 +2427,9 @@ with the :class:`Pool` class.
23712427
the worker processes. Usually a pool is created using the
23722428
function :func:`multiprocessing.Pool` or the :meth:`Pool` method
23732429
of a context object. In both cases *context* is set
2374-
appropriately.
2430+
appropriately. If ``None``, calling this function will have the side effect
2431+
of setting the current global start method if it has not been set already.
2432+
See the :func:`get_context` function.
23752433

23762434
Note that the methods of the pool object should only be called by
23772435
the process which created the pool.

0 commit comments

Comments
 (0)