@@ -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
0 commit comments