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):
# 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
......
......@@ -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)
......
......@@ -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']
......
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