[pypy-commit] pypy py3k: Disallow implicit concatenation of bytes and strings

amauryfa noreply at buildbot.pypy.org
Sun Nov 6 20:59:18 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
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&#228;nner &#228;rgen sich!"
         source = u"# coding: utf-7\nstuff = u'%s'" % (sentence,)
         info = pyparse.CompileInfo("<test>", "exec")


More information about the pypy-commit mailing list