#------------------------------------------------------------------------------------
- c_api_import_code = [
+ get_exception_utility_code = [
"""
- static int __Pyx_InitCApi(PyObject *module); /*proto*/
- static int __Pyx_ImportModuleCApi(__Pyx_CApiTabEntry *t); /*proto*/
+ static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
""","""
- static int __Pyx_ImportModuleCApi(__Pyx_CApiTabEntry *t) {
- __Pyx_CApiTabEntry *api_t;
- while (t->s) {
- if (*t->s == '\\0')
- continue; /* shortcut for erased string entries */
- api_t = %(API_TAB)s;
- while ((api_t->s) && (strcmp(api_t->s, t->s) < 0))
- ++api_t;
- if ((!api_t->p) || (strcmp(api_t->s, t->s) != 0)) {
- PyErr_Format(PyExc_ValueError,
- "Unknown function name in C API: %%s", t->s);
- return -1;
- }
- *t->p = api_t->p;
- ++t;
- }
+ static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) {
++ PyObject *tmp_type, *tmp_value, *tmp_tb;
+ PyThreadState *tstate = PyThreadState_Get();
+ PyErr_Fetch(type, value, tb);
+ PyErr_NormalizeException(type, value, tb);
+ if (PyErr_Occurred())
+ goto bad;
+ Py_INCREF(*type);
+ Py_INCREF(*value);
+ Py_INCREF(*tb);
- Py_XDECREF(tstate->exc_type);
- Py_XDECREF(tstate->exc_value);
- Py_XDECREF(tstate->exc_traceback);
++ tmp_type = tstate->exc_type;
++ tmp_value = tstate->exc_value;
++ tmp_tb = tstate->exc_traceback;
+ tstate->exc_type = *type;
+ tstate->exc_value = *value;
+ tstate->exc_traceback = *tb;
++ /* Make sure tstate is in a consistent state when we XDECREF
++ these objects (XDECREF may run arbitrary code). */
++ Py_XDECREF(tmp_type);
++ Py_XDECREF(tmp_value);
++ Py_XDECREF(tmp_tb);
return 0;
+ bad:
+ Py_XDECREF(*type);
+ Py_XDECREF(*value);
+ Py_XDECREF(*tb);
+ return -1;
}
+ """]
- static int __Pyx_InitCApi(PyObject *module) {
- int result;
- PyObject* cobj = PyCObject_FromVoidPtr(&__Pyx_ImportModuleCApi, NULL);
- if (!cobj)
- return -1;
-
- result = PyObject_SetAttrString(module, "_import_c_api", cobj);
- Py_DECREF(cobj);
- return result;
- }
- """ % {'API_TAB' : Naming.c_api_tab_cname}
- ]
#------------------------------------------------------------------------------------