Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
dd8ec79c
Commit
dd8ec79c
authored
Sep 08, 2015
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Align cpdef enums with PEP 435.
parent
abd86f36
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
17 deletions
+47
-17
Cython/Utility/CpdefEnums.pyx
Cython/Utility/CpdefEnums.pyx
+34
-12
tests/run/cpdef_enums.pyx
tests/run/cpdef_enums.pyx
+13
-5
No files found.
Cython/Utility/CpdefEnums.pyx
View file @
dd8ec79c
...
...
@@ -2,40 +2,62 @@
cimport
cython
cdef
extern
from
*
:
int
PY_VERSION_HEX
cdef
object
__Pyx_OrderedDict
if
PY_VERSION_HEX
>=
0x02070000
:
from
collections
import
OrderedDict
as
__Pyx_OrderedDict
else
:
__Pyx_OrderedDict
=
dict
@
cython
.
internal
cdef
class
__Pyx_EnumMeta
(
type
):
def
__init__
(
cls
,
name
,
parents
,
dct
):
type
.
__init__
(
cls
,
name
,
parents
,
dct
)
cls
.
__
values__
=
[]
cls
.
__
members__
=
__Pyx_OrderedDict
()
def
__iter__
(
cls
):
return
iter
(
getattr
(
cls
,
'__values__'
,
()))
return
iter
(
cls
.
__members__
.
values
())
def
__getitem__
(
cls
,
name
):
return
cls
.
__members__
[
name
]
# @cython.internal
cdef
type
__Pyx_EnumBase
class
__Pyx_EnumBase
(
int
):
__metaclass__
=
__Pyx_EnumMeta
def
__new__
(
cls
,
value
,
name
=
None
):
for
v
in
cls
.
__values__
:
if
v
==
value
or
v
.
name
==
value
:
for
v
in
cls
:
if
v
==
value
:
return
v
if
name
is
None
:
raise
ValueError
(
"Unknown enum value: '%s'"
%
value
)
res
=
int
.
__new__
(
cls
,
value
)
res
.
name
=
name
setattr
(
cls
,
name
,
res
)
cls
.
__
values__
.
append
(
res
)
cls
.
__
members__
[
name
]
=
res
return
res
def
__repr__
(
self
):
return
self
.
name
return
"<%s.%s: %d>"
%
(
self
.
__class__
.
__name__
,
self
.
name
,
self
)
def
__str__
(
self
):
return
self
.
name
return
"%s.%s"
%
(
self
.
__class__
.
__name__
,
self
.
name
)
#################### EnumType ####################
#@requires: EnumBase
class
{{
name
}}(
__Pyx_EnumBase
):
pass
cdef
dict
__Pyx_globals
=
globals
()
{{
for
item
in
items
}}
__Pyx_globals
[
'{{item}}'
]
=
{{
name
}}({{
item
}},
'{{item}}'
)
{{
endfor
}}
if
PY_VERSION_HEX
>=
0x03040000
:
from
enum
import
IntEnum
{{
name
}}
=
IntEnum
(
'{{name}}'
,
__Pyx_OrderedDict
([
{{
for
item
in
items
}}
(
'{{item}}'
,
{{
item
}}),
{{
endfor
}}
]))
{{
for
item
in
items
}}
__Pyx_globals
[
'{{item}}'
]
=
{{
name
}}.{{
item
}}
{{
endfor
}}
else
:
class
{{
name
}}(
__Pyx_EnumBase
):
pass
{{
for
item
in
items
}}
__Pyx_globals
[
'{{item}}'
]
=
{{
name
}}({{
item
}},
'{{item}}'
)
{{
endfor
}}
tests/run/cpdef_enums.pyx
View file @
dd8ec79c
...
...
@@ -33,11 +33,13 @@ True
Traceback (most recent call last):
NameError: ...name 'RANK_3' is not defined
>>> list(PyxEnum)
[TWO, THREE, FIVE]
>>> 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
>>> PyxEnum(2) is PyxEnum
["TWO"]
is PyxEnum.TWO
True
"""
...
...
@@ -63,8 +65,14 @@ def test_as_variable_from_cython():
"""
>>> test_as_variable_from_cython()
"""
assert
list
(
PyxEnum
)
==
[
TWO
,
THREE
,
FIVE
]
assert
list
(
PxdEnum
)
==
[
RANK_0
,
RANK_1
,
RANK_2
]
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment