Skip to content

Commit baa39b8

Browse files
committed
Refine tracing start logic to handle top-level external calls
- Add caller frame checks to detect if an external call is triggered by user script code beyond the import threshold. - Ensure tracing begins even for top-level calls to external libraries after imports. - Prevent unnecessary tracing of functions invoked during the import phase.
1 parent 6795485 commit baa39b8

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

pyftrace/engine/pyftrace_setprofile.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def run_python_script(self, script_path, script_args):
4141
try:
4242
exec(code_object, {"__file__": script_path, "__name__": "__main__"})
4343
finally:
44+
self.tracing_started = False
4445
self.cleanup_tracing()
4546
sys.path = old_sys_path
4647
sys.argv = old_sys_argv
@@ -85,10 +86,21 @@ def handle_call_event(self, frame, arg, is_c_call=False):
8586
return
8687

8788
if not self.tracing_started:
88-
if filename == self.script_name and frame.f_lineno > self.import_end_line:
89-
self.tracing_started = True
89+
start_tracing = False
90+
if filename == self.script_name and code and frame.f_lineno > self.import_end_line:
91+
start_tracing = True
9092
else:
93+
if caller_frame and caller_frame.f_code:
94+
caller_filename = resolve_filename(caller_frame.f_code, None)
95+
if caller_filename:
96+
caller_filename = os.path.abspath(caller_filename)
97+
if (caller_filename == self.script_name
98+
and caller_frame.f_lineno > self.import_end_line):
99+
start_tracing = True
100+
101+
if not start_tracing:
91102
return
103+
self.tracing_started = True
92104

93105
if func_name == '<module>':
94106
return
@@ -100,14 +112,19 @@ def handle_call_event(self, frame, arg, is_c_call=False):
100112
return
101113
trace_this = True
102114
else:
103-
if self.should_trace(filename):
104-
trace_this = True
105-
elif self.verbose and module_name == 'builtins':
106-
trace_this = True
115+
if filename is None:
116+
# filename is None: C-extension or similar. If verbose, trace anyway.
117+
if self.verbose:
118+
trace_this = True
119+
else:
120+
# Normal logic for user-defined or third-party code
121+
if self.should_trace(filename):
122+
trace_this = True
123+
elif self.verbose and module_name == 'builtins':
124+
trace_this = True
107125

108126
if trace_this:
109127
indent = " " * self.current_depth()
110-
111128
if not is_c_call and code:
112129
func_def_lineno = code.co_firstlineno
113130
else:
@@ -124,11 +141,9 @@ def handle_call_event(self, frame, arg, is_c_call=False):
124141

125142
if self.show_path:
126143
if func_def_lineno:
127-
func_location = f"{func_name}@{filename}:{func_def_lineno}"
128-
elif filename:
129-
func_location = f"{func_name}@{filename}"
144+
func_location = f"{func_name}@{filename}:{func_def_lineno}" if filename else func_name
130145
else:
131-
func_location = func_name
146+
func_location = f"{func_name}@{filename}" if filename else func_name
132147
if call_filename and call_lineno:
133148
call_location = f"from {call_filename}:{call_lineno}"
134149
else:
@@ -183,17 +198,19 @@ def handle_return_event(self, frame, arg, is_c_return):
183198
return
184199
trace_this = True
185200
else:
186-
if self.call_stack and self.call_stack[-1] == func_name:
187-
trace_this = True
188-
189-
if not trace_this:
190-
if self.verbose and module_name == 'builtins':
191-
if self.call_stack and self.call_stack[-1] == func_name:
192-
trace_this = True
201+
if filename is None:
202+
if self.verbose and self.call_stack and self.call_stack[-1] == func_name:
203+
trace_this = True
204+
else:
205+
if self.call_stack and self.call_stack[-1] == func_name:
206+
trace_this = True
207+
else:
208+
if self.verbose and module_name == 'builtins':
209+
if self.call_stack and self.call_stack[-1] == func_name:
210+
trace_this = True
193211

194212
if trace_this and self.call_stack and self.call_stack[-1] == func_name:
195-
func_name = self.call_stack.pop()
196-
213+
self.call_stack.pop()
197214
indent = " " * self.current_depth()
198215

199216
if self.show_path:

0 commit comments

Comments
 (0)