Commit 558df10d authored by Jeroen Demeyer's avatar Jeroen Demeyer

Disallow old-style classes for multiple inheritance

parent 5a26934a
...@@ -23,7 +23,17 @@ static int __Pyx_PyType_Ready(PyTypeObject *t) { ...@@ -23,7 +23,17 @@ static int __Pyx_PyType_Ready(PyTypeObject *t) {
Py_ssize_t i, n = PyTuple_GET_SIZE(bases); Py_ssize_t i, n = PyTuple_GET_SIZE(bases);
for (i = 1; i < n; i++) /* Skip first base */ 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)) if (!PyType_HasFeature(b, Py_TPFLAGS_HEAPTYPE))
{ {
PyErr_Format(PyExc_TypeError, "base class '%.200s' is not a heap type", PyErr_Format(PyExc_TypeError, "base class '%.200s' is not a heap type",
......
...@@ -47,8 +47,21 @@ class Py(object): ...@@ -47,8 +47,21 @@ class Py(object):
cdef class X(Base, Py): cdef class X(Base, Py):
pass pass
######## oldstyle.pyx ########
cdef class Base:
cdef dict __dict__
class OldStyle:
pass
cdef class Foo(Base, OldStyle):
pass
######## runner.py ######## ######## runner.py ########
import sys
try: try:
import notheaptype import notheaptype
assert False assert False
...@@ -72,3 +85,10 @@ try: ...@@ -72,3 +85,10 @@ try:
assert False assert False
except TypeError as msg: 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" 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