Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a72418e

Browse files
authored
Restore lineno's for Input mapped files (#13560)
* Implement lineno's for Input mapped files * Adopt In [123], line 123 format * Revert "Set co_name for cells run line by line. Fixes ipython/ipykernel#841" (This reverts commit d11e987.) * Omit mention of ", in <module>" for input tracebacks * Input cell -> Cell * Remove <module> from traceback doctests * Use f-string for `in ...' format * Simplify _format_list logic, converting to f-strings
1 parent ccd5325 commit a72418e

4 files changed

Lines changed: 78 additions & 59 deletions

File tree

IPython/core/interactiveshell.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import atexit
1717
import bdb
1818
import builtins as builtin_mod
19-
import dis
2019
import functools
2120
import inspect
2221
import os
@@ -3212,29 +3211,6 @@ def transform_ast(self, node):
32123211
ast.fix_missing_locations(node)
32133212
return node
32143213

3215-
def _update_code_co_name(self, code):
3216-
"""Python 3.10 changed the behaviour so that whenever a code object
3217-
is assembled in the compile(ast) the co_firstlineno would be == 1.
3218-
3219-
This makes pydevd/debugpy think that all cells invoked are the same
3220-
since it caches information based on (co_firstlineno, co_name, co_filename).
3221-
3222-
Given that, this function changes the code 'co_name' to be unique
3223-
based on the first real lineno of the code (which also has a nice
3224-
side effect of customizing the name so that it's not always <module>).
3225-
3226-
See: https://github.com/ipython/ipykernel/issues/841
3227-
"""
3228-
if not hasattr(code, "replace"):
3229-
# It may not be available on older versions of Python (only
3230-
# available for 3.8 onwards).
3231-
return code
3232-
try:
3233-
first_real_line = next(dis.findlinestarts(code))[1]
3234-
except StopIteration:
3235-
return code
3236-
return code.replace(co_name="<cell line: %s>" % (first_real_line,))
3237-
32383214
async def run_ast_nodes(
32393215
self,
32403216
nodelist: ListType[stmt],
@@ -3333,7 +3309,6 @@ def compare(code):
33333309
else 0x0
33343310
):
33353311
code = compiler(mod, cell_name, mode)
3336-
code = self._update_code_co_name(code)
33373312
asy = compare(code)
33383313
if await self.run_code(code, result, async_=asy):
33393314
return True

IPython/core/tests/test_iplib.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def doctest_tb_plain():
4545
4646
In [19]: run simpleerr.py
4747
Traceback (most recent call last):
48-
File ...:... in <module>
48+
File ...:...
4949
bar(mode)
5050
File ...:... in bar
5151
div0()
@@ -64,7 +64,7 @@ def doctest_tb_context():
6464
---------------------------------------------------------------------------
6565
ZeroDivisionError Traceback (most recent call last)
6666
<BLANKLINE>
67-
... in <module>
67+
...
6868
30 except IndexError:
6969
31 mode = 'div'
7070
---> 33 bar(mode)
@@ -93,7 +93,7 @@ def doctest_tb_verbose():
9393
---------------------------------------------------------------------------
9494
ZeroDivisionError Traceback (most recent call last)
9595
<BLANKLINE>
96-
... in <module>
96+
...
9797
30 except IndexError:
9898
31 mode = 'div'
9999
---> 33 bar(mode)
@@ -134,7 +134,7 @@ def doctest_tb_sysexit():
134134
Traceback (most recent call last):
135135
File ...:... in execfile
136136
exec(compiler(f.read(), fname, "exec"), glob, loc)
137-
File ...:... in <module>
137+
File ...:...
138138
bar(mode)
139139
File ...:... in bar
140140
sysexit(stat, mode)
@@ -152,7 +152,7 @@ def doctest_tb_sysexit():
152152
... with open(fname, "rb") as f:
153153
... compiler = compiler or compile
154154
---> ... exec(compiler(f.read(), fname, "exec"), glob, loc)
155-
...<module>
155+
...
156156
30 except IndexError:
157157
31 mode = 'div'
158158
---> 33 bar(mode)
@@ -189,7 +189,7 @@ def doctest_tb_sysexit_verbose():
189189
---------------------------------------------------------------------------
190190
SystemExit Traceback (most recent call last)
191191
<BLANKLINE>
192-
... in <module>
192+
...
193193
30 except IndexError:
194194
31 mode = 'div'
195195
---> 33 bar(mode)

IPython/core/ultratb.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
187187

188188
if ipinst is not None and file in ipinst.compile._filename_map:
189189
file = "[%s]" % ipinst.compile._filename_map[file]
190-
tpl_link = f"Input {ColorFilename}In {{file}}{ColorNormal}"
190+
if lineno is None:
191+
tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}"
192+
else:
193+
tpl_link = f"Cell {ColorFilename}In {{file}}, line {{lineno}}{ColorNormal}"
191194
else:
192195
file = util_path.compress_user(
193196
py3compat.cast_unicode(file, util_path.fs_encoding)
@@ -463,34 +466,25 @@ def _format_list(self, extracted_list):
463466

464467
Colors = self.Colors
465468
list = []
466-
for filename, lineno, name, line in extracted_list[:-1]:
467-
item = " %s in %s%s%s\n" % (
468-
_format_filename(
469-
filename, Colors.filename, Colors.Normal, lineno=lineno
470-
),
471-
Colors.name,
472-
name,
473-
Colors.Normal,
469+
for ind, (filename, lineno, name, line) in enumerate(extracted_list):
470+
normalCol, nameCol, fileCol, lineCol = (
471+
# Emphasize the last entry
472+
(Colors.normalEm, Colors.nameEm, Colors.filenameEm, Colors.line)
473+
if ind == len(extracted_list) - 1
474+
else (Colors.Normal, Colors.name, Colors.filename, "")
474475
)
476+
477+
fns = _format_filename(filename, fileCol, normalCol, lineno=lineno)
478+
item = f"{normalCol} {fns}"
479+
480+
if name != "<module>":
481+
item += f" in {nameCol}{name}{normalCol}\n"
482+
else:
483+
item += "\n"
475484
if line:
476-
item += ' %s\n' % line.strip()
485+
item += f"{lineCol} {line.strip()}{normalCol}\n"
477486
list.append(item)
478-
# Emphasize the last entry
479-
filename, lineno, name, line = extracted_list[-1]
480-
item = "%s %s in %s%s%s%s\n" % (
481-
Colors.normalEm,
482-
_format_filename(
483-
filename, Colors.filenameEm, Colors.normalEm, lineno=lineno
484-
),
485-
Colors.nameEm,
486-
name,
487-
Colors.normalEm,
488-
Colors.Normal,
489-
)
490-
if line:
491-
item += '%s %s%s\n' % (Colors.line, line.strip(),
492-
Colors.Normal)
493-
list.append(item)
487+
494488
return list
495489

496490
def _format_exception_only(self, etype, value):
@@ -687,7 +681,7 @@ def format_record(self, frame_info):
687681

688682
func = frame_info.executing.code_qualname()
689683
if func == "<module>":
690-
call = tpl_call.format(file=func, scope="")
684+
call = ""
691685
else:
692686
# Decide whether to include variable details or not
693687
var_repr = eqrepr if self.include_vars else nullrepr
@@ -731,7 +725,7 @@ def format_record(self, frame_info):
731725
if lvals_list:
732726
lvals = '%s%s' % (indent, em_normal.join(lvals_list))
733727

734-
result = "%s, %s\n" % (link, call)
728+
result = f'{link}{", " if call else ""}{call}\n'
735729

736730
result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
737731
return result
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Restore line numbers for Input
2+
==================================
3+
4+
Line number information in tracebacks from input are restored.
5+
Line numbers from input were removed during the transition to v8 enhanced traceback reporting.
6+
7+
So, instead of::
8+
9+
---------------------------------------------------------------------------
10+
ZeroDivisionError Traceback (most recent call last)
11+
Input In [3], in <cell line: 1>()
12+
----> 1 myfunc(2)
13+
14+
Input In [2], in myfunc(z)
15+
1 def myfunc(z):
16+
----> 2 foo.boo(z-1)
17+
18+
File ~/code/python/ipython/foo.py:3, in boo(x)
19+
2 def boo(x):
20+
----> 3 return 1/(1-x)
21+
22+
ZeroDivisionError: division by zero
23+
24+
The error traceback now looks like::
25+
26+
---------------------------------------------------------------------------
27+
ZeroDivisionError Traceback (most recent call last)
28+
Cell In [3], line 1
29+
----> 1 myfunc(2)
30+
31+
Cell In [2], line 2, in myfunc(z)
32+
1 def myfunc(z):
33+
----> 2 foo.boo(z-1)
34+
35+
File ~/code/python/ipython/foo.py:3, in boo(x)
36+
2 def boo(x):
37+
----> 3 return 1/(1-x)
38+
39+
ZeroDivisionError: division by zero
40+
41+
or, with xmode=Plain::
42+
43+
Traceback (most recent call last):
44+
Cell In [12], line 1
45+
myfunc(2)
46+
Cell In [6], line 2 in myfunc
47+
foo.boo(z-1)
48+
File ~/code/python/ipython/foo.py:3 in boo
49+
return 1/(1-x)
50+
ZeroDivisionError: division by zero

0 commit comments

Comments
 (0)