Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/docx/opc/phys_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions tests/opc/test_phys_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down