fix: restore ayanamsa state and keep tests hermetic
This commit is contained in:
@@ -10,7 +10,8 @@ comparison fixtures must pass an explicit `lahiri` name.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from contextlib import contextmanager
|
||||
from typing import Any, Iterator
|
||||
|
||||
try:
|
||||
import swisseph as swe
|
||||
@@ -91,6 +92,7 @@ def current_ayanamsa_name(args: Any = None) -> str:
|
||||
|
||||
def apply_ayanamsa(name: Any = None, swe_module: Any = None) -> bool:
|
||||
"""Set Swiss Ephemeris sidereal mode explicitly."""
|
||||
global ACTIVE_AYANAMSA_NAME
|
||||
module = swe_module or swe
|
||||
if module is None:
|
||||
return False
|
||||
@@ -99,9 +101,24 @@ def apply_ayanamsa(name: Any = None, swe_module: Any = None) -> bool:
|
||||
module.set_sid_mode(AYANAMSA_MODES[key])
|
||||
except Exception:
|
||||
return False
|
||||
ACTIVE_AYANAMSA_NAME = key
|
||||
return True
|
||||
|
||||
|
||||
@contextmanager
|
||||
def temporary_ayanamsa(name: Any = None, swe_module: Any = None) -> Iterator[str]:
|
||||
"""Apply a sidereal mode and restore the previous process-wide mode."""
|
||||
previous = ACTIVE_AYANAMSA_NAME
|
||||
target = normalize_ayanamsa_name(name)
|
||||
if not apply_ayanamsa(target, swe_module):
|
||||
raise RuntimeError(f"failed to apply ayanamsa {target}")
|
||||
try:
|
||||
yield target
|
||||
finally:
|
||||
if not apply_ayanamsa(previous, swe_module):
|
||||
raise RuntimeError(f"failed to restore ayanamsa {previous}")
|
||||
|
||||
|
||||
def sidereal_flags(swe_module: Any, ayanamsa_name: Any = None) -> int:
|
||||
apply_ayanamsa(ayanamsa_name, swe_module)
|
||||
return getattr(swe_module, 'FLG_SWIEPH', 2) | getattr(swe_module, 'FLG_SIDEREAL', 65536)
|
||||
|
||||
Reference in New Issue
Block a user