Commit 5efab9f6 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2083 from jdemeyer/inherit_check_type

Disallow old-style classes for multiple inheritance
parents 5a26934a 558df10d
......@@ -23,7 +23,17 @@ static int __Pyx_PyType_Ready(PyTypeObject *t) {
Py_ssize_t i, n = PyTuple_GET_SIZE(bases);
for (i = 1; i < n; i++) /* Skip first base */
{
PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i);
PyObject *b0 = PyTuple_GET_ITEM(bases, i);
#if PY_MAJOR_VERSION < 3
/* Disallow old-style classes */
if (PyClass_Check(b0))
{
PyErr_Format(PyExc_TypeError, "base class '%.200s' is an old-style class",
PyString_AS_STRING(((PyClassObject*)b0)->cl_name));
return -1;
}
#endif
PyTypeObject *b = (PyTypeObject*)b0;
if (!PyType_HasFeature(b, Py_TPFLAGS_HEAPTYPE))
{
PyErr_Format(PyExc_TypeError, "base class '%.200s' is not a heap type",
......
......@@ -47,8 +47,21 @@ class Py(object):
cdef class X(Base, Py):
pass
######## oldstyle.pyx ########
cdef class Base:
cdef dict __dict__
class OldStyle:
pass
cdef class Foo(Base, OldStyle):
pass
######## runner.py ########
import sys
try:
import notheaptype
assert False
......@@ -72,3 +85,10 @@ try:
assert False
except TypeError as msg:
assert str(msg) == "extension type 'nodict.X' has no __dict__ slot, but base type 'Py' has: either add 'cdef dict __dict__' to the extension type or add '__slots__ = [...]' to the base type"
try:
# This should work on Python 3 but fail on Python 2
import oldstyle
assert sys.version_info[0] >= 3
except TypeError as msg:
assert str(msg) == "base class 'OldStyle' is an old-style class"
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