Commit ba63d040 authored by Stefan Behnel's avatar Stefan Behnel

reuse current thread state in more places

parent ac32262b
...@@ -1965,6 +1965,8 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1965,6 +1965,8 @@ class FuncDefNode(StatNode, BlockNode):
if buffers_present: if buffers_present:
code.globalstate.use_utility_code(restore_exception_utility_code) code.globalstate.use_utility_code(restore_exception_utility_code)
code.putln("{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;") code.putln("{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;")
code.putln("__Pyx_PyThreadState_declare")
code.putln("__Pyx_PyThreadState_assign")
code.putln("__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);") code.putln("__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);")
for entry in used_buffer_entries: for entry in used_buffer_entries:
Buffer.put_release_buffer_code(code, entry) Buffer.put_release_buffer_code(code, entry)
...@@ -5762,7 +5764,7 @@ class ReraiseStatNode(StatNode): ...@@ -5762,7 +5764,7 @@ class ReraiseStatNode(StatNode):
code.put_giveref(vars[1]) code.put_giveref(vars[1])
# fresh exceptions may not have a traceback yet (-> finally!) # fresh exceptions may not have a traceback yet (-> finally!)
code.put_xgiveref(vars[2]) code.put_xgiveref(vars[2])
code.putln("__Pyx_ErrRestore(%s, %s, %s);" % tuple(vars)) code.putln("__Pyx_ErrRestoreWithState(%s, %s, %s);" % tuple(vars))
for varname in vars: for varname in vars:
code.put("%s = 0; " % varname) code.put("%s = 0; " % varname)
code.putln() code.putln()
...@@ -8164,7 +8166,7 @@ class ParallelStatNode(StatNode, ParallelNode): ...@@ -8164,7 +8166,7 @@ class ParallelStatNode(StatNode, ParallelNode):
code.putln( code.putln(
"if (!%s) {" % Naming.parallel_exc_type) "if (!%s) {" % Naming.parallel_exc_type)
code.putln("__Pyx_ErrFetch(&%s, &%s, &%s);" % self.parallel_exc) code.putln("__Pyx_ErrFetchWithState(&%s, &%s, &%s);" % self.parallel_exc)
pos_info = chain(*zip(self.parallel_pos_info, self.pos_info)) pos_info = chain(*zip(self.parallel_pos_info, self.pos_info))
code.funcstate.uses_error_indicator = True code.funcstate.uses_error_indicator = True
code.putln("%s = %s; %s = %s; %s = %s;" % tuple(pos_info)) code.putln("%s = %s; %s = %s; %s = %s;" % tuple(pos_info))
...@@ -8182,7 +8184,7 @@ class ParallelStatNode(StatNode, ParallelNode): ...@@ -8182,7 +8184,7 @@ class ParallelStatNode(StatNode, ParallelNode):
code.put_ensure_gil(declare_gilstate=True) code.put_ensure_gil(declare_gilstate=True)
code.put_giveref(Naming.parallel_exc_type) code.put_giveref(Naming.parallel_exc_type)
code.putln("__Pyx_ErrRestore(%s, %s, %s);" % self.parallel_exc) code.putln("__Pyx_ErrRestoreWithState(%s, %s, %s);" % self.parallel_exc)
pos_info = chain(*zip(self.pos_info, self.parallel_pos_info)) pos_info = chain(*zip(self.pos_info, self.parallel_pos_info))
code.putln("%s = %s; %s = %s; %s = %s;" % tuple(pos_info)) code.putln("%s = %s; %s = %s; %s = %s;" % tuple(pos_info))
......
...@@ -310,6 +310,7 @@ static int __pyx_Generator_init(void); /*proto*/ ...@@ -310,6 +310,7 @@ static int __pyx_Generator_init(void); /*proto*/
//////////////////// CoroutineBase //////////////////// //////////////////// CoroutineBase ////////////////////
//@substitute: naming //@substitute: naming
//@requires: Exceptions.c::PyErrFetchRestore //@requires: Exceptions.c::PyErrFetchRestore
//@requires: Exceptions.c::PyThreadStateGet
//@requires: Exceptions.c::SwapException //@requires: Exceptions.c::SwapException
//@requires: Exceptions.c::RaiseException //@requires: Exceptions.c::RaiseException
//@requires: ObjectHandling.c::PyObjectCallMethod1 //@requires: ObjectHandling.c::PyObjectCallMethod1
...@@ -335,6 +336,8 @@ static PyObject *__Pyx_Coroutine_Throw(PyObject *gen, PyObject *args); ...@@ -335,6 +336,8 @@ static PyObject *__Pyx_Coroutine_Throw(PyObject *gen, PyObject *args);
static int __Pyx_PyGen_FetchStopIterationValue(PyObject **pvalue) { static int __Pyx_PyGen_FetchStopIterationValue(PyObject **pvalue) {
PyObject *et, *ev, *tb; PyObject *et, *ev, *tb;
PyObject *value = NULL; PyObject *value = NULL;
__Pyx_PyThreadState_declare
__Pyx_PyThreadState_assign
__Pyx_ErrFetch(&et, &ev, &tb); __Pyx_ErrFetch(&et, &ev, &tb);
...@@ -802,6 +805,7 @@ static void __Pyx_Coroutine_del(PyObject *self) { ...@@ -802,6 +805,7 @@ static void __Pyx_Coroutine_del(PyObject *self) {
PyObject *res; PyObject *res;
PyObject *error_type, *error_value, *error_traceback; PyObject *error_type, *error_value, *error_traceback;
__pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self; __pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self;
__Pyx_PyThreadState_declare
if (gen->resume_label <= 0) if (gen->resume_label <= 0)
return ; return ;
...@@ -813,6 +817,7 @@ static void __Pyx_Coroutine_del(PyObject *self) { ...@@ -813,6 +817,7 @@ static void __Pyx_Coroutine_del(PyObject *self) {
#endif #endif
// Save the current exception, if any. // Save the current exception, if any.
__Pyx_PyThreadState_assign
__Pyx_ErrFetch(&error_type, &error_value, &error_traceback); __Pyx_ErrFetch(&error_type, &error_value, &error_traceback);
res = __Pyx_Coroutine_Close(self); res = __Pyx_Coroutine_Close(self);
...@@ -1390,6 +1395,7 @@ static void __Pyx__ReturnWithStopIteration(PyObject* value); /*proto*/ ...@@ -1390,6 +1395,7 @@ static void __Pyx__ReturnWithStopIteration(PyObject* value); /*proto*/
/////////////// ReturnWithStopIteration /////////////// /////////////// ReturnWithStopIteration ///////////////
//@requires: Exceptions.c::PyErrFetchRestore //@requires: Exceptions.c::PyErrFetchRestore
//@requires: Exceptions.c::PyThreadStateGet
//@substitute: naming //@substitute: naming
// 1) Instantiating an exception just to pass back a value is costly. // 1) Instantiating an exception just to pass back a value is costly.
...@@ -1400,6 +1406,7 @@ static void __Pyx__ReturnWithStopIteration(PyObject* value); /*proto*/ ...@@ -1400,6 +1406,7 @@ static void __Pyx__ReturnWithStopIteration(PyObject* value); /*proto*/
static void __Pyx__ReturnWithStopIteration(PyObject* value) { static void __Pyx__ReturnWithStopIteration(PyObject* value) {
PyObject *exc, *args; PyObject *exc, *args;
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
__Pyx_PyThreadState_declare
if ((PY_VERSION_HEX >= 0x03030000 && PY_VERSION_HEX < 0x030500B1) || PyTuple_Check(value)) { if ((PY_VERSION_HEX >= 0x03030000 && PY_VERSION_HEX < 0x030500B1) || PyTuple_Check(value)) {
args = PyTuple_New(1); args = PyTuple_New(1);
if (unlikely(!args)) return; if (unlikely(!args)) return;
...@@ -1413,7 +1420,8 @@ static void __Pyx__ReturnWithStopIteration(PyObject* value) { ...@@ -1413,7 +1420,8 @@ static void __Pyx__ReturnWithStopIteration(PyObject* value) {
Py_INCREF(value); Py_INCREF(value);
exc = value; exc = value;
} }
if (!PyThreadState_GET()->exc_type) { __Pyx_PyThreadState_assign
if (!$local_tstate_cname->exc_type) {
// no chaining needed => avoid the overhead in PyErr_SetObject() // no chaining needed => avoid the overhead in PyErr_SetObject()
Py_INCREF(PyExc_StopIteration); Py_INCREF(PyExc_StopIteration);
__Pyx_ErrRestore(PyExc_StopIteration, exc, NULL); __Pyx_ErrRestore(PyExc_StopIteration, exc, NULL);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
/////////////// PyThreadStateGet.proto /////////////// /////////////// PyThreadStateGet.proto ///////////////
//@substitute: naming //@substitute: naming
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_PyThreadState_declare PyThreadState *$local_tstate_cname; #define __Pyx_PyThreadState_declare PyThreadState *$local_tstate_cname;
#define __Pyx_PyThreadState_assign $local_tstate_cname = PyThreadState_GET(); #define __Pyx_PyThreadState_assign $local_tstate_cname = PyThreadState_GET();
...@@ -21,8 +22,8 @@ ...@@ -21,8 +22,8 @@
//@substitute: naming //@substitute: naming
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_PyErr_ExceptionMatches(err) __Pyx_PyErr_ExceptionMatchesState(err, $local_tstate_cname) #define __Pyx_PyErr_ExceptionMatches(err) __Pyx_PyErr_ExceptionMatchesInState($local_tstate_cname, err)
static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesState(PyObject* err, PyThreadState* tstate); static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesInState(PyThreadState* tstate, PyObject* err);
#else #else
#define __Pyx_PyErr_ExceptionMatches(err) PyErr_ExceptionMatches(exc_type) #define __Pyx_PyErr_ExceptionMatches(err) PyErr_ExceptionMatches(exc_type)
#endif #endif
...@@ -30,7 +31,7 @@ static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesState(PyObject* err, PyThre ...@@ -30,7 +31,7 @@ static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesState(PyObject* err, PyThre
/////////////// PyErrExceptionMatches /////////////// /////////////// PyErrExceptionMatches ///////////////
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesState(PyObject* err, PyThreadState* tstate) { static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesInState(PyThreadState* tstate, PyObject* err) {
PyObject *exc_type = tstate->curexc_type; PyObject *exc_type = tstate->curexc_type;
if (exc_type == err) return 1; if (exc_type == err) return 1;
if (unlikely(!exc_type)) return 0; if (unlikely(!exc_type)) return 0;
...@@ -39,17 +40,29 @@ static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesState(PyObject* err, PyThre ...@@ -39,17 +40,29 @@ static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesState(PyObject* err, PyThre
#endif #endif
/////////////// PyErrFetchRestore.proto /////////////// /////////////// PyErrFetchRestore.proto ///////////////
//@substitute: naming
//@requires: PyThreadStateGet
#if CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_ErrRestoreWithState(type, value, tb) __Pyx_ErrRestoreInState(PyThreadState_GET(), type, value, tb)
#define __Pyx_ErrFetchWithState(type, value, tb) __Pyx_ErrFetchInState(PyThreadState_GET(), type, value, tb)
#define __Pyx_ErrRestore(type, value, tb) __Pyx_ErrRestoreInState($local_tstate_cname, type, value, tb)
#define __Pyx_ErrFetch(type, value, tb) __Pyx_ErrFetchInState($local_tstate_cname, type, value, tb)
static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); /*proto*/
static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); /*proto*/
static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ #else
static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ #define __Pyx_ErrRestoreWithState(type, value, tb) PyErr_Restore(type, value, tb)
#define __Pyx_ErrFetchWithState(type, value, tb) PyErr_Fetch(type, value, tb)
#define __Pyx_ErrRestore(type, value, tb) PyErr_Restore(type, value, tb)
#define __Pyx_ErrFetch(type, value, tb) PyErr_Fetch(type, value, tb)
#endif
/////////////// PyErrFetchRestore /////////////// /////////////// PyErrFetchRestore ///////////////
static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) {
PyObject *tmp_type, *tmp_value, *tmp_tb; PyObject *tmp_type, *tmp_value, *tmp_tb;
PyThreadState *tstate = PyThreadState_GET();
tmp_type = tstate->curexc_type; tmp_type = tstate->curexc_type;
tmp_value = tstate->curexc_value; tmp_value = tstate->curexc_value;
tmp_tb = tstate->curexc_traceback; tmp_tb = tstate->curexc_traceback;
...@@ -59,25 +72,17 @@ static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyOb ...@@ -59,25 +72,17 @@ static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyOb
Py_XDECREF(tmp_type); Py_XDECREF(tmp_type);
Py_XDECREF(tmp_value); Py_XDECREF(tmp_value);
Py_XDECREF(tmp_tb); Py_XDECREF(tmp_tb);
#else
PyErr_Restore(type, value, tb);
#endif
} }
static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) { static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) {
#if CYTHON_COMPILING_IN_CPYTHON
PyThreadState *tstate = PyThreadState_GET();
*type = tstate->curexc_type; *type = tstate->curexc_type;
*value = tstate->curexc_value; *value = tstate->curexc_value;
*tb = tstate->curexc_traceback; *tb = tstate->curexc_traceback;
tstate->curexc_type = 0; tstate->curexc_type = 0;
tstate->curexc_value = 0; tstate->curexc_value = 0;
tstate->curexc_traceback = 0; tstate->curexc_traceback = 0;
#else
PyErr_Fetch(type, value, tb);
#endif
} }
#endif
/////////////// RaiseException.proto /////////////// /////////////// RaiseException.proto ///////////////
...@@ -85,6 +90,7 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject ...@@ -85,6 +90,7 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject
/////////////// RaiseException /////////////// /////////////// RaiseException ///////////////
//@requires: PyErrFetchRestore //@requires: PyErrFetchRestore
//@requires: PyThreadStateGet
// The following function is based on do_raise() from ceval.c. There // The following function is based on do_raise() from ceval.c. There
// are separate versions for Python2 and Python3 as exception handling // are separate versions for Python2 and Python3 as exception handling
...@@ -93,6 +99,7 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject ...@@ -93,6 +99,7 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,
CYTHON_UNUSED PyObject *cause) { CYTHON_UNUSED PyObject *cause) {
__Pyx_PyThreadState_declare
/* 'cause' is only used in Py3 */ /* 'cause' is only used in Py3 */
Py_XINCREF(type); Py_XINCREF(type);
if (!value || value == Py_None) if (!value || value == Py_None)
...@@ -140,6 +147,7 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, ...@@ -140,6 +147,7 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,
} }
} }
__Pyx_PyThreadState_assign
__Pyx_ErrRestore(type, value, tb); __Pyx_ErrRestore(type, value, tb);
return; return;
raise_error: raise_error:
...@@ -477,12 +485,14 @@ static void __Pyx_WriteUnraisable(const char *name, int clineno, ...@@ -477,12 +485,14 @@ static void __Pyx_WriteUnraisable(const char *name, int clineno,
/////////////// WriteUnraisableException /////////////// /////////////// WriteUnraisableException ///////////////
//@requires: PyErrFetchRestore //@requires: PyErrFetchRestore
//@requires: PyThreadStateGet
static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno, static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename, CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename,
int full_traceback, CYTHON_UNUSED int nogil) { int full_traceback, CYTHON_UNUSED int nogil) {
PyObject *old_exc, *old_val, *old_tb; PyObject *old_exc, *old_val, *old_tb;
PyObject *ctx; PyObject *ctx;
__Pyx_PyThreadState_declare
#ifdef WITH_THREAD #ifdef WITH_THREAD
PyGILState_STATE state; PyGILState_STATE state;
if (nogil) if (nogil)
...@@ -492,6 +502,7 @@ static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno, ...@@ -492,6 +502,7 @@ static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
else state = (PyGILState_STATE)-1; else state = (PyGILState_STATE)-1;
#endif #endif
#endif #endif
__Pyx_PyThreadState_assign
__Pyx_ErrFetch(&old_exc, &old_val, &old_tb); __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
if (full_traceback) { if (full_traceback) {
Py_XINCREF(old_exc); Py_XINCREF(old_exc);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment