Skip to content

Commit

Permalink
Add support for _source_code_ attribute for runtime generated class d…
Browse files Browse the repository at this point in the history
…efinitions
  • Loading branch information
leonardt authored and Kuree committed Feb 12, 2021
1 parent a7f0ed1 commit 4eef029
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pysv/pyast.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ def has_return(func: typing.Callable):
def get_class_src(cls: type):
# we need to remove all the @dpi decorators in the ast
# so that they can actually be called
code = inspect.getsource(cls)
if hasattr(cls, "_source_code_"):
# If we define a class inside exec (e.g. when doing AST rewrites on the
# Python source) we can't use inspect.getsource (since the code comes
# froms a string rather than a file). So, we provide a special
# attribute that downstream tools can use to store the generated source
# code
code = cls._source_code_
else:
code = inspect.getsource(cls)
class_tree = ast.parse(textwrap.dedent(code))
# need to clear out any DPI function decorator
class_ast = class_tree.body[0]
Expand Down

0 comments on commit 4eef029

Please sign in to comment.