Author: Armin Rigo <[email protected]>
Branch: cpyext-ext
Changeset: r82645:11338c928fad
Date: 2016-03-01 21:07 +0100
http://bitbucket.org/pypy/pypy/changeset/11338c928fad/
Log: fix slot_nb_int, and attempt a test, but blocked by issue 2248
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
@@ -337,10 +337,6 @@
return 0
@cpython_api([PyObject], PyObject, header=None)
-def slot_nb_int(space, w_self):
- return space.int(w_self)
-
-@cpython_api([PyObject], PyObject, header=None)
def slot_tp_iter(space, w_self):
return space.iter(w_self)
@@ -395,6 +391,17 @@
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:
diff --git a/pypy/module/cpyext/test/test_typeobject.py
b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -469,21 +469,28 @@
def test_nb_int(self):
module = self.import_extension('foo', [
- ("nb_int", "METH_O",
+ ("nb_int", "METH_VARARGS",
'''
- if (!args->ob_type->tp_as_number ||
- !args->ob_type->tp_as_number->nb_int)
+ PyTypeObject *type = (PyTypeObject *)PyTuple_GET_ITEM(args,
0);
+ PyObject *obj = PyTuple_GET_ITEM(args, 1);
+ if (!type->tp_as_number ||
+ !type->tp_as_number->nb_int)
{
PyErr_SetNone(PyExc_ValueError);
return NULL;
}
- return args->ob_type->tp_as_number->nb_int(args);
+ return type->tp_as_number->nb_int(obj);
'''
)
])
- assert module.nb_int(10) == 10
- assert module.nb_int(-12.3) == -12
- raises(ValueError, module.nb_int, "123")
+ assert module.nb_int(int, 10) == 10
+ assert module.nb_int(float, -12.3) == -12
+ raises(ValueError, module.nb_int, str, "123")
+ class F(float):
+ def __int__(self):
+ return 666
+ skip("XXX fix issue 2248 first")
+ assert module.nb_int(float, F(-12.3)) == -12
def test_nb_float(self):
module = self.import_extension('foo', [
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit