[pypy-commit] pypy cpyext-fast-typecheck: WIP: refactor again: there is no point in having tons of wrappers and a different class for each wrapper. It is cleaner and easier to inline the content of the wrapper directly inside each subclass

antocuni pypy.commits at gmail.com
Thu Mar 22 11:37:54 EDT 2018


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-fast-typecheck
Changeset: r94078:430955eada2e
Date: 2018-03-22 16:36 +0100
http://bitbucket.org/pypy/pypy/changeset/430955eada2e/

Log:	WIP: refactor again: there is no point in having tons of wrappers
	and a different class for each wrapper. It is cleaner and easier to
	inline the content of the wrapper directly inside each subclass

diff --git a/pypy/module/cpyext/methodobject.py b/pypy/module/cpyext/methodobject.py
--- a/pypy/module/cpyext/methodobject.py
+++ b/pypy/module/cpyext/methodobject.py
@@ -261,18 +261,6 @@
                                    self.w_objclass.name))
 
 
-class W_PyCWrapperObjectBinary(W_PyCWrapperObject):
-
-    def __init__(self, space, pto, method_name, wrapper_func, doc, func, offset):
-        W_PyCWrapperObject.__init__(self, space, pto, method_name, doc, func, offset)
-        self.wrap_binaryfunc = wrapper_func
-
-    def call(self, space, w_self, __args__):
-        self.check_args(__args__, 1)
-        func = self.get_func_to_call()
-        w_o = __args__.arguments_w[0]
-        return self.wrap_binaryfunc(space, func, w_self, w_o)
-
 
 class W_PyCWrapperObjectGeneric(W_PyCWrapperObject):
     """
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -19,6 +19,7 @@
 from pypy.module.cpyext.state import State
 from pypy.module.cpyext import userslot
 from pypy.module.cpyext.buffer import CBuffer, CPyBuffer, fq
+from pypy.module.cpyext.methodobject import W_PyCWrapperObject
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.argument import Arguments
 from rpython.rlib.unroll import unrolling_iterable
@@ -84,9 +85,13 @@
     check_num_args(space, w_args, 0)
     return generic_cpy_call(space, func_unary, w_self)
 
-def wrap_binaryfunc(space, func, w_self, w_x):
-    func_binary = rffi.cast(binaryfunc, func)
-    return generic_cpy_call(space, func_binary, w_self, w_x)
+class W_WrapBinaryFunc(W_PyCWrapperObject):
+    def call(self, space, w_self, __args__):
+        self.check_args(__args__, 1)
+        func = self.get_func_to_call()
+        func_binary = rffi.cast(binaryfunc, func)
+        w_x = __args__.arguments_w[0]
+        return generic_cpy_call(space, func_binary, w_self, w_x)
 
 def _get_ob_type(space, w_obj):
     # please ensure that w_obj stays alive
@@ -915,7 +920,7 @@
 static slotdef slotdefs[] = {
         SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
                "x.__len__() <==> len(x)"),
-        SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
+        SQSLOT("__add__", sq_concat, slot_sq_concat, W_WrapBinaryFunc,
           "x.__add__(y) <==> x+y"),
         SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
           "x.__mul__(n) <==> x*n"),
@@ -943,14 +948,14 @@
         SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
                "x.__contains__(y) <==> y in x"),
         SQSLOT("__iadd__", sq_inplace_concat, NULL,
-          wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
+          W_WrapBinaryFunc, "x.__iadd__(y) <==> x+=y"),
         SQSLOT("__imul__", sq_inplace_repeat, NULL,
           wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
 
         MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
                "x.__len__() <==> len(x)"),
         MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
-               wrap_binaryfunc,
+               W_WrapBinaryFunc,
                "x.__getitem__(y) <==> x[y]"),
         MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
                wrap_objobjargproc,
@@ -1019,35 +1024,35 @@
         NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
                "x[y:z] <==> x[y.__index__():z.__index__()]"),
         IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
-               wrap_binaryfunc, "+"),
+               W_WrapBinaryFunc, "+"),
         IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
-               wrap_binaryfunc, "-"),
+               W_WrapBinaryFunc, "-"),
         IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
-               wrap_binaryfunc, "*"),
+               W_WrapBinaryFunc, "*"),
         IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
-               wrap_binaryfunc, "/"),
+               W_WrapBinaryFunc, "/"),
         IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
-               wrap_binaryfunc, "%"),
+               W_WrapBinaryFunc, "%"),
         IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
-               wrap_binaryfunc, "**"),
+               W_WrapBinaryFunc, "**"),
         IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
-               wrap_binaryfunc, "<<"),
+               W_WrapBinaryFunc, "<<"),
         IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
-               wrap_binaryfunc, ">>"),
+               W_WrapBinaryFunc, ">>"),
         IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
-               wrap_binaryfunc, "&"),
+               W_WrapBinaryFunc, "&"),
         IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
-               wrap_binaryfunc, "^"),
+               W_WrapBinaryFunc, "^"),
         IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
-               wrap_binaryfunc, "|"),
+               W_WrapBinaryFunc, "|"),
         BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
         RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
         BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
         RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
         IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
-               slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
+               slot_nb_inplace_floor_divide, W_WrapBinaryFunc, "//"),
         IBSLOT("__itruediv__", nb_inplace_true_divide,
-               slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
+               slot_nb_inplace_true_divide, W_WrapBinaryFunc, "/"),
 
         TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
                "x.__str__() <==> str(x)"),
@@ -1062,7 +1067,7 @@
         FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
                "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
         TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
-               wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
+               W_WrapBinaryFunc, "x.__getattribute__('name') <==> x.name"),
         TPSLOT("__getattr__", tp_getattro, slot_tp_getattr, NULL, ""),
         TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
         TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -27,7 +27,7 @@
 from pypy.module.cpyext.methodobject import (W_PyCClassMethodObject,
     PyCFunction_NewEx, PyCFunction, PyMethodDef,
     W_PyCMethodObject, W_PyCFunctionObject,
-    W_PyCWrapperObjectGeneric, W_PyCWrapperObjectBinary)
+    W_PyCWrapperObject, W_PyCWrapperObjectGeneric)
 from pypy.module.cpyext.modsupport import convert_method_defs
 from pypy.module.cpyext.pyobject import (
     PyObject, make_ref, from_ref, get_typedescr, make_typedescr,
@@ -340,12 +340,14 @@
         if wrapper_func is None and wrapper_func_kwds is None:
             continue
 
-        from pypy.module.cpyext.slotdefs import wrap_binaryfunc
-        if wrapper_func is wrap_binaryfunc:
-            # XXX: this is just a quick hack, we need an official way to
-            # specify specialization
-            w_obj = W_PyCWrapperObjectBinary(space, pto, method_name, wrap_binaryfunc,
-                                             doc, func_voidp, offset=offset)
+        # XXX: this is just a quick hack to distinguish the old wrappers from
+        # the new ones: eventually, all of them will be subclasses of
+        # W_PyCWrapperObject
+        if type(wrapper_func) is type and issubclass(wrapper_func, W_PyCWrapperObject):
+            # new style
+            wrapper_class = wrapper_func
+            w_obj = wrapper_class(space, pto, method_name, doc, func_voidp,
+                                  offset=offset)
         else:
             w_obj = W_PyCWrapperObjectGeneric(space, pto, method_name,  wrapper_func,
                                               wrapper_func_kwds, doc,


More information about the pypy-commit mailing list