[pypy-svn] r45998 - in pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem: . test

fijal at codespeak.net fijal at codespeak.net
Sun Aug 26 22:09:36 CEST 2007


Author: fijal
Date: Sun Aug 26 22:09:35 2007
New Revision: 45998

Modified:
   pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/rffi.py
   pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/test/test_rffi.py
Log:
Add rffi.make(STRUCT, **kwds)


Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/rffi.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/rffi.py	Sun Aug 26 22:09:35 2007
@@ -1,4 +1,5 @@
 
+from pypy.annotation import model as annmodel
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.lltypesystem.lloperation import llop
 from pypy.rpython.lltypesystem import ll2ctypes
@@ -6,6 +7,8 @@
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib.objectmodel import Symbolic, CDefinedIntSymbolic
 from pypy.rlib import rarithmetic
+from pypy.rpython.rbuiltin import parse_kwds
+from pypy.rpython.extregistry import ExtRegistryEntry
 import os
 
 class CConstant(Symbolic):
@@ -239,3 +242,38 @@
     if tp is lltype.Signed:
         return ULONG._type.BITS/8
     return tp._type.BITS/8
+
+# ********************** some helpers *******************
+
+def make(STRUCT, **fields):
+    """ Malloc a structure and populate it's fields
+    """
+    ptr = lltype.malloc(STRUCT, flavor='raw')
+    for name, value in fields.items():
+        setattr(ptr, name, value)
+    return ptr
+
+class MakeEntry(ExtRegistryEntry):
+    _about_ = make
+
+    def compute_result_annotation(self, s_type, **s_fields):
+        TP = s_type.const
+        if not isinstance(TP, lltype.Struct):
+            raise TypeError("make called with %s instead of Struct as first argument" % TP)
+        return annmodel.SomePtr(lltype.Ptr(TP))
+
+    def specialize_call(self, hop, **fields):
+        assert hop.args_s[0].is_constant()
+        hop.exception_cannot_occur()
+        vlist = [hop.inputarg(lltype.Void, arg=0)]
+        flags = {'flavor':'raw'}
+        vlist.append(hop.inputconst(lltype.Void, flags))
+        v_ptr = hop.genop('malloc', vlist, resulttype=hop.r_result.lowleveltype)
+        hop.has_implicit_exception(MemoryError)   # record that we know about it
+        hop.exception_is_here()
+        for name, i in fields.items():
+            name = name[2:]
+            v_arg = hop.inputarg(hop.args_r[i], arg=i)
+            v_name = hop.inputconst(lltype.Void, name)
+            hop.genop('setfield', [v_ptr, v_name, v_arg])
+        return v_ptr

Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/test/test_rffi.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/test/test_rffi.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/lltypesystem/test/test_rffi.py	Sun Aug 26 22:09:35 2007
@@ -260,3 +260,45 @@
     assert not size_and_sign(lltype.Signed)[1]
     assert not size_and_sign(lltype.Char)[1]
     assert size_and_sign(UINT)[1]
+
+def test_prebuild_constant():
+    py.test.skip("WIP")
+    h_source = py.code.Source("""
+    #ifndef _CONSTANTS
+    #define _CONSTANTS
+    int x = 3;
+    char** z = NULL;
+    #endif
+    """)
+    h_include = udir.join('constants.h')
+    h_include.write(h_source)
+
+    kwds = {'includes':['constants.h'], 'include_dirs':[str(udir)]}
+    get_x, set_x = CExternVariable(lltype.Signed, 'x', **kwds)
+    get_z, set_z = CExternVariable(CCHARPP, 'z', **kwds)
+
+    def f():
+        one = get_x()
+        set_x(13)
+        return one + get_x()
+
+    def g():
+        l = liststr2charpp(["a", "b", "c"])
+        try:
+            set_z(l)
+            return charp2str(get_z()[2])
+        finally:
+            free_charpp(l)
+
+    fn = compile(f, [])
+    assert fn() == 16
+    gn = compile(g, [])
+    assert gn() == "c"
+    
+def test_struct_create():
+    X = CStruct('xx', ('one', INT))
+    def f():
+        p = make(X, c_one=3)
+        return p.c_one
+    assert f() == 3
+    assert interpret(f, []) == 3



More information about the Pypy-commit mailing list