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

Commit56c0bd7

Browse files
committed
Give sensible exceptions when we know we can't open a file.
1 parent2cabada commit56c0bd7

File tree

2 files changed

+20
-39
lines changed

2 files changed

+20
-39
lines changed

‎tests/test_open_workbook.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
importtempfile
44
fromunittestimportTestCase
55

6-
fromxlrdimportopen_workbook
6+
importpytest
7+
8+
fromxlrdimportopen_workbook,XLRDError
79

810
from .helpersimportfrom_sample
911

1012

11-
classTestOpen(TestCase):
13+
classTestOpen(object):
1214
# test different uses of open_workbook
1315

1416
deftest_names_demo(self):
1517
# For now, we just check this doesn't raise an error.
16-
open_workbook(
17-
from_sample(from_sample('namesdemo.xls')),
18-
)
18+
open_workbook(from_sample('namesdemo.xls'))
1919

2020
deftest_ragged_rows_tidied_with_formatting(self):
2121
# For now, we just check this doesn't raise an error.
@@ -26,3 +26,11 @@ def test_BYTES_X00(self):
2626
# For now, we just check this doesn't raise an error.
2727
open_workbook(from_sample('picture_in_cell.xls'),
2828
formatting_info=True)
29+
30+
deftest_open_xlsx(self):
31+
withpytest.raises(XLRDError,match='Excel xlsx file; not supported'):
32+
open_workbook(from_sample('sample.xlsx'))
33+
34+
deftest_open_unknown(self):
35+
withpytest.raises(XLRDError,match="Unsupported format, or corrupt file"):
36+
open_workbook(from_sample('sample.txt'))

‎xlrd/__init__.py‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -150,41 +150,13 @@ def open_workbook(filename=None,
150150
:returns: An instance of the :class:`~xlrd.book.Book` class.
151151
"""
152152

153-
peeksz=4
154-
iffile_contents:
155-
peek=file_contents[:peeksz]
156-
else:
157-
filename=os.path.expanduser(filename)
158-
withopen(filename,"rb")asf:
159-
peek=f.read(peeksz)
160-
ifpeek==b"PK\x03\x04":# a ZIP file
161-
iffile_contents:
162-
zf=zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
163-
else:
164-
zf=zipfile.ZipFile(filename)
165-
166-
defconvert_filename(name):
167-
returnname.replace('\\','/').lower()
168-
169-
# Workaround for some third party files that use forward slashes and
170-
# lower case names. We map the expected name in lowercase to the
171-
# actual filename in the zip container.
172-
component_names=dict([(convert_filename(name),name)
173-
fornameinzf.namelist()])
153+
file_format=inspect_format(filename,file_contents)
154+
# We have to let unknown file formats pass through here, as some ancient
155+
# files that xlrd can parse don't start with the expected signature.
156+
iffile_formatandfile_format!='xls':
157+
raiseXLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
174158

175-
ifverbosity:
176-
logfile.write('ZIP component_names:\n')
177-
pprint.pprint(component_names,logfile)
178-
if'xl/workbook.xml'incomponent_names:
179-
raiseNotImplementedError('no xlsx!')
180-
if'xl/workbook.bin'incomponent_names:
181-
raiseXLRDError('Excel 2007 xlsb file; not supported')
182-
if'content.xml'incomponent_names:
183-
raiseXLRDError('Openoffice.org ODS file; not supported')
184-
raiseXLRDError('ZIP file contents not a known type of workbook')
185-
186-
from .importbook
187-
bk=book.open_workbook_xls(
159+
bk=open_workbook_xls(
188160
filename=filename,
189161
logfile=logfile,
190162
verbosity=verbosity,
@@ -196,6 +168,7 @@ def convert_filename(name):
196168
ragged_rows=ragged_rows,
197169
ignore_workbook_corruption=ignore_workbook_corruption,
198170
)
171+
199172
returnbk
200173

201174

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp