# 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
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
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)
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)
"""
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']