Skip to content

Commit f3beb18

Browse files
committed
Merge remote-tracking branch 'upstream/main' into nested-async-comp
2 parents 08f657d + 0ee0a74 commit f3beb18

36 files changed

+629
-189
lines changed

.github/workflows/doc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: 'Build documentation'
4141
run: xvfb-run make -C Doc/ PYTHON=../python SPHINXOPTS="-q -W --keep-going -j4" doctest html
4242
- name: 'Upload'
43-
uses: actions/[email protected].3
43+
uses: actions/[email protected].4
4444
with:
4545
name: doc-html
4646
path: Doc/build/html

Doc/c-api/code.rst

+8
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ bound into a function.
5959
6060
For efficiently iterating over the line numbers in a code object, use `the API described in PEP 626
6161
<https://www.python.org/dev/peps/pep-0626/#out-of-process-debuggers-and-profilers>`_.
62+
63+
.. c:function:: int PyCode_Addr2Location(PyObject *co, int byte_offset, int *start_line, int *start_column, int *end_line, int *end_column)
64+
65+
Sets the passed ``int`` pointers to the source code line and column numbers
66+
for the instruction at ``byte_offset``. Sets the value to ``0`` when
67+
information is not available for any particular element.
68+
69+
Returns ``1`` if the function succeeds and 0 otherwise.

Doc/library/argparse.rst

+2
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ is available in ``argparse`` and adds support for boolean actions such as
853853
>>> parser.parse_args(['--no-foo'])
854854
Namespace(foo=False)
855855

856+
.. versionadded:: 3.9
857+
856858
The recommended way to create a custom action is to extend :class:`Action`,
857859
overriding the ``__call__`` method and optionally the ``__init__`` and
858860
``format_usage`` methods.

Doc/library/bz2.rst

+5
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,8 @@ Writing and reading a bzip2-compressed file in binary mode:
325325
... content = f.read()
326326
>>> content == data # Check equality to original object after round-trip
327327
True
328+
329+
.. testcleanup::
330+
331+
import os
332+
os.remove("myfile.bz2")

Doc/library/configparser.rst

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ can be customized by end users easily.
4646

4747
import configparser
4848

49+
.. testcleanup::
50+
51+
import os
52+
os.remove("example.ini")
53+
4954

5055
Quick Start
5156
-----------

Doc/library/importlib.metadata.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
:synopsis: The implementation of the importlib metadata.
99

1010
.. versionadded:: 3.8
11+
.. versionchanged:: 3.10
12+
``importlib.metadata`` is no longer provisional.
1113

1214
**Source code:** :source:`Lib/importlib/metadata.py`
1315

14-
.. note::
15-
This functionality is provisional and may deviate from the usual
16-
version semantics of the standard library.
17-
1816
``importlib.metadata`` is a library that provides for access to installed
1917
package metadata. Built in part on Python's import system, this library
2018
intends to replace similar functionality in the `entry point

Doc/library/io.rst

+3
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ I/O Base Classes
391391
to control the number of lines read: no more lines will be read if the
392392
total size (in bytes/characters) of all lines so far exceeds *hint*.
393393

394+
*hint* values of ``0`` or less, as well as ``None``, are treated as no
395+
hint.
396+
394397
Note that it's already possible to iterate on file objects using ``for
395398
line in file: ...`` without calling ``file.readlines()``.
396399

Doc/library/os.path.rst

+8
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ the :mod:`glob` module.)
340340
that contains symbolic links. On Windows, it converts forward slashes to
341341
backward slashes. To normalize case, use :func:`normcase`.
342342

343+
.. note::
344+
On POSIX systems, in accordance with `IEEE Std 1003.1 2013 Edition; 4.13
345+
Pathname Resolution <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>`_,
346+
if a pathname begins with exactly two slashes, the first component
347+
following the leading characters may be interpreted in an implementation-defined
348+
manner, although more than two leading characters shall be treated as a
349+
single character.
350+
343351
.. versionchanged:: 3.6
344352
Accepts a :term:`path-like object`.
345353

Doc/library/traceback.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ The output for the example would look similar to this:
473473
['Traceback (most recent call last):\n',
474474
' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
475475
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
476-
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ^^^^^^^^^^\n',
476+
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ~~~~~~~^^^\n',
477477
'IndexError: tuple index out of range\n']
478478
*** extract_tb:
479479
[<FrameSummary file <doctest...>, line 10 in <module>>,
@@ -482,7 +482,7 @@ The output for the example would look similar to this:
482482
*** format_tb:
483483
[' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
484484
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
485-
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ^^^^^^^^^^\n']
485+
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ~~~~~~~^^^\n']
486486
*** tb_lineno: 10
487487

488488

Doc/reference/datamodel.rst

+33
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,39 @@ Internal types
10151015
If a code object represents a function, the first item in :attr:`co_consts` is
10161016
the documentation string of the function, or ``None`` if undefined.
10171017

1018+
.. method:: codeobject.co_positions()
1019+
1020+
Returns an iterable over the source code positions of each bytecode
1021+
instruction in the code object.
1022+
1023+
The iterator returns tuples containing the ``(start_line, end_line,
1024+
start_column, end_column)``. The *i-th* tuple corresponds to the
1025+
position of the source code that compiled to the *i-th* instruction.
1026+
Column information is 0-indexed utf-8 byte offsets on the given source
1027+
line.
1028+
1029+
This positional information can be missing. A non-exhaustive lists of
1030+
cases where this may happen:
1031+
1032+
- Running the interpreter with :option:`-X` ``no_debug_ranges``.
1033+
- Loading a pyc file compiled while using :option:`-X` ``no_debug_ranges``.
1034+
- Position tuples corresponding to artificial instructions.
1035+
- Line and column numbers that can't be represented due to
1036+
implementation specific limitations.
1037+
1038+
When this occurs, some or all of the tuple elements can be
1039+
:const:`None`.
1040+
1041+
.. versionadded:: 3.11
1042+
1043+
.. note::
1044+
This feature requires storing column positions in code objects which may
1045+
result in a small increase of disk usage of compiled Python files or
1046+
interpreter memory usage. To avoid storing the extra information and/or
1047+
deactivate printing the extra traceback information, the
1048+
:option:`-X` ``no_debug_ranges`` command line flag or the :envvar:`PYTHONNODEBUGRANGES`
1049+
environment variable can be used.
1050+
10181051
.. _frame-objects:
10191052

10201053
Frame objects

Doc/whatsnew/3.11.rst

+87-9
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,97 @@ Summary -- Release highlights
7070
New Features
7171
============
7272

73-
* Asynchronous comprehensions are now allowed inside comprehensions in
74-
asynchronous functions. Outer comprehensions implicitly become
75-
asynchronous. (Contributed by Serhiy Storchaka in :issue:`33346`.)
73+
.. _whatsnew311-pep657:
74+
75+
Enhanced error locations in tracebacks
76+
--------------------------------------
77+
78+
When printing tracebacks, the interpreter will now point to the exact expression
79+
that caused the error instead of just the line. For example:
80+
81+
.. code-block:: python
82+
83+
Traceback (most recent call last):
84+
File "distance.py", line 11, in <module>
85+
print(manhattan_distance(p1, p2))
86+
^^^^^^^^^^^^^^^^^^^^^^^^^^
87+
File "distance.py", line 6, in manhattan_distance
88+
return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y)
89+
^^^^^^^^^
90+
AttributeError: 'NoneType' object has no attribute 'x'
91+
92+
Previous versions of the interpreter would point to just the line making it
93+
ambiguous which object was ``None``. These enhanced errors can also be helpful
94+
when dealing with deeply nested dictionary objects and multiple function calls,
95+
96+
.. code-block:: python
97+
98+
Traceback (most recent call last):
99+
File "query.py", line 37, in <module>
100+
magic_arithmetic('foo')
101+
^^^^^^^^^^^^^^^^^^^^^^^
102+
File "query.py", line 18, in magic_arithmetic
103+
return add_counts(x) / 25
104+
^^^^^^^^^^^^^
105+
File "query.py", line 24, in add_counts
106+
return 25 + query_user(user1) + query_user(user2)
107+
^^^^^^^^^^^^^^^^^
108+
File "query.py", line 32, in query_user
109+
return 1 + query_count(db, response['a']['b']['c']['user'], retry=True)
110+
~~~~~~~~~~~~~~~~~~^^^^^
111+
TypeError: 'NoneType' object is not subscriptable
112+
113+
as well as complex arithmetic expressions:
114+
115+
.. code-block:: python
116+
117+
Traceback (most recent call last):
118+
File "calculation.py", line 54, in <module>
119+
result = (x / y / z) * (a / b / c)
120+
~~~~~~^~~
121+
ZeroDivisionError: division by zero
122+
123+
See :pep:`657` for more details. (Contributed by Pablo Galindo, Batuhan Taskaya
124+
and Ammar Askar in :issue:`43950`.)
125+
126+
.. note::
127+
This feature requires storing column positions in code objects which may
128+
result in a small increase of disk usage of compiled Python files or
129+
interpreter memory usage. To avoid storing the extra information and/or
130+
deactivate printing the extra traceback information, the
131+
:option:`-X` ``no_debug_ranges`` command line flag or the :envvar:`PYTHONNODEBUGRANGES`
132+
environment variable can be used.
133+
134+
Column information for code objects
135+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136+
137+
The information used by the enhanced traceback feature is made available as a
138+
general API that can be used to correlate bytecode instructions with source
139+
code. This information can be retrieved using:
140+
141+
- The :meth:`codeobject.co_positions` method in Python.
142+
- The :c:func:`PyCode_Addr2Location` function in the C-API.
143+
144+
The :option:`-X` ``no_debug_ranges`` option and the environment variable
145+
:envvar:`PYTHONNODEBUGRANGES` can be used to disable this feature.
146+
147+
See :pep:`657` for more details. (Contributed by Pablo Galindo, Batuhan Taskaya
148+
and Ammar Askar in :issue:`43950`.)
149+
76150

77151
Other Language Changes
78152
======================
79153

80-
A :exc:`TypeError` is now raised instead of an :exc:`AttributeError` in
81-
:meth:`contextlib.ExitStack.enter_context` and
82-
:meth:`contextlib.AsyncExitStack.enter_async_context` for objects which do not
83-
support the :term:`context manager` or :term:`asynchronous context manager`
84-
protocols correspondingly.
85-
(Contributed by Serhiy Storchaka in :issue:`44471`.)
154+
* Asynchronous comprehensions are now allowed inside comprehensions in
155+
asynchronous functions. Outer comprehensions implicitly become
156+
asynchronous. (Contributed by Serhiy Storchaka in :issue:`33346`.)
157+
158+
* A :exc:`TypeError` is now raised instead of an :exc:`AttributeError` in
159+
:meth:`contextlib.ExitStack.enter_context` and
160+
:meth:`contextlib.AsyncExitStack.enter_async_context` for objects which do not
161+
support the :term:`context manager` or :term:`asynchronous context manager`
162+
protocols correspondingly.
163+
(Contributed by Serhiy Storchaka in :issue:`44471`.)
86164

87165
* A :exc:`TypeError` is now raised instead of an :exc:`AttributeError` in
88166
:keyword:`with` and :keyword:`async with` statements for objects which do not

Include/funcobject.h

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ typedef struct {
4242
PyObject *func_module; /* The __module__ attribute, can be anything */
4343
PyObject *func_annotations; /* Annotations, a dict or NULL */
4444
vectorcallfunc vectorcall;
45+
/* Version number for use by specializer.
46+
* Can set to non-zero when we want to specialize.
47+
* Will be set to zero if any of these change:
48+
* defaults
49+
* kwdefaults (only if the object changes, not the contents of the dict)
50+
* code
51+
* annotations */
52+
uint32_t func_version;
4553

4654
/* Invariant:
4755
* func_closure contains the bindings for func_code->co_freevars, so
@@ -74,6 +82,8 @@ PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
7482
PyObject *const *stack,
7583
size_t nargsf,
7684
PyObject *kwnames);
85+
86+
uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
7787
#endif
7888

7989
/* Macros for direct access to these values. Type checks are *not*

Lib/configparser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ class RawConfigParser(MutableMapping):
563563
# Regular expressions for parsing section headers and options
564564
_SECT_TMPL = r"""
565565
\[ # [
566-
(?P<header>[^]]+) # very permissive!
566+
(?P<header>.+) # very permissive!
567567
\] # ]
568568
"""
569569
_OPT_TMPL = r"""

Lib/ctypes/test/test_bitfields.py

-63
Original file line numberDiff line numberDiff line change
@@ -233,69 +233,6 @@ class X(Structure):
233233
else:
234234
self.assertEqual(sizeof(X), sizeof(c_int) * 2)
235235

236-
@unittest.skipIf(os.name == 'nt', reason='Posix only')
237-
def test_packed_posix(self):
238-
test_cases = {
239-
(
240-
("a", c_uint8, 4),
241-
("b", c_uint8, 4),
242-
): 1,
243-
(
244-
("a", c_uint8, 1),
245-
("b", c_uint16, 1),
246-
("c", c_uint32, 1),
247-
("d", c_uint64, 1),
248-
): 1,
249-
(
250-
("a", c_uint8, 8),
251-
("b", c_uint16, 1),
252-
("c", c_uint32, 1),
253-
("d", c_uint64, 1),
254-
): 2,
255-
(
256-
("a", c_uint32, 9),
257-
("b", c_uint16, 10),
258-
("c", c_uint32, 25),
259-
("d", c_uint64, 1),
260-
): 6,
261-
(
262-
("a", c_uint32, 9),
263-
("b", c_uint16, 10),
264-
("c", c_uint32, 25),
265-
("d", c_uint64, 5),
266-
): 7,
267-
(
268-
("a", c_uint16),
269-
("b", c_uint16, 9),
270-
("c", c_uint16, 1),
271-
("d", c_uint16, 1),
272-
("e", c_uint16, 1),
273-
("f", c_uint16, 1),
274-
("g", c_uint16, 3),
275-
("h", c_uint32, 10),
276-
("i", c_uint32, 20),
277-
("j", c_uint32, 2),
278-
): 8,
279-
(
280-
("a", c_uint16, 9),
281-
("b", c_uint16, 10),
282-
("d", c_uint16),
283-
("c", c_uint8, 8),
284-
): 6,
285-
(
286-
("a", c_uint32, 9),
287-
("b", c_uint32),
288-
("c", c_uint32, 8),
289-
): 7,
290-
}
291-
292-
for fields, size in test_cases.items():
293-
with self.subTest(fields=fields):
294-
class X(Structure):
295-
_pack_ = 1
296-
_fields_ = list(fields)
297-
self.assertEqual(sizeof(X), size)
298-
299236
def test_anon_bitfields(self):
300237
# anonymous bit-fields gave a strange error message
301238
class X(Structure):

Lib/inspect.py

-4
Original file line numberDiff line numberDiff line change
@@ -2096,10 +2096,6 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
20962096
"""Private helper to parse content of '__text_signature__'
20972097
and return a Signature based on it.
20982098
"""
2099-
# Lazy import ast because it's relatively heavy and
2100-
# it's not used for other than this function.
2101-
import ast
2102-
21032099
Parameter = cls._parameter_cls
21042100

21052101
clean_signature, self_parameter, last_positional_only = \

Lib/lib2to3/btm_utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ def reduce_tree(node, parent=None):
220220
else:
221221
#TODO: handle {min, max} repeaters
222222
raise NotImplementedError
223-
pass
224223

225224
#add children
226225
if details_node and new_node is not None:

Lib/multiprocessing/managers.py

-1
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,6 @@ def __init__(self, *args, **kwargs):
13371337

13381338
def __del__(self):
13391339
util.debug(f"{self.__class__.__name__}.__del__ by pid {getpid()}")
1340-
pass
13411340

13421341
def get_server(self):
13431342
'Better than monkeypatching for now; merge into Server ultimately'

Lib/platform.py

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ def _syscmd_ver(system='', release='', version='',
280280
for cmd in ('ver', 'command /c ver', 'cmd /c ver'):
281281
try:
282282
info = subprocess.check_output(cmd,
283+
stdin=subprocess.DEVNULL,
283284
stderr=subprocess.DEVNULL,
284285
text=True,
285286
shell=True)

0 commit comments

Comments
 (0)