Argument parsing error handling
authorRobert Bradshaw <robertwb@math.washington.edu>
Tue, 15 Jan 2008 21:49:48 +0000 (13:49 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Tue, 15 Jan 2008 21:49:48 +0000 (13:49 -0800)
Cython/Compiler/ExprNodes.py
Cython/Compiler/Nodes.py
Cython/Compiler/PyrexTypes.py

index c64d5577b8765cd096a52a164950f14589a9a439..0e8e0987383010a4f0c3783c15a2b35b89e09bf3 100644 (file)
@@ -3588,20 +3588,7 @@ class CoerceFromPyTypeNode(CoercionNode):
         code.putln('%s = %s; %s' % (
             self.result_code, 
             rhs,
-            code.error_goto_if(self.error_cond(), self.pos)))
-
-    def error_cond(self):
-        conds = []
-        if self.type.is_string:
-            conds.append("(!%s)" % self.result_code)
-        elif 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
+            code.error_goto_if(self.type.error_condition(self.result_code), self.pos)))
 
 
 class CoerceToBooleanNode(CoercionNode):
index d55b2faa413f15ae79f64e2c9f50d01cc91c7356..1730b31b6de7db41203294122b8c13ab039f5901 100644 (file)
@@ -1311,12 +1311,13 @@ class DefNode(FuncDefNode):
     def generate_arg_conversion_from_pyobject(self, arg, code):
         new_type = arg.type
         func = new_type.from_py_function
+        # copied from CoerceFromPyTypeNode
         if func:
             code.putln("%s = %s(%s); %s" % (
                 arg.entry.cname,
                 func,
                 arg.hdr_cname,
-                code.error_goto_if_PyErr(arg.pos)))
+                code.error_goto_if(new_type.error_condition(arg.entry.cname), arg.pos)))
         else:
             error(arg.pos, 
                 "Cannot convert Python object argument to type '%s'" 
index 78d79bd52f0c38dd776adda1011e3ce1ec017e3a..476a2336d186d926e17e7111262c5bb7eb4bffbd 100644 (file)
@@ -302,6 +302,19 @@ class CType(PyrexType):
     from_py_function = None
     exception_value = None
     exception_check = 1
+    
+    def error_condition(self, result_code):
+        conds = []
+        if self.is_string:
+            conds.append("(!%s)" % result_code)
+        elif self.exception_value is not None:
+            conds.append("(%s == (%s)%s)" % (result_code, self.sign_and_name(), self.exception_value))
+        if self.exception_check:
+            conds.append("PyErr_Occurred()")
+        if len(conds) > 0:
+            return " && ".join(conds)
+        else:
+            return 0
 
 
 class CVoidType(CType):
@@ -432,7 +445,7 @@ class CUIntType(CIntType):
 
     to_py_function = "PyLong_FromUnsignedLong"
     from_py_function = "PyInt_AsUnsignedLongMask"
-    exception_value = None
+    exception_value = -1
 
 
 class CULongType(CUIntType):