Commit 2c79c3fb authored by Boxiang Sun's avatar Boxiang Sun

Initialize the base class in PyType_Ready if it not get initialzied.

CPython has this check. I think Pyston missed it when switch to the
CPython way of handling some inheritance functionality.

References:
CPython code:
https://github.com/python/cpython/blob/2.7/Objects/typeobject.c#L4074
Pyston commit that switch to the CPython way of handling it:
https://github.com/dropbox/pyston/commit/251f417abd68c4b018217ae3b1901b3f1687384d#diff-a5cc3ff8c761716ba33b328ad1d042dfR1499

And also add an extension for test.
parent dc0c3422
......@@ -3557,6 +3557,12 @@ extern "C" int PyType_Ready(PyTypeObject* cls) noexcept {
if (base == NULL)
base = cls->tp_base = object_cls;
// Initialize the base class
if (base != NULL && unlikely(base->tp_dict == NULL) && !(base->is_pyston_class)) {
int ret = PyType_Ready(base);
assert(ret == 0);
}
// CPython only increfs the base if it picked one, not if it one was passed in.
// Not sure why.
Py_INCREF(base);
......
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so
COMMAND python setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7
DEPENDS basic_test.c descr_test.c slots_test.c
DEPENDS basic_test.c descr_test.c slots_test.c type_test.c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/basic_test.pyston.so
COMMAND ${CMAKE_BINARY_DIR}/pyston setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS pyston copy_stdlib copy_libpyston basic_test.c descr_test.c slots_test.c
DEPENDS pyston copy_stdlib copy_libpyston basic_test.c descr_test.c slots_test.c type_test.c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(ext_cpython DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so)
......
......@@ -6,6 +6,7 @@ extensions = [
Extension("basic_test", sources = ["basic_test.c"]),
Extension("descr_test", sources = ["descr_test.c"]),
Extension("slots_test", sources = ["slots_test.c"]),
Extension("type_test", sources = ["type_test.c"]),
]
def relpath(fn):
......
#include <Python.h>
typedef struct {
PyObject_HEAD
} SimpleObject;
static PyTypeObject BaseObjectType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"type_test.Base", /* tp_name */
sizeof(SimpleObject), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* 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 */
"Act as a ase type.", /* 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 */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
static PyTypeObject SubObjectType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"type_test.Sub", /* tp_name */
sizeof(SimpleObject), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* 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 */
"Act as a subtype", /* 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 */
&BaseObjectType,/* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
PyMODINIT_FUNC
inittype_test(void)
{
PyObject* m;
SubObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&SubObjectType) < 0)
return;
m = Py_InitModule3("type_test", NULL,
"A module that creates two extension type.");
if (m == NULL)
return;
Py_INCREF(&SubObjectType);
PyModule_AddObject(m, "Sub", (PyObject *)&SubObjectType);
}
import type_test
print("Hello")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment