[pypy-commit] pypy issue2968: rework test for python2, python3
mattip
pypy.commits at gmail.com
Thu Mar 14 11:18:55 EDT 2019
Author: Matti Picus <matti.picus at gmail.com>
Branch: issue2968
Changeset: r96310:4285c5076c8e
Date: 2019-03-14 17:03 +0200
http://bitbucket.org/pypy/pypy/changeset/4285c5076c8e/
Log: rework test for python2, python3
diff --git a/pypy/module/cpyext/test/THPSize.c b/pypy/module/cpyext/test/THPSize.c
deleted file mode 100644
--- a/pypy/module/cpyext/test/THPSize.c
+++ /dev/null
@@ -1,85 +0,0 @@
-#include "Python.h"
-
-struct THPSize {
- PyTupleObject tuple;
-} THPSize;
-
-static PyObject * THPSize_pynew(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
- return PyTuple_Type.tp_new(type, args, kwargs);
-}
-
-static PyMappingMethods THPSize_as_mapping = {
- 0, //PyTuple_Type.tp_as_mapping->mp_length,
- 0,
- 0
-};
-
-
-PyTypeObject THPSizeType = {
- PyVarObject_HEAD_INIT(0, 0)
- "torch.Size", /* tp_name */
- sizeof(THPSize), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- &THPSize_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- &PyTuple_Type, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- THPSize_pynew, /* tp_new */
-};
-
-static struct PyModuleDef moduledef = {
- PyModuleDef_HEAD_INIT,
- "THPSize",
- "Module Doc",
- -1,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
-};
-
-PyMODINIT_FUNC
-PyInit_THPSize(void)
-{
- PyObject *module = PyModule_Create(&moduledef);
- THPSize_as_mapping.mp_length = PyTuple_Type.tp_as_mapping->mp_length;
- if (PyType_Ready(&THPSizeType) < 0) {
- return NULL;
- }
- Py_INCREF(&THPSizeType);
- if (PyModule_AddObject(module, "Size", (PyObject*)&THPSizeType) < 0) {
- return NULL;
- }
- return module;
-}
diff --git a/pypy/module/cpyext/test/test_tupleobject.py b/pypy/module/cpyext/test/test_tupleobject.py
--- a/pypy/module/cpyext/test/test_tupleobject.py
+++ b/pypy/module/cpyext/test/test_tupleobject.py
@@ -227,6 +227,43 @@
else:
module.set_after_use(s)
- def test_torch(self):
+ def test_mp_length(self):
+ # issue 2968: creating a subclass of tuple in C led to recursion
+ # since the default tp_new needs to build a w_obj, but that needs
+ # to call space.len_w, which needs to call tp_new.
module = self.import_module('THPSize')
- module.Size()
+ module = self.import_extension('foo', [
+ ("get_size", "METH_NOARGS",
+ """
+ return (PyObject*)&THPSizeType;
+ """),
+ ], prologue='''
+ #include "Python.h"
+
+ struct THPSize {
+ PyTupleObject tuple;
+ } THPSize;
+
+ static PyMappingMethods THPSize_as_mapping = {
+ 0, //PyTuple_Type.tp_as_mapping->mp_length,
+ 0,
+ 0
+ };
+
+ PyTypeObject THPSizeType = {
+ PyVarObject_HEAD_INIT(0, 0)
+ "torch.Size", /* tp_name */
+ sizeof(THPSize), /* tp_basicsize */
+ };
+ ''' , more_init = '''
+ THPSize_as_mapping.mp_length = PyTuple_Type.tp_as_mapping->mp_length;
+ THPSizeType.tp_base = &PyTuple_Type;
+ THPSizeType.tp_flags = Py_TPFLAGS_DEFAULT;
+ THPSizeType.tp_as_mapping = &THPSize_as_mapping;
+ THPSizeType.tp_new = PyTuple_Type.tp_new;
+ if (PyType_Ready(&THPSizeType) < 0) INITERROR;
+ ''')
+ SZ = module.get_size()
+ s = SZ((1, 2, 3))
+ assert len(s) == 3
+
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
@@ -687,6 +687,7 @@
else:
update_all_slots_builtin(space, w_type, pto)
+ # XXX generlize this pattern for various slot functions implemented in C
if space.is_w(w_type, space.w_tuple):
pto.c_tp_new = state.C.tuple_new
More information about the pypy-commit
mailing list