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
7cec62b7
Commit
7cec62b7
authored
Feb 13, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support tuples as compile time constants
parent
eabbb046
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
27 deletions
+49
-27
Cython/Compiler/Parsing.pxd
Cython/Compiler/Parsing.pxd
+1
-0
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+32
-16
tests/run/ct_DEF.pyx
tests/run/ct_DEF.pyx
+16
-11
No files found.
Cython/Compiler/Parsing.pxd
View file @
7cec62b7
...
...
@@ -59,6 +59,7 @@ cdef p_atom(PyrexScanner s)
@
cython
.
locals
(
value
=
unicode
)
cdef
p_int_literal
(
PyrexScanner
s
)
cdef
p_name
(
PyrexScanner
s
,
name
)
cdef
wrap_compile_time_constant
(
pos
,
value
)
cdef
p_cat_string_literal
(
PyrexScanner
s
)
cdef
p_opt_string_literal
(
PyrexScanner
s
,
required_type
=*
)
cdef
bint
check_for_non_ascii_characters
(
unicode
string
)
...
...
Cython/Compiler/Parsing.py
View file @
7cec62b7
...
...
@@ -678,27 +678,43 @@ def p_int_literal(s):
unsigned
=
unsigned
,
longness
=
longness
)
def
p_name
(
s
,
name
):
pos
=
s
.
position
()
if
not
s
.
compile_time_expr
and
name
in
s
.
compile_time_env
:
value
=
s
.
compile_time_env
.
lookup_here
(
name
)
node
=
wrap_compile_time_constant
(
pos
,
value
)
if
node
is
not
None
:
return
node
return
ExprNodes
.
NameNode
(
pos
,
name
=
name
)
def
wrap_compile_time_constant
(
pos
,
value
):
rep
=
repr
(
value
)
if
isinstance
(
value
,
bool
):
return
ExprNodes
.
BoolNode
(
pos
,
value
=
value
)
return
ExprNodes
.
BoolNode
(
pos
,
value
=
value
)
elif
isinstance
(
value
,
int
):
return
ExprNodes
.
IntNode
(
pos
,
value
=
rep
)
return
ExprNodes
.
IntNode
(
pos
,
value
=
rep
)
elif
isinstance
(
value
,
long
):
return
ExprNodes
.
IntNode
(
pos
,
value
=
rep
,
longness
=
"L"
)
return
ExprNodes
.
IntNode
(
pos
,
value
=
rep
,
longness
=
"L"
)
elif
isinstance
(
value
,
float
):
return
ExprNodes
.
FloatNode
(
pos
,
value
=
rep
)
return
ExprNodes
.
FloatNode
(
pos
,
value
=
rep
)
elif
isinstance
(
value
,
_unicode
):
return
ExprNodes
.
UnicodeNode
(
pos
,
value
=
value
)
return
ExprNodes
.
UnicodeNode
(
pos
,
value
=
value
)
elif
isinstance
(
value
,
_bytes
):
return
ExprNodes
.
BytesNode
(
pos
,
value
=
value
)
else
:
return
ExprNodes
.
BytesNode
(
pos
,
value
=
value
)
elif
isinstance
(
value
,
tuple
):
args
=
[
wrap_compile_time_constant
(
pos
,
arg
)
for
arg
in
value
]
if
None
not
in
args
:
return
ExprNodes
.
TupleNode
(
pos
,
args
=
args
)
else
:
# error already reported
return
None
error
(
pos
,
"Invalid type for compile-time constant: %s"
%
value
.
__class__
.
__name__
)
return
ExprNodes
.
NameNode
(
pos
,
name
=
name
)
return
None
def
p_cat_string_literal
(
s
):
# A sequence of one or more adjacent string literals.
...
...
tests/run/ct_DEF.pyx
View file @
7cec62b7
...
...
@@ -8,7 +8,7 @@ if sys.version_info[0] < 3:
__doc__
=
__doc__
.
replace
(
u" b'"
,
u" '"
)
DEF
TUPLE
=
(
1
,
2
,
u
"buckle my shoe"
)
DEF
TUPLE
=
(
1
,
2
,
"buckle my shoe"
)
DEF
TRUE_FALSE
=
(
True
,
False
)
DEF
CHAR
=
c
'x'
...
...
@@ -89,10 +89,21 @@ def s():
cdef
char
*
s
=
STR
return
s
# this does not work!
#def t():
# cdef object t = TUPLE
# return t
def
constant_tuple
():
"""
>>> constant_tuple()
(1, 2, 'buckle my shoe')
"""
cdef
object
t
=
TUPLE
return
t
def
tuple_indexing
():
"""
>>> tuple_indexing()
2
"""
cdef
int
two
=
INT_TUPLE1
[
-
1
]
return
two
def
two
():
"""
...
...
@@ -102,12 +113,6 @@ def two():
cdef
int
two
=
TWO
return
two
# this doesn't currently work!
#def two2():
# cdef int two
# two = INT_TUPLE1[-1]
# return two
def
five
():
"""
>>> five()
...
...
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