Skip to content

Commit 331bcb9

Browse files
c0llab0rat0rntninja
authored andcommitted
Deduplicate closing of file descriptor on filescanner.walk
1 parent 9de4852 commit 331bcb9

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

ipfshttpclient/filescanner.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,11 @@ def __init__(
599599
intermediate_dirs
600600
)
601601

602+
def _close_file_descriptor(self) -> None:
603+
if self._close_fd is not None:
604+
os.close(self._close_fd)
605+
self._close_fd = None
606+
602607
def __iter__(self) -> 'walk[AnyStr]':
603608
return self
604609

@@ -634,9 +639,7 @@ def throw(self, typ: ty.Union[ty.Type[BaseException], BaseException],
634639
assert val is None
635640
return self._generator.throw(typ, val, tb)
636641
except:
637-
if self._close_fd is not None:
638-
os.close(self._close_fd)
639-
self._close_fd = None
642+
self._close_file_descriptor()
640643
raise
641644

642645
def close(self) -> None:
@@ -788,11 +791,7 @@ def _walk(
788791
finally:
789792
# Make sure the file descriptors bound by `os.fwalk` are freed on error
790793
walk_iter.close()
791-
792-
# Close root file descriptor of `os.fwalk` as well
793-
if self._close_fd is not None:
794-
os.close(self._close_fd)
795-
self._close_fd = None
794+
self._close_file_descriptor()
796795

797796

798797
if HAVE_FWALK: # pragma: no cover

0 commit comments

Comments
 (0)