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

Commitaeed9ac

Browse files
committed
Fix#2
Gone about as far as we can with the tests for now. Still need to figureout a way to test excessively large FATs and mini-FATs though
1 parentc245c4a commitaeed9ac

File tree

3 files changed

+178
-11
lines changed

3 files changed

+178
-11
lines changed

‎compoundfiles/mmap.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def __getitem__(self, key):
9494
key.stop
9595
)))
9696
self._file.seek(start)
97+
ifstart>=stop:
98+
returnb''
9799
returnself._file.read(stop-start)[::step]
98100
elifstep<0:
99101
start=min(self._size,max(0, (
@@ -107,14 +109,20 @@ def __getitem__(self, key):
107109
key.start
108110
)+1))
109111
self._file.seek(start)
110-
return''.join(reversed(self._file.read(stop-start)))[::-step]
112+
ifstart>=stop:
113+
returnb''
114+
returnb''.join(reversed(self._file.read(stop-start)))[::-step]
111115
else:
112116
raiseValueError('slice step cannot be zero')
113117
finally:
114118
self._file.seek(save_pos)
115119

116120
def__contains__(self,value):
117-
returnself.find(value)!=-1
121+
# This operates rather oddly with memory-maps; it returns a valid
122+
# answer if value is a single byte. Otherwise, it returns False
123+
iflen(value)==1:
124+
returnself.find(value)!=1
125+
returnFalse
118126

119127
def__setitem__(self,index,value):
120128
self._read_only()
@@ -132,16 +140,18 @@ def find(self, string, start=None, end=None):
132140
))
133141
end=min(self._size,max(0,
134142
self._sizeifendisNoneelse
135-
self._size+stopifstop<0else
136-
stop
143+
self._size+endifend<0else
144+
end
137145
))
138-
foriinrange(start,end-l):
146+
foriinrange(start,end-l+1):
139147
ifself[i:i+l]==string:
140148
returni
141149
return-1
142150

143151
defflush(self,offset=None,size=None):
144-
self._read_only()
152+
# Seems like this should raise a read-only error, but real read-only
153+
# mmaps don't so we don't either
154+
pass
145155

146156
defmove(self,dest,src,count):
147157
self._read_only()
@@ -159,23 +169,25 @@ def readline(self):
159169
withself._lock:
160170
returnself._file.readline()
161171

162-
defresize(self):
172+
defresize(self,newsize):
163173
self._read_only()
164174

165175
defrfind(self,string,start=None,end=None):
166176
# XXX Naive find; replace with Boyer-Moore?
167177
l=len(string)
168178
start=min(self._size,max(0,
169-
self._size-1ifstartisNoneelse
179+
0ifstartisNoneelse
170180
self._size+startifstart<0else
171181
start
172182
))
173-
stop=min(self._size,max(0,
174-
-1ifendisNoneelse
183+
end=min(self._size,max(0,
184+
self._sizeifendisNoneelse
175185
self._size+endifend<0else
176186
end
177187
))
178-
foriinrange(start-l,end,-1):
188+
print(start,end,l)
189+
print(list(range(end-l,start-1,-1)))
190+
foriinrange(end-l,start-1,-1):
179191
ifself[i:i+l]==string:
180192
returni
181193
return-1

‎tests/mmap.dat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
abcdefghijklmnopqrstuvwxyz

‎tests/test_mmap.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/usr/bin/env python
2+
# vim: set et sw=4 sts=4 fileencoding=utf-8:
3+
#
4+
# A library for reading Microsoft's OLE Compound Document format
5+
# Copyright (c) 2014 Dave Hughes <dave@waveform.org.uk>
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
from __future__import (
26+
unicode_literals,
27+
absolute_import,
28+
print_function,
29+
division,
30+
)
31+
str=type('')
32+
33+
34+
importio
35+
importmmap
36+
importcompoundfiles.mmapasfake_mmap
37+
importpytest
38+
39+
40+
@pytest.fixture(scope='session')
41+
defsource():
42+
returnio.open('tests/mmap.dat','rb')
43+
44+
@pytest.fixture(params=(False,True))
45+
deftest_map(request,source):
46+
# Run all tests against a real memory map, and our fake memory map, both
47+
# covering the same underlying file (which just contains a..z), to ensure
48+
# that the emulated behaviour matches the real implementation
49+
ifrequest.param:
50+
returnfake_mmap.FakeMemoryMap(source)
51+
else:
52+
returnmmap.mmap(source.fileno(),0,access=mmap.ACCESS_READ)
53+
54+
55+
deftest_read_only(test_map):
56+
withpytest.raises(TypeError):
57+
test_map.write(b'foo')
58+
withpytest.raises(TypeError):
59+
test_map.write_byte(b'f')
60+
withpytest.raises(TypeError):
61+
test_map[:6]=b'foobar'
62+
withpytest.raises(TypeError):
63+
test_map.move(5,10,4)
64+
withpytest.raises(TypeError):
65+
test_map.resize(1000)
66+
# This doesn't raise an error ... even with a read-only mmap!
67+
test_map.flush()
68+
69+
deftest_len(test_map):
70+
assertlen(test_map)==26
71+
72+
deftest_indexing(test_map):
73+
asserttest_map[0]==b'a'
74+
asserttest_map[4]==b'e'
75+
asserttest_map[-1]==b'z'
76+
asserttest_map[-3]==b'x'
77+
withpytest.raises(IndexError):
78+
test_map[30]
79+
withpytest.raises(IndexError):
80+
test_map[-30]
81+
82+
deftest_slicing(test_map):
83+
asserttest_map[:0]==b''
84+
asserttest_map[:1]==b'a'
85+
asserttest_map[:5]==b'abcde'
86+
asserttest_map[1:5]==b'bcde'
87+
asserttest_map[1:5:2]==b'bd'
88+
asserttest_map[2:5:3]==b'c'
89+
asserttest_map[:-3]==b'abcdefghijklmnopqrstuvw'
90+
asserttest_map[0:]==b'abcdefghijklmnopqrstuvwxyz'
91+
asserttest_map[-3:]==b'xyz'
92+
asserttest_map[-3::1]==b'xyz'
93+
asserttest_map[-3::2]==b'xz'
94+
asserttest_map[-5:-1:2]==b'vx'
95+
96+
deftest_negative_slicing(test_map):
97+
asserttest_map[::-1]==b''.join(reversed(test_map[:]))
98+
asserttest_map[::-2]==b'zxvtrpnljhfdb'
99+
asserttest_map[-2::-2]==b'ywusqomkigeca'
100+
asserttest_map[5:10:-1]==b''
101+
asserttest_map[10:5:-1]==b'kjihg'
102+
103+
deftest_bad_slice(test_map):
104+
withpytest.raises(ValueError):
105+
test_map[5:10:0]
106+
107+
deftest_contains(test_map):
108+
# See comments in FakeMemoryMap.__contains__ to understand this!
109+
assertb'a'intest_map
110+
assertb'abc'notintest_map
111+
assertb'd'intest_map
112+
assertb'def'notintest_map
113+
assertb'vwxyz'notintest_map
114+
assertb'blah'notintest_map
115+
116+
deftest_find(test_map):
117+
asserttest_map.find(b'abc')==0
118+
asserttest_map.find(b'abc',5)==-1
119+
asserttest_map.find(b'xyz')==23
120+
asserttest_map.find(b'xyz',0,-1)==-1
121+
asserttest_map.find(b'foobar')==-1
122+
123+
deftest_rfind(test_map):
124+
asserttest_map.rfind(b'abc')==0
125+
asserttest_map.rfind(b'abc',5)==-1
126+
asserttest_map.rfind(b'xyz')==23
127+
asserttest_map.rfind(b'xyz',0,-1)==-1
128+
asserttest_map.rfind(b'foobar')==-1
129+
130+
deftest_seek_n_read(test_map):
131+
test_map.seek(0)
132+
asserttest_map.read(26)==b'abcdefghijklmnopqrstuvwxyz'
133+
test_map.seek(0)
134+
asserttest_map.read(5)==b'abcde'
135+
asserttest_map.read(2)==b'fg'
136+
test_map.seek(-3,io.SEEK_END)
137+
asserttest_map.read(-1)==b'xyz'
138+
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'
143+
test_map.seek(0,io.SEEK_SET)
144+
asserttest_map.readline()==b'abcdefghijklmnopqrstuvwxyz'
145+
146+
deftest_seek_n_tell(test_map):
147+
test_map.seek(0)
148+
asserttest_map.tell()==0
149+
test_map.seek(0,io.SEEK_END)
150+
asserttest_map.tell()==26
151+
test_map.seek(-3,io.SEEK_CUR)
152+
asserttest_map.tell()==23
153+
test_map.seek(0,io.SEEK_SET)
154+
asserttest_map.tell()==0

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp