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

mwh at codespeak.net mwh at codespeak.net
Thu Jul 21 17:21:39 CEST 2005


Author: mwh
Date: Thu Jul 21 17:21:38 2005
New Revision: 14874

Modified:
   pypy/dist/pypy/objspace/std/test/test_stringformat.py
   pypy/dist/pypy/objspace/std/unicodeobject.py
Log:
fix u"%(foo)s"%UserDefinedMapping() in the same way as revision 13045
did for strings.

Add a test -- the first for unicode % anything !? -- but py.test has 
gone insane on me so I haven't run them :(


Modified: pypy/dist/pypy/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stringformat.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_stringformat.py	Thu Jul 21 17:21:38 2005
@@ -38,6 +38,11 @@
             def __getitem__(self, key):
                 py.test.fail('should not be here')
         assert '' % MyMapping() == ''
+        class MyMapping2(object):
+            def __getitem__(self, key):
+                return key
+        assert '%(key)s'%MyMapping2() == 'key'
+        assert u'%(key)s'%MyMapping2() == u'key'
 
 class AppTestStringObject:
 

Modified: pypy/dist/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/unicodeobject.py	Thu Jul 21 17:21:38 2005
@@ -846,9 +846,17 @@
     import _formatting
     if isinstance(values, tuple):
         return _formatting.format(format, values, None, do_unicode=True)
-    if hasattr(values, "keys"):
-        return _formatting.format(format, (values,), values, do_unicode=True)
-    return _formatting.format(format, (values,), None, do_unicode=True)
+    else:
+        # 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, basestring)):
+            return _formatting.format(format, (values,), values,
+                                      do_unicode=True)
+        else:
+            return _formatting.format(format, (values,), None,
+                                      do_unicode=True)
 
 def unicode_encode__Unicode_ANY_ANY(unistr, encoding=None, errors=None):
     import codecs, sys



More information about the Pypy-commit mailing list