(self, program: str | doc.AST)
| 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: |
| 86 | # It's a work around for Jupyter problem. |
| 87 | # Since `findsource` is an internal API of inspect, we just use it |
| 88 | # as a fallback method. |
| 89 | src, _ = inspect.findsource(program) # type: ignore |
| 90 | self.full_source = "".join(src) |
| 91 | |
| 92 | def as_ast(self) -> doc.AST: |
| 93 | """Parse the source code into AST. |
nothing calls this directly
no test coverage detected