Author: Matti Picus <[email protected]>
Branch: cpyext-more-slots
Changeset: r84309:efc2f2833423
Date: 2016-05-07 21:52 +0300
http://bitbucket.org/pypy/pypy/changeset/efc2f2833423/

Log:    simplify large switch for unary functions

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
@@ -374,7 +374,29 @@
     header = pypy_decl
     if mangle_name('', typedef.name) is None:
         header = None
-    if name == 'tp_setattro':
+    handled = False
+    # unary functions
+    for tp_name, attr in [('tp_as_number.c_nb_int', '__int__'),
+                          ('tp_as_number.c_nb_float', '__float__'),
+                          ('tp_str', '__str__'),
+                          ('tp_iter', '__iter__'),
+                          ('tp_iternext', 'next'),
+                          ]:
+        if name == tp_name:
+            slot_fn = w_type.getdictvalue(space, attr)
+            if int_fn is None:
+                return
+
+            @cpython_api([PyObject], PyObject, header=header)
+            @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), 
typedef.name))
+            def slot_func(space, w_self):
+                return space.call_function(slot_fn, w_self)
+            api_func = slot_func.api_func
+            handled = True
+
+    if handled:
+        pass
+    elif name == 'tp_setattro':
         setattr_fn = w_type.getdictvalue(space, '__setattr__')
         delattr_fn = w_type.getdictvalue(space, '__delattr__')
         if setattr_fn is None:
@@ -401,28 +423,6 @@
             return space.call_function(getattr_fn, w_self, w_name)
         api_func = slot_tp_getattro.api_func
 
-    elif name == 'tp_as_number.c_nb_int':
-        int_fn = w_type.getdictvalue(space, '__int__')
-        if int_fn is None:
-            return
-
-        @cpython_api([PyObject], PyObject, header=header)
-        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
-        def slot_nb_int(space, w_self):
-            return space.call_function(int_fn, w_self)
-        api_func = slot_nb_int.api_func
-
-    elif name == 'tp_as_number.c_nb_float':
-        float_fn = w_type.getdictvalue(space, '__float__')
-        if float_fn is None:
-            return
-
-        @cpython_api([PyObject], PyObject, header=header)
-        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
-        def slot_nb_float(space, w_self):
-            return space.call_function(float_fn, w_self)
-        api_func = slot_nb_float.api_func
-
     elif name == 'tp_call':
         call_fn = w_type.getdictvalue(space, '__call__')
         if call_fn is None:
@@ -436,44 +436,6 @@
             return space.call_args(call_fn, args)
         api_func = slot_tp_call.api_func
 
-    elif name == 'tp_str':
-        str_fn = w_type.getdictvalue(space, '__str__')
-        if str_fn is None:
-            return
-
-        @cpython_api([PyObject], PyObject, header=header)
-        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
-        def slot_tp_str(space, w_self):
-            return space.call_function(str_fn, w_self)
-        api_func = slot_tp_str.api_func
-
-    elif name == 'tp_iter':
-        iter_fn = w_type.getdictvalue(space, '__iter__')
-        if iter_fn is None:
-            return
-
-        @cpython_api([PyObject], PyObject, header=header)
-        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
-        def slot_tp_iter(space, w_self):
-            return space.call_function(iter_fn, w_self)
-        api_func = slot_tp_iter.api_func
-
-    elif name == 'tp_iternext':
-        iternext_fn = w_type.getdictvalue(space, 'next')
-        if iternext_fn is None:
-            return
-
-        @cpython_api([PyObject], PyObject, header=header)
-        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
-        def slot_tp_iternext(space, w_self):
-            try:
-                return space.call_function(iternext_fn, w_self)
-            except OperationError as e:
-                if not e.match(space, space.w_StopIteration):
-                    raise
-                return None
-        api_func = slot_tp_iternext.api_func
-
     elif name == 'tp_init':
         init_fn = w_type.getdictvalue(space, '__init__')
         if init_fn is None:
@@ -501,6 +463,7 @@
             return space.call_args(space.get(new_fn, w_self), args)
         api_func = slot_tp_new.api_func
     else:
+        print 'unhandled slot',name,'for',w_type
         return
 
     return lambda: llhelper(api_func.functype, api_func.get_wrapper(space))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to