[pypy-svn] r14089 - in pypy/dist/pypy/objspace/std: . test

gintas at codespeak.net gintas at codespeak.net
Sat Jul 2 14:13:37 CEST 2005


Author: gintas
Date: Sat Jul  2 14:13:37 2005
New Revision: 14089

Modified:
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/test/test_stringobject.py
Log:
Fixed a bug in string formatting ("a" % u"b" would not raise a TypeError).


Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Sat Jul  2 14:13:37 2005
@@ -1020,7 +1020,8 @@
             # CPython's logic for deciding if  ""%values  is
             # an error (1 value, 0 %-formatters) or not
             # (values is of a mapping type)
-            if hasattr(values, '__getitem__') and not isinstance(values, str):
+            if (hasattr(values, '__getitem__')
+                and not isinstance(values, basestring)):
                 return _formatting.format(format, (values,), values)
             else:
                 return _formatting.format(format, (values,), None)

Modified: pypy/dist/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_stringobject.py	Sat Jul  2 14:13:37 2005
@@ -116,9 +116,24 @@
         assert self.space.eq_w(space.getitem(w_str, w_slice), w('el'))
 
 class AppTestStringObject:
+
     def test_format_wrongchar(self):
         raises(ValueError, 'a%Zb'.__mod__, ((23,),))
 
+    def test_format(self):
+        raises(TypeError, "foo".__mod__, "bar")
+        raises(TypeError, u"foo".__mod__, "bar")
+        raises(TypeError, "foo".__mod__, u"bar")
+
+        for format, arg, cls in [("a %s b", "foo", str),
+                                 (u"a %s b", "foo", unicode),
+                                 ("a %s b", u"foo", unicode),
+                                 (u"a %s b", u"foo", unicode)]:
+            raises(TypeError, format[:2].__mod__, arg)
+            result = format % arg
+            assert result == "a foo b"
+            assert isinstance(result, cls)
+
     def test_split(self):
         assert "".split() == []
         assert " ".split() == []



More information about the Pypy-commit mailing list