[pypy-commit] pypy hpy: (antocuni, ronan, arigo)

arigo pypy.commits at gmail.com
Sun Nov 17 09:17:05 EST 2019


Author: Armin Rigo <arigo at tunes.org>
Branch: hpy
Changeset: r98097:44e3a984eb36
Date: 2019-11-17 15:16 +0100
http://bitbucket.org/pypy/pypy/changeset/44e3a984eb36/

Log:	(antocuni, ronan, arigo)

	More general progress

diff --git a/pypy/module/hpy_universal/handles.py b/pypy/module/hpy_universal/handles.py
--- a/pypy/module/hpy_universal/handles.py
+++ b/pypy/module/hpy_universal/handles.py
@@ -20,6 +20,10 @@
         self.handles_w[index] = None
         self.free_list.append(index)
 
+    def deref(self, index):
+        assert index > 0
+        return self.handles_w[index]
+
     def consume(self, index):
         """
         Like close, but also return the w_object which was pointed by the handled
@@ -41,7 +45,11 @@
 def close(space, index):
     mgr = space.fromcache(HandleManager)
     mgr.close(index)
-    
+
+def deref(space, index):
+    mgr = space.fromcache(HandleManager)
+    return mgr.deref(index)
+
 def consume(space, index):
     mgr = space.fromcache(HandleManager)
     return mgr.consume(index)
diff --git a/pypy/module/hpy_universal/interp_extfunc.py b/pypy/module/hpy_universal/interp_extfunc.py
--- a/pypy/module/hpy_universal/interp_extfunc.py
+++ b/pypy/module/hpy_universal/interp_extfunc.py
@@ -47,7 +47,9 @@
         return handles.consume(space, h_result)
 
     def call_varargs(self, space, arguments_w):
-        raise NotImplementedError("later")
+        w_tuple = space.newtuple(arguments_w)
+        # xxx here we just invoke call_o() with the w_tuple
+        return self.call_o(space, w_tuple)
 
     def descr_call(self, space, __args__):
         flags = self.flags
diff --git a/pypy/module/hpy_universal/interp_hpy.py b/pypy/module/hpy_universal/interp_hpy.py
--- a/pypy/module/hpy_universal/interp_hpy.py
+++ b/pypy/module/hpy_universal/interp_hpy.py
@@ -55,6 +55,21 @@
 def HPy_Dup(space, ctx, h):
     return handles.dup(space, h)
 
+ at apifunc([llapi.HPyContext, rffi.LONG], llapi.HPy, error=0)
+def HPyLong_FromLong(space, ctx, value):
+    w_obj = space.newint(rffi.cast(lltype.Signed, value))
+    return handles.new(space, w_obj)
+
+ at apifunc([llapi.HPyContext, llapi.HPy], rffi.LONG, error=0)
+def HPyLong_AsLong(space, ctx, h):
+    w_obj = handles.deref(space, h)
+    #w_obj = space.int(w_obj)     --- XXX write a test for this
+    value = space.int_w(w_obj)
+    result = rffi.cast(rffi.LONG, value)
+    #if rffi.cast(lltype.Signed, result) != value: --- XXX on Windows 64
+    #    ...
+    return result
+
 
 def create_hpy_module(space, name, origin, lib, initfunc):
     state = space.fromcache(State)
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
@@ -30,6 +30,7 @@
     ('ctx_Dup', rffi.VOIDP),
     ('ctx_Close', rffi.VOIDP),
     ('ctx_Long_FromLong', rffi.VOIDP),
+    ('ctx_Long_AsLong', rffi.VOIDP),
     ('ctx_Arg_ParseTuple', rffi.VOIDP),
     ('ctx_Number_Add', rffi.VOIDP),
     ('ctx_Unicode_FromString', rffi.VOIDP),
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
@@ -49,3 +49,9 @@
         #
         funcptr = interp_hpy.HPy_Dup.get_llhelper(space)
         self.ctx.c_ctx_Dup = rffi.cast(rffi.VOIDP, funcptr)
+        #
+        funcptr = interp_hpy.HPyLong_FromLong.get_llhelper(space)
+        self.ctx.c_ctx_Long_FromLong = rffi.cast(rffi.VOIDP, funcptr)
+        #
+        funcptr = interp_hpy.HPyLong_AsLong.get_llhelper(space)
+        self.ctx.c_ctx_Long_AsLong = rffi.cast(rffi.VOIDP, funcptr)
diff --git a/pypy/module/hpy_universal/test/_vendored/include/cpython/hpy.h b/pypy/module/hpy_universal/test/_vendored/include/cpython/hpy.h
--- a/pypy/module/hpy_universal/test/_vendored/include/cpython/hpy.h
+++ b/pypy/module/hpy_universal/test/_vendored/include/cpython/hpy.h
@@ -111,6 +111,12 @@
     return _py2h(PyLong_FromLong(v));
 }
 
+HPyAPI_FUNC(long)
+HPyLong_AsLong(HPyContext ctx, HPy h)
+{
+    return PyLong_AsLong(_h2py(h));
+}
+
 HPyAPI_FUNC(HPy)
 HPyNumber_Add(HPyContext ctx, HPy x, HPy y)
 {
diff --git a/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_ctx.h b/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_ctx.h
--- a/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_ctx.h
+++ b/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_ctx.h
@@ -15,6 +15,7 @@
     HPy (*ctx_Dup)(HPyContext ctx, HPy h);
     void (*ctx_Close)(HPyContext ctx, HPy h);
     HPy (*ctx_Long_FromLong)(HPyContext ctx, long value);
+    long (*ctx_Long_AsLong)(HPyContext ctx, HPy h);
     int (*ctx_Arg_ParseTuple)(HPyContext ctx, HPy args, const char *fmt, va_list _vl);
     HPy (*ctx_Number_Add)(HPyContext ctx, HPy x, HPy y);
     HPy (*ctx_Unicode_FromString)(HPyContext ctx, const char *utf8);
diff --git a/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_func.h b/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_func.h
--- a/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_func.h
+++ b/pypy/module/hpy_universal/test/_vendored/include/universal/autogen_func.h
@@ -28,6 +28,10 @@
      return ctx->ctx_Long_FromLong ( ctx, value ); 
 }
 
+static inline long HPyLong_AsLong(HPyContext ctx, HPy h) {
+     return ctx->ctx_Long_AsLong ( ctx, h ); 
+}
+
 static inline int HPyArg_ParseTuple(HPyContext ctx, HPy args, const char *fmt, ...) {
      va_list _vl; va_start(_vl, fmt); int _res = ctx->ctx_Arg_ParseTuple ( ctx, args, fmt, _vl ); va_end(_vl); return _res; 
 }
diff --git a/pypy/module/hpy_universal/test/_vendored/include/universal/hpy.h b/pypy/module/hpy_universal/test/_vendored/include/universal/hpy.h
--- a/pypy/module/hpy_universal/test/_vendored/include/universal/hpy.h
+++ b/pypy/module/hpy_universal/test/_vendored/include/universal/hpy.h
@@ -39,8 +39,9 @@
     HPyMethodDef *m_methods;
 } HPyModuleDef;
 
+#define _HPy_HIDDEN   __attribute__((visibility("hidden")))
 #define HPy_MODINIT(modname)                                   \
-    HPyContext _ctx_for_trampolines;                           \
+    _HPy_HIDDEN HPyContext _ctx_for_trampolines;               \
     static HPy init_##modname##_impl(HPyContext ctx);          \
     HPy HPyInit_##modname(HPyContext ctx)                      \
     {                                                          \
diff --git a/pypy/module/hpy_universal/test/test_basic.py b/pypy/module/hpy_universal/test/test_basic.py
--- a/pypy/module/hpy_universal/test/test_basic.py
+++ b/pypy/module/hpy_universal/test/test_basic.py
@@ -66,6 +66,19 @@
         x = object()
         assert mod.f(x) is x
 
+    def test_long_aslong(self):
+        mod = self.make_module("""
+            HPy_FUNCTION(f)
+            static HPy f_impl(HPyContext ctx, HPy self, HPy arg)
+            {
+                long a = HPyLong_AsLong(ctx, arg);
+                return HPyLong_FromLong(ctx, a * 2);
+            }
+            @EXPORT f METH_O
+            @INIT
+        """)
+        assert mod.f(45) == 90
+
     def test_wrong_number_of_arguments(self):
         # XXX: this test was manually modified to turn pytest.raises into raises :(
         mod = self.make_module("""
diff --git a/pypy/module/hpy_universal/test/test_handles.py b/pypy/module/hpy_universal/test/test_handles.py
--- a/pypy/module/hpy_universal/test/test_handles.py
+++ b/pypy/module/hpy_universal/test/test_handles.py
@@ -3,6 +3,11 @@
 
 class TestHandleManager(object):
 
+    def test_first_handle_is_not_zero(self):
+        mgr = HandleManager(None)
+        h = mgr.new('hello')
+        assert h > 0
+
     def test_new(self):
         mgr = HandleManager(None)
         h = mgr.new('hello')
@@ -14,6 +19,12 @@
         assert mgr.close(h) is None
         assert mgr.handles_w[h] is None
 
+    def test_deref(self):
+        mgr = HandleManager(None)
+        h = mgr.new('hello')
+        assert mgr.deref(h) == 'hello'     # 'hello' is a fake W_Root
+        assert mgr.deref(h) == 'hello'
+
     def test_consume(self):
         mgr = HandleManager(None)
         h = mgr.new('hello')


More information about the pypy-commit mailing list