[TorchScript] attach target function to OSError when source can't be found (#125248)

Before, it would be hard to figure out which function/module in particular was causing the OSError. Now we'll try to print the function/module string.

Differential Revision: [D56768365](https://our.internmc.facebook.com/intern/diff/D56768365)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/125248
Approved by: https://github.com/eellison
This commit is contained in:
David Berard
2024-04-30 11:30:32 -07:00
committed by PyTorch MergeBot
parent 8f31988088
commit e9ce23985f

View File

@ -380,7 +380,12 @@ def get_type_hint_captures(fn):
# This may happen in cases where the function is synthesized dynamically at runtime.
src = loader.get_source(fn)
if src is None:
src = inspect.getsource(fn)
try:
src = inspect.getsource(fn)
except OSError as e:
raise OSError(
f"Failed to get source for {fn} using inspect.getsource"
) from e
# Gather a dictionary of parameter name -> type, skipping any parameters whose annotated
# types are strings. These are only understood by TorchScript in the context of a type annotation