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