Skip to content

Commit 05d912c

Browse files
committed
fix: ensure that pin metadata files are closed after reading them in
1 parent 7ded069 commit 05d912c

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

pins/boards.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import logging
23
import tempfile
34
import shutil
@@ -160,13 +161,10 @@ def pin_meta(self, name, version: str = None) -> Meta:
160161
meta_name = self.meta_factory.get_meta_name(*components)
161162

162163
path_meta = self.construct_path([*components, meta_name])
163-
f, local = self._open_pin_meta(path_meta)
164-
165-
meta = self.meta_factory.read_pin_yaml(
166-
f, pin_name, selected_version, local=local
167-
)
168-
169-
return meta
164+
with self._open_pin_meta(path_meta) as (f, local):
165+
return self.meta_factory.read_pin_yaml(
166+
f, pin_name, selected_version, local=local
167+
)
170168

171169
def pin_list(self):
172170
"""List names of all pins in a board.
@@ -610,14 +608,18 @@ def _load_data(self, meta, pin_version_path):
610608

611609
# filesystem and cache methods --------------------------------------------
612610

611+
@contextlib.contextmanager
613612
def _open_pin_meta(self, path):
614613
f = self.fs.open(path)
615-
self._touch_cache(path)
614+
try:
615+
self._touch_cache(path)
616616

617-
# optional additional data to put in Meta.local
618-
local = {}
617+
# optional additional data to put in Meta.local
618+
local = {}
619619

620-
return f, local
620+
yield f, local
621+
finally:
622+
self.fs.close(f)
621623

622624
def _get_cache_path(self, pin_name, version=None, fname=None):
623625
version_part = [version] if version is not None else []
@@ -703,8 +705,8 @@ def pin_meta(self, name, version=None):
703705
# note that pins on this board should point to versions, so we use an
704706
# empty string to mark version (it ultimately is ignored)
705707
path_meta = self.construct_path([pin_name, "", meta_name])
706-
f, local = self._open_pin_meta(path_meta)
707-
meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local)
708+
with self._open_pin_meta(path_meta) as (f, local):
709+
meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local)
708710

709711
# TODO(#59,#83): handle caching, and then re-enable pin_read.
710712
# self._touch_cache(path_meta)

0 commit comments

Comments
 (0)