Commit 188e66ac authored by Stefan Behnel's avatar Stefan Behnel

allow all sequence literals to coerce to ctuples

parent 81cd759d
...@@ -6643,6 +6643,17 @@ class SequenceNode(ExprNode): ...@@ -6643,6 +6643,17 @@ class SequenceNode(ExprNode):
# not setting self.type here, subtypes do this # not setting self.type here, subtypes do this
return self 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): def _create_merge_node_if_necessary(self, env):
self._flatten_starred_args() self._flatten_starred_args()
if not any(arg.is_starred for arg in self.args): if not any(arg.is_starred for arg in self.args):
...@@ -7306,6 +7317,8 @@ class ListNode(SequenceNode): ...@@ -7306,6 +7317,8 @@ class ListNode(SequenceNode):
arg = arg.arg arg = arg.arg
self.args[i] = arg.coerce_to(member.type, env) self.args[i] = arg.coerce_to(member.type, env)
self.type = dst_type self.type = dst_type
elif dst_type.is_ctuple:
return SequenceNode.coerce_to(self, dst_type, env)
else: else:
self.type = error_type self.type = error_type
error(self.pos, "Cannot coerce list to type '%s'" % dst_type) error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
......
...@@ -57,6 +57,14 @@ def packing_tuple(int x, double y): ...@@ -57,6 +57,14 @@ def packing_tuple(int x, double y):
cdef (int, double) xy = (x, y) cdef (int, double) xy = (x, y)
return xy 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): def coerce_packing_tuple(int x, int y):
cdef (int, double) xy = (x, y) cdef (int, double) xy = (x, y)
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment