Skip to content

Commit 94d29be

Browse files
Deploy preview for PR 1231 🛫
1 parent cf117ec commit 94d29be

600 files changed

Lines changed: 2061 additions & 737 deletions

File tree

Some content is hidden

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

pr-preview/pr-1231/_sources/faq/design.rst.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ you can always change a list's elements. Only immutable elements can be used as
428428
dictionary keys, and hence only tuples and not lists can be used as keys.
429429

430430

431+
.. _how-are-lists-implemented:
432+
431433
How are lists implemented in CPython?
432434
-------------------------------------
433435

@@ -443,6 +445,10 @@ cleverness is applied to improve the performance of appending items repeatedly;
443445
when the array must be grown, some extra space is allocated so the next few
444446
times don't require an actual resize.
445447

448+
See :ref:`time-complexity` for the costs of the various list operations.
449+
450+
451+
.. _how-are-dictionaries-implemented:
446452

447453
How are dictionaries implemented in CPython?
448454
--------------------------------------------
@@ -460,6 +466,8 @@ internal array where the value will be stored. Assuming that you're storing
460466
keys that all have different hash values, this means that dictionaries take
461467
constant time -- *O*\ (1), in Big-O notation -- to retrieve a key.
462468

469+
See :ref:`time-complexity` for the costs of the various dictionary operations.
470+
463471

464472
Why must dictionary keys be immutable?
465473
--------------------------------------

pr-preview/pr-1231/_sources/faq/programming.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ What is the most efficient way to concatenate many strings together?
11361136
:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
11371137
many strings together is inefficient as each concatenation creates a new
11381138
object. In the general case, the total runtime cost is quadratic in the
1139-
total string length.
1139+
total string length. See :ref:`time-complexity` for more information.
11401140

11411141
To accumulate many :class:`str` objects, the recommended idiom is to place
11421142
them into a list and call :meth:`str.join` at the end::

pr-preview/pr-1231/_sources/glossary.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ Glossary
942942
list
943943
A built-in Python :term:`sequence`. Despite its name it is more akin
944944
to an array in other languages than to a linked list since access to
945-
elements is *O*\ (1).
945+
elements is *O*\ (1). See :ref:`time-complexity`.
946946

947947
list comprehension
948948
A compact way to process all or part of the elements in a sequence and

pr-preview/pr-1231/_sources/howto/curses.rst.txt

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,18 @@ the next subsection.
298298

299299
The :meth:`~curses.window.addstr` method takes a Python string or
300300
bytestring as the value to be displayed. The contents of bytestrings
301-
are sent to the terminal as-is. Strings are encoded to bytes using
302-
the value of the window's :attr:`~window.encoding` attribute; this defaults to
303-
the default system encoding as returned by :func:`locale.getencoding`.
301+
are sent to the terminal as-is.
302+
On a build without wide-character support strings are encoded
303+
using the value of the window's :attr:`~window.encoding` attribute;
304+
this defaults to the default system encoding
305+
as returned by :func:`locale.getencoding`.
304306

305307
The :meth:`~curses.window.addch` methods take a character, which can be
306308
either a string of length 1, a bytestring of length 1, or an integer.
307309

308-
Constants are provided for extension characters; these constants are
309-
integers greater than 255. For example, :const:`ACS_PLMINUS` is a +/-
310+
Constants are provided for the characters of the terminal's alternate
311+
character set.
312+
For example, :const:`ACS_PLMINUS` is a +/-
310313
symbol, and :const:`ACS_ULCORNER` is the upper left corner of a box
311314
(handy for drawing borders). You can also use the appropriate Unicode
312315
character.
@@ -320,11 +323,11 @@ won't be distracting; it can be confusing to have the cursor blinking at some
320323
apparently random location.
321324

322325
If your application doesn't need a blinking cursor at all, you can
323-
call ``curs_set(False)`` to make it invisible. For compatibility
324-
with older curses versions, there's a ``leaveok(bool)`` function
325-
that's a synonym for :func:`~curses.curs_set`. When *bool* is true, the
326-
curses library will attempt to suppress the flashing cursor, and you
327-
won't need to worry about leaving it in odd locations.
326+
call ``curs_set(False)`` to make it invisible.
327+
The window method :meth:`~curses.window.leaveok` does something different:
328+
when its argument is true,
329+
curses leaves the cursor wherever the last update put it,
330+
instead of moving it back to the window's cursor position.
328331

329332

330333
Attributes and Color
@@ -430,40 +433,48 @@ The C curses library offers only very simple input mechanisms. Python's
430433
:mod:`curses` module adds a basic text-input widget. (Other libraries
431434
such as :pypi:`Urwid` have more extensive collections of widgets.)
432435

433-
There are two methods for getting input from a window:
436+
There are three methods for getting input from a window:
434437

435-
* :meth:`~curses.window.getch` refreshes the screen and then waits for
438+
* :meth:`~curses.window.get_wch` refreshes the screen and then waits for
436439
the user to hit a key, displaying the key if :func:`~curses.echo` has been
437440
called earlier. You can optionally specify a coordinate to which
438441
the cursor should be moved before pausing.
439442

440-
* :meth:`~curses.window.getkey` does the same thing but converts the
441-
integer to a string. Individual characters are returned as
442-
1-character strings, and special keys such as function keys return
443-
longer strings containing a key name such as ``KEY_UP`` or ``^G``.
443+
* :meth:`~curses.window.getch` does the same thing but returns the code of
444+
the key instead of a character.
445+
With ncurses this is a single byte of the key's encoding in the current
446+
locale, so a character encoded with several bytes takes several calls,
447+
one byte per call.
448+
449+
* :meth:`~curses.window.getkey` does the same as :meth:`!getch` but returns
450+
a string:
451+
an ordinary key as a 1-character string,
452+
and a special key as its name, such as ``KEY_UP``.
444453

445454
It's possible to not wait for the user using the
446455
:meth:`~curses.window.nodelay` window method. After ``nodelay(True)``,
447-
:meth:`!getch` and :meth:`!getkey` for the window become
448-
non-blocking. To signal that no input is ready, :meth:`!getch` returns
449-
``curses.ERR`` (a value of -1) and :meth:`!getkey` raises an exception.
456+
the reads for the window become non-blocking.
457+
To signal that no input is ready,
458+
:meth:`!get_wch` and :meth:`!getkey` raise an exception,
459+
and :meth:`!getch` returns ``-1``.
450460
There's also a :func:`~curses.halfdelay` function, which can be used to (in
451-
effect) set a timer on each :meth:`!getch`; if no input becomes
461+
effect) set a timer on each read; if no input becomes
452462
available within a specified delay (measured in tenths of a second),
453-
curses raises an exception.
463+
the read fails the same way.
454464

455-
The :meth:`!getch` method returns an integer; if it's between 0 and 255, it
456-
represents the ASCII code of the key pressed. Values greater than 255 are
457-
special keys such as Page Up, Home, or the cursor keys. You can compare the
458-
value returned to constants such as :const:`curses.KEY_PPAGE`,
465+
Special keys such as Page Up, Home, or the cursor keys are returned by all
466+
three as one of the :ref:`KEY_* constants <curses-key-constants>`,
467+
all larger than 255.
468+
You can compare the value returned to constants such as
469+
:const:`curses.KEY_PPAGE`,
459470
:const:`curses.KEY_HOME`, or :const:`curses.KEY_LEFT`. The main loop of
460471
your program may look something like this::
461472

462473
while True:
463-
c = stdscr.getch()
464-
if c == ord('p'):
474+
c = stdscr.get_wch()
475+
if c == 'p':
465476
PrintDocument()
466-
elif c == ord('q'):
477+
elif c == 'q':
467478
break # Exit the while loop
468479
elif c == curses.KEY_HOME:
469480
x = y = 0
@@ -475,15 +486,16 @@ conversion functions that take either integer or 1-character-string arguments
475486
and return the same type. For example, :func:`curses.ascii.ctrl` returns the
476487
control character corresponding to its argument.
477488

478-
There's also a method to retrieve an entire string,
489+
There's also a method to retrieve an entire line,
479490
:meth:`~curses.window.getstr`. It isn't used very often, because its
480491
functionality is quite limited; the only editing keys available are
481-
the backspace key and the Enter key, which terminates the string. It
482-
can optionally be limited to a fixed number of characters. ::
492+
the erase and kill characters, and the Enter key, which terminates the line.
493+
It returns a bytes object,
494+
and can optionally be limited to a fixed number of bytes. ::
483495

484496
curses.echo() # Enable echoing of characters
485497

486-
# Get a 15-character string, with the cursor on the top line
498+
# Get a line of at most 15 bytes, with the cursor on the top line
487499
s = stdscr.getstr(0,0, 15)
488500

489501
The :mod:`curses.textpad` module supplies a text box that supports an

pr-preview/pr-1231/_sources/library/atexit.rst.txt

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,69 @@
99

1010
--------------
1111

12-
The :mod:`!atexit` module defines functions to register and unregister cleanup
13-
functions. Functions thus registered are automatically executed upon normal
14-
interpreter termination. :mod:`!atexit` runs these functions in the *reverse*
15-
order in which they were registered; if you register ``A``, ``B``, and ``C``,
16-
at interpreter termination time they will be run in the order ``C``, ``B``,
17-
``A``.
18-
19-
**Note:** The functions registered via this module are not called when the
12+
The :mod:`!atexit` module defines functions to register and unregister
13+
:dfn:`exit handlers`: functions that are automatically executed
14+
"at exit", that is, upon normal program termination (for instance,
15+
if :func:`sys.exit` is called or the main module's execution completes)
16+
or, more generally, upon :term:`interpreter shutdown`.
17+
18+
At exit, all registered exit handlers are called
19+
in the *reverse* order in which they were registered.
20+
If you register ``A``, ``B``, and ``C``, at interpreter shutdown time they
21+
will be run in the order ``C``, ``B``, ``A``.
22+
The assumption is that lower level modules will normally be imported before
23+
higher level modules and thus must be cleaned up later.
24+
25+
If an exception is raised during execution of an exit handler, a traceback is
26+
printed (unless :exc:`SystemExit` is raised) and the exception information is
27+
saved. After all exit handlers have had a chance to run, the last exception to
28+
be raised is re-raised.
29+
30+
In programs that use multiple interpreters, each interpreter has its own stack
31+
of exit handlers, which are executed when the interpreter shuts down
32+
(for example, with :meth:`concurrent.interpreters.Interpreter.close` or the
33+
C API :c:func:`Py_EndInterpreter`).
34+
Registration functions in this module only affect the interpreter they are
35+
called from.
36+
37+
**Note:** Exit handlers are not called when the
2038
program is killed by a signal not handled by Python, when a Python fatal
2139
internal error is detected, or when :func:`os._exit` is called.
2240

2341
**Note:** The effect of registering or unregistering functions from within
2442
a cleanup function is undefined.
2543

26-
.. versionchanged:: 3.7
27-
When used with C-API subinterpreters, registered functions
28-
are local to the interpreter they were registered in.
44+
.. warning::
45+
When writing exit handlers, especially in C API extensions, keep in mind
46+
that other exit handlers may still run arbitrary Python code after you
47+
clean up.
48+
Such code should succeed or fail with an exception, rather than crash.
2949

30-
.. function:: register(func, *args, **kwargs)
50+
.. versionchanged:: 3.12
51+
Attempts to start a new thread or :func:`os.fork` a new process
52+
in an exit handler now leads to :exc:`RuntimeError`.
53+
Previously, this could cause race conditions between the main Python
54+
runtime thread freeing thread states while internal :mod:`threading`
55+
routines or the new process try to use that state, which could lead to
56+
crashes rather than clean shutdown.
3157

32-
Register *func* as a function to be executed at termination. Any optional
33-
arguments that are to be passed to *func* must be passed as arguments to
34-
:func:`register`. It is possible to register the same function and arguments
35-
more than once.
58+
.. versionchanged:: 3.7
59+
When used with subinterpreters, registered functions
60+
are local to the interpreter they were registered in.
3661

37-
At normal program termination (for instance, if :func:`sys.exit` is called or
38-
the main module's execution completes), all functions registered are called in
39-
last in, first out order. The assumption is that lower level modules will
40-
normally be imported before higher level modules and thus must be cleaned up
41-
later.
62+
.. function:: register(func, *args, **kwargs)
4263

43-
If an exception is raised during execution of the exit handlers, a traceback is
44-
printed (unless :exc:`SystemExit` is raised) and the exception information is
45-
saved. After all exit handlers have had a chance to run, the last exception to
46-
be raised is re-raised.
64+
Register *func* as an exit handler.
65+
Any optional arguments that are to be passed to *func* must be passed as
66+
arguments to :func:`register`.
67+
It is possible to register the same function and arguments more than once.
4768

4869
This function returns *func*, which makes it possible to use it as a
4970
decorator.
5071

51-
.. warning::
52-
Starting new threads or calling :func:`os.fork` from a registered
53-
function can lead to race condition between the main Python
54-
runtime thread freeing thread states while internal :mod:`threading`
55-
routines or the new process try to use that state. This can lead to
56-
crashes rather than clean shutdown.
57-
58-
.. versionchanged:: 3.12
59-
Attempts to start a new thread or :func:`os.fork` a new process
60-
in a registered function now leads to :exc:`RuntimeError`.
61-
6272
.. function:: unregister(func)
6373

64-
Remove *func* from the list of functions to be run at interpreter shutdown.
74+
Remove *func* from the list of exit handlers.
6575
:func:`unregister` silently does nothing if *func* was not previously
6676
registered. If *func* has been registered more than once, every occurrence
6777
of that function in the :mod:`!atexit` call stack will be removed. Equality

pr-preview/pr-1231/_sources/library/curses.rst.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,8 @@ Window objects
987987

988988
.. attribute:: window.encoding
989989

990-
Encoding used to encode method arguments (Unicode strings and characters).
990+
Encoding used to encode the string arguments of the methods and to decode
991+
their results on a build without wide-character support.
991992
The encoding attribute is inherited from the parent window when a subwindow
992993
is created, for example with :meth:`window.subwin`.
993994
By default, current locale encoding is used (see :func:`locale.getencoding`).

pr-preview/pr-1231/_sources/library/index.rst.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ the `Python Package Index <https://pypi.org>`_.
4444
stdtypes.rst
4545
exceptions.rst
4646
threadsafety.rst
47+
time-complexity.rst
4748

4849
text.rst
4950
binary.rst

pr-preview/pr-1231/_sources/library/inspect.rst.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
468468
Functions wrapped in :func:`functools.partial` now return ``True`` if the
469469
wrapped function is a Python generator function.
470470

471+
.. versionchanged:: 3.10.6
472+
:term:`Duck-typed <duck-typing>` function-like objects now return
473+
``True`` if their code object has the :data:`CO_GENERATOR` flag.
474+
471475
.. versionchanged:: 3.13
472476
Functions wrapped in :func:`functools.partialmethod` now return ``True``
473477
if the wrapped function is a Python generator function.
@@ -490,6 +494,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
490494
Functions wrapped in :func:`functools.partial` now return ``True`` if the
491495
wrapped function is a :term:`coroutine function`.
492496

497+
.. versionchanged:: 3.10.6
498+
:term:`Duck-typed <duck-typing>` function-like objects now return
499+
``True`` if their code object has the :data:`CO_COROUTINE` flag.
500+
493501
.. versionchanged:: 3.12
494502
Sync functions marked with :func:`markcoroutinefunction` now return
495503
``True``.
@@ -564,6 +572,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
564572
Functions wrapped in :func:`functools.partial` now return ``True`` if the
565573
wrapped function is an :term:`asynchronous generator` function.
566574

575+
.. versionchanged:: 3.10.6
576+
:term:`Duck-typed <duck-typing>` function-like objects now return
577+
``True`` if their code object has the :data:`CO_ASYNC_GENERATOR` flag.
578+
567579
.. versionchanged:: 3.13
568580
Functions wrapped in :func:`functools.partialmethod` now return ``True``
569581
if the wrapped function is a :term:`asynchronous generator` function.

pr-preview/pr-1231/_sources/library/stdtypes.rst.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,9 @@ The ``in`` and ``not in`` operations have the same priorities as the
999999
comparison operations. The ``+`` (concatenation) and ``*`` (repetition)
10001000
operations have the same priority as the corresponding numeric operations. [3]_
10011001

1002+
See :ref:`time-complexity` for the costs of the various sequence
1003+
operations.
1004+
10021005
.. index::
10031006
triple: operations on; sequence; types
10041007
pair: built-in function; len
@@ -1121,6 +1124,8 @@ Notes:
11211124
"end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
11221125
If *k* is ``None``, it is treated like ``1``.
11231126

1127+
.. _typesseq-repeated-concatenation:
1128+
11241129
(6)
11251130
Concatenating immutable sequences always results in a new object. This
11261131
means that building up a sequence by repeated concatenation will have a
@@ -5083,6 +5088,7 @@ computing mathematical operations such as intersection, union, difference, and
50835088
symmetric difference.
50845089
(For other containers see the built-in :class:`dict`, :class:`list`,
50855090
and :class:`tuple` classes, and the :mod:`collections` module.)
5091+
See :ref:`time-complexity` for the costs of the various set operations.
50865092

50875093
Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
50885094
set``. Being an unordered collection, sets do not record element position or
@@ -5306,6 +5312,8 @@ Mappings are mutable objects. There is currently only one standard mapping
53065312
type, the :dfn:`dictionary`. (For other containers see the built-in
53075313
:class:`list`, :class:`set`, and :class:`tuple` classes, and the
53085314
:mod:`collections` module.)
5315+
See :ref:`time-complexity` for the costs of the various dictionary
5316+
operations.
53095317

53105318
A dictionary's keys are *almost* arbitrary values. Values that are not
53115319
:term:`hashable`, that is, values containing lists, dictionaries or other

0 commit comments

Comments
 (0)