Commit 339d06d3 authored by Stefan Behnel's avatar Stefan Behnel

use explicit helper method for sequence->ctuple coercion rather than calling...

use explicit helper method for sequence->ctuple coercion rather than calling generic SequenceNode.coerce_to()
parent ba79eb61
...@@ -6643,16 +6643,15 @@ class SequenceNode(ExprNode): ...@@ -6643,16 +6643,15 @@ 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): def coerce_to_ctuple(self, dst_type, env):
if dst_type.is_ctuple: if self.type == dst_type:
assert not self.type.is_ctuple, self # should have been caught by TupleNode return self
if len(self.args) != dst_type.size: assert not self.mult_factor
error(self.pos, "trying to coerce sequence to ctuple of wrong length, expected %d, got %d" % ( if len(self.args) != dst_type.size:
dst_type.size, len(self.args))) error(self.pos, "trying to coerce sequence to ctuple of wrong length, expected %d, got %d" % (
coerced_args = [arg.coerce_to(type, env) for arg, type in zip(self.args, dst_type.components)] dst_type.size, len(self.args)))
return TupleNode(self.pos, args=coerced_args, type=dst_type, is_temp=True) coerced_args = [arg.coerce_to(type, env) for arg, type in zip(self.args, dst_type.components)]
else: return TupleNode(self.pos, args=coerced_args, type=dst_type, is_temp=True)
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()
...@@ -7168,15 +7167,14 @@ class TupleNode(SequenceNode): ...@@ -7168,15 +7167,14 @@ class TupleNode(SequenceNode):
def coerce_to(self, dst_type, env): def coerce_to(self, dst_type, env):
if self.type.is_ctuple: if self.type.is_ctuple:
if dst_type.is_ctuple and self.type.size == dst_type.size: if dst_type.is_ctuple and self.type.size == dst_type.size:
if self.type == dst_type: return self.coerce_to_ctuple(dst_type, env)
return self
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=1)
elif dst_type is tuple_type or dst_type is py_object_type: elif dst_type is tuple_type or dst_type is py_object_type:
coerced_args = [arg.coerce_to_pyobject(env) for arg in self.args] coerced_args = [arg.coerce_to_pyobject(env) for arg in self.args]
return TupleNode(self.pos, args=coerced_args, type=tuple_type, is_temp=1).analyse_types(env, skip_children=True) return TupleNode(self.pos, args=coerced_args, type=tuple_type, is_temp=1).analyse_types(env, skip_children=True)
else: else:
return self.coerce_to_pyobject(env).coerce_to(dst_type, env) return self.coerce_to_pyobject(env).coerce_to(dst_type, env)
elif dst_type.is_ctuple and not self.mult_factor:
return self.coerce_to_ctuple(dst_type, env)
else: else:
return SequenceNode.coerce_to(self, dst_type, env) return SequenceNode.coerce_to(self, dst_type, env)
...@@ -7318,7 +7316,7 @@ class ListNode(SequenceNode): ...@@ -7318,7 +7316,7 @@ class ListNode(SequenceNode):
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: elif dst_type.is_ctuple:
return SequenceNode.coerce_to(self, dst_type, env) return self.coerce_to_ctuple(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)
......
...@@ -55,6 +55,10 @@ def packing_tuple(int x, double y): ...@@ -55,6 +55,10 @@ def packing_tuple(int x, double y):
(1, 2.0) (1, 2.0)
""" """
cdef (int, double) xy = (x, y) cdef (int, double) xy = (x, y)
assert xy == (x, y), xy
xy = (x, y) * 1
assert xy == (x, y), xy
xy = 1 * (x, y)
return xy return xy
def packing_list(int x, double y): def packing_list(int x, double y):
...@@ -63,6 +67,10 @@ def packing_list(int x, double y): ...@@ -63,6 +67,10 @@ def packing_list(int x, double y):
(1, 2.0) (1, 2.0)
""" """
cdef (int, double) xy = [x, y] cdef (int, double) xy = [x, y]
assert xy == (x, y), xy
xy = [x, y] * 1
assert xy == (x, y), xy
xy = 1 * [x, y]
return xy return xy
def coerce_packing_tuple(int x, int y): def coerce_packing_tuple(int x, int 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