From e1d01becb3fbcc4772fc09150a1a33fb862c5a40 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 9 Apr 2010 06:18:24 +0200 Subject: [PATCH] fix bytes.decode() without arguments --- Cython/Compiler/Optimize.py | 29 +++++++++++++++++------------ tests/run/charptr_decode.pyx | 13 +++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) 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(): -- 2.26.2