Skip to content

Commit

Permalink
fix(frontend): remove ResourceWarning from using TemporaryDirectory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fd0r authored and umut-sahin committed Apr 25, 2024
1 parent 16f0041 commit 5c5f573
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions frontends/concrete-python/concrete/fhe/compilation/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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")
Expand Down Expand Up @@ -388,7 +386,7 @@ def cleanup(self):
"""

if self._output_dir is not None:
self._output_dir.cleanup()
shutil.rmtree(Path(self._output_dir).resolve())

@property
def size_of_secret_keys(self) -> int:
Expand Down

0 comments on commit 5c5f573

Please sign in to comment.