# HG changeset patch # User Paul Boddie # Date 1486315034 -3600 # Node ID 1eb65d050225c357701855bd16b332fd6e2c4f31 # Parent c74e02576c73804bbb471cfe48f72833af375ad1 Added missing support for item and slice deletion, fixing the syntax tree so that the operation nodes become wrapped by Discard nodes. Added missing operator functions for deletion. Implemented __delitem__ in the list class. diff -r c74e02576c73 -r 1eb65d050225 common.py --- a/common.py Sun Feb 05 18:12:40 2017 +0100 +++ b/common.py Sun Feb 05 18:17:14 2017 +0100 @@ -626,12 +626,21 @@ Process the given slice node 'n' as an operator function invocation. """ - op = n.flags == "OP_ASSIGN" and "setslice" or "getslice" + if n.flags == "OP_ASSIGN": op = "setslice" + elif n.flags == "OP_DELETE": op = "delslice" + else: op = "getslice" + invocation = compiler.ast.CallFunc( compiler.ast.Name("$op%s" % op), [n.expr, n.lower or compiler.ast.Name("None"), n.upper or compiler.ast.Name("None")] + (expr and [expr] or []) ) + + # Fix parse tree structure. + + if op == "delslice": + invocation = compiler.ast.Discard(invocation) + return self.process_structure_node(invocation) def process_sliceobj_node(self, n): @@ -653,11 +662,20 @@ Process the given subscript node 'n' as an operator function invocation. """ - op = n.flags == "OP_ASSIGN" and "setitem" or "getitem" + if n.flags == "OP_ASSIGN": op = "setitem" + elif n.flags == "OP_DELETE": op = "delitem" + else: op = "getitem" + invocation = compiler.ast.CallFunc( compiler.ast.Name("$op%s" % op), [n.expr] + list(n.subs) + (expr and [expr] or []) ) + + # Fix parse tree structure. + + if op == "delitem": + invocation = compiler.ast.Discard(invocation) + return self.process_structure_node(invocation) def process_attribute_chain(self, n): diff -r c74e02576c73 -r 1eb65d050225 lib/__builtins__/list.py --- a/lib/__builtins__/list.py Sun Feb 05 18:12:40 2017 +0100 +++ b/lib/__builtins__/list.py Sun Feb 05 18:17:14 2017 +0100 @@ -20,7 +20,7 @@ """ from __builtins__.iteration.iterator import itemiterator -from __builtins__.sequence import sequence +from __builtins__.sequence import sequence, _get_absolute_index from native import list_append, list_concat, list_element, list_init, \ list_len, list_nonempty, list_setelement, list_setsize @@ -40,7 +40,22 @@ if args is not None: self.extend(args) - def __delitem__(self, index): pass + def __delitem__(self, index): + + "Delete the item at 'index'." + + length = self.__len__() + index = _get_absolute_index(index, length) + last = length - 1 + + while index < last: + self[index] = self[index + 1] + index += 1 + + # NOTE: Should truncate the allocated list after several pops. + + list_setsize(self.__data__, last) + def __setslice__(self, start, end, slice): pass def __delslice__(self, start, end): pass diff -r c74e02576c73 -r 1eb65d050225 lib/operator/__init__.py --- a/lib/operator/__init__.py Sun Feb 05 18:12:40 2017 +0100 +++ b/lib/operator/__init__.py Sun Feb 05 18:17:14 2017 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 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 @@ -67,8 +67,10 @@ ) from operator.sequence import ( + delitem, getitem, setitem, + delslice, getslice, setslice, ) diff -r c74e02576c73 -r 1eb65d050225 lib/operator/sequence.py --- a/lib/operator/sequence.py Sun Feb 05 18:12:40 2017 +0100 +++ b/lib/operator/sequence.py Sun Feb 05 18:17:14 2017 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie +Copyright (C) 2010, 2013, 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 @@ -21,12 +21,18 @@ # Access and slicing functions. +def delitem(a, b): + a.__delitem__(b) + def getitem(a, b): return a.__getitem__(b) def setitem(a, b, c): a.__setitem__(b, c) +def delslice(a, b, c): + a.__delitem__(slice(b, c)) + def getslice(a, b, c): return a.__getitem__(slice(b, c))