Skip to content

Commit

Permalink
SandboxRepositoryBackend: move imports out of destructor (#5316)
Browse files Browse the repository at this point in the history
The import `from aiida.common.folders import SandboxFolder` was added to
multiple functions and is added to the top of the module. This is not
only best-practice in terms of reduction of code deduplication, in the
case of its use in the `__del__` destructor, it also fixes a bug.

When the destructor of the class is called, the interpreter may already
have cleaned up certain things, for example parts of the `sys` module.
This will make importing fail and one is greated with the exception:

    ImportError: sys.meta_path is None, Python is likely shutting down

By moving the import to the top of the module, this is no longer the
case.
  • Loading branch information
sphuber authored Jan 21, 2022
1 parent 472bff5 commit fe1acf9
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions aiida/repository/backend/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import BinaryIO, Iterable, Iterator, List, Optional, Tuple
import uuid

from aiida.common.folders import SandboxFolder

from .abstract import AbstractRepositoryBackend

__all__ = ('SandboxRepositoryBackend',)
Expand All @@ -15,7 +17,6 @@ class SandboxRepositoryBackend(AbstractRepositoryBackend):
"""Implementation of the ``AbstractRepositoryBackend`` using a sandbox folder on disk as the backend."""

def __init__(self):
from aiida.common.folders import SandboxFolder
self._sandbox: Optional[SandboxFolder] = None

def __str__(self) -> str:
Expand Down Expand Up @@ -51,14 +52,11 @@ def initialise(self, **kwargs) -> None:
@property
def is_initialised(self) -> bool:
"""Return whether the repository has been initialised."""
from aiida.common.folders import SandboxFolder
return isinstance(self._sandbox, SandboxFolder)

@property
def sandbox(self):
"""Return the sandbox instance of this repository."""
from aiida.common.folders import SandboxFolder

if self._sandbox is None:
self._sandbox = SandboxFolder()

Expand Down

0 comments on commit fe1acf9

Please sign in to comment.