Author: Matti Picus <[email protected]>
Branch: 
Changeset: r91846:26b88e87fcc8
Date: 2017-07-09 17:18 +0300
http://bitbucket.org/pypy/pypy/changeset/26b88e87fcc8/

Log:    replace getdictvalue() with lookup() to properly traverse the mro as
        CPython does

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
@@ -466,7 +466,7 @@
                           ('tp_iter', '__iter__'),
                           ]:
         if name == tp_name:
-            slot_fn = w_type.getdictvalue(space, attr)
+            slot_fn = w_type.lookup(attr)
             if slot_fn is None:
                 return
 
@@ -481,7 +481,7 @@
                           ('tp_as_mapping.c_mp_length', '__len__'),
                          ]:
         if name == tp_name:
-            slot_fn = w_type.getdictvalue(space, attr)
+            slot_fn = w_type.lookup(attr)
             if slot_fn is None:
                 return
             @slot_function([PyObject], lltype.Signed, error=-1)
@@ -508,7 +508,7 @@
                           ('tp_as_mapping.c_mp_subscript', '__getitem__'),
                           ]:
         if name == tp_name:
-            slot_fn = w_type.getdictvalue(space, attr)
+            slot_fn = w_type.lookup(attr)
             if slot_fn is None:
                 return
 
@@ -525,7 +525,7 @@
                           ('tp_as_sequence.c_sq_inplace_repeat', '__imul__'),
                           ]:
         if name == tp_name:
-            slot_fn = w_type.getdictvalue(space, attr)
+            slot_fn = w_type.lookup(attr)
             if slot_fn is None:
                 return
 
@@ -539,7 +539,7 @@
     for tp_name, attr in [('tp_as_number.c_nb_power', '__pow__'),
                           ]:
         if name == tp_name:
-            slot_fn = w_type.getdictvalue(space, attr)
+            slot_fn = w_type.lookup(attr)
             if slot_fn is None:
                 return
 
@@ -552,10 +552,10 @@
     for tp_name, attr in [('tp_as_mapping.c_mp_ass_subscript', '__setitem__'),
                          ]:
         if name == tp_name:
-            slot_ass = w_type.getdictvalue(space, attr)
+            slot_ass = w_type.lookup(attr)
             if slot_ass is None:
                 return
-            slot_del = w_type.getdictvalue(space, '__delitem__')
+            slot_del = w_type.lookup('__delitem__')
             if slot_del is None:
                 return
 
@@ -573,10 +573,10 @@
     for tp_name, attr in [('tp_as_sequence.c_sq_ass_item', '__setitem__'),
                          ]:
         if name == tp_name:
-            slot_ass = w_type.getdictvalue(space, attr)
+            slot_ass = w_type.lookup(attr)
             if slot_ass is None:
                 return
-            slot_del = w_type.getdictvalue(space, '__delitem__')
+            slot_del = w_type.lookup('__delitem__')
             if slot_del is None:
                 return
 
@@ -593,8 +593,8 @@
     if handled:
         pass
     elif name == 'tp_setattro':
-        setattr_fn = w_type.getdictvalue(space, '__setattr__')
-        delattr_fn = w_type.getdictvalue(space, '__delattr__')
+        setattr_fn = w_type.lookup('__setattr__')
+        delattr_fn = w_type.lookup('__delattr__')
         if setattr_fn is None:
             return
 
@@ -609,7 +609,7 @@
             return 0
         slot_func = slot_tp_setattro
     elif name == 'tp_getattro':
-        getattr_fn = w_type.getdictvalue(space, '__getattribute__')
+        getattr_fn = w_type.lookup('__getattribute__')
         if getattr_fn is None:
             return
 
@@ -620,7 +620,7 @@
         slot_func = slot_tp_getattro
 
     elif name == 'tp_call':
-        call_fn = w_type.getdictvalue(space, '__call__')
+        call_fn = w_type.lookup('__call__')
         if call_fn is None:
             return
 
@@ -633,7 +633,7 @@
         slot_func = slot_tp_call
 
     elif name == 'tp_iternext':
-        iternext_fn = w_type.getdictvalue(space, 'next')
+        iternext_fn = w_type.lookup('next')
         if iternext_fn is None:
             return
 
@@ -649,7 +649,7 @@
         slot_func = slot_tp_iternext
 
     elif name == 'tp_init':
-        init_fn = w_type.getdictvalue(space, '__init__')
+        init_fn = w_type.lookup('__init__')
         if init_fn is None:
             return
 
@@ -662,7 +662,7 @@
             return 0
         slot_func = slot_tp_init
     elif name == 'tp_new':
-        new_fn = w_type.getdictvalue(space, '__new__')
+        new_fn = w_type.lookup('__new__')
         if new_fn is None:
             return
 
@@ -674,7 +674,7 @@
             return space.call_args(space.get(new_fn, w_self), args)
         slot_func = slot_tp_new
     elif name == 'tp_as_buffer.c_bf_getbuffer':
-        buff_fn = w_type.getdictvalue(space, '__buffer__')
+        buff_fn = w_type.lookup('__buffer__')
         if buff_fn is not None:
             buff_w = slot_from___buffer__(space, typedef, buff_fn)
         elif typedef.buffer:
@@ -683,7 +683,7 @@
             return
         slot_func = buff_w
     elif name == 'tp_descr_get':
-        get_fn = w_type.getdictvalue(space, '__get__')
+        get_fn = w_type.lookup('__get__')
         if get_fn is None:
             return
 
@@ -695,8 +695,8 @@
             return space.call_function(get_fn, w_self, w_obj, w_value)
         slot_func = slot_tp_descr_get
     elif name == 'tp_descr_set':
-        set_fn = w_type.getdictvalue(space, '__set__')
-        delete_fn = w_type.getdictvalue(space, '__delete__')
+        set_fn = w_type.lookup('__set__')
+        delete_fn = w_type.lookup('__delete__')
         if set_fn is None and delete_fn is None:
             return
 
diff --git a/pypy/module/cpyext/test/test_boolobject.py 
b/pypy/module/cpyext/test/test_boolobject.py
--- a/pypy/module/cpyext/test/test_boolobject.py
+++ b/pypy/module/cpyext/test/test_boolobject.py
@@ -26,3 +26,20 @@
             ])
         assert module.get_true() == True
         assert module.get_false() == False
+
+    def test_toint(self):
+        module = self.import_extension('foo', [
+            ("to_int", "METH_O",
+            '''
+                if (args->ob_type->tp_as_number && 
args->ob_type->tp_as_number->nb_int) {
+                    return args->ob_type->tp_as_number->nb_int(args);
+                }
+                else {
+                    PyErr_SetString(PyExc_TypeError,"cannot convert bool to 
int");
+                    return NULL;
+                }
+            '''), ])
+        assert module.to_int(False) == 0
+        assert module.to_int(True) == 1
+
+            
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to