allow coercion of temp values to C++ classes
authorStefan Behnel <stefan_ml@behnel.de>
Sun, 8 Jul 2012 13:22:27 +0000 (15:22 +0200)
committerStefan Behnel <stefan_ml@behnel.de>
Sun, 8 Jul 2012 13:22:27 +0000 (15:22 +0200)
Cython/Compiler/ExprNodes.py
tests/run/cpp_stl_conversion.pyx

index f89a37b..bc242e0 100755 (executable)
@@ -7297,8 +7297,9 @@ class TypecastNode(ExprNode):
         self.operand.analyse_types(env)
         to_py = self.type.is_pyobject
         from_py = self.operand.type.is_pyobject
-        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 from_py and not to_py and self.operand.is_ephemeral():
+            if not self.type.is_numeric and not self.type.is_cpp_class:
+                error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
         if to_py and not from_py:
             if self.type is bytes_type and self.operand.type.is_int:
                 # FIXME: the type cast node isn't needed in this case
index 75fd215..beb5f85 100644 (file)
@@ -39,6 +39,16 @@ def test_encode_to_string(o):
     cdef string s = o.encode('ascii')
     return s
 
+def test_encode_to_string_cast(o):
+    """
+    >>> normalize(test_encode_to_string_cast('abc'))
+    'abc'
+    >>> normalize(test_encode_to_string_cast('abc\\x00def'))
+    'abc\\x00def'
+    """
+    s = <string>o.encode('ascii')
+    return s
+
 def test_bytes_encode_to_string(bytes o):
     """
     >>> normalize(test_bytes_encode_to_string('abc'))