Skip to content

Commit 9f58ac9

Browse files
author
Philip Guo
committed
improved handling of exceptions and got namedtuple working!
1 parent c1d3e2d commit 9f58ac9

10 files changed

Lines changed: 5901 additions & 26 deletions

File tree

‎v3/example-code/nonlocal.golden‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"code": "# 'nonlocal' keyword is only in Python 3\ndef outer():\n x = 1\n def inner():\n nonlocal x\n x = 2\n y = x\n print(\"inner:\", x, y)\n inner()\n print(\"outer:\", x)\n\nouter()\n",
33
"trace": [
44
{
5-
"exception_msg": "Error: invalid syntax",
5+
"exception_msg": "SyntaxError: invalid syntax (<string>, line 5)",
66
"line": 5,
77
"event": "uncaught_exception",
88
"offset": 18

‎v3/js/opt-frontend.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ $(document).ready(function() {
161161

162162
alert(trace[0].exception_msg);
163163
}
164+
else if (trace[trace.length - 1].exception_msg) {
165+
alert(trace[trace.length - 1].exception_msg);
166+
}
164167
else {
165168
alert("Whoa, unknown error! Reload to try again, or report a bug to philip@pgbovine.net\n\n(Click the 'Generate URL' button to include a unique URL in your email bug report.)");
166169
}

‎v3/pg_logger.py‎

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,18 @@ def interaction(self, frame, traceback, event_type):
312312
# (e.g., those from imported modules -- e.g., random, re -- or the
313313
# __restricted_import__ function in this file)
314314
#
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>':
318-
return
315+
# empirically, it seems like the FIRST entry in self.stack is
316+
# the 'run' function from bdb.py, but everything else on the
317+
# stack is the user program's "real stack"
318+
for (cur_frame, cur_line) in self.stack[1:]:
319+
# it seems like user-written code has a filename of '<string>',
320+
# but maybe there are false positives too?
321+
if self.canonic(cur_frame.f_code.co_filename) != '<string>':
322+
return
323+
# also don't trace inside of the magic "constructor" code
324+
if cur_frame.f_code.co_name == '__new__':
325+
return
326+
319327

320328
# don't trace inside of our __restricted_import__ helper function
321329
# (this check is now subsumed by the above check)
@@ -708,19 +716,26 @@ def _runscript(self, script_str):
708716

709717
trace_entry = dict(event='uncaught_exception')
710718

711-
exc = sys.exc_info()[1]
712-
if hasattr(exc, 'lineno'):
713-
trace_entry['line'] = exc.lineno
714-
if hasattr(exc, 'offset'):
715-
trace_entry['offset'] = exc.offset
719+
(exc_type, exc_val, exc_tb) = sys.exc_info()
720+
if hasattr(exc_val, 'lineno'):
721+
trace_entry['line'] = exc_val.lineno
722+
if hasattr(exc_val, 'offset'):
723+
trace_entry['offset'] = exc_val.offset
724+
725+
trace_entry['exception_msg'] = type(exc_val).__name__ + ": " + str(exc_val)
726+
727+
# SUPER SUBTLE! if this exact same exception has already been
728+
# recorded by the program, then DON'T record it again as an
729+
# uncaught_exception
730+
already_caught = False
731+
for e in self.trace:
732+
if e['event'] == 'exception' and e['exception_msg'] == trace_entry['exception_msg']:
733+
already_caught = True
734+
break
716735

717-
if hasattr(exc, 'msg'):
718-
trace_entry['exception_msg'] = "Error: " + exc.msg
719-
else:
720-
trace_entry['exception_msg'] = "Unknown error"
736+
if not already_caught:
737+
self.trace.append(trace_entry)
721738

722-
self.trace.append(trace_entry)
723-
#self.finalize()
724739
raise bdb.BdbQuit # need to forceably STOP execution
725740

726741

@@ -734,15 +749,20 @@ def finalize(self):
734749

735750
assert len(self.trace) <= (MAX_EXECUTED_LINES + 1)
736751

752+
# don't do this anymore ...
753+
'''
737754
# filter all entries after 'return' from '<module>', since they
738755
# seem extraneous:
739756
res = []
740757
for e in self.trace:
741758
res.append(e)
742759
if e['event'] == 'return' and e['func_name'] == '<module>':
743760
break
761+
'''
762+
763+
res = self.trace
744764

745-
# another hack: if the SECOND to last entry is an 'exception'
765+
# if the SECOND to last entry is an 'exception'
746766
# and the last entry is return from <module>, then axe the last
747767
# entry, for aesthetic reasons :)
748768
if len(res) >= 2 and \
@@ -752,8 +772,6 @@ def finalize(self):
752772

753773
self.trace = res
754774

755-
#for e in self.trace: print >> sys.stderr, e
756-
757775
self.finalizer_func(self.executed_script, self.trace)
758776

759777

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,26 @@
3434
{
3535
"ordered_globals": [],
3636
"stdout": "",
37-
"exception_msg": "ImportError: os not supported",
3837
"func_name": "<module>",
3938
"stack_to_render": [],
4039
"globals": {},
4140
"heap": {},
4241
"line": 1,
43-
"event": "exception"
42+
"event": "return"
43+
},
44+
{
45+
"ordered_globals": [],
46+
"stdout": "",
47+
"func_name": "<module>",
48+
"stack_to_render": [],
49+
"globals": {},
50+
"heap": {},
51+
"line": 1,
52+
"event": "return"
53+
},
54+
{
55+
"exception_msg": "ImportError: os not supported",
56+
"event": "uncaught_exception"
4457
}
4558
]
4659
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
{
1515
"ordered_globals": [],
1616
"stdout": "",
17-
"exception_msg": "ImportError: os not supported",
1817
"func_name": "<module>",
1918
"stack_to_render": [],
2019
"globals": {},
2120
"heap": {},
2221
"line": 2,
23-
"event": "exception"
22+
"event": "return"
23+
},
24+
{
25+
"exception_msg": "ImportError: os not supported",
26+
"event": "uncaught_exception"
2427
}
2528
]
2629
}

0 commit comments

Comments
 (0)