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
8e34cc36
Commit
8e34cc36
authored
Sep 03, 2015
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow enum type access patterns from Cython as well as Python.
parent
60e419e6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
67 additions
and
36 deletions
+67
-36
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+45
-34
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+5
-1
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+1
-1
tests/run/cpdef_enums.pyx
tests/run/cpdef_enums.pyx
+16
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
8e34cc36
...
...
@@ -1859,7 +1859,12 @@ class NameNode(AtomicExprNode):
entry
=
self
.
entry
if
entry
.
is_type
and
entry
.
type
.
is_extension_type
:
self
.
type_entry
=
entry
if
not
(
entry
.
is_const
or
entry
.
is_variable
if
entry
.
is_type
and
entry
.
type
.
is_enum
:
py_entry
=
Symtab
.
Entry
(
self
.
name
,
None
,
py_object_type
)
py_entry
.
is_pyglobal
=
True
py_entry
.
scope
=
self
.
entry
.
scope
self
.
entry
=
py_entry
elif
not
(
entry
.
is_const
or
entry
.
is_variable
or
entry
.
is_builtin
or
entry
.
is_cfunction
or
entry
.
is_cpp_class
):
if
self
.
entry
.
as_variable
:
...
...
@@ -5832,7 +5837,7 @@ class AttributeNode(ExprNode):
node
=
self
.
analyse_as_cimported_attribute_node
(
env
,
target
=
False
)
if
node
is
not
None
:
return
node
.
entry
.
type
node
=
self
.
analyse_as_
unbound_cmethod_nod
e
(
env
)
node
=
self
.
analyse_as_
type_attribut
e
(
env
)
if
node
is
not
None
:
return
node
.
entry
.
type
obj_type
=
self
.
obj
.
infer_type
(
env
)
...
...
@@ -5859,7 +5864,7 @@ class AttributeNode(ExprNode):
self
.
initialized_check
=
env
.
directives
[
'initializedcheck'
]
node
=
self
.
analyse_as_cimported_attribute_node
(
env
,
target
)
if
node
is
None
and
not
target
:
node
=
self
.
analyse_as_
unbound_cmethod_nod
e
(
env
)
node
=
self
.
analyse_as_
type_attribut
e
(
env
)
if
node
is
None
:
node
=
self
.
analyse_as_ordinary_attribute_node
(
env
,
target
)
assert
node
is
not
None
...
...
@@ -5883,7 +5888,7 @@ class AttributeNode(ExprNode):
return
self
.
as_name_node
(
env
,
entry
,
target
)
return
None
def
analyse_as_
unbound_cmethod_nod
e
(
self
,
env
):
def
analyse_as_
type_attribut
e
(
self
,
env
):
# Try to interpret this as a reference to an unbound
# C method of an extension type or builtin type. If successful,
# creates a corresponding NameNode and returns it, otherwise
...
...
@@ -5891,37 +5896,43 @@ class AttributeNode(ExprNode):
if
self
.
obj
.
is_string_literal
:
return
type
=
self
.
obj
.
analyse_as_type
(
env
)
if
type
and
(
type
.
is_extension_type
or
type
.
is_builtin_type
or
type
.
is_cpp_class
):
entry
=
type
.
scope
.
lookup_here
(
self
.
attribute
)
if
entry
and
(
entry
.
is_cmethod
or
type
.
is_cpp_class
and
entry
.
type
.
is_cfunction
):
if
type
.
is_builtin_type
:
if
not
self
.
is_called
:
# must handle this as Python object
return
None
ubcm_entry
=
entry
else
:
# Create a temporary entry describing the C method
# as an ordinary function.
if
entry
.
func_cname
and
not
hasattr
(
entry
.
type
,
'op_arg_struct'
):
cname
=
entry
.
func_cname
if
entry
.
type
.
is_static_method
:
ctype
=
entry
.
type
elif
type
.
is_cpp_class
:
error
(
self
.
pos
,
"%s not a static member of %s"
%
(
entry
.
name
,
type
))
ctype
=
PyrexTypes
.
error_type
else
:
# Fix self type.
ctype
=
copy
.
copy
(
entry
.
type
)
ctype
.
args
=
ctype
.
args
[:]
ctype
.
args
[
0
]
=
PyrexTypes
.
CFuncTypeArg
(
'self'
,
type
,
'self'
,
None
)
if
type
:
if
type
.
is_extension_type
or
type
.
is_builtin_type
or
type
.
is_cpp_class
:
entry
=
type
.
scope
.
lookup_here
(
self
.
attribute
)
if
entry
and
(
entry
.
is_cmethod
or
type
.
is_cpp_class
and
entry
.
type
.
is_cfunction
):
if
type
.
is_builtin_type
:
if
not
self
.
is_called
:
# must handle this as Python object
return
None
ubcm_entry
=
entry
else
:
cname
=
"%s->%s"
%
(
type
.
vtabptr_cname
,
entry
.
cname
)
ctype
=
entry
.
type
ubcm_entry
=
Symtab
.
Entry
(
entry
.
name
,
cname
,
ctype
)
ubcm_entry
.
is_cfunction
=
1
ubcm_entry
.
func_cname
=
entry
.
func_cname
ubcm_entry
.
is_unbound_cmethod
=
1
return
self
.
as_name_node
(
env
,
ubcm_entry
,
target
=
False
)
# Create a temporary entry describing the C method
# as an ordinary function.
if
entry
.
func_cname
and
not
hasattr
(
entry
.
type
,
'op_arg_struct'
):
cname
=
entry
.
func_cname
if
entry
.
type
.
is_static_method
:
ctype
=
entry
.
type
elif
type
.
is_cpp_class
:
error
(
self
.
pos
,
"%s not a static member of %s"
%
(
entry
.
name
,
type
))
ctype
=
PyrexTypes
.
error_type
else
:
# Fix self type.
ctype
=
copy
.
copy
(
entry
.
type
)
ctype
.
args
=
ctype
.
args
[:]
ctype
.
args
[
0
]
=
PyrexTypes
.
CFuncTypeArg
(
'self'
,
type
,
'self'
,
None
)
else
:
cname
=
"%s->%s"
%
(
type
.
vtabptr_cname
,
entry
.
cname
)
ctype
=
entry
.
type
ubcm_entry
=
Symtab
.
Entry
(
entry
.
name
,
cname
,
ctype
)
ubcm_entry
.
is_cfunction
=
1
ubcm_entry
.
func_cname
=
entry
.
func_cname
ubcm_entry
.
is_unbound_cmethod
=
1
return
self
.
as_name_node
(
env
,
ubcm_entry
,
target
=
False
)
elif
type
.
is_enum
:
if
self
.
attribute
in
type
.
values
:
return
self
.
as_name_node
(
env
,
env
.
lookup
(
self
.
attribute
),
target
=
False
)
else
:
error
(
self
.
pos
,
"%s not a known value of %s"
%
(
self
.
attribute
,
type
))
return
None
def
analyse_as_type
(
self
,
env
):
...
...
Cython/Compiler/Nodes.py
View file @
8e34cc36
...
...
@@ -1496,11 +1496,15 @@ class CEnumDefNode(StatNode):
self
.
entry
.
defined_in_pxd
=
1
for
item
in
self
.
items
:
item
.
analyse_declarations
(
env
,
self
.
entry
)
if
self
.
name
is
not
None
:
self
.
entry
.
type
.
values
=
set
(
(
item
.
name
)
for
item
in
self
.
items
)
if
self
.
create_wrapper
and
self
.
name
is
not
None
:
from
.UtilityCode
import
CythonUtilityCode
env
.
use_utility_code
(
CythonUtilityCode
.
load
(
"EnumType"
,
"CpdefEnums.pyx"
,
context
=
{
"name"
:
self
.
name
,
"items"
:
tuple
(
item
.
name
for
item
in
self
.
items
)},
context
=
{
"name"
:
self
.
name
,
"items"
:
tuple
(
item
.
name
for
item
in
self
.
items
)},
outer_module_scope
=
env
.
global_scope
()))
def
analyse_expressions
(
self
,
env
):
...
...
Cython/Compiler/Optimize.py
View file @
8e34cc36
...
...
@@ -2098,7 +2098,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
entry
=
type_entry
,
type
=
type_entry
.
type
),
attribute
=
attr_name
,
is_called
=
True
).
analyse_as_
unbound_cmethod_nod
e
(
self
.
current_env
())
is_called
=
True
).
analyse_as_
type_attribut
e
(
self
.
current_env
())
if
method
is
None
:
return
node
args
=
node
.
args
...
...
tests/run/cpdef_enums.pyx
View file @
8e34cc36
...
...
@@ -58,3 +58,19 @@ cpdef enum PyxEnum:
cdef
enum
SecretPyxEnum
:
SEVEN
=
7
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
]
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
()
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