fix bytes.decode() without arguments
authorStefan Behnel <scoder@users.berlios.de>
Fri, 9 Apr 2010 04:18:24 +0000 (06:18 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Fri, 9 Apr 2010 04:18:24 +0000 (06:18 +0200)
Cython/Compiler/Optimize.py
tests/run/charptr_decode.pyx

index c5e4fc967e45cf45ef128b71e599a5311fff8d7b..1917c9cf3594713f6d3e6e716b18d5ed97a23d11 100644 (file)
@@ -1944,20 +1944,25 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
         return None
 
     def _unpack_encoding_and_error_mode(self, pos, args):
-        encoding_node = args[1]
-        if isinstance(encoding_node, ExprNodes.CoerceToPyTypeNode):
-            encoding_node = encoding_node.arg
-        if isinstance(encoding_node, (ExprNodes.UnicodeNode, ExprNodes.StringNode,
-                                      ExprNodes.BytesNode)):
-            encoding = encoding_node.value
-            encoding_node = ExprNodes.BytesNode(encoding_node.pos, value=encoding,
-                                                 type=PyrexTypes.c_char_ptr_type)
-        elif encoding_node.type.is_string:
-            encoding = None
+        null_node = ExprNodes.NullNode(pos)
+
+        if len(args) >= 2:
+            encoding_node = args[1]
+            if isinstance(encoding_node, ExprNodes.CoerceToPyTypeNode):
+                encoding_node = encoding_node.arg
+            if isinstance(encoding_node, (ExprNodes.UnicodeNode, ExprNodes.StringNode,
+                                          ExprNodes.BytesNode)):
+                encoding = encoding_node.value
+                encoding_node = ExprNodes.BytesNode(encoding_node.pos, value=encoding,
+                                                     type=PyrexTypes.c_char_ptr_type)
+            elif encoding_node.type.is_string:
+                encoding = None
+            else:
+                return None
         else:
-            return None
+            encoding = None
+            encoding_node = null_node
 
-        null_node = ExprNodes.NullNode(pos)
         if len(args) == 3:
             error_handling_node = args[2]
             if isinstance(error_handling_node, ExprNodes.CoerceToPyTypeNode):
index 4194f79214674cf34234a7b2bebe2fa6105dd0dc..1e4fe726e4507b7ea1f906fd24fdb1f444657bef 100644 (file)
@@ -17,6 +17,19 @@ def slice_charptr_decode():
             cstring[:3].decode('UTF-8'),
             cstring[:9].decode('UTF-8'))
 
+@cython.test_assert_path_exists("//PythonCapiCallNode")
+@cython.test_fail_if_path_exists("//AttributeNode")
+def slice_charptr_decode_platform_encoding():
+    """
+    >>> print(str(slice_charptr_decode()).replace("u'", "'"))
+    ('a', 'abc', 'abcABCqtp')
+    """
+    cdef bytes s = u'abcABCqtp'.encode()
+    cdef char* cstr = s
+    return (cstr[:1].decode(),
+            cstr[:3].decode(),
+            cstr[:9].decode())
+
 @cython.test_assert_path_exists("//PythonCapiCallNode")
 @cython.test_fail_if_path_exists("//AttributeNode")
 def slice_charptr_decode_unknown_encoding():