[pypy-commit] pypy hpy: move the attach_legacy_methods logic into interp_cpy_compat, where it belongs

antocuni pypy.commits at gmail.com
Tue Jan 14 13:02:42 EST 2020


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: hpy
Changeset: r98535:12ac759ab903
Date: 2020-01-14 18:55 +0100
http://bitbucket.org/pypy/pypy/changeset/12ac759ab903/

Log:	move the attach_legacy_methods logic into interp_cpy_compat, where
	it belongs

diff --git a/pypy/module/hpy_universal/interp_cpy_compat.py b/pypy/module/hpy_universal/interp_cpy_compat.py
--- a/pypy/module/hpy_universal/interp_cpy_compat.py
+++ b/pypy/module/hpy_universal/interp_cpy_compat.py
@@ -1,8 +1,12 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
+from rpython.rlib.rarithmetic import widen
 from pypy.interpreter.error import OperationError, oefmt
+from pypy.module.cpyext import pyobject
+from pypy.module.cpyext.methodobject import PyMethodDef, PyCFunction
+from pypy.module.cpyext.modsupport import convert_method_defs
 from pypy.module.hpy_universal.apiset import API
 from pypy.module.hpy_universal import handles
-from pypy.module.cpyext import pyobject
+from pypy.module.hpy_universal import llapi
 
 @API.func("HPy HPy_FromPyObject(HPyContext ctx, void *obj)")
 def HPy_FromPyObject(space, ctx, obj):
@@ -14,3 +18,33 @@
     w_obj = handles.deref(space, h)
     pyobj = pyobject.make_ref(space, w_obj)
     return rffi.cast(rffi.VOIDP, pyobj)
+
+
+def attach_legacy_methods(space, hpymethods, w_mod, modname):
+    """
+    Convert HPyMethodDef[] into PyMethodDef[], and wrap the methods into the
+    proper cpyext.W_*Function objects
+    """
+    PyMethodDefP = rffi.CArrayPtr(PyMethodDef)
+
+    # convert hpymethods into a C array of PyMethodDef
+    dict_w = {}
+    n = len(hpymethods)
+    with lltype.scoped_alloc(PyMethodDefP.TO, n+1) as pymethods:
+        for i in range(n):
+            src = hpymethods[i] # HPyMethodDef
+            dst = pymethods[i]  # PyMethodDef
+            dst.c_ml_name = src.c_ml_name
+            dst.c_ml_doc = src.c_ml_doc
+            # for legacy methods, ml_meth contains a PyCFunction which can be
+            # called using the old C-API/cpyext calling convention
+            dst.c_ml_meth = rffi.cast(PyCFunction, src.c_ml_meth)
+            rffi.setintfield(dst, 'c_ml_flags', widen(src.c_ml_flags) & ~llapi._HPy_METH)
+        pymethods[n].c_ml_name = lltype.nullptr(rffi.CONST_CCHARP.TO)
+        #
+        # convert_method_defs expects a PyMethodDef*, not a PyMethodDef[]
+        p_pymethods = rffi.cast(lltype.Ptr(PyMethodDef), pymethods)
+        convert_method_defs(space, dict_w, p_pymethods, None, w_mod, modname)
+
+    for key, w_func in dict_w.items():
+        space.setattr(w_mod, space.newtext(key), w_func)
diff --git a/pypy/module/hpy_universal/interp_module.py b/pypy/module/hpy_universal/interp_module.py
--- a/pypy/module/hpy_universal/interp_module.py
+++ b/pypy/module/hpy_universal/interp_module.py
@@ -6,6 +6,7 @@
 from pypy.module.hpy_universal import llapi
 from pypy.module.hpy_universal import handles
 from pypy.module.hpy_universal import interp_extfunc
+from pypy.module.hpy_universal.interp_cpy_compat import attach_legacy_methods
 
 
 @API.func("HPy HPyModule_Create(HPyContext ctx, HPyModuleDef *def)")
@@ -31,31 +32,3 @@
             attach_legacy_methods(space, legacy_methoddefs, w_mod, modname)
     #
     return handles.new(space, w_mod)
-
-
-def attach_legacy_methods(space, hpymethods, w_mod, modname):
-    from pypy.module.cpyext.methodobject import PyMethodDef, PyCFunction
-    from pypy.module.cpyext.modsupport import convert_method_defs
-    PyMethodDefP = rffi.CArrayPtr(PyMethodDef)
-
-    # convert hpymethods into a C array of PyMethodDef
-    dict_w = {}
-    n = len(hpymethods)
-    with lltype.scoped_alloc(PyMethodDefP.TO, n+1) as pymethods:
-        for i in range(n):
-            src = hpymethods[i] # HPyMethodDef
-            dst = pymethods[i]  # PyMethodDef
-            dst.c_ml_name = src.c_ml_name
-            dst.c_ml_doc = src.c_ml_doc
-            # for legacy methods, ml_meth contains a PyCFunction which can be
-            # called using the old C-API/cpyext calling convention
-            dst.c_ml_meth = rffi.cast(PyCFunction, src.c_ml_meth)
-            rffi.setintfield(dst, 'c_ml_flags', widen(src.c_ml_flags) & ~llapi._HPy_METH)
-        pymethods[n].c_ml_name = lltype.nullptr(rffi.CONST_CCHARP.TO)
-        #
-        # convert_method_defs expects a PyMethodDef*, not a PyMethodDef[]
-        p_pymethods = rffi.cast(lltype.Ptr(PyMethodDef), pymethods)
-        convert_method_defs(space, dict_w, p_pymethods, None, w_mod, modname)
-
-    for key, w_func in dict_w.items():
-        space.setattr(w_mod, space.newtext(key), w_func)


More information about the pypy-commit mailing list