Skip to content

Commit

Permalink
fix: rename syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams committed Apr 30, 2024
1 parent 477a84a commit 901879b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python/py_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ def format_exception(*, exception, traceback, filename, new_filename=None):
_replace_startswith(trace, f' File "{filename}"', f' File "{new_filename}"')
for trace in traces
]
return build_message(
traces=renamed_traces, exception_list=format_exception_only(exception)
)
renamed_exception = [
_replace_startswith(e, f' File "{filename}"', f' File "{new_filename}"')
for e in format_exception_only(exception)
]
return build_message(traces=renamed_traces, exception_list=renamed_exception)
21 changes: 21 additions & 0 deletions python/py_helpers.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,27 @@ def nest():
File "main.py", line 4, in <module>
File "main.py", line 3, in nest
ValueError: This error has no value
"""

try:
exec(code)
except Exception:
_last_type, last_value, last_traceback = sys.exc_info()
formatted_exception = format_exception(
exception=last_value,
traceback=last_traceback,
filename="<string>",
new_filename="main.py",
)
self.assertEqual(formatted_exception, expected_str)

def test_format_and_rename_syntax_error(self):
code = "var ="
expected_str = """Traceback (most recent call last):
File "main.py", line 1
var =
^
SyntaxError: invalid syntax
"""

try:
Expand Down

0 comments on commit 901879b

Please sign in to comment.