Commit 8829d7b1 authored by Stefan Behnel's avatar Stefan Behnel

streamline exception type tests

parent 7fad6270
...@@ -2253,9 +2253,11 @@ class NameNode(AtomicExprNode): ...@@ -2253,9 +2253,11 @@ class NameNode(AtomicExprNode):
key_error_code = ( key_error_code = (
'{ PyErr_Clear(); PyErr_Format(PyExc_NameError, "name \'%%s\' is not defined", "%s"); }' % '{ PyErr_Clear(); PyErr_Format(PyExc_NameError, "name \'%%s\' is not defined", "%s"); }' %
self.entry.name) self.entry.name)
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyErrExceptionMatches", "Exceptions.c"))
code.putln( code.putln(
'if (unlikely(PyObject_DelItem(%s, %s) < 0)) {' 'if (unlikely(PyObject_DelItem(%s, %s) < 0)) {'
' if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) %s' ' if (likely(__Pyx_PyErr_ExceptionMatches(PyExc_KeyError))) %s'
' %s ' ' %s '
'}' % (namespace, interned_cname, '}' % (namespace, interned_cname,
key_error_code, key_error_code,
...@@ -2267,9 +2269,12 @@ class NameNode(AtomicExprNode): ...@@ -2267,9 +2269,12 @@ class NameNode(AtomicExprNode):
del_code = '__Pyx_PyObject_DelAttrStr(%s, %s)' % ( del_code = '__Pyx_PyObject_DelAttrStr(%s, %s)' % (
Naming.module_cname, interned_cname) Naming.module_cname, interned_cname)
if ignore_nonexisting: if ignore_nonexisting:
code.putln('if (unlikely(%s < 0)) { if (likely(PyErr_ExceptionMatches(PyExc_AttributeError))) PyErr_Clear(); else %s }' % ( code.globalstate.use_utility_code(
del_code, UtilityCode.load_cached("PyErrExceptionMatches", "Exceptions.c"))
code.error_goto(self.pos))) code.putln(
'if (unlikely(%s < 0)) {'
' if (likely(__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError))) PyErr_Clear(); else %s '
'}' % (del_code, code.error_goto(self.pos)))
else: else:
code.put_error_if_neg(self.pos, del_code) code.put_error_if_neg(self.pos, del_code)
elif self.entry.type.is_pyobject or self.entry.type.is_memoryviewslice: elif self.entry.type.is_pyobject or self.entry.type.is_memoryviewslice:
......
...@@ -1725,8 +1725,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1725,8 +1725,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln( code.putln(
"PyObject *v = PyObject_GenericGetAttr(o, n);") "PyObject *v = PyObject_GenericGetAttr(o, n);")
if getattr_entry is not None: if getattr_entry is not None:
code.globalstate.use_utility_code(UtilityCode.load_cached("PyErrExceptionMatches", "Exceptions.c"))
code.putln( code.putln(
"if (!v && PyErr_ExceptionMatches(PyExc_AttributeError)) {") "if (!v && __Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)) {")
code.putln( code.putln(
"PyErr_Clear();") "PyErr_Clear();")
code.putln( code.putln(
......
...@@ -4122,7 +4122,8 @@ class GeneratorBodyDefNode(DefNode): ...@@ -4122,7 +4122,8 @@ class GeneratorBodyDefNode(DefNode):
if Future.generator_stop in env.global_scope().context.future_directives: if Future.generator_stop in env.global_scope().context.future_directives:
# PEP 479: turn accidental StopIteration exceptions into a RuntimeError # PEP 479: turn accidental StopIteration exceptions into a RuntimeError
code.globalstate.use_utility_code(UtilityCode.load_cached("pep479", "Coroutine.c")) code.globalstate.use_utility_code(UtilityCode.load_cached("pep479", "Coroutine.c"))
code.putln("if (unlikely(PyErr_ExceptionMatches(PyExc_StopIteration))) " code.globalstate.use_utility_code(UtilityCode.load_cached("PyErrExceptionMatches", "Exceptions.c"))
code.putln("if (unlikely(__Pyx_PyErr_ExceptionMatches(PyExc_StopIteration))) "
"__Pyx_Generator_Replace_StopIteration();") "__Pyx_Generator_Replace_StopIteration();")
for cname, type in code.funcstate.all_managed_temps(): for cname, type in code.funcstate.all_managed_temps():
code.put_xdecref(cname, type) code.put_xdecref(cname, type)
......
...@@ -167,12 +167,13 @@ bad: ...@@ -167,12 +167,13 @@ bad:
static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/ static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
//////////////////// GetAttr3 //////////////////// //////////////////// GetAttr3 ////////////////////
//@requires: Exceptions.c::PyErrExceptionMatches
//@requires: ObjectHandling.c::GetAttr //@requires: ObjectHandling.c::GetAttr
static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) { static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
PyObject *r = __Pyx_GetAttr(o, n); PyObject *r = __Pyx_GetAttr(o, n);
if (unlikely(!r)) { if (unlikely(!r)) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) if (!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError))
goto bad; goto bad;
PyErr_Clear(); PyErr_Clear();
r = d; r = d;
......
...@@ -177,6 +177,7 @@ static CYTHON_INLINE PyObject *__Pyx_Coroutine_AsyncIterNext(PyObject *o); /*pro ...@@ -177,6 +177,7 @@ static CYTHON_INLINE PyObject *__Pyx_Coroutine_AsyncIterNext(PyObject *o); /*pro
//////////////////// AsyncIter //////////////////// //////////////////// AsyncIter ////////////////////
//@requires: GetAwaitIter //@requires: GetAwaitIter
//@requires: Exceptions.c::PyErrExceptionMatches
//@requires: ObjectHandling.c::PyObjectCallMethod0 //@requires: ObjectHandling.c::PyObjectCallMethod0
static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAsyncIter(PyObject *obj) { static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAsyncIter(PyObject *obj) {
...@@ -192,7 +193,7 @@ static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAsyncIter(PyObject *obj) { ...@@ -192,7 +193,7 @@ static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAsyncIter(PyObject *obj) {
if (likely(iter)) if (likely(iter))
return iter; return iter;
// FIXME: for the sake of a nicely conforming exception message, assume any AttributeError meant '__aiter__' // FIXME: for the sake of a nicely conforming exception message, assume any AttributeError meant '__aiter__'
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) if (!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL; return NULL;
} }
#else #else
...@@ -219,7 +220,7 @@ static CYTHON_INLINE PyObject *__Pyx_Coroutine_AsyncIterNext(PyObject *obj) { ...@@ -219,7 +220,7 @@ static CYTHON_INLINE PyObject *__Pyx_Coroutine_AsyncIterNext(PyObject *obj) {
return value; return value;
} }
// FIXME: for the sake of a nicely conforming exception message, assume any AttributeError meant '__anext__' // FIXME: for the sake of a nicely conforming exception message, assume any AttributeError meant '__anext__'
if (PyErr_ExceptionMatches(PyExc_AttributeError)) if (__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError))
#endif #endif
PyErr_Format(PyExc_TypeError, "'async for' requires an object with __anext__ method, got %.100s", PyErr_Format(PyExc_TypeError, "'async for' requires an object with __anext__ method, got %.100s",
Py_TYPE(obj)->tp_name); Py_TYPE(obj)->tp_name);
...@@ -309,6 +310,7 @@ static int __pyx_Generator_init(void); /*proto*/ ...@@ -309,6 +310,7 @@ static int __pyx_Generator_init(void); /*proto*/
//@requires: Exceptions.c::PyErrFetchRestore //@requires: Exceptions.c::PyErrFetchRestore
//@requires: Exceptions.c::SwapException //@requires: Exceptions.c::SwapException
//@requires: Exceptions.c::RaiseException //@requires: Exceptions.c::RaiseException
//@requires: Exceptions.c::PyErrExceptionMatches
//@requires: ObjectHandling.c::PyObjectCallMethod1 //@requires: ObjectHandling.c::PyObjectCallMethod1
//@requires: ObjectHandling.c::PyObjectGetAttrStr //@requires: ObjectHandling.c::PyObjectGetAttrStr
//@requires: CommonTypes.c::FetchCommonType //@requires: CommonTypes.c::FetchCommonType
...@@ -602,7 +604,7 @@ static int __Pyx_Coroutine_CloseIter(__pyx_CoroutineObject *gen, PyObject *yf) { ...@@ -602,7 +604,7 @@ static int __Pyx_Coroutine_CloseIter(__pyx_CoroutineObject *gen, PyObject *yf) {
gen->is_running = 1; gen->is_running = 1;
meth = __Pyx_PyObject_GetAttrStr(yf, PYIDENT("close")); meth = __Pyx_PyObject_GetAttrStr(yf, PYIDENT("close"));
if (unlikely(!meth)) { if (unlikely(!meth)) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { if (!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_WriteUnraisable(yf); PyErr_WriteUnraisable(yf);
} }
PyErr_Clear(); PyErr_Clear();
...@@ -718,7 +720,7 @@ static PyObject *__Pyx_Coroutine_Throw(PyObject *self, PyObject *args) { ...@@ -718,7 +720,7 @@ static PyObject *__Pyx_Coroutine_Throw(PyObject *self, PyObject *args) {
PyObject *meth = __Pyx_PyObject_GetAttrStr(yf, PYIDENT("throw")); PyObject *meth = __Pyx_PyObject_GetAttrStr(yf, PYIDENT("throw"));
if (unlikely(!meth)) { if (unlikely(!meth)) {
Py_DECREF(yf); Py_DECREF(yf);
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { if (!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)) {
gen->is_running = 0; gen->is_running = 0;
return NULL; return NULL;
} }
......
...@@ -5,6 +5,25 @@ ...@@ -5,6 +5,25 @@
// 'except' statement, curexc_* is moved over to exc_* by // 'except' statement, curexc_* is moved over to exc_* by
// __Pyx_GetException() // __Pyx_GetException()
/////////////// PyErrExceptionMatches.proto ///////////////
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatches(PyObject* err);
#else
#define __Pyx_PyErr_ExceptionMatches(exc_type) PyErr_ExceptionMatches(exc_type)
#endif
/////////////// PyErrExceptionMatches ///////////////
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatches(PyObject* err) {
PyObject *exc_type = PyThreadState_GET()->curexc_type;
if (exc_type == err) return 1;
if (unlikely(!exc_type)) return 0;
return PyErr_GivenExceptionMatches(exc_type, err);
}
#endif
/////////////// PyErrFetchRestore.proto /////////////// /////////////// PyErrFetchRestore.proto ///////////////
static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
......
...@@ -308,6 +308,7 @@ bad: ...@@ -308,6 +308,7 @@ bad:
static int __Pyx_MergeKeywords(PyObject *kwdict, PyObject *source_mapping); /*proto*/ static int __Pyx_MergeKeywords(PyObject *kwdict, PyObject *source_mapping); /*proto*/
//////////////////// MergeKeywords //////////////////// //////////////////// MergeKeywords ////////////////////
//@requires: Exceptions.c::PyErrExceptionMatches
//@requires: RaiseDoubleKeywords //@requires: RaiseDoubleKeywords
//@requires: Optimize.c::dict_iter //@requires: Optimize.c::dict_iter
...@@ -320,7 +321,7 @@ static int __Pyx_MergeKeywords(PyObject *kwdict, PyObject *source_mapping) { ...@@ -320,7 +321,7 @@ static int __Pyx_MergeKeywords(PyObject *kwdict, PyObject *source_mapping) {
if (unlikely(!iter)) { if (unlikely(!iter)) {
// slow fallback: try converting to dict, then iterate // slow fallback: try converting to dict, then iterate
PyObject *args; PyObject *args;
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad; if (!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
PyErr_Clear(); PyErr_Clear();
args = PyTuple_Pack(1, source_mapping); args = PyTuple_Pack(1, source_mapping);
if (likely(args)) { if (likely(args)) {
......
...@@ -268,6 +268,7 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, ...@@ -268,6 +268,7 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
int is_list, int wraparound, int boundscheck); int is_list, int wraparound, int boundscheck);
/////////////// GetItemInt /////////////// /////////////// GetItemInt ///////////////
//@requires: Exceptions.c::PyErrExceptionMatches
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) { static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
PyObject *r; PyObject *r;
...@@ -324,10 +325,9 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, ...@@ -324,10 +325,9 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
i += l; i += l;
} else { } else {
// if length > max(Py_ssize_t), maybe the object can wrap around itself? // if length > max(Py_ssize_t), maybe the object can wrap around itself?
if (PyErr_ExceptionMatches(PyExc_OverflowError)) if (!__Pyx_PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_Clear();
else
return NULL; return NULL;
PyErr_Clear();
} }
} }
return m->sq_item(o, i); return m->sq_item(o, i);
...@@ -354,6 +354,7 @@ static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObje ...@@ -354,6 +354,7 @@ static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObje
int is_list, int wraparound, int boundscheck); int is_list, int wraparound, int boundscheck);
/////////////// SetItemInt /////////////// /////////////// SetItemInt ///////////////
//@requires: Exceptions.c::PyErrExceptionMatches
static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) { static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
int r; int r;
...@@ -385,10 +386,9 @@ static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObje ...@@ -385,10 +386,9 @@ static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObje
i += l; i += l;
} else { } else {
// if length > max(Py_ssize_t), maybe the object can wrap around itself? // if length > max(Py_ssize_t), maybe the object can wrap around itself?
if (PyErr_ExceptionMatches(PyExc_OverflowError)) if (!__Pyx_PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_Clear();
else
return -1; return -1;
PyErr_Clear();
} }
} }
return m->sq_ass_item(o, i, v); return m->sq_ass_item(o, i, v);
...@@ -420,6 +420,7 @@ static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i, ...@@ -420,6 +420,7 @@ static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
int is_list, int wraparound); int is_list, int wraparound);
/////////////// DelItemInt /////////////// /////////////// DelItemInt ///////////////
//@requires: Exceptions.c::PyErrExceptionMatches
static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) { static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
int r; int r;
...@@ -445,10 +446,9 @@ static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i, ...@@ -445,10 +446,9 @@ static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
i += l; i += l;
} else { } else {
// if length > max(Py_ssize_t), maybe the object can wrap around itself? // if length > max(Py_ssize_t), maybe the object can wrap around itself?
if (PyErr_ExceptionMatches(PyExc_OverflowError)) if (!__Pyx_PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_Clear();
else
return -1; return -1;
PyErr_Clear();
} }
} }
return m->sq_ass_item(o, i, (PyObject *)NULL); return m->sq_ass_item(o, i, (PyObject *)NULL);
...@@ -478,6 +478,7 @@ static CYTHON_INLINE int __Pyx_PyObject_SetSlice( ...@@ -478,6 +478,7 @@ static CYTHON_INLINE int __Pyx_PyObject_SetSlice(
{{endif}} {{endif}}
/////////////// SliceObject /////////////// /////////////// SliceObject ///////////////
//@requires: Exceptions.c::PyErrExceptionMatches
{{if access == 'Get'}} {{if access == 'Get'}}
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(PyObject* obj, static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(PyObject* obj,
...@@ -519,10 +520,9 @@ static CYTHON_INLINE int __Pyx_PyObject_SetSlice(PyObject* obj, PyObject* value, ...@@ -519,10 +520,9 @@ static CYTHON_INLINE int __Pyx_PyObject_SetSlice(PyObject* obj, PyObject* value,
} }
} else { } else {
// if length > max(Py_ssize_t), maybe the object can wrap around itself? // if length > max(Py_ssize_t), maybe the object can wrap around itself?
if (PyErr_ExceptionMatches(PyExc_OverflowError)) if (!__Pyx_PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_Clear();
else
goto bad; goto bad;
PyErr_Clear();
} }
} }
{{if access == 'Get'}} {{if access == 'Get'}}
...@@ -833,6 +833,7 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj ...@@ -833,6 +833,7 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj
PyObject *mkw, int calculate_metaclass, int allow_py2_metaclass); /*proto*/ PyObject *mkw, int calculate_metaclass, int allow_py2_metaclass); /*proto*/
/////////////// Py3ClassCreate /////////////// /////////////// Py3ClassCreate ///////////////
//@requires: Exceptions.c::PyErrExceptionMatches
//@requires: PyObjectGetAttrStr //@requires: PyObjectGetAttrStr
//@requires: CalculateMetaclass //@requires: CalculateMetaclass
...@@ -851,7 +852,7 @@ static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, ...@@ -851,7 +852,7 @@ static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases,
Py_DECREF(prep); Py_DECREF(prep);
Py_DECREF(pargs); Py_DECREF(pargs);
} else { } else {
if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError))) if (unlikely(!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)))
return NULL; return NULL;
PyErr_Clear(); PyErr_Clear();
ns = PyDict_New(); ns = PyDict_New();
...@@ -883,7 +884,7 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj ...@@ -883,7 +884,7 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj
owned_metaclass = PyObject_GetItem(dict, PYIDENT("__metaclass__")); owned_metaclass = PyObject_GetItem(dict, PYIDENT("__metaclass__"));
if (owned_metaclass) { if (owned_metaclass) {
metaclass = owned_metaclass; metaclass = owned_metaclass;
} else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) { } else if (likely(__Pyx_PyErr_ExceptionMatches(PyExc_KeyError))) {
PyErr_Clear(); PyErr_Clear();
} else { } else {
return NULL; return NULL;
...@@ -1495,6 +1496,7 @@ static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y); ...@@ -1495,6 +1496,7 @@ static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y);
#endif #endif
/////////////// MatrixMultiply /////////////// /////////////// MatrixMultiply ///////////////
//@requires: Exceptions.c::PyErrExceptionMatches
//@requires: PyObjectGetAttrStr //@requires: PyObjectGetAttrStr
//@requires: PyObjectCallOneArg //@requires: PyObjectCallOneArg
...@@ -1537,7 +1539,7 @@ bad: ...@@ -1537,7 +1539,7 @@ bad:
return result; \ return result; \
Py_DECREF(result); \ Py_DECREF(result); \
} else { \ } else { \
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) \ if (!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)) \
return NULL; \ return NULL; \
PyErr_Clear(); \ PyErr_Clear(); \
} \ } \
......
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