another fix for str() handling that works around internal use of EncodedString in...
authorStefan Behnel <scoder@users.berlios.de>
Tue, 23 Nov 2010 10:20:59 +0000 (11:20 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Tue, 23 Nov 2010 10:20:59 +0000 (11:20 +0100)
Cython/Compiler/ExprNodes.py
Cython/Compiler/ParseTreeTransforms.py
tests/run/cython3.pyx

index cb95f18822e1b7fefa7169df0a38b15ef4055ee2..9bcd2b98c9263920afcedaa76c5982d9c46e6cb6 100755 (executable)
@@ -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
 
index 5ffc476fddf6f021d7184c31b3168184599ed6b9..3d32076e07b3d9625ccae57fde33947ea0ae2764 100644 (file)
@@ -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)
index 1712998633896ea3d93937b10c67dd588e4e5940..213b4a8154946ef942c5bcd6249a3fd187415016 100644 (file)
@@ -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']