Skip to content

Commit bfa3b88

Browse files
authored
[mypyc] Report file and line number on uncaught exceptions (#21584)
For example, this will help debug uncaught exceptions in the reference counting transform.
1 parent 363be14 commit bfa3b88

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

mypyc/codegen/emitmodule.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
shared_lib_name,
5757
short_id_from_name,
5858
)
59+
from mypyc.crash import catch_errors
5960
from mypyc.errors import Errors
6061
from mypyc.ir.deps import (
6162
LIBRT_BASE64,
@@ -259,29 +260,31 @@ def compile_scc_to_ir(
259260
env_user_functions[cls.env_user_function] = cls
260261

261262
for module in modules.values():
263+
module_path = result.graph[module.fullname].xpath
262264
for fn in module.functions:
263-
# Insert checks for uninitialized values.
264-
insert_uninit_checks(fn, compiler_options.strict_traceback_checks)
265-
# Insert exception handling.
266-
insert_exception_handling(fn, compiler_options.strict_traceback_checks)
267-
# Insert reference count handling.
268-
insert_ref_count_opcodes(fn)
269-
270-
if fn in env_user_functions:
271-
insert_spills(fn, env_user_functions[fn])
272-
273-
if compiler_options.log_trace:
274-
insert_event_trace_logging(fn, compiler_options)
275-
276-
# Switch to lower abstraction level IR.
277-
lower_ir(fn, compiler_options)
278-
# Calculate implicit module dependencies (needed for librt)
279-
deps = find_implicit_op_dependencies(fn)
280-
if deps is not None:
281-
module.dependencies.update(deps)
282-
# Perform optimizations.
283-
do_copy_propagation(fn, compiler_options)
284-
do_flag_elimination(fn, compiler_options)
265+
with catch_errors(module_path, fn.line):
266+
# Insert checks for uninitialized values.
267+
insert_uninit_checks(fn, compiler_options.strict_traceback_checks)
268+
# Insert exception handling.
269+
insert_exception_handling(fn, compiler_options.strict_traceback_checks)
270+
# Insert reference count handling.
271+
insert_ref_count_opcodes(fn)
272+
273+
if fn in env_user_functions:
274+
insert_spills(fn, env_user_functions[fn])
275+
276+
if compiler_options.log_trace:
277+
insert_event_trace_logging(fn, compiler_options)
278+
279+
# Switch to lower abstraction level IR.
280+
lower_ir(fn, compiler_options)
281+
# Calculate implicit module dependencies (needed for librt)
282+
deps = find_implicit_op_dependencies(fn)
283+
if deps is not None:
284+
module.dependencies.update(deps)
285+
# Perform optimizations.
286+
do_copy_propagation(fn, compiler_options)
287+
do_flag_elimination(fn, compiler_options)
285288

286289
# Calculate implicit dependencies from class attribute types
287290
for cl in module.classes:

mypyc/crash.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ def crash_report(module_path: str, line: int) -> NoReturn:
2929
for s in traceback.format_list(tb + tb2):
3030
print(s.rstrip("\n"))
3131
print(f"{module_path}:{line}: {type(err).__name__}: {err}")
32+
print(
33+
f"{module_path}:{line}: note: this is an internal mypyc error; "
34+
"please report a bug at https://github.com/mypyc/mypyc/issues"
35+
)
3236
raise SystemExit(2)

0 commit comments

Comments
 (0)