cython

Форк
0
/
cpdef_enums.pyx 
242 строки · 5.5 Кб
1
"""
2
>>> import sys
3

4
>>> ONE, TEN, HUNDRED
5
(1, 10, 100)
6
>>> THOUSAND        # doctest: +ELLIPSIS
7
Traceback (most recent call last):
8
NameError: ...name 'THOUSAND' is not defined
9

10
>>> TWO == 2 or TWO
11
True
12
>>> THREE == 3 or THREE
13
True
14
>>> FIVE == 5 or FIVE
15
True
16
>>> ELEVEN == 11 or ELEVEN
17
True
18
>>> SEVEN           # doctest: +ELLIPSIS
19
Traceback (most recent call last):
20
NameError: ...name 'SEVEN' is not defined
21

22
>>> FOUR == 4 or FOUR
23
True
24
>>> EIGHT == 8 or EIGHT
25
True
26
>>> SIXTEEN        # doctest: +ELLIPSIS
27
Traceback (most recent call last):
28
NameError: ...name 'SIXTEEN' is not defined
29

30
>>> RANK_0 == 11 or RANK_0
31
True
32
>>> RANK_1 == 37 or RANK_1
33
True
34
>>> RANK_2 == 389 or RANK_2
35
True
36
>>> RANK_6 == 159 or RANK_6
37
True
38
>>> RANK_7 == 889 or RANK_7
39
True
40
>>> RANK_3         # doctest: +ELLIPSIS
41
Traceback (most recent call last):
42
NameError: ...name 'RANK_3' is not defined
43

44
>>> set(PyxEnum) == {TWO, THREE, FIVE}
45
True
46
>>> str(PyxEnum.TWO).split(".")[-1]  if sys.version_info < (3,11) else  "TWO" # Py3.10/11 changed the output here
47
'TWO'
48
>>> str(PyxEnum.TWO)  if sys.version_info >= (3,11) else  "2" # Py3.10/11 changed the output here
49
'2'
50
>>> PyxEnum.TWO + PyxEnum.THREE == PyxEnum.FIVE
51
True
52
>>> PyxEnum(2) is PyxEnum["TWO"] is PyxEnum.TWO
53
True
54

55
# not leaking into module namespace
56
>>> IntEnum        # doctest: +ELLIPSIS
57
Traceback (most recent call last):
58
NameError: ...name 'IntEnum' is not defined
59
"""
60

61
cdef extern from *:
62
    cpdef enum: # ExternPyx
63
        ONE "1"
64
        TEN "10"
65
        HUNDRED "100"
66

67
    cdef enum: # ExternSecretPyx
68
        THOUSAND "1000"
69

70
cpdef enum PyxEnum:
71
    TWO = 2
72
    THREE = 3
73
    FIVE = 5
74

75
cpdef enum cpdefPyxDocEnum:
76
    """Home is where...
77
    """
78
    ELEVEN = 11
79

80
cpdef enum cpdefPyxDocLineEnum:
81
    """Home is where..."""
82
    FOURTEEN = 14
83

84
cdef enum SecretPyxEnum:
85
    SEVEN = 7
86

87
cdef enum cdefPyxDocEnum:
88
    """the heart is.
89
    """
90
    FIVE_AND_SEVEN = 5077
91

92
cdef extern from *:
93
    """
94
    enum ExternHasDuplicates {
95
        EX_DUP_A,
96
        EX_DUP_B=EX_DUP_A,
97
        EX_DUP_C=EX_DUP_A
98
    };
99
    """
100
    # Cython doesn't know about the duplicates though
101
    cpdef enum ExternHasDuplicates:
102
        EX_DUP_A
103
        EX_DUP_B
104
        EX_DUP_C
105

106

107
cpdef enum CyDefinedHasDuplicates1:
108
    CY_DUP1_A
109
    CY_DUP1_B = 0x00000000
110

111

112
cpdef enum CyDefinedHasDuplicates2:
113
    CY_DUP2_A
114
    CY_DUP2_B = CY_DUP2_A
115

116
cpdef enum CyDefinedHasDuplicates3:
117
    CY_DUP3_A = 1
118
    CY_DUP3_B = 0
119
    CY_DUP3_C  # = 1
120

121

122
def test_as_variable_from_cython():
123
    """
124
    >>> test_as_variable_from_cython()
125
    """
126
    assert list(PyxEnum) == [TWO, THREE, FIVE], list(PyxEnum)
127
    assert list(PxdEnum) == [RANK_0, RANK_1, RANK_2], list(PxdEnum)
128

129
cdef int verify_pure_c() nogil:
130
    cdef int x = TWO
131
    cdef int y = PyxEnum.THREE
132
    cdef int z = SecretPyxEnum.SEVEN
133
    return x + y + z
134

135
# Use it to suppress warning.
136
verify_pure_c()
137

138
def verify_resolution_GH1533():
139
    """
140
    >>> verify_resolution_GH1533()
141
    3
142
    """
143
    THREE = 100
144
    return int(PyxEnum.THREE)
145

146

147
def check_docs():
148
    """
149
    >>> PxdEnum.__doc__ not in ("Home is where...\\n    ", "Home is where...")
150
    True
151
    >>> PyxEnum.__doc__ not in ("Home is where...\\n    ", "Home is where...")
152
    True
153
    >>> cpdefPyxDocEnum.__doc__ == "Home is where...\\n    "
154
    True
155
    >>> cpdefPxdDocEnum.__doc__ == "Home is where...\\n    "
156
    True
157
    >>> cpdefPyxDocLineEnum.__doc__
158
    'Home is where...'
159
    >>> cpdefPxdDocLineEnum.__doc__
160
    'Home is where...'
161
    """
162
    pass
163

164

165
def to_from_py_conversion(PxdEnum val):
166
    """
167
    >>> to_from_py_conversion(RANK_1) is PxdEnum.RANK_1
168
    True
169

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)
173
    True
174
    """
175
    return val
176

177

178
def to_from_py_conversion_with_duplicates1(ExternHasDuplicates val):
179
    """
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
182
    True
183
    """
184
    return val
185

186

187
def to_from_py_conversion_with_duplicates2(CyDefinedHasDuplicates1 val):
188
    """
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
191
    True
192
    """
193
    return val
194

195

196
def to_from_py_conversion_with_duplicates3(CyDefinedHasDuplicates2 val):
197
    """
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
200
    True
201
    """
202
    return val
203

204

205
def to_from_py_conversion_with_duplicates4(CyDefinedHasDuplicates3 val):
206
    """
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
209
    True
210
    """
211
    return val
212

213

214
def test_pickle():
215
    """
216
    >>> from pickle import loads, dumps
217
    >>> import sys
218

219
    Python 3.11.4 has a bug that breaks pickling: https://github.com/python/cpython/issues/105332
220

221
    >>> if sys.version_info[:3] == (3,11,4):
222
    ...     loads = dumps = lambda x: x
223

224
    >>> loads(dumps(PyxEnum.TWO)) == PyxEnum.TWO
225
    True
226
    >>> loads(dumps(PxdEnum.RANK_2)) == PxdEnum.RANK_2
227
    True
228
    """
229
    pass
230

231
def test_as_default_value(PxdEnum val=PxdEnum.RANK_1):
232
    """
233
    In order to work, this requires the utility code to be evaluated
234
    before the function definition
235
    >>> test_as_default_value()
236
    True
237
    >>> test_as_default_value(PxdEnum.RANK_2)
238
    False
239
    >>> test_as_default_value.__defaults__[0] == PxdEnum.RANK_1
240
    True
241
    """
242
    return val == PxdEnum.RANK_1
243

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.