Skip to content

Commit

Permalink
add decorator event
Browse files Browse the repository at this point in the history
  • Loading branch information
smacke committed Feb 23, 2024
1 parent c9ebdb6 commit 08459f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyccolo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
after_while_test = TraceEvent.after_while_test
before_lambda = TraceEvent.before_lambda
after_lambda = TraceEvent.after_lambda
decorator = TraceEvent.decorator
before_call = TraceEvent.before_call
after_call = TraceEvent.after_call
before_argument = TraceEvent.before_argument
Expand Down
29 changes: 29 additions & 0 deletions pyccolo/expr_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,35 @@ def visit_Dict(self, node: ast.Dict):
node.values = traced_values
return self.visit_literal(node, should_inner_visit=False)

def visit_FunctionDef_or_AsyncFunctionDef(
self, node: Union[ast.FunctionDef, ast.AsyncFunctionDef]
):
self.generic_visit(node.args)
for elt in node.body:
self.visit(elt)
new_decorator_list = []
for decorator in node.decorator_list:
if not self.handler_predicate_by_event[TraceEvent.decorator](decorator):
new_decorator_list.append(self.visit(decorator))
continue
with fast.location_of(decorator):
new_decorator_list.append(
self.emit(
TraceEvent.decorator,
decorator,
ret=self.visit(decorator),
func_node_id=self.get_copy_id_ast(node),
)
)
node.decorator_list = new_decorator_list
return node

def visit_FunctionDef(self, node: ast.FunctionDef):
return self.visit_FunctionDef_or_AsyncFunctionDef(node)

def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef):
return self.visit_FunctionDef_or_AsyncFunctionDef(node)

def visit_Return(self, node: ast.Return):
if node.value is None:
return node
Expand Down
1 change: 1 addition & 0 deletions pyccolo/trace_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TraceEvent(Enum):
before_lambda = "before_lambda"
after_lambda = "after_lambda"

decorator = "decorator"
before_call = "before_call"
after_call = "after_call"
before_argument = "before_argument"
Expand Down
1 change: 1 addition & 0 deletions test/test_trace_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def foo():
pyc.after_function_execution,
pyc.after_call,
pyc.after_load_complex_symbol,
pyc.decorator,
pyc.before_function_body,
pyc.before_stmt,
pyc.before_assign_rhs,
Expand Down

0 comments on commit 08459f1

Please sign in to comment.