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

Skip to content

Commit e023dcb

Browse files
committed
Add line number next to the file in the traceback
Fix some "skip" error messages and docstrings for test. We use filename:lineno as this syntax is understood by many tools to directly open the relevant file at given line.
3 parents 1d7bb78 + c177aef + a680045 commit e023dcb

5 files changed

Lines changed: 51 additions & 45 deletions

File tree

IPython/core/tests/test_completer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ def test_all_completions_dups(self):
419419
with provisionalcompleter():
420420
ip.Completer.use_jedi = jedi_status
421421
matches = c.all_completions("TestCl")
422-
assert matches == ['TestClass'], jedi_status
422+
assert matches == ["TestClass"], (jedi_status, matches)
423423
matches = c.all_completions("TestClass.")
424-
assert len(matches) > 2, jedi_status
424+
assert len(matches) > 2, (jedi_status, matches)
425425
matches = c.all_completions("TestClass.a")
426426
assert matches == ['TestClass.a', 'TestClass.a1'], jedi_status
427427

IPython/core/tests/test_iplib.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def doctest_tb_plain():
4545
4646
In [19]: run simpleerr.py
4747
Traceback (most recent call last):
48-
...line ..., in <module>
48+
File ...:... in <module>
4949
bar(mode)
50-
...line ..., in bar
50+
File ...:... in bar
5151
div0()
52-
...line ..., in div0
52+
File ...:... in div0
5353
x/y
5454
ZeroDivisionError: ...
5555
"""
@@ -132,13 +132,13 @@ def doctest_tb_sysexit():
132132
133133
In [20]: %tb
134134
Traceback (most recent call last):
135-
File ..., in execfile
135+
File ...:... in execfile
136136
exec(compiler(f.read(), fname, "exec"), glob, loc)
137-
File ..., in <module>
137+
File ...:... in <module>
138138
bar(mode)
139-
File ..., in bar
139+
File ...:... in bar
140140
sysexit(stat, mode)
141-
File ..., in sysexit
141+
File ...:... in sysexit
142142
raise SystemExit(stat, f"Mode = {mode}")
143143
SystemExit: (2, 'Mode = exit')
144144

IPython/core/tests/test_ultratb.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,6 @@ def test_indentationerror_shows_line(self):
188188
"""
189189

190190
class SyntaxErrorTest(unittest.TestCase):
191-
def test_syntaxerror_without_lineno(self):
192-
with tt.AssertNotPrints("TypeError"):
193-
with tt.AssertPrints("line unknown"):
194-
ip.run_cell("raise SyntaxError()")
195191

196192
def test_syntaxerror_no_stacktrace_at_compile_time(self):
197193
syntax_error_at_compile_time = """

IPython/core/ultratb.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _format_traceback_lines(lines, Colors, has_colors, lvals):
169169
return res
170170

171171

172-
def _format_filename(file, ColorFilename, ColorNormal):
172+
def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
173173
"""
174174
Format filename lines with `In [n]` if it's the nth code cell or `File *.py` if it's a module.
175175
@@ -185,14 +185,17 @@ def _format_filename(file, ColorFilename, ColorNormal):
185185

186186
if ipinst is not None and file in ipinst.compile._filename_map:
187187
file = "[%s]" % ipinst.compile._filename_map[file]
188-
tpl_link = "Input %sIn %%s%s" % (ColorFilename, ColorNormal)
188+
tpl_link = f"Input {ColorFilename}In {{file}}{ColorNormal}"
189189
else:
190190
file = util_path.compress_user(
191191
py3compat.cast_unicode(file, util_path.fs_encoding)
192192
)
193-
tpl_link = "File %s%%s%s" % (ColorFilename, ColorNormal)
193+
if lineno is None:
194+
tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}"
195+
else:
196+
tpl_link = f"File {ColorFilename}{{file}}:{{lineno}}{ColorNormal}"
194197

195-
return tpl_link % file
198+
return tpl_link.format(file=file, lineno=lineno)
196199

197200
#---------------------------------------------------------------------------
198201
# Module classes
@@ -439,11 +442,10 @@ def _format_list(self, extracted_list):
439442
Colors = self.Colors
440443
list = []
441444
for filename, lineno, name, line in extracted_list[:-1]:
442-
item = " %s, line %s%d%s, in %s%s%s\n" % (
443-
_format_filename(filename, Colors.filename, Colors.Normal),
444-
Colors.lineno,
445-
lineno,
446-
Colors.Normal,
445+
item = " %s in %s%s%s\n" % (
446+
_format_filename(
447+
filename, Colors.filename, Colors.Normal, lineno=lineno
448+
),
447449
Colors.name,
448450
name,
449451
Colors.Normal,
@@ -453,12 +455,11 @@ def _format_list(self, extracted_list):
453455
list.append(item)
454456
# Emphasize the last entry
455457
filename, lineno, name, line = extracted_list[-1]
456-
item = "%s %s, line %s%d%s, in %s%s%s%s\n" % (
457-
Colors.normalEm,
458-
_format_filename(filename, Colors.filenameEm, Colors.normalEm),
459-
Colors.linenoEm,
460-
lineno,
458+
item = "%s %s in %s%s%s%s\n" % (
461459
Colors.normalEm,
460+
_format_filename(
461+
filename, Colors.filenameEm, Colors.normalEm, lineno=lineno
462+
),
462463
Colors.nameEm,
463464
name,
464465
Colors.normalEm,
@@ -501,14 +502,15 @@ def _format_exception_only(self, etype, value):
501502
lineno = "unknown"
502503
textline = ""
503504
list.append(
504-
"%s %s, line %s%s%s\n"
505+
"%s %s%s\n"
505506
% (
506507
Colors.normalEm,
507508
_format_filename(
508-
value.filename, Colors.filenameEm, Colors.normalEm
509+
value.filename,
510+
Colors.filenameEm,
511+
Colors.normalEm,
512+
lineno=(None if lineno == "unknown" else lineno),
509513
),
510-
Colors.linenoEm,
511-
lineno,
512514
Colors.Normal,
513515
)
514516
)
@@ -628,27 +630,35 @@ def format_record(self, frame_info):
628630
return ' %s[... skipping similar frames: %s]%s\n' % (
629631
Colors.excName, frame_info.description, ColorsNormal)
630632

631-
indent = ' ' * INDENT_SIZE
632-
em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
633-
tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
634-
ColorsNormal)
635-
tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
636-
(Colors.vName, Colors.valEm, ColorsNormal)
637-
tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
633+
indent = " " * INDENT_SIZE
634+
em_normal = "%s\n%s%s" % (Colors.valEm, indent, ColorsNormal)
635+
tpl_call = f"in {Colors.vName}{{file}}{Colors.valEm}{{scope}}{ColorsNormal}"
636+
tpl_call_fail = "in %s%%s%s(***failed resolving arguments***)%s" % (
637+
Colors.vName,
638+
Colors.valEm,
639+
ColorsNormal,
640+
)
641+
tpl_name_val = "%%s %s= %%s%s" % (Colors.valEm, ColorsNormal)
638642

639-
link = _format_filename(frame_info.filename, Colors.filenameEm, ColorsNormal)
643+
link = _format_filename(
644+
frame_info.filename,
645+
Colors.filenameEm,
646+
ColorsNormal,
647+
lineno=frame_info.lineno,
648+
)
640649
args, varargs, varkw, locals_ = inspect.getargvalues(frame_info.frame)
641650

642651
func = frame_info.executing.code_qualname()
643-
if func == '<module>':
644-
call = tpl_call % (func, '')
652+
if func == "<module>":
653+
call = tpl_call.format(file=func, scope="")
645654
else:
646655
# Decide whether to include variable details or not
647656
var_repr = eqrepr if self.include_vars else nullrepr
648657
try:
649-
call = tpl_call % (func, inspect.formatargvalues(args,
650-
varargs, varkw,
651-
locals_, formatvalue=var_repr))
658+
scope = inspect.formatargvalues(
659+
args, varargs, varkw, locals_, formatvalue=var_repr
660+
)
661+
call = tpl_call.format(file=func, scope=scope)
652662
except KeyError:
653663
# This happens in situations like errors inside generator
654664
# expressions, where local variables are listed in the

IPython/testing/plugin/pytest_ipdoctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def _check_all_skipped(test: "doctest.DocTest") -> None:
503503

504504
all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
505505
if all_skipped:
506-
pytest.skip("all tests skipped by +SKIP option")
506+
pytest.skip("all docstests skipped by +SKIP option")
507507

508508

509509
def _is_mocked(obj: object) -> bool:

0 commit comments

Comments
 (0)