From ea0c2ffa20817d4f6503cea56d875e7388f4215f Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 23 Nov 2010 11:20:59 +0100 Subject: [PATCH] another fix for str() handling that works around internal use of EncodedString in StringNode, fix test for locals() in -3 mode --- Cython/Compiler/ExprNodes.py | 15 ++++++------ Cython/Compiler/ParseTreeTransforms.py | 4 ++-- tests/run/cython3.pyx | 32 +++++++++++++++----------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index cb95f188..9bcd2b98 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1076,8 +1076,8 @@ class StringNode(PyConstNode): # A Python str object, i.e. a byte string in Python 2.x and a # unicode string in Python 3.x # - # value BytesLiteral - # unicode_value EncodedString + # value BytesLiteral (or EncodedString with ASCII content) + # unicode_value EncodedString or None # is_identifier boolean type = str_type @@ -1094,12 +1094,13 @@ class StringNode(PyConstNode): self.check_for_coercion_error(dst_type, fail=True) # this will be a unicode string in Py3, so make sure we can decode it - if self.value.encoding and self.unicode_value is None: - encoding = self.value.encoding + if self.value.encoding and isinstance(self.value, StringEncoding.BytesLiteral): try: - self.value.decode(encoding) - except (UnicodeDecodeError, AttributeError): - error(self.pos, "String decoding as '%s' failed. Consider using a byte string or unicode string explicitly, or adjust the source code encoding." % encoding) + self.value.decode(self.value.encoding) + except UnicodeDecodeError: + error(self.pos, ("Decoding unprefixed string literal from '%s' failed. Consider using" + "a byte string or unicode string explicitly, " + "or adjust the source code encoding.") % self.value.encoding) return self diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 5ffc476f..3d32076e 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -1448,8 +1448,8 @@ class TransformBuiltinMethods(EnvTransform): error(self.pos, "Builtin 'locals()' called with wrong number of args, expected 0, got %d" % len(node.args)) return node pos = node.pos - items = [ ExprNodes.DictItemNode(pos, - key=ExprNodes.StringNode(pos, value=var, unicode_value=var), + items = [ ExprNodes.DictItemNode(pos, + key=ExprNodes.StringNode(pos, value=var), value=ExprNodes.NameNode(pos, name=var)) for var in lenv.entries ] return ExprNodes.DictNode(pos, key_value_pairs=items) diff --git a/tests/run/cython3.pyx b/tests/run/cython3.pyx index 17129986..213b4a81 100644 --- a/tests/run/cython3.pyx +++ b/tests/run/cython3.pyx @@ -10,6 +10,25 @@ except NameError: seq.sort() return seq +__doc__ = """ +>>> items = list(locals_function(1).items()) +>>> items.sort() +>>> for item in items: +... print('%s = %r' % item) +a = 1 +b = 2 +x = u'abc' +""" + +import sys +if sys.version_info[0] >= 3: + __doc__ = __doc__.replace(" u'", " '") + +def locals_function(a, b=2): + x = 'abc' + return locals() + + def print_function(*args): """ >>> print_function(1,2,3) @@ -17,19 +36,6 @@ def print_function(*args): """ print(*args) # this isn't valid Py2 syntax -def locals_function(a, b=2): - """ - >>> items = list(locals_function(1).items()) - >>> items.sort() - >>> for item in items: - ... print('%s = %r' % item) - a = 1 - b = 2 - x = 'abc' - """ - x = 'abc' - return locals() - def exec3_function(cmd): """ >>> exec3_function('a = 1+1')['a'] -- 2.26.2