File: cpdef_enums_import.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 (119 lines) | stat: -rw-r--r-- 2,684 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
PYTHON setup.py build_ext --inplace
PYTHON -c "import import_enums_test"

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

from Cython.Build.Dependencies import cythonize

from distutils.core import setup

setup(
  ext_modules = cythonize(["enums.pyx", "enums_same_name.pyx", "no_enums.pyx"]),
)

######## enums.pyx ########

cpdef enum:
    BAR

cpdef foo(): pass

######## enums.pxd ########

cpdef enum:
    FOO

cpdef enum NamedEnumType:
    NamedEnumValue = 389

cpdef foo()

######## enums_same_name.pyx ############

######## enums_same_name.pxd ############

# Note - same name as enums.pxd but shouldn't conflict
cpdef enum NamedEnumType:
    Value = 1

######## enums_without_pyx.pxd #####

cpdef enum EnumTypeNotInPyx:
    AnotherEnumValue = 500

######## c_enum.h #############

enum CEnum {
    CEnumVal1, CEnumVal2
};

######## external_enums1.pxd ######

cdef extern from "c_enum.h":
    cpdef enum CEnum:
        CEnumVal1
        CEnumVal2

######## external_enums1.pyx ######

######## external_enums2.pxd ######

# external_enums1 and external_enums2 expose the same
# enum - this shouldn't lead to duplicate utility code
cdef extern from "c_enum.h":
    cpdef enum CEnum:
        CEnumVal1
        CEnumVal2

######## external_enums2.pyx ######

######## no_enums.pyx ########

from enums cimport *
from enums_without_pyx cimport *
cimport enums_same_name
cimport external_enums1
cimport external_enums2

def get_named_enum_value():
    return NamedEnumType.NamedEnumValue

def get_from_enums_same_name():
    # This should not generate conflicting "to py" functions with the other
    # identically named enum from a different pxd file.
    return enums_same_name.NamedEnumType.Value

def get_named_without_pyx():
    # This'll generate a warning but return a c int
    return EnumTypeNotInPyx.AnotherEnumValue

def get_external1():
    return external_enums1.CEnumVal1

def get_external2():
    return external_enums2.CEnumVal1

######## import_enums_test.py ########

# We can import enums with a star import.
from enums import *
import enums_same_name

print(dir())
assert 'BAR' in dir() and 'FOO' in dir()
assert 'NamedEnumType' in dir()

# enums not generated in the wrong module
import no_enums
print(dir(no_enums))
assert 'FOO' not in dir(no_enums)
assert 'foo' not in dir(no_enums)

assert no_enums.get_named_enum_value() == NamedEnumType.NamedEnumValue
# In this case the enum isn't accessible from Python (by design)
# but the conversion to Python goes through a reasonable fallback
assert no_enums.get_named_without_pyx() == 500

assert no_enums.get_from_enums_same_name() == enums_same_name.NamedEnumType.Value

assert no_enums.get_external1() == no_enums.get_external2()