From 90971f82cdf5f27dc4fd3b0613351c01992eafad Mon Sep 17 00:00:00 2001 From: Luis Montero <17201527+fd0r@users.noreply.github.com> Date: Tue, 23 Apr 2024 14:28:37 +0200 Subject: [PATCH] fix(frontend): remove `ResourceWarning` from using `TemporaryDirectory` We currently have something like `var = TemporaryDirectory` without explicitly calling the `close` method on `var` thus raising a `ResourceWarning` when the resource is implicitely cleaned-up. This could be avoided by using `mkdtemp` instead. --- .../concrete/fhe/compilation/server.py | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/frontends/concrete-python/concrete/fhe/compilation/server.py b/frontends/concrete-python/concrete/fhe/compilation/server.py index 5a13fdef97..7840efad11 100644 --- a/frontends/concrete-python/concrete/fhe/compilation/server.py +++ b/frontends/concrete-python/concrete/fhe/compilation/server.py @@ -56,7 +56,7 @@ class Server: client_specs: ClientSpecs is_simulated: bool - _output_dir: Optional[tempfile.TemporaryDirectory] + _output_dir: Union[None, str, Path] _support: LibrarySupport _compilation_result: LibraryCompilationResult _compilation_feedback: ProgramCompilationFeedback @@ -68,7 +68,7 @@ class Server: def __init__( self, client_specs: ClientSpecs, - output_dir: Optional[tempfile.TemporaryDirectory], + output_dir: Union[None, str, Path], support: LibrarySupport, compilation_result: LibraryCompilationResult, server_program: ServerProgram, @@ -189,10 +189,8 @@ def create( if configuration.compiler_verbose_mode: # pragma: no cover set_compiler_logging(True) - # pylint: disable=consider-using-with - output_dir = tempfile.TemporaryDirectory() - output_dir_path = Path(output_dir.name) - # pylint: enable=consider-using-with + output_dir = tempfile.mkdtemp() + output_dir_path = Path(output_dir) support = LibrarySupport.new( str(output_dir_path), generateCppHeader=False, generateStaticLib=False @@ -268,13 +266,13 @@ def save(self, path: Union[str, Path], via_mlir: bool = False): message = "Output directory must be provided" raise RuntimeError(message) - with open(Path(self._output_dir.name) / "client.specs.json", "wb") as f: + with open(Path(self._output_dir) / "client.specs.json", "wb") as f: f.write(self.client_specs.serialize()) - with open(Path(self._output_dir.name) / "is_simulated", "w", encoding="utf-8") as f: + with open(Path(self._output_dir) / "is_simulated", "w", encoding="utf-8") as f: f.write("1" if self.is_simulated else "0") - shutil.make_archive(path, "zip", self._output_dir.name) + shutil.make_archive(path, "zip", self._output_dir) @staticmethod def load(path: Union[str, Path]) -> "Server": @@ -291,8 +289,8 @@ def load(path: Union[str, Path]) -> "Server": """ # pylint: disable=consider-using-with - output_dir = tempfile.TemporaryDirectory() - output_dir_path = Path(output_dir.name) + output_dir = tempfile.mkdtemp() + output_dir_path = Path(output_dir) # pylint: enable=consider-using-with shutil.unpack_archive(path, str(output_dir_path), "zip") @@ -388,7 +386,7 @@ def cleanup(self): """ if self._output_dir is not None: - self._output_dir.cleanup() + Path(self._output_dir).unlink() @property def size_of_secret_keys(self) -> int: