diff --git a/src/docx/opc/phys_pkg.py b/src/docx/opc/phys_pkg.py index 5ec32237c..0a379f3f3 100644 --- a/src/docx/opc/phys_pkg.py +++ b/src/docx/opc/phys_pkg.py @@ -15,10 +15,14 @@ def __new__(cls, pkg_file): if isinstance(pkg_file, str): if os.path.isdir(pkg_file): reader_cls = _DirPkgReader + elif not os.path.isfile(pkg_file): + raise FileNotFoundError("No such file or directory: '%s'" % pkg_file) elif is_zipfile(pkg_file): reader_cls = _ZipPkgReader else: - raise PackageNotFoundError("Package not found at '%s'" % pkg_file) + raise PackageNotFoundError( + "Package at '%s' is not a ZIP archive" % pkg_file + ) else: # assume it's a stream and pass it to Zip reader to sort out reader_cls = _ZipPkgReader diff --git a/tests/opc/test_phys_pkg.py b/tests/opc/test_phys_pkg.py index 6de0d868b..239992cea 100644 --- a/tests/opc/test_phys_pkg.py +++ b/tests/opc/test_phys_pkg.py @@ -64,10 +64,17 @@ def dir_reader(self): class DescribePhysPkgReader: - def it_raises_when_pkg_path_is_not_a_package(self): - with pytest.raises(PackageNotFoundError): + def it_raises_when_pkg_path_does_not_exist(self): + with pytest.raises(FileNotFoundError): PhysPkgReader("foobar") + def it_raises_when_pkg_path_exists_but_is_not_a_zip(self, tmpdir): + not_a_pkg = str(tmpdir.join("not-a-package.docx")) + with open(not_a_pkg, "w") as f: + f.write("not a zip") + with pytest.raises(PackageNotFoundError): + PhysPkgReader(not_a_pkg) + class DescribeZipPkgReader: def it_is_used_by_PhysPkgReader_when_pkg_is_a_zip(self):