Commit ea0c2ffa authored by Stefan Behnel's avatar Stefan Behnel

another fix for str() handling that works around internal use of EncodedString...

another fix for str() handling that works around internal use of EncodedString in StringNode, fix test for locals() in -3 mode
parent 3e544076
...@@ -1076,8 +1076,8 @@ class StringNode(PyConstNode): ...@@ -1076,8 +1076,8 @@ class StringNode(PyConstNode):
# A Python str object, i.e. a byte string in Python 2.x and a # A Python str object, i.e. a byte string in Python 2.x and a
# unicode string in Python 3.x # unicode string in Python 3.x
# #
# value BytesLiteral # value BytesLiteral (or EncodedString with ASCII content)
# unicode_value EncodedString # unicode_value EncodedString or None
# is_identifier boolean # is_identifier boolean
type = str_type type = str_type
...@@ -1094,12 +1094,13 @@ class StringNode(PyConstNode): ...@@ -1094,12 +1094,13 @@ class StringNode(PyConstNode):
self.check_for_coercion_error(dst_type, fail=True) self.check_for_coercion_error(dst_type, fail=True)
# this will be a unicode string in Py3, so make sure we can decode it # 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: if self.value.encoding and isinstance(self.value, StringEncoding.BytesLiteral):
encoding = self.value.encoding
try: try:
self.value.decode(encoding) self.value.decode(self.value.encoding)
except (UnicodeDecodeError, AttributeError): except UnicodeDecodeError:
error(self.pos, "String decoding as '%s' failed. Consider using a byte string or unicode string explicitly, or adjust the source code encoding." % encoding) 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 return self
......
...@@ -1449,7 +1449,7 @@ class TransformBuiltinMethods(EnvTransform): ...@@ -1449,7 +1449,7 @@ class TransformBuiltinMethods(EnvTransform):
return node return node
pos = node.pos pos = node.pos
items = [ ExprNodes.DictItemNode(pos, items = [ ExprNodes.DictItemNode(pos,
key=ExprNodes.StringNode(pos, value=var, unicode_value=var), key=ExprNodes.StringNode(pos, value=var),
value=ExprNodes.NameNode(pos, name=var)) value=ExprNodes.NameNode(pos, name=var))
for var in lenv.entries ] for var in lenv.entries ]
return ExprNodes.DictNode(pos, key_value_pairs=items) return ExprNodes.DictNode(pos, key_value_pairs=items)
......
...@@ -10,6 +10,25 @@ except NameError: ...@@ -10,6 +10,25 @@ except NameError:
seq.sort() seq.sort()
return seq 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): def print_function(*args):
""" """
>>> print_function(1,2,3) >>> print_function(1,2,3)
...@@ -17,19 +36,6 @@ def print_function(*args): ...@@ -17,19 +36,6 @@ def print_function(*args):
""" """
print(*args) # this isn't valid Py2 syntax 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): def exec3_function(cmd):
""" """
>>> exec3_function('a = 1+1')['a'] >>> exec3_function('a = 1+1')['a']
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment