Skip to content

Commit

Permalink
Merge pull request #47 from jaraco/feature/exceptions
Browse files Browse the repository at this point in the history
Add exceptions for open
  • Loading branch information
jaraco authored Mar 3, 2020
2 parents 5a06e14 + 200f0dd commit ec6ff0f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v3.1.0
======

#47: ``.open`` now raises ``FileNotFoundError`` and
``IsADirectoryError`` when appropriate.

v3.0.0
======

Expand Down
16 changes: 16 additions & 0 deletions test_zipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ def test_open_write(self):
with zf.joinpath('file.txt').open('w') as strm:
strm.write('text file')

def test_open_extant_directory(self):
"""
Attempting to open a directory raises IsADirectoryError.
"""
zf = zipp.Path(add_dirs(build_alpharep_fixture()))
with self.assertRaises(IsADirectoryError):
zf.joinpath('b').open()

def test_open_missing_directory(self):
"""
Attempting to open a missing directory raises FileNotFoundError.
"""
zf = zipp.Path(add_dirs(build_alpharep_fixture()))
with self.assertRaises(FileNotFoundError):
zf.joinpath('z').open()

def test_read(self):
for alpharep in self.zipfile_alpharep():
root = zipp.Path(alpharep)
Expand Down
4 changes: 4 additions & 0 deletions zipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
of ``pathlib.Path.open()`` by passing arguments through
to io.TextIOWrapper().
"""
if self.is_dir():
raise IsADirectoryError(self)
zip_mode = mode[0]
if not self.exists() and zip_mode == 'r':
raise FileNotFoundError(self)
stream = self.root.open(self.at, zip_mode, pwd=pwd)
if 'b' in mode:
if args or kwargs:
Expand Down

0 comments on commit ec6ff0f

Please sign in to comment.