From f7014a87a87107e37dab7dc4542b021260d4a4f6 2023-05-01 04:19:51 From: EtiennePelletier Date: 2023-05-01 04:19:51 Subject: [PATCH] Apply some small formatting fixes in ultratb --- diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index 87f6b73..c4de95d 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -51,24 +51,24 @@ def recursionlimit(frames): class ChangedPyFileTest(unittest.TestCase): def test_changing_py_file(self): """Traceback produced if the line where the error occurred is missing? - + https://github.com/ipython/ipython/issues/1456 """ with TemporaryDirectory() as td: fname = os.path.join(td, "foo.py") with open(fname, "w", encoding="utf-8") as f: f.write(file_1) - + with prepended_to_syspath(td): ip.run_cell("import foo") - + with tt.AssertPrints("ZeroDivisionError"): ip.run_cell("foo.f()") - + # Make the file shorter, so the line of the error is missing. with open(fname, "w", encoding="utf-8") as f: f.write(file_2) - + # For some reason, this was failing on the *second* call after # changing the file, so we call f() twice. with tt.AssertNotPrints("Internal Python error", channel='stderr'): @@ -92,27 +92,27 @@ class NonAsciiTest(unittest.TestCase): fname = os.path.join(td, u"fooé.py") with open(fname, "w", encoding="utf-8") as f: f.write(file_1) - + with prepended_to_syspath(td): ip.run_cell("import foo") - + with tt.AssertPrints("ZeroDivisionError"): ip.run_cell("foo.f()") - + def test_iso8859_5(self): with TemporaryDirectory() as td: fname = os.path.join(td, 'dfghjkl.py') with io.open(fname, 'w', encoding='iso-8859-5') as f: f.write(iso_8859_5_file) - + with prepended_to_syspath(td): ip.run_cell("from dfghjkl import fail") - + with tt.AssertPrints("ZeroDivisionError"): with tt.AssertPrints(u'дбИЖ', suppress=False): ip.run_cell('fail()') - + def test_nonascii_msg(self): cell = u"raise Exception('é')" expected = u"Exception('é')" @@ -167,12 +167,12 @@ class IndentationErrorTest(unittest.TestCase): with tt.AssertPrints("IndentationError"): with tt.AssertPrints("zoon()", suppress=False): ip.run_cell(indentationerror_file) - + with TemporaryDirectory() as td: fname = os.path.join(td, "foo.py") with open(fname, "w", encoding="utf-8") as f: f.write(indentationerror_file) - + with tt.AssertPrints("IndentationError"): with tt.AssertPrints("zoon()", suppress=False): ip.magic('run %s' % fname) diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 30367d7..e0d9ddd 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -591,7 +591,7 @@ class ListTB(TBTools): """ Colors = self.Colors - list = [] + output_list = [] for ind, (filename, lineno, name, line) in enumerate(extracted_list): normalCol, nameCol, fileCol, lineCol = ( # Emphasize the last entry @@ -609,9 +609,9 @@ class ListTB(TBTools): item += "\n" if line: item += f"{lineCol} {line.strip()}{normalCol}\n" - list.append(item) + output_list.append(item) - return list + return output_list def _format_exception_only(self, etype, value): """Format the exception part of a traceback. @@ -628,11 +628,11 @@ class ListTB(TBTools): """ have_filedata = False Colors = self.Colors - list = [] + output_list = [] stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) if value is None: # Not sure if this can still happen in Python 2.6 and above - list.append(stype + '\n') + output_list.append(stype + "\n") else: if issubclass(etype, SyntaxError): have_filedata = True @@ -643,7 +643,7 @@ class ListTB(TBTools): else: lineno = "unknown" textline = "" - list.append( + output_list.append( "%s %s%s\n" % ( Colors.normalEm, @@ -663,31 +663,33 @@ class ListTB(TBTools): i = 0 while i < len(textline) and textline[i].isspace(): i += 1 - list.append('%s %s%s\n' % (Colors.line, - textline.strip(), - Colors.Normal)) + output_list.append( + "%s %s%s\n" % (Colors.line, textline.strip(), Colors.Normal) + ) if value.offset is not None: s = ' ' for c in textline[i:value.offset - 1]: if c.isspace(): s += c else: - s += ' ' - list.append('%s%s^%s\n' % (Colors.caret, s, - Colors.Normal)) + s += " " + output_list.append( + "%s%s^%s\n" % (Colors.caret, s, Colors.Normal) + ) try: s = value.msg except Exception: s = self._some_str(value) if s: - list.append('%s%s:%s %s\n' % (stype, Colors.excName, - Colors.Normal, s)) + output_list.append( + "%s%s:%s %s\n" % (stype, Colors.excName, Colors.Normal, s) + ) else: - list.append('%s\n' % stype) + output_list.append("%s\n" % stype) # PEP-678 notes - list.extend(f"{x}\n" for x in getattr(value, "__notes__", [])) + output_list.extend(f"{x}\n" for x in getattr(value, "__notes__", [])) # sync with user hooks if have_filedata: @@ -695,7 +697,7 @@ class ListTB(TBTools): if ipinst is not None: ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) - return list + return output_list def get_exception_only(self, etype, value): """Only print the exception type and message, without a traceback. @@ -1012,6 +1014,7 @@ class VerboseTB(TBTools): etype, evalue = str, sys.exc_info()[:2] etype_str, evalue_str = map(str, (etype, evalue)) + # PEP-678 notes notes = getattr(evalue, "__notes__", []) if not isinstance(notes, Sequence) or isinstance(notes, (str, bytes)): notes = [_safe_string(notes, "__notes__", func=repr)]