Skip to content

Commit 9274f67

Browse files
committed
Add scipy array API check in init
1 parent d66309a commit 9274f67

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

drone_controllers/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@
66
broadcasting to support batching of arbitrary leading dimensions.
77
"""
88

9+
import os
10+
import sys
911
from typing import Callable
1012

13+
# SciPy array API check. We use the most recent array API features, which require the
14+
# SCIPY_ARRAY_API environment variable to be set to "1". This flag MUST be set before importing
15+
# scipy, because scipy's C extensions cannot be unloaded once they have been imported. Therefore, we
16+
# have to error out if the flag is not set. Otherwise, we immediately import scipy to ensure that no
17+
# other package sets the flag to a different value before importing scipy.
18+
19+
if "scipy" in sys.modules and os.environ.get("SCIPY_ARRAY_API") != "1":
20+
msg = """scipy has already been imported and the 'SCIPY_ARRAY_API' environment variable has not
21+
been set. Please restart your Python session and set SCIPY_ARRAY_API="1" before importing any
22+
packages that depend on scipy, or import this package first to automatically set the flag."""
23+
raise RuntimeError(msg)
24+
25+
os.environ["SCIPY_ARRAY_API"] = "1"
26+
import scipy # noqa: F401, ensure scipy uses array API features
27+
1128
from drone_controllers.core import parametrize
12-
from drone_controllers.mellinger import (
13-
attitude2force_torque as mellinger_attitude2force_torque,
14-
)
29+
from drone_controllers.mellinger import attitude2force_torque as mellinger_attitude2force_torque
1530
from drone_controllers.mellinger import state2attitude as mellinger_state2attitude
1631

1732
available_controller: dict[str, Callable] = {

0 commit comments

Comments
 (0)