From: Stefan Behnel Date: Fri, 9 Apr 2010 04:18:24 +0000 (+0200) Subject: fix bytes.decode() without arguments X-Git-Tag: 0.13.beta0~236 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=e1d01becb3fbcc4772fc09150a1a33fb862c5a40;p=cython.git fix bytes.decode() without arguments --- diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index c5e4fc96..1917c9cf 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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): diff --git a/tests/run/charptr_decode.pyx b/tests/run/charptr_decode.pyx index 4194f792..1e4fe726 100644 --- a/tests/run/charptr_decode.pyx +++ b/tests/run/charptr_decode.pyx @@ -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():