[pypy-commit] pypy default: Issue #2354: improve the error message for "x % y" where x is a

arigo pypy.commits at gmail.com
Sat Jul 30 11:39:34 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r85931:cb37f4a3cb6e
Date: 2016-07-30 17:41 +0200
http://bitbucket.org/pypy/pypy/changeset/cb37f4a3cb6e/

Log:	Issue #2354: improve the error message for "x % y" where x is a non-
	constant string

diff --git a/rpython/annotator/binaryop.py b/rpython/annotator/binaryop.py
--- a/rpython/annotator/binaryop.py
+++ b/rpython/annotator/binaryop.py
@@ -401,6 +401,9 @@
 class __extend__(pairtype(SomeString, SomeTuple),
                  pairtype(SomeUnicodeString, SomeTuple)):
     def mod((s_string, s_tuple)):
+        if not s_string.is_constant():
+            raise AnnotatorError("string formatting requires a constant "
+                                 "string/unicode on the left of '%'")
         is_string = isinstance(s_string, SomeString)
         is_unicode = isinstance(s_string, SomeUnicodeString)
         assert is_string or is_unicode
diff --git a/rpython/annotator/test/test_annrpython.py b/rpython/annotator/test/test_annrpython.py
--- a/rpython/annotator/test/test_annrpython.py
+++ b/rpython/annotator/test/test_annrpython.py
@@ -4623,6 +4623,14 @@
         a = self.RPythonAnnotator()
         a.build_types(main, [int])
 
+    def test_string_mod_nonconstant(self):
+        def f(x):
+            return x % 5
+        a = self.RPythonAnnotator()
+        e = py.test.raises(AnnotatorError, a.build_types, f, [str])
+        assert ('string formatting requires a constant string/unicode'
+                in str(e.value))
+
 
 def g(n):
     return [0, 1, 2, n]


More information about the pypy-commit mailing list