[pypy-commit] pypy hpy: introduce HPyS_real, which is the actualy C struct used to represent handles in C, and use it in the definition of HPyContextS. This fixes test_ztranslation, and hopefully the real translation as well

antocuni pypy.commits at gmail.com
Thu Nov 21 10:28:51 EST 2019


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: hpy
Changeset: r98128:60cbdc3bb88a
Date: 2019-11-21 16:28 +0100
http://bitbucket.org/pypy/pypy/changeset/60cbdc3bb88a/

Log:	introduce HPyS_real, which is the actualy C struct used to represent
	handles in C, and use it in the definition of HPyContextS. This
	fixes test_ztranslation, and hopefully the real translation as well

diff --git a/pypy/module/hpy_universal/llapi.py b/pypy/module/hpy_universal/llapi.py
--- a/pypy/module/hpy_universal/llapi.py
+++ b/pypy/module/hpy_universal/llapi.py
@@ -33,14 +33,25 @@
 
 """ % SRC_DIR.join('getargs.c')])
 
+HPy_ssize_t = lltype.Signed # XXXXXXXXX?
 
-HPy = lltype.Signed
+# for practical reason, we use a primitive type to represent HPy almost
+# everywhere in RPython. HOWEVER, the "real" HPy C type which is defined in
+# universal/hpy.h is an anonymous struct: we need to use it e.g. to represent
+# fields inside HPyContextS
+HPy = HPy_ssize_t
+HPyS_real = rffi.CStruct('HPy',
+    ('_i', HPy_ssize_t),
+    hints={'eci': eci, 'typedef': True},
+)
+
+
 HPyContextS = rffi.CStruct('_HPyContext_s',
     ('ctx_version', rffi.INT_real),
-    ('h_None', HPy),
-    ('h_True', HPy),
-    ('h_False', HPy),
-    ('h_ValueError', HPy),
+    ('h_None', HPyS_real),
+    ('h_True', HPyS_real),
+    ('h_False', HPyS_real),
+    ('h_ValueError', HPyS_real),
     ('ctx_Module_Create', rffi.VOIDP),
     ('ctx_Dup', rffi.VOIDP),
     ('ctx_Close', rffi.VOIDP),
@@ -56,9 +67,6 @@
     hints={'eci': eci},
 )
 HPyContext = lltype.Ptr(HPyContextS)
-
-HPy_ssize_t = lltype.Signed # XXXXXXXXX?
-
 HPyInitFuncPtr = lltype.Ptr(lltype.FuncType([HPyContext], HPy))
 
 _HPyCFunctionPtr = lltype.Ptr(lltype.FuncType([HPyContext, HPy, HPy], HPy))
diff --git a/pypy/module/hpy_universal/state.py b/pypy/module/hpy_universal/state.py
--- a/pypy/module/hpy_universal/state.py
+++ b/pypy/module/hpy_universal/state.py
@@ -6,7 +6,7 @@
 from rpython.rlib.objectmodel import specialize
 from pypy.module.hpy_universal import llapi, handles
 
-CONTEXT_NAMES = unrolling_iterable(llapi.HPyContext.TO._names)
+CONTEXT_FIELDS = unrolling_iterable(llapi.HPyContext.TO._names)
 CONSTANT_NAMES = unrolling_iterable([name for name, _ in handles.CONSTANTS])
 DUMMY_FUNC = lltype.FuncType([], lltype.Void)
 
@@ -33,17 +33,21 @@
         space = self.space
         self.ctx = llapi._HPy_GetGlobalCtx()
 
-        for name in CONTEXT_NAMES:
+        for name in CONTEXT_FIELDS:
             if name == 'c_ctx_version':
                 continue
             if name.startswith('c_ctx_'):
+                # this is a function pointer: assign a default value so we get
+                # a reasonable error message if it's called without being
+                # assigned to something else
                 missing_function = make_missing_function(name)
                 funcptr = llhelper(lltype.Ptr(DUMMY_FUNC), missing_function)
                 setattr(self.ctx, name, rffi.cast(rffi.VOIDP, funcptr))
         i = 0
         for name in CONSTANT_NAMES:
             if name != 'NULL':
-                setattr(self.ctx, 'c_h_' + name, i)
+                h_struct = getattr(self.ctx, 'c_h_' + name)
+                h_struct.c__i = i
             i = i + 1
 
         # XXX collect all these functions automatically


More information about the pypy-commit mailing list