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

Commite27e206

Browse files
committed
Fix#6
Large files should now work (albeit slowly) on 32-bit Pythoninstallations via an mmap emulation (which has its own warning toindicate it's being used)
1 parent1f320f5 commite27e206

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

‎compoundfiles/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
CompoundFileDirSectorWarning,
105105
CompoundFileDirSizeWarning,
106106
CompoundFileTruncatedWarning,
107+
CompoundFileEmulationWarning,
107108
)
108109
fromcompoundfiles.streamsimportCompoundFileStream
109110
fromcompoundfiles.entitiesimportCompoundFileEntity

‎compoundfiles/errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,8 @@ class CompoundFileTruncatedWarning(CompoundFileWarning):
198198
Warning about a truncated compound file.
199199
"""
200200

201+
classCompoundFileEmulationWarning(CompoundFileWarning):
202+
"""
203+
Warning about using an emulated memory-map.
204+
"""
205+

‎compoundfiles/reader.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
importstructasst
3737
importwarnings
3838
importmmap
39-
importtempfile
40-
importshutil
39+
importerrno
4140
fromarrayimportarray
4241

4342
from .errorsimport (
@@ -57,6 +56,7 @@
5756
CompoundFileSectorSizeWarning,
5857
CompoundFileMasterSectorWarning,
5958
CompoundFileNormalSectorWarning,
59+
CompoundFileEmulationWarning,
6060
)
6161
from .mmapimportFakeMemoryMap
6262
from .entitiesimportCompoundFileEntity
@@ -173,28 +173,40 @@ def __init__(self, filename_or_obj):
173173
ifisinstance(filename_or_obj, (str,bytes)):
174174
self._opened=True
175175
self._file=io.open(filename_or_obj,'rb')
176-
self._mmap=mmap.mmap(self._file.fileno(),0,access=mmap.ACCESS_READ)
177176
else:
178177
self._opened=False
179178
self._file=filename_or_obj
179+
try:
180+
fd=self._file.fileno()
181+
except (IOError,AttributeError):
182+
# It's a file-like object without a valid file descriptor; use
183+
# our fake mmap class (if it supports seek and tell)
180184
try:
181-
fd=filename_or_obj.fileno()
185+
self._file.seek(0)
186+
self._file.tell()
182187
except (IOError,AttributeError):
183-
# It's a file-like object without a valid file descriptor; use
184-
# our fake mmap class (if it supports seek and tell)
185-
try:
186-
self._file.seek(0)
187-
self._file.tell()
188-
except (IOError,AttributeError):
189-
raiseIOError(
190-
'filename_or_obj must support fileno(), '
191-
'or seek() and tell()')
192-
else:
193-
self._mmap=FakeMemoryMap(filename_or_obj)
188+
raiseTypeError(
189+
'filename_or_obj must support fileno(), '
190+
'or seek() and tell()')
194191
else:
195-
# It's a file-like object with a valid file descriptor; just
196-
# reference the object and mmap it
192+
warnings.warn(
193+
CompoundFileEmulationWarning(
194+
'file-like object has no file descriptor; using '
195+
'slower emulated mmap'))
196+
self._mmap=FakeMemoryMap(filename_or_obj)
197+
else:
198+
try:
197199
self._mmap=mmap.mmap(fd,0,access=mmap.ACCESS_READ)
200+
exceptEnvironmentErrorase:
201+
ife.errno==errno.ENOMEM:
202+
warnings.warn(
203+
CompoundFileEmulationWarning(
204+
'unable to map all of file into memory; using '
205+
'slower emulated mmap (use a 64-bit Python '
206+
'installation to avoid this)'))
207+
self._mmap=FakeMemoryMap(filename_or_obj)
208+
else:
209+
raise
198210

199211
self._master_fat=None
200212
self._normal_fat=None

‎tests/test_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_sample_from_fake_mmap(sample):
131131
verify_contents(doc,contents)
132132

133133
deftest_reader_invalid_source():
134-
withpytest.raises(IOError):
134+
withpytest.raises(TypeError):
135135
cf.CompoundFileReader(object())
136136

137137
deftest_reader_bad_sector():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp