File: check_size.srctree

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (234 lines) | stat: -rw-r--r-- 5,112 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
PYTHON setup.py build_ext --inplace
PYTHON -c "import runner"

######## setup.py ########

from Cython.Build.Dependencies import cythonize
from Cython.Compiler.Errors import CompileError
from distutils.core import setup

# force the build order
setup(ext_modules= cythonize("check_size.pyx"))

setup(ext_modules = cythonize("_check_size*.pyx"))

try:
    setup(ext_modules= cythonize("check_size_invalid.pyx"))
    assert False
except CompileError as e:
    pass

######## check_size_nominal.h ########

#include <Python.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    PyObject_HEAD
    int f0;
    int f1;
    int f2;
} FooStructNominal;

#ifdef __cplusplus
}
#endif

######## check_size_bigger.h ########

#include <Python.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    PyObject_HEAD
    int f0;
    int f1;
    int f2;
    int f3;
    int f4;
} FooStructBig;

#ifdef __cplusplus
}
#endif

######## check_size_smaller.h ########

#include <Python.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    PyObject_HEAD
    int f9;
} FooStructSmall;

#ifdef __cplusplus
}
#endif


######## check_size.pyx ########

cdef class Foo:
    cdef public int field0, field1, field2;

    def __init__(self, f0, f1, f2):
        self.field0 = f0
        self.field1 = f1
        self.field2 = f2

######## _check_size_exact.pyx ########

cdef extern from "check_size_nominal.h":

    ctypedef class check_size.Foo [object FooStructNominal]:
        cdef:
            int f0
            int f1


cpdef public int testme(Foo f) except -1:
    return f.f0 + f.f1

######## _check_size_too_small.pyx ########

cdef extern from "check_size_bigger.h":

    ctypedef class check_size.Foo [object FooStructBig]:
        cdef:
            int f0
            int f1
            int f2


cpdef public int testme(Foo f, int f2) except -1:
    f.f2 = f2
    return f.f0 + f.f1 + f.f2

######## _check_size_default.pyx ########

cdef extern from "check_size_smaller.h":

    ctypedef class check_size.Foo [object FooStructSmall]:
        cdef:
            int f9


cpdef public int testme(Foo f) except -1:
    return f.f9

######## _check_size_warn.pyx ########

cdef extern from "check_size_smaller.h":

    # make sure missing check_size is equivalent to warn
    ctypedef class check_size.Foo [object FooStructSmall, check_size warn]:
        cdef:
            int f9


cpdef public int testme(Foo f) except -1:
    return f.f9

######## _check_size_ignore.pyx ########

cdef extern from "check_size_smaller.h":

    # Allow size to be larger
    ctypedef class check_size.Foo [object FooStructSmall, check_size ignore]:
        cdef:
            int f9


cpdef public int testme(Foo f) except -1:
    return f.f9

######## _check_size_error.pyx ########

cdef extern from "check_size_smaller.h":

    # Strict checking, will raise an error
    ctypedef class check_size.Foo [object FooStructSmall, check_size error]:
        cdef:
            int f9


cpdef public int testme(Foo f) except -1:
    return f.f9

######## check_size_invalid.pyx ########

cdef extern from "check_size_smaller.h":

    # Raise CompileError when using bad value
    ctypedef class check_size.Foo [object FooStructSmall, check_size hihi]:
        cdef:
            int f9


cpdef public int testme(Foo f) except -1:
    return f.f9

######## runner.py ########

import check_size, _check_size_exact, warnings

foo = check_size.Foo(23, 123, 1023)

assert foo.field0 == 23
assert foo.field1 == 123

ret =  _check_size_exact.testme(foo)
assert ret == 23 + 123

# ValueError since check_size.Foo's tp_basicsize is smaller than what is needed
# for FooStructBig. Messing with f2 will access memory outside the struct!
try:
    import _check_size_too_small
    assert False
except ValueError as e:
    assert str(e).startswith('check_size.Foo size changed')

# Warning since check_size.Foo's tp_basicsize is larger than what is needed
# for FooStructSmall. There is "spare", accessing FooStructSmall's fields will
# never access invalid memory. This can happen, for instance, when using old
# headers with a newer runtime, or when using an old _check_size{2,3} with a newer
# check_size, where the developers of check_size are careful to be backward
# compatible.

with warnings.catch_warnings(record=True) as w:
    warnings.simplefilter("always")
    import _check_size_default
    import _check_size_warn
    assert len(w) == 2, 'expected two warnings, got %d' % len(w)
    assert str(w[0].message).startswith('check_size.Foo size changed')
    assert str(w[1].message).startswith('check_size.Foo size changed')

ret = _check_size_default.testme(foo)
assert ret == 23
ret = _check_size_warn.testme(foo)
assert ret == 23

with warnings.catch_warnings(record=True) as w:
    # No warning, runtime vendor must provide backward compatibility
    import _check_size_ignore
    assert len(w) == 0

ret = _check_size_ignore.testme(foo)
assert ret == 23

try:
    # Enforce strict checking
    import _check_size_error
    assert False
except ValueError as e:
    assert str(e).startswith('check_size.Foo size changed')