1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
# mode: run
PYTHON setup.py build_ext --inplace
PYTHON -c "import classes"
PYTHON -c "import test_inherit"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules=cythonize("*.pyx"),
)
###### dummy_module.py ###########
tpl = tuple
lst = list
###### classes.pxd ################
cdef extern from *:
# apart from list, these are all variable sized types
# and Cython shouldn't trip up about the struct size
ctypedef class dummy_module.tpl [object PyTupleObject]:
pass
ctypedef class dummy_module.lst [object PyListObject]:
pass
ctypedef class types.CodeType [object PyCodeObject]:
pass
# Note that bytes doesn't work here because it further
# the tp_basicsize to save space
##### classes.pyx #################
def check_tuple(tpl x):
assert isinstance(x, tuple)
def check_list(lst x):
assert isinstance(x, list)
def check_code(CodeType x):
import types
assert isinstance(x, types.CodeType)
check_tuple((1, 2))
check_list([1, 2])
check_code(eval("lambda: None").__code__)
##### failed_inherit1.pyx #############
from classes cimport tpl
cdef class SuperTuple(tpl):
cdef int a # importing this gives an error message
##### failed_inherit2.pyx #############
from classes cimport tpl
cdef class SuperTuple(tpl):
# adding a method creates a vtab so should also fail
cdef int func(self):
return 1
##### successful_inherit.pyx #########
from classes cimport lst, tpl
cdef class SuperList(lst):
cdef int a # This works OK
cdef class SuperTuple(tpl):
# This is actually OK because it doesn't add anything
pass
##### test_inherit.py ################
try:
import failed_inherit1
except TypeError as e:
assert e.args[0] == "inheritance from PyVarObject types like 'tuple' not currently supported", e.args[0]
else:
assert False
try:
import failed_inherit2
except TypeError as e:
assert e.args[0] == "inheritance from PyVarObject types like 'tuple' not currently supported", e.args[0]
else:
assert False
import successful_inherit
|