Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3583761

Browse files
committed
#17413: make sure settrace funcs get passed exception instances for 'value'.
Patch by Ingrid Cheung and Brendan McLoughlin.
1 parent 0aa685a commit 3583761

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lib/test/test_sys_settrace.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,29 @@ def g(frame, why, extra):
458458
self.fail("exception not propagated")
459459

460460

461+
def test_exception_arguments(self):
462+
def f():
463+
x = 0
464+
# this should raise an error
465+
x.no_such_attr
466+
def g(frame, event, arg):
467+
if (event == 'exception'):
468+
type, exception, trace = arg
469+
self.assertIsInstance(exception, Exception)
470+
return g
471+
472+
existing = sys.gettrace()
473+
try:
474+
sys.settrace(g)
475+
try:
476+
f()
477+
except AttributeError:
478+
# this is expected
479+
pass
480+
finally:
481+
sys.settrace(existing)
482+
483+
461484
# 'Jump' tests: assigning to frame.f_lineno within a trace function
462485
# moves the execution position - it's how debuggers implement a Jump
463486
# command (aka. "Set next statement").

Misc/ACKS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ David Chaum
208208
Nicolas Chauvat
209209
Jerry Chen
210210
Michael Chermside
211+
Ingrid Cheung
211212
Albert Chin-A-Young
212213
Adal Chiriliuc
213214
Matt Chisholm
@@ -786,6 +787,7 @@ Chris McDonough
786787
Greg McFarlane
787788
Alan McIntyre
788789
Michael McLay
790+
Brendan McLoughlin
789791
Mark Mc Mahon
790792
Gordon McMillan
791793
Andrew McNamara

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.3.2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #17413: sys.settrace callbacks were being passed a string instead of an
16+
exception instance for the 'value' element of the arg tuple if the exception
17+
originated from C code; now an exception instance is always provided.
18+
1519
- Issue #17782: Fix undefined behaviour on platforms where
1620
``struct timespec``'s "tv_nsec" member is not a C long.
1721

Python/ceval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3712,6 +3712,7 @@ call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
37123712
value = Py_None;
37133713
Py_INCREF(value);
37143714
}
3715+
PyErr_NormalizeException(&type, &value, &traceback);
37153716
arg = PyTuple_Pack(3, type, value, traceback);
37163717
if (arg == NULL) {
37173718
PyErr_Restore(type, value, traceback);

0 commit comments

Comments
 (0)