summaryrefslogtreecommitdiff
path: root/docutils/statemachine.py
diff options
context:
space:
mode:
authorgoodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2003-01-01 15:50:23 +0000
committergoodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2003-01-01 15:50:23 +0000
commitc9cc44e987ddc86c342b6b797e38e4836aedcd62 (patch)
tree4b658cbb305c3cd25245d62672fa1f16b7fe6c50 /docutils/statemachine.py
parentf6d2823bed3483be7c3a126c2499d7e9018fae8d (diff)
downloaddocutils-c9cc44e987ddc86c342b6b797e38e4836aedcd62.tar.gz
Fixed slice handling for Python 2.3. Patch from (and thanks to) Fred Drake.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1049 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/statemachine.py')
-rw-r--r--docutils/statemachine.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/docutils/statemachine.py b/docutils/statemachine.py
index 076a9df2f..d0ddb6330 100644
--- a/docutils/statemachine.py
+++ b/docutils/statemachine.py
@@ -110,6 +110,7 @@ __docformat__ = 'restructuredtext'
import sys
import re
+from types import SliceType as _SliceType
class StateMachine:
@@ -1104,22 +1105,23 @@ class ViewList:
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 @@ class ViewList:
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: