Skip to content

Commit 56c0bd7

Browse files
committed
Give sensible exceptions when we know we can't open a file.
1 parent 2cabada commit 56c0bd7

File tree

2 files changed

+20
-39
lines changed

2 files changed

+20
-39
lines changed

tests/test_open_workbook.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import tempfile
44
from unittest import TestCase
55

6-
from xlrd import open_workbook
6+
import pytest
7+
8+
from xlrd import open_workbook, XLRDError
79

810
from .helpers import from_sample
911

1012

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

1416
def test_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
def test_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+
def test_open_xlsx(self):
31+
with pytest.raises(XLRDError, match='Excel xlsx file; not supported'):
32+
open_workbook(from_sample('sample.xlsx'))
33+
34+
def test_open_unknown(self):
35+
with pytest.raises(XLRDError, match="Unsupported format, or corrupt file"):
36+
open_workbook(from_sample('sample.txt'))

xlrd/__init__.py

+7-34
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-
if file_contents:
155-
peek = file_contents[:peeksz]
156-
else:
157-
filename = os.path.expanduser(filename)
158-
with open(filename, "rb") as f:
159-
peek = f.read(peeksz)
160-
if peek == b"PK\x03\x04": # a ZIP file
161-
if file_contents:
162-
zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
163-
else:
164-
zf = zipfile.ZipFile(filename)
165-
166-
def convert_filename(name):
167-
return name.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-
for name in zf.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+
if file_format and file_format != 'xls':
157+
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
174158

175-
if verbosity:
176-
logfile.write('ZIP component_names:\n')
177-
pprint.pprint(component_names, logfile)
178-
if 'xl/workbook.xml' in component_names:
179-
raise NotImplementedError('no xlsx!')
180-
if 'xl/workbook.bin' in component_names:
181-
raise XLRDError('Excel 2007 xlsb file; not supported')
182-
if 'content.xml' in component_names:
183-
raise XLRDError('Openoffice.org ODS file; not supported')
184-
raise XLRDError('ZIP file contents not a known type of workbook')
185-
186-
from . import book
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
return bk
200173

201174

0 commit comments

Comments
 (0)