summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-20 21:29:06 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-20 21:29:06 +0100
commitaf24bdd0e8b67d2a0eaf053c7bf69a5b7c37639c (patch)
treef65b4adff394b89d90ae59a1f4194386b52f6674
parent329e9277bd77b3bed671bc03778eef3e857e03d6 (diff)
Continue after any IOError while attempting to read image
-rw-r--r--mammoth/docx/files.py10
-rw-r--r--tests/mammoth_tests.py6
2 files changed, 10 insertions, 6 deletions
diff --git a/mammoth/docx/files.py b/mammoth/docx/files.py
index ce557f6..bde488d 100644
--- a/mammoth/docx/files.py
+++ b/mammoth/docx/files.py
@@ -22,10 +22,12 @@ class Files(object):
if _is_absolute(uri):
return contextlib.closing(urlopen(uri))
elif self._base is not None:
- path = os.path.join(self._base, uri)
- if not os.path.exists(path):
- raise InvalidFileReferenceError("could not find external image '{0}' from document directory '{1}'".format(uri, self._base))
- return open(path, "rb")
+ try:
+ return open(os.path.join(self._base, uri), "rb")
+ except IOError as error:
+ message = "could not open external image '{0}' from document directory '{1}'\n{2}".format(
+ uri, self._base, str(error))
+ raise InvalidFileReferenceError(message)
else:
raise InvalidFileReferenceError("could not find external image '{0}', fileobj has no name".format(uri))
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index aa70c1c..f29ae0f 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -96,8 +96,10 @@ def warn_if_images_stored_outside_of_document_are_not_found():
with open(document_path, "rb") as fileobj:
result = mammoth.convert_to_html(fileobj=fileobj)
assert_equal("", result.value)
- expected_warning = "could not find external image 'tiny-picture.png' from document directory '{0}'".format(temp_dir.path)
- assert_equal([results.warning(expected_warning)], result.messages)
+ expected_warning = "could not open external image 'tiny-picture.png' from document directory '{0}'\n[Errno 2] No such file or directory".format(temp_dir.path)
+ assert_equal("warning", result.messages[0].type)
+ assert result.messages[0].message.startswith(expected_warning), "message was: " + result.messages[0].message
+ assert_equal(1, len(result.messages))
@istest