Author: Matti Picus <[email protected]>
Branch: cpyext-slotdefs2
Changeset: r85708:58297846176c
Date: 2016-07-13 09:27 -0500
http://bitbucket.org/pypy/pypy/changeset/58297846176c/
Log: test, add tp_as_sequence slots when creating PyType_Object from PyPy
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
@@ -408,6 +408,8 @@
('tp_as_number.c_nb_and', '__and__'),
('tp_as_number.c_nb_xor', '__xor__'),
('tp_as_number.c_nb_or', '__or__'),
+ ('tp_as_sequence.c_sq_concat', '__add__'),
+ ('tp_as_sequence.c_sq_inplace_concat', '__iadd__')
]:
if name == tp_name:
slot_fn = w_type.getdictvalue(space, attr)
@@ -421,6 +423,23 @@
api_func = slot_func.api_func
handled = True
+ # binary-with-Py_ssize_t-type
+ for tp_name, attr in [('tp_as_sequence.c_sq_item', '__getitem'),
+ ('tp_as_sequence.c_sq_repeat', '__mul__'),
+ ('tp_as_sequence.c_sq_inplace_repeat', '__imul__'),
+ ]:
+ if name == tp_name:
+ slot_fn = w_type.getdictvalue(space, attr)
+ if slot_fn is None:
+ return
+
+ @cpython_api([PyObject, Py_ssize_t], PyObject, header=header)
+ @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'),
typedef.name))
+ def slot_func(space, w_self, arg):
+ return space.call_function(slot_fn, w_self, space.wrap(arg))
+ api_func = slot_func.api_func
+ handled = True
+
# ternary functions
for tp_name, attr in [('tp_as_number.c_nb_power', ''),
]:
@@ -522,6 +541,7 @@
api_func = slot_tp_new.api_func
else:
# missing: tp_as_number.nb_nonzero, tp_as_number.nb_coerce
+ # tp_as_sequence.c_sq_contains, tp_as_sequence.c_sq_length
return
return lambda: llhelper(api_func.functype, api_func.get_wrapper(space))
diff --git a/pypy/module/cpyext/test/test_listobject.py
b/pypy/module/cpyext/test/test_listobject.py
--- a/pypy/module/cpyext/test/test_listobject.py
+++ b/pypy/module/cpyext/test/test_listobject.py
@@ -64,7 +64,7 @@
assert space.unwrap(w_s) == [2, 1]
class AppTestListObject(AppTestCpythonExtensionBase):
- def test_listobject(self):
+ def test_basic_listobject(self):
import sys
module = self.import_extension('foo', [
("newlist", "METH_NOARGS",
@@ -104,6 +104,15 @@
Py_RETURN_NONE;
"""
),
+ ('test_tp_as_', "METH_NOARGS",
+ '''
+ PyObject *l = PyList_New(3);
+ int ok = l->ob_type->tp_as_sequence != NULL; /* 1 */
+ ok += 2 * (l->ob_type->tp_as_number == NULL); /* 2 */
+ Py_DECREF(l);
+ return PyLong_FromLong(ok); /* should be 3 */
+ '''
+ ),
])
l = module.newlist()
assert l == [3, -5, 1000]
@@ -137,6 +146,9 @@
module.setlistitem(l,0)
assert l == [None, 2, 3]
+ # tp_as_sequence should be filled, but tp_as_number should be NULL
+ assert module.test_tp_as_() == 3
+
def test_list_macros(self):
"""The PyList_* macros cast, and calls expecting that build."""
module = self.import_extension('foo', [
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit