shairubin / fun_with_ast

let's have fun manipulating ASTs in Python
Apache License 2.0
0 stars 1 forks source link

support multi line jstr without f prefix #157

Closed shairubin closed 10 months ago

shairubin commented 10 months ago

def get_source_lines_and_file( obj: Any, error_msg: Optional[str] = None, ) -> Tuple[List[str], int, Optional[str]]: """ Wrapper around inspect.getsourcelines and inspect.getsourcefile.

Returns: (sourcelines, file_lino, filename)
"""
filename = None  # in case getsourcefile throws
try:
    filename = inspect.getsourcefile(obj)
    sourcelines, file_lineno = inspect.getsourcelines(obj)
except OSError as e:
    msg = (
        f"Can't get source for {obj}. TorchScript requires source access in "
        "order to carry out compilation, make sure original .py files are "
        "available."
    )
    if error_msg:
        msg += "\n" + error_msg
    raise OSError(msg) from e

return sourcelines, file_lineno, filename