Skip to content

Commit 8f4095b

Browse files
Replace __getslice__ with __getitem__.
1 parent 40272a1 commit 8f4095b

File tree

6 files changed

+67
-38
lines changed

6 files changed

+67
-38
lines changed

test/gen/genprottest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,15 +1019,20 @@ def build_bin(bin):
10191019
import struct
10201020
import array
10211021
1022-
class CmpArray:
1022+
class CmpArray(object):
10231023
def __init__(self, *args, **kws):
10241024
self.array = array.array(*args, **kws)
10251025
10261026
def __len__(self):
10271027
return len(self.array)
10281028
1029-
def __getslice__(self, x, y):
1030-
return list(self.array[x:y])
1029+
def __getitem__(self, key):
1030+
if isinstance(key, slice):
1031+
x = key.start
1032+
y = key.stop
1033+
return list(self.array[x:y])
1034+
else:
1035+
return self.array[key]
10311036
10321037
def __getattr__(self, attr):
10331038
return getattr(self.array, attr)

test/test_events_be.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import struct
1111
import array
1212

13-
class CmpArray:
13+
class CmpArray(object):
1414
def __init__(self, *args, **kws):
1515
self.array = array.array(*args, **kws)
1616

1717
def __len__(self):
1818
return len(self.array)
1919

20-
def __getslice__(self, x, y):
21-
return list(self.array[x:y])
20+
def __getitem__(self, key):
21+
if isinstance(key, slice):
22+
x = key.start
23+
y = key.stop
24+
return list(self.array[x:y])
25+
else:
26+
return self.array[key]
2227

2328
def __getattr__(self, attr):
2429
return getattr(self.array, attr)

test/test_events_le.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ def _ordb(c):
1919
"""Integer representation of a byte indexed from a byte string - Py2"""
2020
return ord(c)
2121

22-
class CmpArray:
22+
class CmpArray(object):
2323
def __init__(self, *args, **kws):
2424
self.array = array.array(*args, **kws)
2525

2626
def __len__(self):
2727
return len(self.array)
2828

29-
def __getslice__(self, x, y):
30-
return list(self.array[x:y])
29+
def __getitem__(self, key):
30+
if isinstance(key, slice):
31+
x = key.start
32+
y = key.stop
33+
return list(self.array[x:y])
34+
else:
35+
return self.array[key]
3136

3237
def __getattr__(self, attr):
3338
return getattr(self.array, attr)

test/test_requests_be.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import struct
1111
import array
1212

13-
class CmpArray:
13+
class CmpArray(object):
1414
def __init__(self, *args, **kws):
1515
self.array = array.array(*args, **kws)
1616

1717
def __len__(self):
1818
return len(self.array)
1919

20-
def __getslice__(self, x, y):
21-
return list(self.array[x:y])
20+
def __getitem__(self, key):
21+
if isinstance(key, slice):
22+
x = key.start
23+
y = key.stop
24+
return list(self.array[x:y])
25+
else:
26+
return self.array[key]
2227

2328
def __getattr__(self, attr):
2429
return getattr(self.array, attr)

test/test_requests_le.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import struct
1111
import array
1212

13-
class CmpArray:
13+
class CmpArray(object):
1414
def __init__(self, *args, **kws):
1515
self.array = array.array(*args, **kws)
1616

1717
def __len__(self):
1818
return len(self.array)
1919

20-
def __getslice__(self, x, y):
21-
return list(self.array[x:y])
20+
def __getitem__(self, key):
21+
if isinstance(key, slice):
22+
x = key.start
23+
y = key.stop
24+
return list(self.array[x:y])
25+
else:
26+
return self.array[key]
2227

2328
def __getattr__(self, attr):
2429
return getattr(self.array, attr)

utils/parsexbug.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,42 @@ def __init__(self, datafunc):
6565
self.get_data = datafunc
6666
self.data = ''
6767

68-
def __getitem__(self, i):
69-
if i < 0:
70-
raise ValueError('bad string index: %d' % i)
68+
def __getitem__(self, key):
69+
if not isinstance(key, slice):
70+
if key < 0:
71+
raise ValueError('bad string index: {0}'.format(key))
7172

72-
if len(self.data) <= i:
73-
if not self.get_data:
74-
raise RuntimeError('attempt to allocate more data after returning a new ParseString')
73+
if len(self.data) <= key:
74+
if not self.get_data:
75+
raise RuntimeError('attempt to allocate more data after returning a new ParseString')
7576

76-
self.data = self.data + self.get_data(i - len(self.data) + 1)
77+
self.data = self.data + self.get_data(key - len(self.data) + 1)
7778

78-
return self.data[i]
79+
return self.data[key]
7980

80-
def __getslice__(self, i, j):
81-
if j == sys.maxint:
82-
if self.get_data:
83-
ps = ParseString(self.get_data)
84-
self.get_data = None
85-
return ps
86-
else:
87-
raise RuntimeError('attempt to allocate another ParseString')
81+
else:
82+
# replacement of __getslice__
83+
i = key.start
84+
j = key.stop
85+
if j == sys.maxint:
86+
if self.get_data:
87+
ps = ParseString(self.get_data)
88+
self.get_data = None
89+
return ps
90+
else:
91+
raise RuntimeError('attempt to allocate another ParseString')
8892

8993

90-
if i < 0 or j < 0 or i > j:
91-
raise ValueError('bad slice indices: [%d:%d]' % (i, j))
94+
if i < 0 or j < 0 or i > j:
95+
raise ValueError('bad slice indices: [%d:%d]' % (i, j))
9296

93-
if len(self.data) < j:
94-
if not self.get_data:
95-
raise RuntimeError('attempt to allocate more data after returning a new ParseString')
97+
if len(self.data) < j:
98+
if not self.get_data:
99+
raise RuntimeError('attempt to allocate more data after returning a new ParseString')
96100

97-
self.data = self.data + self.get_data(j - len(self.data))
101+
self.data = self.data + self.get_data(j - len(self.data))
98102

99-
return self.data[i:j]
103+
return self.data[i:j]
100104

101105
class DummyDisplay:
102106
def get_resource_class(self, name):

0 commit comments

Comments
 (0)