[pypy-svn] r13818 - in pypy/dist/pypy/rpython: . test

mwh at codespeak.net mwh at codespeak.net
Fri Jun 24 17:18:58 CEST 2005


Author: mwh
Date: Fri Jun 24 17:18:57 2005
New Revision: 13818

Modified:
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
%-formatting for instances.


Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Fri Jun 24 17:18:57 2005
@@ -11,6 +11,8 @@
 from pypy.rpython.rslice import startstop_slice_repr, startonly_slice_repr
 from pypy.rpython.lltype import GcStruct, Signed, Array, Char, Ptr, malloc
 from pypy.rpython.lltype import Bool, Void, GcArray, nullptr
+from pypy.rpython.rclass import InstanceRepr, ll_instance_str, getinstancerepr
+
 
 # ____________________________________________________________
 #
@@ -221,9 +223,15 @@
         if isinstance(thing, tuple):
             code = thing[0]
             vitem, r_arg = argsiter.next()
-            if code == 's':
-                assert isinstance(r_arg, StringRepr)
-                vchunk = hop.llops.convertvar(vitem, r_arg, string_repr)
+            if code == 's' or (code == 'r' and isinstance(r_arg, InstanceRepr)):
+                if isinstance(r_arg, StringRepr):
+                    vchunk = hop.llops.convertvar(vitem, r_arg, string_repr)
+                elif isinstance(r_arg, InstanceRepr):
+                    vinst = hop.llops.convertvar(
+                        vitem, r_arg, getinstancerepr(hop.rtyper, None))
+                    vchunk = hop.gendirectcall(ll_instance_str, vinst)
+                else:
+                    assert 0
             elif code == 'd':
                 assert isinstance(r_arg, IntegerRepr)
                 vchunk = hop.gendirectcall(rint.ll_int2str, vitem)

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Fri Jun 24 17:18:57 2005
@@ -265,3 +265,26 @@
 
     res = ev_fun(0)
     assert ''.join(res.chars) == '<D object>'
+
+def test_percentformat_instance():
+    class C:
+        pass
+    class D(C):
+        pass
+    
+    def dummy(i):
+        if i:
+            x = C()
+            y = D()
+        else:
+            x = D()
+            y = C()
+        return "what a nice %s, much nicer than %r"%(x, y)
+        
+    ev_fun = make_interpreter(dummy, [0])
+    
+    res = ev_fun(1)
+    assert ''.join(res.chars) == 'what a nice <C object>, much nicer than <D object>'
+
+    res = ev_fun(0)
+    assert ''.join(res.chars) == 'what a nice <D object>, much nicer than <C object>'



More information about the Pypy-commit mailing list