Skip to content

Commit cd975c3

Browse files
committed
update
1 parent 50e8e8c commit cd975c3

13 files changed

Lines changed: 554 additions & 28492 deletions

ControlFlowGraphExample.jpeg

-64 Bytes
Loading

NeXT.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,26 @@ def __init__(self):
1111
self.tracer = trace_execution.Trace(count=False, trace=True, countfuncs=False, countcallers=False)
1212
self.execution_trace = []
1313
self.idx = 0
14+
self.error = None
15+
self.asserterror = False
1416

1517
def trace_execution(self, frame, event, arg):
16-
if '__builtins__' not in frame.f_locals and ".0" not in frame.f_locals and len(frame.f_locals) > 0 and 'code' not in frame.f_locals:
18+
if '__builtins__' not in frame.f_locals and ".0" not in frame.f_locals and len(frame.f_locals) > 0:
1719
code = frame.f_code
1820
lineno = frame.f_lineno
1921
locals_snapshot = copy.deepcopy(frame.f_locals)
2022
self.execution_trace.append([lineno, locals_snapshot])
2123
return self.trace_execution
2224

23-
def start_tracing(self, func):
25+
def start_tracing(self, code):
2426
sys.settrace(self.trace_execution)
25-
func()
27+
try:
28+
exec(code, {})
29+
except AssertionError as e:
30+
self.error = e
31+
self.asserterror = True
32+
except Exception as e:
33+
self.error = e
2634
sys.settrace(None)
2735

2836
def get_execution_trace(self):
@@ -44,16 +52,16 @@ def generate_commented_code(file_path, in_line_cmt):
4452
last_comment = comments[-1]
4553

4654
# Handle NO_CHANGE scenario
47-
if isinstance(first_comment[1], str) and first_comment[1] == "NO_CHANGE":
48-
first_comment_str = f"({first_comment[0]}) NO_CHANGE"
55+
if isinstance(first_comment[1], str):
56+
first_comment_str = f"({first_comment[0]}) {first_comment[1]}"
4957
else:
5058
first_comment_str = f"({first_comment[0]}) " + "; ".join([f"{k}={v}" for k, v in first_comment[1].items()])
5159

5260
if first_comment == last_comment:
5361
inline_comment = f" # {first_comment_str}"
5462
else:
55-
if isinstance(last_comment[1], str) and last_comment[1] == "NO_CHANGE":
56-
last_comment_str = f"({last_comment[0]}) NO_CHANGE"
63+
if isinstance(last_comment[1], str):
64+
last_comment_str = f"({last_comment[0]}) {last_comment[1]}"
5765
else:
5866
last_comment_str = f"({last_comment[0]}) " + "; ".join([f"{k}={v}" for k, v in last_comment[1].items()])
5967

@@ -65,30 +73,17 @@ def generate_commented_code(file_path, in_line_cmt):
6573
else:
6674
commented_code.append(line)
6775

68-
# Special handling for return statements if necessary
69-
for i, line in enumerate(commented_code):
70-
if 'return' in line:
71-
return_line = in_line_cmt.get(lineno, [])
72-
if return_line:
73-
return_vars = return_line[0][1]
74-
if isinstance(return_vars, str) and return_vars == "NO_CHANGE":
75-
return_comment = "NO_CHANGE"
76-
else:
77-
return_comment = "; ".join([f"{k}={v}" for k, v in return_vars.items()])
78-
commented_code[i] = f"{line} # __return__=({return_comment})"
79-
8076
return "\n".join(commented_code)
8177

8278
def execute_and_trace(file_path):
8379
with open(file_path, 'r') as file:
8480
code = compile(file.read(), file_path, 'exec')
8581

86-
def run_code():
87-
exec(code, {})
88-
8982
tracer = ExecutionTracer()
90-
tracer.start_tracing(run_code)
83+
tracer.start_tracing(code)
9184
execution_trace = tracer.get_execution_trace()
85+
error = tracer.error
86+
asserterror = tracer.asserterror
9287

9388
intermediate_value = []
9489
source = linecache.getlines(file_path)
@@ -98,14 +93,30 @@ def run_code():
9893
for i in range(len(code_line)):
9994
if code_line[i].find('if') != -1 or code_line[i].find('elif') != -1 or code_line[i].find('else') != -1:
10095
condition_line.append(i+1)
96+
if code_line[i].find('assert') != -1:
97+
assert_line = i+1
10198
for i in range(len(execution_trace)-1):
10299
if execution_trace[i][0] not in condition_line:
100+
if i > 0:
101+
if execution_trace[i][0] == execution_trace[i-1][0]:
102+
if execution_trace[i][1] == execution_trace[i-1][1]:
103+
continue
103104
intermediate_value.append([execution_trace[i][0], execution_trace[i+1][1]])
104-
105+
106+
if error != None:
107+
if asserterror:
108+
intermediate_value.append([assert_line , f'__exception__ = AssertionError()'])
109+
else:
110+
intermediate_value[-1][1] = f'__exception__ = {error}'
111+
105112
symbol_table = {}
106113
values = []
107114
for i in range(len(intermediate_value)):
108-
if i == len(intermediate_value)-1:
115+
if i == len(intermediate_value)-1 and error ==None:
116+
values.append([intermediate_value[i][0], intermediate_value[i][1]])
117+
elif i == len(intermediate_value)-2 and error != None:
118+
values.append([intermediate_value[i][0], intermediate_value[i][1]])
119+
elif i == len(intermediate_value)-1 and error != None:
109120
values.append([intermediate_value[i][0], intermediate_value[i][1]])
110121
else:
111122
temp_dict = {}

__pycache__/env.cpython-310.pyc

10 Bytes
Binary file not shown.

__pycache__/eval.cpython-310.pyc

10 Bytes
Binary file not shown.
10 Bytes
Binary file not shown.

__pycache__/utils.cpython-310.pyc

10 Bytes
Binary file not shown.

cfg.png

-42.6 KB
Binary file not shown.

cfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def _show(self, get_coverage: bool = False, fmt: str = 'png', calls: bool = True
278278
self._traverse(self.start, calls=calls)
279279
for k, v in self.func_calls.items():
280280
if node == 0:
281-
if get_coverage or get_execution:
281+
if get_coverage:
282282
self.execution_path, self.error = v.track_execution(self.name, k)
283283
self.graph.subgraph(v._show(False, fmt, calls))
284284
else:

code.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def f(nums):
2+
output = []
3+
for n in nums:
4+
output.append((nums.count(n), n))
5+
output.sort(reverse=True)
6+
return output

execution.gif

-85.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)