6
>>> THOUSAND # doctest: +ELLIPSIS
7
Traceback (most recent call last):
8
NameError: ...name 'THOUSAND' is not defined
12
>>> THREE == 3 or THREE
16
>>> ELEVEN == 11 or ELEVEN
18
>>> SEVEN # doctest: +ELLIPSIS
19
Traceback (most recent call last):
20
NameError: ...name 'SEVEN' is not defined
24
>>> EIGHT == 8 or EIGHT
26
>>> SIXTEEN # doctest: +ELLIPSIS
27
Traceback (most recent call last):
28
NameError: ...name 'SIXTEEN' is not defined
30
>>> RANK_0 == 11 or RANK_0
32
>>> RANK_1 == 37 or RANK_1
34
>>> RANK_2 == 389 or RANK_2
36
>>> RANK_6 == 159 or RANK_6
38
>>> RANK_7 == 889 or RANK_7
40
>>> RANK_3 # doctest: +ELLIPSIS
41
Traceback (most recent call last):
42
NameError: ...name 'RANK_3' is not defined
44
>>> set(PyxEnum) == {TWO, THREE, FIVE}
46
>>> str(PyxEnum.TWO).split(".")[-1] if sys.version_info < (3,11) else "TWO" # Py3.10/11 changed the output here
48
>>> str(PyxEnum.TWO) if sys.version_info >= (3,11) else "2" # Py3.10/11 changed the output here
50
>>> PyxEnum.TWO + PyxEnum.THREE == PyxEnum.FIVE
52
>>> PyxEnum(2) is PyxEnum["TWO"] is PyxEnum.TWO
55
# not leaking into module namespace
56
>>> IntEnum # doctest: +ELLIPSIS
57
Traceback (most recent call last):
58
NameError: ...name 'IntEnum' is not defined
62
cpdef enum: # ExternPyx
67
cdef enum: # ExternSecretPyx
75
cpdef enum cpdefPyxDocEnum:
80
cpdef enum cpdefPyxDocLineEnum:
81
"""Home is where..."""
84
cdef enum SecretPyxEnum:
87
cdef enum cdefPyxDocEnum:
94
enum ExternHasDuplicates {
100
# Cython doesn't know about the duplicates though
101
cpdef enum ExternHasDuplicates:
107
cpdef enum CyDefinedHasDuplicates1:
109
CY_DUP1_B = 0x00000000
112
cpdef enum CyDefinedHasDuplicates2:
114
CY_DUP2_B = CY_DUP2_A
116
cpdef enum CyDefinedHasDuplicates3:
122
def test_as_variable_from_cython():
124
>>> test_as_variable_from_cython()
126
assert list(PyxEnum) == [TWO, THREE, FIVE], list(PyxEnum)
127
assert list(PxdEnum) == [RANK_0, RANK_1, RANK_2], list(PxdEnum)
129
cdef int verify_pure_c() nogil:
131
cdef int y = PyxEnum.THREE
132
cdef int z = SecretPyxEnum.SEVEN
135
# Use it to suppress warning.
138
def verify_resolution_GH1533():
140
>>> verify_resolution_GH1533()
144
return int(PyxEnum.THREE)
149
>>> PxdEnum.__doc__ not in ("Home is where...\\n ", "Home is where...")
151
>>> PyxEnum.__doc__ not in ("Home is where...\\n ", "Home is where...")
153
>>> cpdefPyxDocEnum.__doc__ == "Home is where...\\n "
155
>>> cpdefPxdDocEnum.__doc__ == "Home is where...\\n "
157
>>> cpdefPyxDocLineEnum.__doc__
159
>>> cpdefPxdDocLineEnum.__doc__
165
def to_from_py_conversion(PxdEnum val):
167
>>> to_from_py_conversion(RANK_1) is PxdEnum.RANK_1
170
C enums are commonly enough used as flags that it seems reasonable
171
to allow it in Cython
172
>>> to_from_py_conversion(RANK_1 | RANK_2) == (RANK_1 | RANK_2)
178
def to_from_py_conversion_with_duplicates1(ExternHasDuplicates val):
180
Mainly a compile-time test - we can't optimize to a switch here
181
>>> to_from_py_conversion_with_duplicates1(EX_DUP_A) == ExternHasDuplicates.EX_DUP_A
187
def to_from_py_conversion_with_duplicates2(CyDefinedHasDuplicates1 val):
189
Mainly a compile-time test - we can't optimize to a switch here
190
>>> to_from_py_conversion_with_duplicates2(CY_DUP1_A) == CyDefinedHasDuplicates1.CY_DUP1_A
196
def to_from_py_conversion_with_duplicates3(CyDefinedHasDuplicates2 val):
198
Mainly a compile-time test - we can't optimize to a switch here
199
>>> to_from_py_conversion_with_duplicates3(CY_DUP2_A) == CyDefinedHasDuplicates2.CY_DUP2_A
205
def to_from_py_conversion_with_duplicates4(CyDefinedHasDuplicates3 val):
207
Mainly a compile-time test - we can't optimize to a switch here
208
>>> to_from_py_conversion_with_duplicates4(CY_DUP3_C) == CyDefinedHasDuplicates3.CY_DUP3_C
216
>>> from pickle import loads, dumps
219
Python 3.11.4 has a bug that breaks pickling: https://github.com/python/cpython/issues/105332
221
>>> if sys.version_info[:3] == (3,11,4):
222
... loads = dumps = lambda x: x
224
>>> loads(dumps(PyxEnum.TWO)) == PyxEnum.TWO
226
>>> loads(dumps(PxdEnum.RANK_2)) == PxdEnum.RANK_2
231
def test_as_default_value(PxdEnum val=PxdEnum.RANK_1):
233
In order to work, this requires the utility code to be evaluated
234
before the function definition
235
>>> test_as_default_value()
237
>>> test_as_default_value(PxdEnum.RANK_2)
239
>>> test_as_default_value.__defaults__[0] == PxdEnum.RANK_1
242
return val == PxdEnum.RANK_1