Source code class for TVMScript. It is constructed by source code str or doc AST tree. Parameters ---------- source_name : str The filename of the file where the source code locates. start_line : int The first line number of the source code. start_column :
| 26 | |
| 27 | |
| 28 | class Source: |
| 29 | """Source code class for TVMScript. |
| 30 | |
| 31 | It is constructed by source code str or doc AST tree. |
| 32 | |
| 33 | Parameters |
| 34 | ---------- |
| 35 | source_name : str |
| 36 | The filename of the file where the source code locates. |
| 37 | |
| 38 | start_line : int |
| 39 | The first line number of the source code. |
| 40 | |
| 41 | start_column : int |
| 42 | The first column number of the first line of the source code. |
| 43 | |
| 44 | source : str |
| 45 | The source code str of source code. |
| 46 | |
| 47 | full_source : str |
| 48 | The complete source code of the file where the source code locates. |
| 49 | """ |
| 50 | |
| 51 | source_name: str |
| 52 | start_line: int |
| 53 | start_column: int |
| 54 | source: str |
| 55 | full_source: str |
| 56 | |
| 57 | def __init__(self, program: str | doc.AST): |
| 58 | if isinstance(program, str): |
| 59 | self.source_name = "<str>" |
| 60 | self.start_line = 1 |
| 61 | self.start_column = 0 |
| 62 | self.source = program |
| 63 | self.full_source = program |
| 64 | return |
| 65 | |
| 66 | self.source_name = inspect.getsourcefile(program) # type: ignore |
| 67 | lines, self.start_line = getsourcelines(program) # type: ignore |
| 68 | if lines: |
| 69 | self.start_column = len(lines[0]) - len(lines[0].lstrip()) |
| 70 | else: |
| 71 | self.start_column = 0 |
| 72 | if self.start_column and lines: |
| 73 | self.source = "\n".join([l[self.start_column :].rstrip() for l in lines]) |
| 74 | else: |
| 75 | self.source = "".join(lines) |
| 76 | try: |
| 77 | # It will cause a problem when running in Jupyter Notebook. |
| 78 | # `mod` will be <module '__main__'>, which is a built-in module |
| 79 | # and `getsource` will throw a TypeError |
| 80 | mod = inspect.getmodule(program) |
| 81 | if mod: |
| 82 | self.full_source = inspect.getsource(mod) |
| 83 | else: |
| 84 | self.full_source = self.source |
| 85 | except TypeError: |
no outgoing calls
searching dependent graphs…