1.1 --- a/lib/__builtins__/list.py Wed Nov 23 15:47:45 2016 +0100
1.2 +++ b/lib/__builtins__/list.py Wed Nov 23 15:48:37 2016 +0100
1.3 @@ -20,10 +20,10 @@
1.4 """
1.5
1.6 from __builtins__.iterator import listiterator
1.7 -from __builtins__.sequence import _getitem, _getslice
1.8 +from __builtins__.sequence import sequence
1.9 import native
1.10
1.11 -class list(object):
1.12 +class list(sequence):
1.13
1.14 "Implementation of list."
1.15
1.16 @@ -39,22 +39,9 @@
1.17 if args is not None:
1.18 self.extend(args)
1.19
1.20 - def __getitem__(self, index):
1.21 -
1.22 - "Return the item or slice specified by 'index'."
1.23 -
1.24 - return _getitem(self, index)
1.25 -
1.26 def __contains__(self, value): pass
1.27 def __setitem__(self, index, value): pass
1.28 def __delitem__(self, index): pass
1.29 -
1.30 - def __getslice__(self, start, end=None):
1.31 -
1.32 - "Return a slice starting from 'start', with the optional 'end'."
1.33 -
1.34 - return _getslice(self, start, end)
1.35 -
1.36 def __setslice__(self, start, end, slice): pass
1.37 def __delslice__(self, start, end): pass
1.38
2.1 --- a/lib/__builtins__/sequence.py Wed Nov 23 15:47:45 2016 +0100
2.2 +++ b/lib/__builtins__/sequence.py Wed Nov 23 15:48:37 2016 +0100
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Sequence operations.
2.6
2.7 -Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -21,39 +21,60 @@
2.13
2.14 from native import _isinstance
2.15
2.16 -def _getitem(seq, index):
2.17 +class sequence:
2.18 +
2.19 + "A common base class for sequence types."
2.20
2.21 - "Return the item or slice specified by 'index'."
2.22 + def __getitem__(self, index):
2.23 +
2.24 + "Return the item or slice specified by 'index'."
2.25 +
2.26 + # Normalise any integer indexes, converting negative indexes to positive
2.27 + # ones.
2.28
2.29 - if _isinstance(index, int):
2.30 - index = _normalise_index(index, seq.__len__())
2.31 - return seq.__get_single_item__(index)
2.32 - elif _isinstance(index, slice):
2.33 - return seq.__getslice__(index.start, index.end)
2.34 - else:
2.35 - raise TypeError
2.36 + if _isinstance(index, int):
2.37 + index = _normalise_index(index, self.__len__())
2.38 + return self.__get_single_item__(index)
2.39 +
2.40 + # Handle slices separately.
2.41
2.42 -def _getslice(seq, start, end=None):
2.43 + elif _isinstance(index, slice):
2.44 + return self.__getslice__(index.start, index.end)
2.45 +
2.46 + # No other kinds of objects are supported as indexes.
2.47
2.48 - "Return a slice starting from 'start', with the optional 'end'."
2.49 + else:
2.50 + raise TypeError
2.51 +
2.52 + def __getslice__(self, start, end=None):
2.53
2.54 - length = seq.__len__()
2.55 + "Return a slice starting from 'start', with the optional 'end'."
2.56 +
2.57 + length = self.__len__()
2.58
2.59 - if start is None:
2.60 - start = 0
2.61 - else:
2.62 - start = _normalise_index(start, length)
2.63 + # Handle a null start as the first position, otherwise normalising any
2.64 + # start index.
2.65 +
2.66 + if start is None:
2.67 + start = 0
2.68 + else:
2.69 + start = _normalise_index(start, length)
2.70
2.71 - if end is None:
2.72 - end = length
2.73 - else:
2.74 - end = _normalise_index(end, length)
2.75 + # Handle a null end as the first position after the end of the sequence,
2.76 + # otherwise normalising any end index.
2.77 +
2.78 + if end is None:
2.79 + end = length
2.80 + else:
2.81 + end = _normalise_index(end, length)
2.82
2.83 - result = []
2.84 - while start < end:
2.85 - result.append(seq.__get_single_item__(start))
2.86 - start += 1
2.87 - return result
2.88 + result = []
2.89 +
2.90 + while start < end:
2.91 + result.append(self.__get_single_item__(start))
2.92 + start += 1
2.93 +
2.94 + return result
2.95
2.96 def _get_absolute_index(index, length):
2.97
3.1 --- a/lib/__builtins__/tuple.py Wed Nov 23 15:47:45 2016 +0100
3.2 +++ b/lib/__builtins__/tuple.py Wed Nov 23 15:48:37 2016 +0100
3.3 @@ -20,10 +20,10 @@
3.4 """
3.5
3.6 from __builtins__.iterator import listiterator
3.7 -from __builtins__.sequence import _getitem, _getslice
3.8 +from __builtins__.sequence import sequence
3.9 import native
3.10
3.11 -class tuple(object):
3.12 +class tuple(sequence):
3.13
3.14 "Implementation of tuple."
3.15
3.16 @@ -33,17 +33,11 @@
3.17
3.18 self.__data__ = native._tuple_init(args, len(args)) # allocate and copy elements
3.19
3.20 - def __getitem__(self, index):
3.21 -
3.22 - "Return the item or slice specified by 'index'."
3.23 -
3.24 - return _getitem(self, index)
3.25 -
3.26 def __getslice__(self, start, end=None):
3.27
3.28 "Return a slice starting from 'start', with the optional 'end'."
3.29
3.30 - return native._list_to_tuple(_getslice(self, start, end))
3.31 + return native._list_to_tuple(get_using(sequence.__getslice__, self)(start, end))
3.32
3.33 def __len__(self):
3.34
3.35 @@ -55,6 +49,8 @@
3.36
3.37 def __str__(self): pass
3.38
3.39 + __repr__ = __str__
3.40 +
3.41 def __bool__(self):
3.42
3.43 "Tuples are true if non-empty."