diff --git a/mathstream/__init__.py b/mathstream/__init__.py index 8814edf..5395486 100644 --- a/mathstream/__init__.py +++ b/mathstream/__init__.py @@ -1,6 +1,12 @@ from .engine import clear_logs, add, sub, mul, div, mod, pow, is_even, is_odd from .exceptions import MathStreamError, DivideByZeroError -from .number import StreamNumber, free_stream, active_streams +from .number import ( + StreamNumber, + free_stream, + active_streams, + set_manual_free_only, + manual_free_only_enabled, +) from .utils import collect_garbage, tracked_files __all__ = [ @@ -18,6 +24,8 @@ __all__ = [ "StreamNumber", "free_stream", "active_streams", + "set_manual_free_only", + "manual_free_only_enabled", "MathStreamError", "DivideByZeroError", ] diff --git a/mathstream/number.py b/mathstream/number.py index 1cad0bd..96a299a 100644 --- a/mathstream/number.py +++ b/mathstream/number.py @@ -12,6 +12,7 @@ from .utils import ( ) LOG_DIR = Path("./instance/log") +_MANUAL_FREE_ONLY = False def _ensure_log_dir() -> None: @@ -138,6 +139,8 @@ def _decrement_active(path: Path, delete_file: bool = True) -> None: def _finalize_instance(path_str: str) -> None: + if _MANUAL_FREE_ONLY: + return _decrement_active(Path(path_str)) @@ -149,3 +152,14 @@ def free_stream(number: StreamNumber, *, delete_file: bool = True) -> None: def active_streams() -> Dict[str, int]: """Return the active StreamNumber paths mapped to in-memory reference counts.""" return dict(_ACTIVE_COUNTER) + + +def set_manual_free_only(enabled: bool) -> None: + """Toggle whether garbage collection happens only via explicit .free() calls.""" + global _MANUAL_FREE_ONLY + _MANUAL_FREE_ONLY = bool(enabled) + + +def manual_free_only_enabled() -> bool: + """Return the current manual-free-only toggle.""" + return _MANUAL_FREE_ONLY