Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r48842:a4cd79eb1bb5
Date: 2011-10-26 16:57 +0200
http://bitbucket.org/pypy/pypy/changeset/a4cd79eb1bb5/
Log: Disallow implicit concatenation of bytes and strings
diff --git a/pypy/interpreter/astcompiler/astbuilder.py
b/pypy/interpreter/astcompiler/astbuilder.py
--- a/pypy/interpreter/astcompiler/astbuilder.py
+++ b/pypy/interpreter/astcompiler/astbuilder.py
@@ -1079,14 +1079,18 @@
# UnicodeError in literal: turn into SyntaxError
self.error(e.errorstr(space), atom_node)
sub_strings_w = [] # please annotator
- # This implements implicit string concatenation.
- if len(sub_strings_w) > 1:
- w_sub_strings = space.newlist(sub_strings_w)
- w_join = space.getattr(space.wrap(""), space.wrap("join"))
- final_string = space.call_function(w_join, w_sub_strings)
- else:
- final_string = sub_strings_w[0]
- return ast.Str(final_string, atom_node.lineno, atom_node.column)
+ # Implement implicit string concatenation.
+ w_string = sub_strings_w[0]
+ for i in range(1, len(sub_strings_w)):
+ try:
+ w_string = space.add(w_string, sub_strings_w[i])
+ except error.OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
+ self.error("cannot mix bytes and nonbytes literals",
+ atom_node)
+ # UnicodeError in literal: turn into SyntaxError
+ return ast.Str(w_string, atom_node.lineno, atom_node.column)
elif first_child_type == tokens.NUMBER:
num_value = self.parse_number(first_child.value)
return ast.Num(num_value, atom_node.lineno, atom_node.column)
diff --git a/pypy/interpreter/astcompiler/test/test_astbuilder.py
b/pypy/interpreter/astcompiler/test/test_astbuilder.py
--- a/pypy/interpreter/astcompiler/test/test_astbuilder.py
+++ b/pypy/interpreter/astcompiler/test/test_astbuilder.py
@@ -1026,6 +1026,10 @@
s = self.get_first_expr("'hi' ' implicitly' ' extra'")
assert isinstance(s, ast.Str)
assert space.eq_w(s.s, space.wrap("hi implicitly extra"))
+ s = self.get_first_expr("b'hi' b' implicitly' b' extra'")
+ assert isinstance(s, ast.Str)
+ assert space.eq_w(s.s, space.wrapbytes("hi implicitly extra"))
+ raises(SyntaxError, self.get_first_expr, "b'hello' 'world'")
sentence = u"Die Männer ärgen sich!"
source = u"# coding: utf-7\nstuff = u'%s'" % (sentence,)
info = pyparse.CompileInfo("<test>", "exec")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit