Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1f320f5

Browse files
committed
Fix mmap to match behaviour on py3
Eurgh ... ints from bytes
1 parentaeed9ac commit1f320f5

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

‎compoundfiles/mmap.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
exceptNameError:
3535
pass
3636

37+
38+
importsys
39+
PY2=sys.version_info[0]==2
3740
importio
3841
importthreading
3942

@@ -80,7 +83,10 @@ def __getitem__(self, key):
8083
key+=self._size
8184
ifnot (0<=key<self._size):
8285
raiseIndexError('fake mmap index out of range')
83-
key=slice(key,key+1,1)
86+
self._file.seek(key)
87+
ifPY2:
88+
returnself._file.read(1)
89+
returnord(self._file.read(1))
8490
step=1ifkey.stepisNoneelsekey.step
8591
ifstep>0:
8692
start=min(self._size,max(0, (
@@ -111,7 +117,7 @@ def __getitem__(self, key):
111117
self._file.seek(start)
112118
ifstart>=stop:
113119
returnb''
114-
returnb''.join(reversed(self._file.read(stop-start)))[::-step]
120+
returnself._file.read(stop-start)[::-1][::-step]
115121
else:
116122
raiseValueError('slice step cannot be zero')
117123
finally:
@@ -163,7 +169,9 @@ def read(self, num):
163169
defread_byte(self):
164170
# XXX Beyond EOF = ValueError
165171
withself._lock:
166-
returnself._file.read(1)
172+
ifPY2:
173+
returnself._file.read(1)
174+
returnord(self._file.read(1))
167175

168176
defreadline(self):
169177
withself._lock:

‎tests/test_mmap.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
str=type('')
3232

3333

34+
importsys
3435
importio
3536
importmmap
3637
importcompoundfiles.mmapasfake_mmap
@@ -69,11 +70,23 @@ def test_read_only(test_map):
6970
deftest_len(test_map):
7071
assertlen(test_map)==26
7172

72-
deftest_indexing(test_map):
73+
@pytest.mark.skipif(sys.version_info[0]==3,
74+
reason="py2 mmap is an array of chars")
75+
deftest_indexing_v2(test_map):
7376
asserttest_map[0]==b'a'
7477
asserttest_map[4]==b'e'
7578
asserttest_map[-1]==b'z'
7679
asserttest_map[-3]==b'x'
80+
81+
@pytest.mark.skipif(sys.version_info[0]==2,
82+
reason="py3 mmap is an array of ints")
83+
deftest_indexing_v3(test_map):
84+
asserttest_map[0]==ord(b'a')
85+
asserttest_map[4]==ord(b'e')
86+
asserttest_map[-1]==ord(b'z')
87+
asserttest_map[-3]==ord(b'x')
88+
89+
deftest_indexing(test_map):
7790
withpytest.raises(IndexError):
7891
test_map[30]
7992
withpytest.raises(IndexError):
@@ -94,7 +107,7 @@ def test_slicing(test_map):
94107
asserttest_map[-5:-1:2]==b'vx'
95108

96109
deftest_negative_slicing(test_map):
97-
asserttest_map[::-1]==b''.join(reversed(test_map[:]))
110+
asserttest_map[::-1]==b'zyxwvutsrqponmlkjihgfedcba'
98111
asserttest_map[::-2]==b'zxvtrpnljhfdb'
99112
asserttest_map[-2::-2]==b'ywusqomkigeca'
100113
asserttest_map[5:10:-1]==b''
@@ -127,6 +140,22 @@ def test_rfind(test_map):
127140
asserttest_map.rfind(b'xyz',0,-1)==-1
128141
asserttest_map.rfind(b'foobar')==-1
129142

143+
@pytest.mark.skipif(sys.version_info[0]==3,
144+
reason="py2 read_byte returns bytes")
145+
deftest_seek_n_read_v2(test_map):
146+
test_map.seek(-3,io.SEEK_END)
147+
asserttest_map.read_byte()==b'x'
148+
asserttest_map.read_byte()==b'y'
149+
asserttest_map.read_byte()==b'z'
150+
151+
@pytest.mark.skipif(sys.version_info[0]==2,
152+
reason="py3 read_byte returns an int")
153+
deftest_seek_n_read_v3(test_map):
154+
test_map.seek(-3,io.SEEK_END)
155+
asserttest_map.read_byte()==ord(b'x')
156+
asserttest_map.read_byte()==ord(b'y')
157+
asserttest_map.read_byte()==ord(b'z')
158+
130159
deftest_seek_n_read(test_map):
131160
test_map.seek(0)
132161
asserttest_map.read(26)==b'abcdefghijklmnopqrstuvwxyz'
@@ -136,10 +165,6 @@ def test_seek_n_read(test_map):
136165
test_map.seek(-3,io.SEEK_END)
137166
asserttest_map.read(-1)==b'xyz'
138167
asserttest_map.read(-1)==b''
139-
test_map.seek(-3,io.SEEK_CUR)
140-
asserttest_map.read_byte()==b'x'
141-
asserttest_map.read_byte()==b'y'
142-
asserttest_map.read_byte()==b'z'
143168
test_map.seek(0,io.SEEK_SET)
144169
asserttest_map.readline()==b'abcdefghijklmnopqrstuvwxyz'
145170

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp