self.run_and_compare(func, EXPECTED_EVENTS)
+ def test_settrace_error(self):
+
+ raised = False
+ def error_once(frame, event, arg):
+ nonlocal raised
+ if not raised:
+ raised = True
+ raise Exception
+ return error
+
+ try:
+ sys._getframe().f_trace = error_once
+ sys.settrace(error_once)
+ len([])
+ except Exception as ex:
+ count = 0
+ tb = ex.__traceback__
+ print(tb)
+ while tb:
+ if tb.tb_frame.f_code.co_name == "test_settrace_error":
+ count += 1
+ tb = tb.tb_next
+ if count == 0:
+ self.fail("Traceback is missing frame")
+ elif count > 1:
+ self.fail("Traceback has frame more than once")
+ else:
+ self.fail("No exception raised")
+ finally:
+ sys.settrace(None)
+
+ @support.cpython_only
+ def test_testcapi_settrace_error(self):
+
+ # Skip this test if the _testcapi module isn't available.
+ _testcapi = import_helper.import_module('_testcapi')
+
+ try:
+ _testcapi.settrace_to_error([])
+ len([])
+ except Exception as ex:
+ count = 0
+ tb = ex.__traceback__
+ while tb:
+ if tb.tb_frame.f_code.co_name == "test_testcapi_settrace_error":
+ count += 1
+ tb = tb.tb_next
+ if count == 0:
+ self.fail("Traceback is missing frame")
+ elif count > 1:
+ self.fail("Traceback has frame more than once")
+ else:
+ self.fail("No exception raised")
+ finally:
+ sys.settrace(None)
+
def test_very_large_function(self):
# There is a separate code path when the number of lines > (1 << 15).
d = {}
Py_RETURN_NONE;
}
+static int
+error_func(PyObject *obj, PyFrameObject *f, int what, PyObject *arg)
+{
+ assert(PyList_Check(obj));
+ /* Only raise if list is empty, otherwise append None
+ * This ensures that we only raise once */
+ if (PyList_GET_SIZE(obj)) {
+ return 0;
+ }
+ if (PyList_Append(obj, Py_None)) {
+ return -1;
+ }
+ PyErr_SetString(PyExc_Exception, "an exception");
+ return -1;
+}
+
+static PyObject *
+settrace_to_error(PyObject *self, PyObject *list)
+{
+ if (!PyList_Check(list)) {
+ PyErr_SetString(PyExc_TypeError, "argument must be a list");
+ return NULL;
+ }
+ PyEval_SetTrace(error_func, list);
+ Py_RETURN_NONE;
+}
+
static PyObject *
clear_managed_dict(PyObject *self, PyObject *obj)
{
{"gen_get_code", gen_get_code, METH_O, NULL},
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
{"test_code_api", test_code_api, METH_NOARGS, NULL},
+ {"settrace_to_error", settrace_to_error, METH_O, NULL},
{"settrace_to_record", settrace_to_record, METH_O, NULL},
{"test_macros", test_macros, METH_NOARGS, NULL},
{"clear_managed_dict", clear_managed_dict, METH_O, NULL},