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

Skip to content

Commit fb4d6ec

Browse files
author
Michael W. Hudson
committed
Fix for the recursion_level bug Armin Rigo reported in sf
patch #617312, both on the trunk and the 22-maint branch. Also added a test case, and ported the test_trace I wrote for HEAD to 2.2.2 (with all those horrible extra 'line' events ;-).
1 parent 3c6d6f2 commit fb4d6ec

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/test/test_trace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,28 @@ def test_8_settrace_and_return(self):
177177
def test_9_settrace_and_raise(self):
178178
self.run_test2(settrace_and_raise)
179179

180+
class RaisingTraceFuncTestCase(unittest.TestCase):
181+
def test_it(self):
182+
def tr(frame, event, arg):
183+
raise ValueError # just something that isn't RuntimeError
184+
def f():
185+
return 1
186+
try:
187+
for i in xrange(sys.getrecursionlimit() + 1):
188+
sys.settrace(tr)
189+
try:
190+
f()
191+
except ValueError:
192+
pass
193+
else:
194+
self.fail("exception not thrown!")
195+
except RuntimeError:
196+
self.fail("recursion counter not reset")
197+
198+
180199
def test_main():
181200
test_support.run_unittest(TraceTestCase)
201+
test_support.run_unittest(RaisingTraceFuncTestCase)
182202

183203
if __name__ == "__main__":
184204
test_main()

Python/ceval.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ eval_frame(PyFrameObject *f)
636636
if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
637637
f, PyTrace_CALL, Py_None)) {
638638
/* Trace function raised an error */
639+
--tstate->recursion_depth;
640+
tstate->frame = f->f_back;
639641
return NULL;
640642
}
641643
}
@@ -646,6 +648,8 @@ eval_frame(PyFrameObject *f)
646648
tstate->c_profileobj,
647649
f, PyTrace_CALL, Py_None)) {
648650
/* Profile function raised an error */
651+
--tstate->recursion_depth;
652+
tstate->frame = f->f_back;
649653
return NULL;
650654
}
651655
}

0 commit comments

Comments
 (0)