6
def test_constructor(x, y, int color):
8
>>> sorted(test_constructor(1,2,255).items())
9
[('color', 255), ('x', 1.0), ('y', 2.0)]
10
>>> try: test_constructor(1,None,255)
11
... except TypeError: pass
13
cdef Point p = Point(x, y, color)
17
def return_constructor(x, y, int color):
19
>>> sorted(return_constructor(1,2,255).items())
20
[('color', 255), ('x', 1.0), ('y', 2.0)]
21
>>> try: return_constructor(1, None, 255)
22
... except TypeError: pass
24
return Point(x, y, color)
27
def test_constructor_kwds(x, y, color):
29
>>> sorted(test_constructor_kwds(1.25, 2.5, 128).items())
30
[('color', 128), ('x', 1.25), ('y', 2.5)]
31
>>> test_constructor_kwds(1.25, 2.5, None) # doctest: +ELLIPSIS
32
Traceback (most recent call last):
35
cdef Point p = Point(x=x, y=y, color=color)
39
def return_constructor_kwds(double x, y, color):
41
>>> sorted(return_constructor_kwds(1.25, 2.5, 128).items())
42
[('color', 128), ('x', 1.25), ('y', 2.5)]
43
>>> return_constructor_kwds(1.25, 2.5, None) # doctest: +ELLIPSIS
44
Traceback (most recent call last):
47
return Point(x=x, y=y, color=color)
50
def test_dict_construction(x, y, color):
52
>>> sorted(test_dict_construction(4, 5, 64).items())
53
[('color', 64), ('x', 4.0), ('y', 5.0)]
54
>>> try: test_dict_construction("foo", 5, 64)
55
... except TypeError: pass
57
cdef Point p = {'color': color, 'x': x, 'y': y}
60
def test_list_construction(x, y, color):
62
>>> sorted(test_list_construction(4, 5, 64).items())
63
[('color', 64), ('x', 4.0), ('y', 5.0)]
64
>>> try: test_list_construction("foo", 5, 64)
65
... except TypeError: pass
67
cdef Point p = [x, y, color]
71
# FIXME: make this work
72
def test_tuple_construction(x, y, color):
74
>>> sorted(test_tuple_construction(4, 5, 64).items())
75
[('color', 64), ('x', 4.0), ('y', 5.0)]
76
>>> try: test_tuple_construction("foo", 5, 64)
77
... except TypeError: pass
79
cdef Point p = (x, y, color)
83
cdef union int_or_float:
87
def test_union_constructor(n,x):
89
>>> test_union_constructor(1, None)
91
>>> test_union_constructor(None, 2.0)
102
cdef struct with_pointers:
107
def test_pointers(int n, double x):
109
>>> test_pointers(100, 2.71828)
114
cdef with_pointers a = [True, {'n': n}, NULL]
115
cdef with_pointers b = with_pointers(False, {'x': x}, NULL)
118
print a.ptr == b.ptr == NULL
126
bhello = b"hello" # must hold a C reference in PyPy
128
def test_obj_to_struct(MyStruct mystruct):
130
>>> test_obj_to_struct(dict(c=10, i=20, f=6.7, s=bhello))
131
c=10 i=20 f=6.70 s=hello
132
>>> test_obj_to_struct(None)
133
Traceback (most recent call last):
135
TypeError: Expected a mapping, got NoneType
136
>>> test_obj_to_struct(dict(s=b"world"))
137
Traceback (most recent call last):
139
ValueError: No value specified for struct attribute 'c'
140
>>> test_obj_to_struct(dict(c=b"world"))
141
Traceback (most recent call last):
143
TypeError: an integer is required
145
print 'c=%d i=%d f=%.2f s=%s' % (mystruct.c, mystruct.i, mystruct.f, mystruct.s.decode('ascii'))
147
cdef struct NestedStruct:
151
def test_nested_obj_to_struct(NestedStruct nested):
153
>>> test_nested_obj_to_struct(dict(mystruct=dict(c=10, i=20, f=6.7, s=bhello), d=4.5))
154
c=10 i=20 f=6.70 s=hello d=4.50
155
>>> test_nested_obj_to_struct(dict(d=7.6))
156
Traceback (most recent call last):
158
ValueError: No value specified for struct attribute 'mystruct'
159
>>> test_nested_obj_to_struct(dict(mystruct={}, d=7.6))
160
Traceback (most recent call last):
162
ValueError: No value specified for struct attribute 'c'
164
print 'c=%d i=%d f=%.2f s=%s d=%.2f' % (nested.mystruct.c,
167
nested.mystruct.s.decode('UTF-8'),
170
cdef struct OverriddenCname:
173
def test_obj_to_struct_cnames(OverriddenCname s):
175
>>> test_obj_to_struct_cnames({ 'x': 1 })
180
def test_struct_to_obj_cnames():
182
>>> test_struct_to_obj_cnames()
185
return OverriddenCname(2)
187
cdef struct ArrayFieldStruct:
190
def test_array_field_init():
192
>>> test_array_field_init()
195
cdef ArrayFieldStruct s = ArrayFieldStruct([1, 2, 3, 4])