Skip to content

Commit c1d3e2d

Browse files
author
Philip Guo
committed
add support for tracing imported modules (by skipping over module function calls)
1 parent e42afb6 commit c1d3e2d

5 files changed

Lines changed: 151 additions & 22 deletions

File tree

‎v3/pg_logger.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,20 @@ def interaction(self, frame, traceback, event_type):
308308
top_frame = tos[0]
309309
lineno = tos[1]
310310

311-
# don't trace inside of our __restricted_import__ helper function
312-
if top_frame.f_code.co_name == '__restricted_import__':
311+
# don't trace inside of ANY functions that aren't user-written code
312+
# (e.g., those from imported modules -- e.g., random, re -- or the
313+
# __restricted_import__ function in this file)
314+
#
315+
# it seems like user-written code has a filename of '<string>',
316+
# but maybe there are false positives too?
317+
if self.canonic(frame.f_code.co_filename) != '<string>':
313318
return
314319

320+
# don't trace inside of our __restricted_import__ helper function
321+
# (this check is now subsumed by the above check)
322+
#if top_frame.f_code.co_name == '__restricted_import__':
323+
# return
324+
315325
self.encoder.reset_heap() # VERY VERY VERY IMPORTANT,
316326
# or else we won't properly capture heap object mutations in the trace!
317327

‎v3/tests/backend-tests/print_builtins_error.golden‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎v3/tests/backend-tests/print_builtins_error.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"code": "# test to make sure nothing crashes when tracing code in pure-Python\n# function calls imported from modules\nimport random\nrandom.randint(10, 100)\n\nfrom re import compile\nx = compile('foo')\n",
3+
"trace": [
4+
{
5+
"ordered_globals": [],
6+
"stdout": "",
7+
"func_name": "<module>",
8+
"stack_to_render": [],
9+
"globals": {},
10+
"heap": {},
11+
"line": 3,
12+
"event": "step_line"
13+
},
14+
{
15+
"ordered_globals": [
16+
"random"
17+
],
18+
"stdout": "",
19+
"func_name": "<module>",
20+
"stack_to_render": [],
21+
"globals": {
22+
"random": [
23+
"REF",
24+
1
25+
]
26+
},
27+
"heap": {
28+
"1": [
29+
"module",
30+
"random"
31+
]
32+
},
33+
"line": 4,
34+
"event": "step_line"
35+
},
36+
{
37+
"ordered_globals": [
38+
"random"
39+
],
40+
"stdout": "",
41+
"func_name": "<module>",
42+
"stack_to_render": [],
43+
"globals": {
44+
"random": [
45+
"REF",
46+
1
47+
]
48+
},
49+
"heap": {
50+
"1": [
51+
"module",
52+
"random"
53+
]
54+
},
55+
"line": 6,
56+
"event": "step_line"
57+
},
58+
{
59+
"ordered_globals": [
60+
"random",
61+
"compile"
62+
],
63+
"stdout": "",
64+
"func_name": "<module>",
65+
"stack_to_render": [],
66+
"globals": {
67+
"compile": [
68+
"REF",
69+
2
70+
],
71+
"random": [
72+
"REF",
73+
1
74+
]
75+
},
76+
"heap": {
77+
"1": [
78+
"module",
79+
"random"
80+
],
81+
"2": [
82+
"FUNCTION",
83+
"compile(pattern, flags)",
84+
null
85+
]
86+
},
87+
"line": 7,
88+
"event": "step_line"
89+
},
90+
{
91+
"ordered_globals": [
92+
"random",
93+
"compile",
94+
"x"
95+
],
96+
"stdout": "",
97+
"func_name": "<module>",
98+
"stack_to_render": [],
99+
"globals": {
100+
"compile": [
101+
"REF",
102+
2
103+
],
104+
"x": [
105+
"REF",
106+
3
107+
],
108+
"random": [
109+
"REF",
110+
1
111+
]
112+
},
113+
"heap": {
114+
"1": [
115+
"module",
116+
"random"
117+
],
118+
"2": [
119+
"FUNCTION",
120+
"compile(pattern, flags)",
121+
null
122+
],
123+
"3": [
124+
"_sre.SRE_Pattern",
125+
"<_sre.SRE_Pattern object at 0xADDR>"
126+
]
127+
},
128+
"line": 7,
129+
"event": "return"
130+
}
131+
]
132+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# test to make sure nothing crashes when tracing code in pure-Python
2+
# function calls imported from modules
3+
import random
4+
random.randint(10, 100)
5+
6+
from re import compile
7+
x = compile('foo')

0 commit comments

Comments
 (0)