more explicit method to check if a type can coerce to a Python object
authorStefan Behnel <scoder@users.berlios.de>
Thu, 3 Dec 2009 14:05:01 +0000 (15:05 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 3 Dec 2009 14:05:01 +0000 (15:05 +0100)
Cython/Compiler/ExprNodes.py
Cython/Compiler/PyrexTypes.py

index db34be5c946669a701a06b20ce7284b98986c3c9..2b6a9bfc89371d7603bdaa23ceb341d95e30bc0b 100644 (file)
@@ -3013,7 +3013,7 @@ class AttributeNode(ExprNode):
         self.type = py_object_type
         self.is_py_attr = 1
         if not obj_type.is_pyobject and not obj_type.is_error:
-            if obj_type.create_to_py_utility_code(env):
+            if obj_type.can_coerce_to_pyobject(env):
                 self.obj = self.obj.coerce_to_pyobject(env)
             else:
                 error(self.pos,
@@ -4242,7 +4242,7 @@ class TypecastNode(ExprNode):
         if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
             error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
         if to_py and not from_py:
-            if self.operand.type.create_to_py_utility_code(env):
+            if self.operand.type.can_coerce_to_pyobject(env):
                 self.result_ctype = py_object_type
                 self.operand = self.operand.coerce_to_pyobject(env)
             else:
index d187de4b676377768de1c4560846b7b5199f76a6..3dab8abbf5d04c9f515a485e46dc5c5bbb74810e 100644 (file)
@@ -11,6 +11,9 @@ class BaseType(object):
     #
     #  Base class for all Pyrex types including pseudo-types.
 
+    def can_coerce_to_pyobject(self, env):
+        return False
+
     def cast_code(self, expr_code):
         return "((%s)%s)" % (self.declaration_code(""), expr_code)
     
@@ -348,7 +351,10 @@ class PyObjectType(PyrexType):
     
     def __repr__(self):
         return "<PyObjectType>"
-    
+
+    def can_coerce_to_pyobject(self, env):
+        return True
+
     def assignable_from(self, src_type):
         # except for pointers, conversion will be attempted
         return not src_type.is_ptr or src_type.is_string
@@ -555,7 +561,10 @@ class CType(PyrexType):
         
     def create_from_py_utility_code(self, env):
         return self.from_py_function is not None
-        
+
+    def can_coerce_to_pyobject(self, env):
+        return self.create_to_py_utility_code(env)
+
     def error_condition(self, result_code):
         conds = []
         if self.is_string: