Check return values before PyErr_Occurred() for coercion nodes.
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 7 Sep 2007 09:49:43 +0000 (02:49 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 7 Sep 2007 09:49:43 +0000 (02:49 -0700)
Cython/Compiler/ExprNodes.py
Cython/Compiler/PyrexTypes.py

index dfdaf6f2398bbbe763ed0892a29ccd77367983b4..f4e59964851aaeda7da8f7d6f2c79b07206daef3 100644 (file)
@@ -3177,7 +3177,18 @@ class CoerceFromPyTypeNode(CoercionNode):
         code.putln('%s = %s; %s' % (
             self.result_code, 
             rhs, 
-            code.error_goto_if_PyErr(self.pos)))
+            code.error_goto_if(self.error_cond(), self.pos)))
+            
+    def error_cond(self):
+        conds = []
+        if self.type.exception_value is not None:
+            conds.append("(%s == %s)" % (self.result_code, self.type.exception_value))
+        if self.type.exception_check:
+            conds.append("PyErr_Occurred()")
+        if len(conds) > 0:
+            return " && ".join(conds)
+        else:
+            return 0
 
 
 class CoerceToBooleanNode(CoercionNode):
index 594f8836dd0fa83dac773e435c183228596c0a7e..b4ac6ed9c066260ecc28ade09f2b719ad9170ef5 100644 (file)
@@ -260,6 +260,8 @@ class CType(PyrexType):
     
     to_py_function = None
     from_py_function = None
+    exception_value = None
+    exception_check = 1
 
 
 #class CSimpleType(CType):
@@ -332,6 +334,7 @@ class CIntType(CNumericType):
     typedef_flag = 0
     to_py_function = "PyInt_FromLong"
     from_py_function = "PyInt_AsLong"
+    exception_value = -1
 
     def __init__(self, rank, signed, pymemberdef_typecode = None, is_returncode = 0):
         CNumericType.__init__(self, rank, signed, pymemberdef_typecode)
@@ -347,33 +350,35 @@ class CBIntType(CIntType):
     #       and no error checking should be needed (just an incref). 
     to_py_function = "__Pyx_PyBool_FromLong"
     from_py_function = "__Pyx_PyObject_IsTrue"
+    exception_check = 0
     
 
 class CPySSizeTType(CIntType):
 
     to_py_function = "PyInt_FromSsize_t"
     from_py_function = "__pyx_PyIndex_AsSsize_t"
-
+    exception_value = None
 
 class CUIntType(CIntType):
 
     to_py_function = "PyLong_FromUnsignedLong"
     from_py_function = "PyInt_AsUnsignedLongMask"
+    exception_value = None
 
 
-class CULongType(CIntType):
+class CULongType(CUIntType):
 
     to_py_function = "PyLong_FromUnsignedLong"
     from_py_function = "PyInt_AsUnsignedLongMask"
 
 
-class CLongLongType(CIntType):
+class CLongLongType(CUIntType):
 
     to_py_function = "PyLong_FromLongLong"
     from_py_function = "PyInt_AsUnsignedLongLongMask"
 
 
-class CULongLongType(CIntType):
+class CULongLongType(CUIntType):
 
     to_py_function = "PyLong_FromUnsignedLongLong"
     from_py_function = "PyInt_AsUnsignedLongLongMask"
@@ -703,6 +708,7 @@ class CStringType:
     
     to_py_function = "PyString_FromString"
     from_py_function = "PyString_AsString"
+    exception_value = "NULL"
 
     def literal_code(self, value):
         return '"%s"' % value