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

arigo at codespeak.net arigo at codespeak.net
Wed Apr 19 20:37:14 CEST 2006


Author: arigo
Date: Wed Apr 19 20:37:12 2006
New Revision: 25998

Added:
   pypy/dist/pypy/rpython/rctypes/rpyobject.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rctypes/apyobject.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rpyobject.py
Log:
Specialize py_object() calls.  Easy enough because we can reuse the
many existing conversions from various RPython types to PyObjRepr,
even without reusing more of PyObjRepr.


Modified: pypy/dist/pypy/rpython/rctypes/apyobject.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/apyobject.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/apyobject.py	Wed Apr 19 20:37:12 2006
@@ -1,6 +1,8 @@
 from ctypes import py_object
 from pypy.annotation.model import SomeCTypesObject
 from pypy.rpython import extregistry
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.robject import pyobj_repr
 
 
 # __________ py_object() calls __________
@@ -8,14 +10,28 @@
 def py_object_compute_result_annotation(s_obj=None):
     return SomeCTypesObject(py_object, SomeCTypesObject.OWNSMEMORY)
 
+def py_object_specialize_call(hop):
+    r_pyobject = hop.r_result
+    v_result = r_pyobject.allocate_instance(hop.llops)
+    if len(hop.args_s):
+        [v_input] = hop.inputargs(pyobj_repr)
+        r_pyobject.setvalue(hop.llops, v_result, v_input)
+    return v_result
+
 extregistry.register_value(py_object, 
     compute_result_annotation=py_object_compute_result_annotation,
-    )#specialize_call=py_object_specialize_call)
+    specialize_call=py_object_specialize_call)
 
 # __________ prebuilt py_object instances __________
 
 def py_object_instance_compute_annotation(type, instance):
     return SomeCTypesObject(py_object, SomeCTypesObject.OWNSMEMORY)
 
+def py_object_instance_get_repr(rtyper, s_pyobject):
+    from pypy.rpython.rctypes.rpyobject import CTypesPyObjRepr
+    lowleveltype = lltype.Ptr(lltype.PyObject)
+    return CTypesPyObjRepr(rtyper, s_pyobject, lowleveltype)
+
 extregistry.register_type(py_object, 
-    compute_annotation=py_object_instance_compute_annotation)
+    compute_annotation=py_object_instance_compute_annotation,
+    get_repr=py_object_instance_get_repr)

Added: pypy/dist/pypy/rpython/rctypes/rpyobject.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/rpyobject.py	Wed Apr 19 20:37:12 2006
@@ -0,0 +1,7 @@
+from pypy.rpython.rmodel import inputconst
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.rctypes.rmodel import CTypesValueRepr
+
+
+class CTypesPyObjRepr(CTypesValueRepr):
+    pass

Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	Wed Apr 19 20:37:12 2006
@@ -155,3 +155,11 @@
     assert not c_char_p(None)
 
     assert not c_void_p()
+
+def test_py_object():
+    class TAG:
+        pass
+    x = py_object(TAG)
+    assert x.value is TAG
+    x.value = 42
+    assert x.value == 42

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rpyobject.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rpyobject.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rpyobject.py	Wed Apr 19 20:37:12 2006
@@ -11,6 +11,7 @@
 from pypy.translator.c.test.test_genc import compile
 import sys
 from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.lltypesystem import lltype
 
 from ctypes import py_object
 
@@ -44,3 +45,12 @@
         assert s.knowntype == py_object
         if conftest.option.view:
             a.translator.view()
+
+class Test_specialization:
+    def test_specialize_wrapping(self):
+        def wrap(x):
+            return py_object(x)
+
+        res = interpret(wrap, [9])
+        assert lltype.typeOf(res.c_data[0]) == lltype.Ptr(lltype.PyObject)
+        assert res.c_data[0]._obj.value == 9



More information about the Pypy-commit mailing list