[pypy-svn] r13815 - in pypy/dist/pypy/rpython: . test
mwh at codespeak.net
mwh at codespeak.net
Fri Jun 24 17:08:01 CEST 2005
Author: mwh
Date: Fri Jun 24 17:07:59 2005
New Revision: 13815
Modified:
pypy/dist/pypy/rpython/rclass.py
pypy/dist/pypy/rpython/rstr.py
pypy/dist/pypy/rpython/test/test_rstr.py
Log:
simple str(instance)
%-formatting to come.
Modified: pypy/dist/pypy/rpython/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/rclass.py (original)
+++ pypy/dist/pypy/rpython/rclass.py Fri Jun 24 17:07:59 2005
@@ -491,6 +491,23 @@
vinst, vattr, vvalue = hop.inputargs(self, Void, r_value)
self.setfield(vinst, attr, vvalue, hop.llops)
+ def rtype_str(self, hop):
+ v_arg = hop.inputarg(getinstancerepr(hop.rtyper, None), 0)
+ return hop.gendirectcall(ll_instance_str, v_arg)
+
+
+def ll_instance_str(instance):
+ from pypy.rpython import rstr
+ nameLen = len(instance.typeptr.name)
+ nameString = malloc(rstr.STR, nameLen-1)
+ i = 0
+ while i < nameLen - 1:
+ nameString.chars[i] = instance.typeptr.name[i]
+ i += 1
+ return rstr.ll_strconcat(rstr.instance_str_prefix,
+ rstr.ll_strconcat(nameString,
+ rstr.instance_str_suffix))
+
class __extend__(pairtype(InstanceRepr, InstanceRepr)):
def convert_from_to((r_ins1, r_ins2), v, llops):
Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py (original)
+++ pypy/dist/pypy/rpython/rstr.py Fri Jun 24 17:07:59 2005
@@ -549,3 +549,9 @@
raise StopIteration
iter.index = index + 1
return chars[index]
+
+# these should be in rclass, but circular imports prevent (also it's
+# not that insane that a string constant is built in this file).
+
+instance_str_prefix = string_repr.convert_const("<")
+instance_str_suffix = string_repr.convert_const(" object>")
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:07:59 2005
@@ -245,3 +245,23 @@
return s1+s2 == s and s2+s1 == 'lohel'
res = interpret(fn, ())
assert res
+
+def test_strformat_instance():
+ class C:
+ pass
+ class D(C):
+ pass
+ def dummy(i):
+ if i:
+ x = C()
+ else:
+ x = D()
+ return str(x)
+
+ ev_fun = make_interpreter(dummy, [0])
+
+ res = ev_fun(1)
+ assert ''.join(res.chars) == '<C object>'
+
+ res = ev_fun(0)
+ assert ''.join(res.chars) == '<D object>'
More information about the Pypy-commit
mailing list