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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
188e66ac
Commit
188e66ac
authored
Sep 26, 2015
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow all sequence literals to coerce to ctuples
parent
81cd759d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
0 deletions
+21
-0
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+13
-0
tests/run/ctuple.pyx
tests/run/ctuple.pyx
+8
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
188e66ac
...
...
@@ -6643,6 +6643,17 @@ class SequenceNode(ExprNode):
# not setting self.type here, subtypes do this
return
self
def
coerce_to
(
self
,
dst_type
,
env
):
if
dst_type
.
is_ctuple
:
assert
not
self
.
type
.
is_ctuple
,
self
# should have been caught by TupleNode
if
len
(
self
.
args
)
!=
dst_type
.
size
:
error
(
self
.
pos
,
"trying to coerce sequence to ctuple of wrong length, expected %d, got %d"
%
(
dst_type
.
size
,
len
(
self
.
args
)))
coerced_args
=
[
arg
.
coerce_to
(
type
,
env
)
for
arg
,
type
in
zip
(
self
.
args
,
dst_type
.
components
)]
return
TupleNode
(
self
.
pos
,
args
=
coerced_args
,
type
=
dst_type
,
is_temp
=
True
)
else
:
return
ExprNode
.
coerce_to
(
self
,
dst_type
,
env
)
def
_create_merge_node_if_necessary
(
self
,
env
):
self
.
_flatten_starred_args
()
if
not
any
(
arg
.
is_starred
for
arg
in
self
.
args
):
...
...
@@ -7306,6 +7317,8 @@ class ListNode(SequenceNode):
arg
=
arg
.
arg
self
.
args
[
i
]
=
arg
.
coerce_to
(
member
.
type
,
env
)
self
.
type
=
dst_type
elif
dst_type
.
is_ctuple
:
return
SequenceNode
.
coerce_to
(
self
,
dst_type
,
env
)
else
:
self
.
type
=
error_type
error
(
self
.
pos
,
"Cannot coerce list to type '%s'"
%
dst_type
)
...
...
tests/run/ctuple.pyx
View file @
188e66ac
...
...
@@ -57,6 +57,14 @@ def packing_tuple(int x, double y):
cdef
(
int
,
double
)
xy
=
(
x
,
y
)
return
xy
def
packing_list
(
int
x
,
double
y
):
"""
>>> packing_list(1, 2)
(1, 2.0)
"""
cdef
(
int
,
double
)
xy
=
[
x
,
y
]
return
xy
def
coerce_packing_tuple
(
int
x
,
int
y
):
cdef
(
int
,
double
)
xy
=
(
x
,
y
)
"""
...
...
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