Skip to content

Commit

Permalink
Deduplicate closing of file descriptor on filescanner.walk
Browse files Browse the repository at this point in the history
  • Loading branch information
c0llab0rat0r authored and ntninja committed Apr 14, 2021
1 parent 9de4852 commit 331bcb9
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions ipfshttpclient/filescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ def __init__(
intermediate_dirs
)

def _close_file_descriptor(self) -> None:
if self._close_fd is not None:
os.close(self._close_fd)
self._close_fd = None

def __iter__(self) -> 'walk[AnyStr]':
return self

Expand Down Expand Up @@ -634,9 +639,7 @@ def throw(self, typ: ty.Union[ty.Type[BaseException], BaseException],
assert val is None
return self._generator.throw(typ, val, tb)
except:
if self._close_fd is not None:
os.close(self._close_fd)
self._close_fd = None
self._close_file_descriptor()
raise

def close(self) -> None:
Expand Down Expand Up @@ -788,11 +791,7 @@ def _walk(
finally:
# Make sure the file descriptors bound by `os.fwalk` are freed on error
walk_iter.close()

# Close root file descriptor of `os.fwalk` as well
if self._close_fd is not None:
os.close(self._close_fd)
self._close_fd = None
self._close_file_descriptor()


if HAVE_FWALK: # pragma: no cover
Expand Down

0 comments on commit 331bcb9

Please sign in to comment.