Commit edbd9db3 authored by Robert Bradshaw's avatar Robert Bradshaw

Add a check for vtable compatibility.

We should be doing this for pure Python defined classe as well,
but don't have a way to inject it.
parent e1756ee6
......@@ -4809,6 +4809,7 @@ class CClassDefNode(ClassDefNode):
for slot in TypeSlots.slot_table:
slot.generate_dynamic_init_code(scope, code)
if heap_type_bases:
# As of https://bugs.python.org/issue22079
# PyType_Ready enforces that all bases of a non-heap type
# are non-heap. We know this is the case for the solid base,
......@@ -4863,13 +4864,19 @@ class CClassDefNode(ClassDefNode):
if preprocessor_guard:
code.putln('#endif')
if type.vtable_cname:
code.globalstate.use_utility_code(
UtilityCode.load_cached('SetVTable', 'ImportExport.c'))
code.putln(
"if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s" % (
typeobj_cname,
type.vtabptr_cname,
code.error_goto(entry.pos)))
code.globalstate.use_utility_code(
UtilityCode.load_cached('SetVTable', 'ImportExport.c'))
if heap_type_bases:
code.globalstate.use_utility_code(
UtilityCode.load_cached('MergeVTables', 'ImportExport.c'))
code.putln("if (__Pyx_MergeVtables(&%s) < 0) %s" % (
typeobj_cname,
code.error_goto(entry.pos)))
if not type.scope.is_internal and not type.scope.directives['internal']:
# scope.is_internal is set for types defined by
# Cython (such as closures), the 'internal'
......
......@@ -656,6 +656,67 @@ bad:
}
/////////////// MergeVTables.proto ///////////////
//@requires: GetVTable
static int __Pyx_MergeVtables(PyTypeObject *type); /*proto*/
/////////////// MergeVTables ///////////////
static int __Pyx_MergeVtables(PyTypeObject *type) {
int i;
void** base_vtables;
void* unknown = (void*)-1;
PyObject* bases = type->tp_bases;
int base_depth = 0;
{
PyTypeObject* base = type->tp_base;
while (base) {
base_depth += 1;
base = base->tp_base;
}
}
base_vtables = (void**) malloc(sizeof(void*) * (base_depth + 1));
base_vtables[0] = unknown;
// Could do MRO resolution of individual methods in the future, assuming
// compatible vtables, but for now simply require a common vtable base.
// Note that if the vtables of various bases are extended separately,
// resolution isn't possible and we must reject it just as when the
// instance struct is so extended. (It would be good to also do this
// check when a multiple-base class is created in pure Python as well.)
for (i = 1; i < PyTuple_GET_SIZE(bases); i++) {
void* base_vtable = __Pyx_GetVtable(((PyTypeObject*)PyTuple_GET_ITEM(bases, i))->tp_dict);
if (base_vtable != NULL) {
int j;
PyTypeObject* base = type->tp_base;
for (j = 0; j < base_depth; j++) {
if (base_vtables[j] == unknown) {
base_vtables[j] = __Pyx_GetVtable(base->tp_dict);
base_vtables[j + 1] = unknown;
}
if (base_vtables[j] == base_vtable) {
break;
} else if (base_vtables[j] == NULL) {
// No more potential matching bases (with vtables).
goto bad;
}
base = base->tp_base;
}
}
}
PyErr_Clear();
free(base_vtables);
return 0;
bad:
PyErr_Format(
PyExc_TypeError,
"multiple bases have vtable conflict: '%s' and '%s'",
type->tp_base->tp_name, ((PyTypeObject*)PyTuple_GET_ITEM(bases, i))->tp_name);
free(base_vtables);
return -1;
}
/////////////// ImportNumPyArray.proto ///////////////
static PyObject *__pyx_numpy_ndarray = NULL;
......
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