[pypy-svn] r25276 - in pypy/dist/pypy/rpython: lltypesystem test

tismer at codespeak.net tismer at codespeak.net
Tue Apr 4 02:54:25 CEST 2006


Author: tismer
Date: Tue Apr  4 02:54:22 2006
New Revision: 25276

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rbuiltin.py
   pypy/dist/pypy/rpython/test/test_rbuiltin.py
Log:
support for builtin hasattr (PyObject and const only)

Modified: pypy/dist/pypy/rpython/lltypesystem/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rbuiltin.py	Tue Apr  4 02:54:22 2006
@@ -56,10 +56,20 @@
         return hop.genop('cast_pointer', [v_inst],    # v_type implicit in r_result
                          resulttype = hop.r_result.lowleveltype)
 
-
     classdef = s_class.descriptions.keys()[0].getuniqueclassdef()
     return rclass.rtype_new_instance(hop.rtyper, classdef, hop.llops)
 
+def rtype_builtin_hasattr(hop):
+    if hop.s_result.is_constant():
+        return hop.inputconst(lltype.Bool, hop.s_result.const)
+    if hop.args_r[0] == pyobj_repr:
+        v_obj, v_typ = hop.inputargs(pyobj_repr, pyobj_repr)
+        c = hop.inputconst(pyobj_repr, hasattr)
+        v = hop.genop('simple_call', [c, v_obj, v_typ], resulttype = pyobj_repr)
+        return hop.llops.convertvar(v, pyobj_repr, bool_repr)
+    raise TyperError("hasattr is only suported on a constant or on PyObject")
+
 BUILTIN_TYPER = {}
 BUILTIN_TYPER[objectmodel.instantiate] = rtype_instantiate
 BUILTIN_TYPER[isinstance] = rtype_builtin_isinstance
+BUILTIN_TYPER[hasattr] = rtype_builtin_hasattr

Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py	Tue Apr  4 02:54:22 2006
@@ -236,6 +236,20 @@
     res = interpret(f, [1])
     assert res is False    
 
+def test_hasattr():
+    class A(object):
+        def __init__(self):
+            self.x = 42
+    def f(i):
+        a = A()
+        if i==0: return int(hasattr(A, '__init__'))
+        if i==1: return int(hasattr(A, 'y'))
+        if i==2: return int(hasattr(42, 'x'))
+    for x, y in zip(range(3), (1, 0, 0)):
+        res = interpret(f, [x])
+        assert res._obj.value == y
+    # hmm, would like to test against PyObj, is this the wrong place/way?
+
 def test_we_are_translated():
     def f():
         return we_are_translated()



More information about the Pypy-commit mailing list