File: cpdef_enums.pyx

package info (click to toggle)
cython 0.25.2-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 12,764 kB
  • sloc: python: 61,303; ansic: 11,484; cpp: 1,105; xml: 1,031; makefile: 397; lisp: 206; sed: 11; sh: 7
file content (96 lines) | stat: -rw-r--r-- 2,114 bytes parent folder | download | duplicates (2)
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
"""
>>> ONE, TEN, HUNDRED
(1, 10, 100)
>>> THOUSAND        # doctest: +ELLIPSIS
Traceback (most recent call last):
NameError: ...name 'THOUSAND' is not defined

>>> TWO == 2 or TWO
True
>>> THREE == 3 or THREE
True
>>> FIVE == 5 or FIVE
True
>>> SEVEN           # doctest: +ELLIPSIS
Traceback (most recent call last):
NameError: ...name 'SEVEN' is not defined

>>> FOUR == 4 or FOUR
True
>>> EIGHT == 8 or EIGHT
True
>>> SIXTEEN        # doctest: +ELLIPSIS
Traceback (most recent call last):
NameError: ...name 'SIXTEEN' is not defined

>>> RANK_0 == 11 or RANK_0
True
>>> RANK_1 == 37 or RANK_1
True
>>> RANK_2 == 389 or RANK_2
True
>>> RANK_3         # doctest: +ELLIPSIS
Traceback (most recent call last):
NameError: ...name 'RANK_3' is not defined

>>> set(PyxEnum) == set([TWO, THREE, FIVE])
True
>>> str(PyxEnum.TWO)
'PyxEnum.TWO'
>>> PyxEnum.TWO + PyxEnum.THREE == PyxEnum.FIVE
True
>>> PyxEnum(2) is PyxEnum["TWO"] is PyxEnum.TWO
True

>>> IntEnum  # not leaking into module namespace
Traceback (most recent call last):
NameError: name 'IntEnum' is not defined
"""


cdef extern from *:
    cpdef enum: # ExternPyx
        ONE "1"
        TEN "10"
        HUNDRED "100"

    cdef enum: # ExternSecretPyx
        THOUSAND "1000"

cpdef enum PyxEnum:
    TWO = 2
    THREE = 3
    FIVE = 5

cdef enum SecretPyxEnum:
    SEVEN = 7

def test_as_variable_from_cython():
    """
    >>> test_as_variable_from_cython()
    """
    import sys
    if sys.version_info >= (2, 7):
        assert list(PyxEnum) == [TWO, THREE, FIVE], list(PyxEnum)
        assert list(PxdEnum) == [RANK_0, RANK_1, RANK_2], list(PxdEnum)
    else:
        # No OrderedDict.
        assert set(PyxEnum) == {TWO, THREE, FIVE}, list(PyxEnum)
        assert set(PxdEnum) == {RANK_0, RANK_1, RANK_2}, list(PxdEnum)

cdef int verify_pure_c() nogil:
    cdef int x = TWO
    cdef int y = PyxEnum.THREE
    cdef int z = SecretPyxEnum.SEVEN
    return x + y + z

# Use it to suppress warning.
verify_pure_c()

def verify_resolution_GH1533():
    """
    >>> int(verify_resolution_GH1533())
    3
    """
    THREE = 100
    return PyxEnum.THREE