fix: restore ayanamsa state and keep tests hermetic
Independent Staging Quality Gate / validate (push) Successful in 21m34s
Independent Staging Quality Gate / publish (push) Successful in 2m1s

This commit is contained in:
Jesse_Chen
2026-09-03 23:22:15 +08:00
parent 027e4b0827
commit c1e3f32a0f
30 changed files with 227 additions and 53 deletions
+18 -1
View File
@@ -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)