@@ -70,19 +70,97 @@ Summary -- Release highlights
70
70
New Features
71
71
============
72
72
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
+
76
150
77
151
Other Language Changes
78
152
======================
79
153
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 `.)
86
164
87
165
* A :exc: `TypeError ` is now raised instead of an :exc: `AttributeError ` in
88
166
:keyword: `with ` and :keyword: `async with ` statements for objects which do not
0 commit comments