| 
8 | 8 | import ast  | 
9 | 9 | import inspect  | 
10 | 10 | import sys  | 
11 |  | -import textwrap  | 
12 | 11 | import types  | 
13 | 12 | from typing import Any, Callable, Optional, Sequence, cast  | 
14 | 13 | 
 
  | 
15 | 14 | import onnx.helper  | 
16 | 15 | 
 
  | 
17 | 16 | import onnxscript  | 
18 | 17 | from onnxscript import converter, irbuilder, values  | 
19 |  | - | 
20 |  | - | 
21 |  | -def get_src_and_ast(f: types.FunctionType) -> tuple[str, ast.FunctionDef]:  | 
22 |  | -    try:  | 
23 |  | -        src = inspect.getsource(f)  | 
24 |  | -    except OSError as e:  | 
25 |  | -        raise RuntimeError(  | 
26 |  | -            f"Decorator script does not work on dynamically "  | 
27 |  | -            f"compiled function {f.__name__}."  | 
28 |  | -        ) from e  | 
29 |  | -    src = textwrap.dedent(src)  | 
30 |  | -    top_level_ast = ast.parse(src)  | 
31 |  | -    assert isinstance(top_level_ast, ast.Module)  | 
32 |  | -    assert len(top_level_ast.body) == 1  | 
33 |  | -    f_ast = top_level_ast.body[0]  | 
34 |  | -    assert isinstance(f_ast, ast.FunctionDef)  | 
35 |  | -    return src, f_ast  | 
36 |  | - | 
37 |  | - | 
38 |  | -def get_ast(f: types.FunctionType) -> ast.FunctionDef:  | 
39 |  | -    _, f_ast = get_src_and_ast(f)  | 
40 |  | -    return f_ast  | 
 | 18 | +from onnxscript._internal import ast_utils  | 
41 | 19 | 
 
  | 
42 | 20 | 
 
  | 
43 | 21 | def script_check(  | 
@@ -104,7 +82,7 @@ def transform(f: types.FunctionType) -> onnxscript.OnnxFunction:  | 
104 | 82 |         if not inspect.isfunction(f):  | 
105 | 83 |             raise TypeError("The ONNXScript decorator should be applied to functions only.")  | 
106 | 84 | 
 
  | 
107 |  | -        src, f_ast = get_src_and_ast(f)  # pylint: disable=redefined-outer-name  | 
 | 85 | +        src, f_ast = ast_utils.get_src_and_ast(f)  | 
108 | 86 |         # The script should be compiled using the globals/locals at the definition site.  | 
109 | 87 |         # This allows the script to reference names defined outside the script,  | 
110 | 88 |         # which is used for a few different purposes.  | 
 | 
0 commit comments