# HG changeset patch # User Paul Boddie # Date 1485281431 -3600 # Node ID 6d062b05c2501f7d2c966824f1680b63c0e9e13e # Parent ceaa4fac8da5e1b1e649fd28389c39bda7fa06f3 Removed optimised sequence assignment involving equal length sequences because it will not work in general. It could, however, be reintroduced in cases where local or global names are involved and can be shown not to conflict with each other. Made the generator-like test use a swap operation and added a dedicated swap test. diff -r ceaa4fac8da5 -r 6d062b05c250 common.py --- a/common.py Tue Jan 24 17:20:35 2017 +0100 +++ b/common.py Tue Jan 24 19:10:31 2017 +0100 @@ -4,7 +4,7 @@ Common functions. Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, - 2014, 2015, 2016 Paul Boddie + 2014, 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -394,16 +394,9 @@ name_ref = self.process_structure_node(expr) - # Either unpack the items and present them directly to each assignment - # node. + # Have the assignment nodes access each item via the sequence API. - if isinstance(name_ref, LiteralSequenceRef): - self.process_literal_sequence_items(n, name_ref) - - # Or have the assignment nodes access each item via the sequence API. - - else: - self.process_assignment_node_items_by_position(n, expr, name_ref) + self.process_assignment_node_items_by_position(n, expr, name_ref) def process_assignment_node_items_by_position(self, n, expr, name_ref): @@ -417,8 +410,14 @@ assignments = [] - if isinstance(name_ref, NameRef): + # Employ existing names to access the sequence. + # Literal sequences do not provide names of accessible objects. + + if isinstance(name_ref, NameRef) and not isinstance(name_ref, LiteralSequenceRef): temp = name_ref.name + + # For other expressions, create a temporary name to reference the items. + else: temp = self.get_temporary_name() self.next_temporary() @@ -427,6 +426,8 @@ compiler.ast.Assign([compiler.ast.AssName(temp, "OP_ASSIGN")], expr) ) + # Assign the items to the target nodes. + for i, node in enumerate(n.nodes): assignments.append( compiler.ast.Assign([node], compiler.ast.Subscript( diff -r ceaa4fac8da5 -r 6d062b05c250 tests/fib.py --- a/tests/fib.py Tue Jan 24 17:20:35 2017 +0100 +++ b/tests/fib.py Tue Jan 24 19:10:31 2017 +0100 @@ -4,8 +4,7 @@ def next(self): result = self.b - #self.a, self.b = self.b, self.a + self.b - self.b, self.a = self.a + self.b, result + self.a, self.b = self.b, self.a + self.b return result seq = fib() diff -r ceaa4fac8da5 -r 6d062b05c250 tests/swap.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/swap.py Tue Jan 24 19:10:31 2017 +0100 @@ -0,0 +1,10 @@ +class C: + a = 1; b = 2; c = 3 + +print C.a, C.b, C.c # 1 2 3 +C.a, C.b, C.c = C.c, C.b, C.a +print C.a, C.b, C.c # 3 2 1 + +D = C +C.a, C.b, C.c = D.c, D.b, D.a +print C.a, C.b, C.c # 1 2 3