diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index bb836233e1a..25c5835fd6f 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -388,6 +388,15 @@ def test_16_blank_lines(self): (257, 'line'), (257, 'return')]) + def test_17_none_f_trace(self): + # Issue 20041: fix TypeError when f_trace is set to None. + def func(): + sys._getframe().f_trace = None + lineno = 2 + self.run_and_compare(func, + [(0, 'call'), + (1, 'line')]) + class RaisingTraceFuncTestCase(unittest.TestCase): def setUp(self): diff --git a/Misc/NEWS b/Misc/NEWS index 21638b57303..b92b312c0dc 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -120,6 +120,9 @@ Release date: 2016-05-16 Core and Builtins ----------------- +- Issue #20041: Fixed TypeError when frame.f_trace is set to None. + Patch by Xavier de Gaye. + - Issue #26168: Fixed possible refleaks in failing Py_BuildValue() with the "N" format unit. diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 492886fbb7c..b1156144195 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -344,6 +344,8 @@ frame_settrace(PyFrameObject *f, PyObject* v, void *closure) /* We rely on f_lineno being accurate when f_trace is set. */ f->f_lineno = PyFrame_GetLineNumber(f); + if (v == Py_None) + v = NULL; Py_XINCREF(v); Py_XSETREF(f->f_trace, v);