From: David G. <go...@us...> - 2003-01-01 15:50:28
|
Update of /cvsroot/docutils/docutils/docutils In directory sc8-pr-cvs1:/tmp/cvs-serv22519 Modified Files: statemachine.py Log Message: Fixed slice handling for Python 2.3. Patch from (and thanks to) Fred Drake. Index: statemachine.py =================================================================== RCS file: /cvsroot/docutils/docutils/docutils/statemachine.py,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- statemachine.py 8 Nov 2002 01:32:08 -0000 1.12 +++ statemachine.py 1 Jan 2003 15:50:23 -0000 1.13 @@ -110,6 +110,7 @@ import sys import re +from types import SliceType as _SliceType class StateMachine: @@ -1104,22 +1105,23 @@ def __contains__(self, item): return item in self.data def __len__(self): return len(self.data) + # The __getitem__()/__setitem__() methods check whether the index + # is a slice first, since native list objects start supporting + # them directly in Python 2.3 (no exception is raised when + # indexing a list with a slice object; they just work). + def __getitem__(self, i): - try: - return self.data[i] - except TypeError: - assert i.step is None, 'cannot handle slice with stride' + if isinstance(i, _SliceType): + assert i.step in (None, 1), 'cannot handle slice with stride' return self.__class__(self.data[i.start:i.stop], items=self.items[i.start:i.stop], parent=self, parent_offset=i.start) + else: + return self.data[i] def __setitem__(self, i, item): - try: - self.data[i] = item - if self.parent: - self.parent[i + self.parent_offset] = item - except TypeError: - assert i.step is None, 'cannot handle slice with stride' + if isinstance(i, _SliceType): + assert i.step in (None, 1), 'cannot handle slice with stride' if not isinstance(item, ViewList): raise TypeError('assigning non-ViewList to ViewList slice') self.data[i.start:i.stop] = item.data @@ -1128,6 +1130,10 @@ if self.parent: self.parent[i.start + self.parent_offset : i.stop + self.parent_offset] = item + else: + self.data[i] = item + if self.parent: + self.parent[i + self.parent_offset] = item def __delitem__(self, i): try: |