Skip to content

Commit 9c3eaf8

Browse files
ammaraskarpablogsalisidentical
authored
bpo-43950: Add documentation for PEP-657 (GH-27047)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com> Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
1 parent f6954cd commit 9c3eaf8

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

Doc/c-api/code.rst

Lines changed: 8 additions & 0 deletions
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/reference/datamodel.rst

Lines changed: 33 additions & 0 deletions
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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,83 @@ Summary -- Release highlights
7070
New Features
7171
============
7272

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+
73150

74151

75152
Other Language Changes
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Code objects can now provide the column information for instructions when
2+
available. This is levaraged during traceback printing to show the
3+
expressions responsible for errors.
4+
5+
Contributed by Pablo Galindo, Batuhan Taskaya and Ammar Askar as part of
6+
:pep:`657`.

0 commit comments

Comments
 (0)