Skip to content

Commit 8faa948

Browse files
committed
Fixup and factor out native import process
1 parent 86f92a0 commit 8faa948

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

hstrat/_auxiliary_lib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ._caretup_marker import caretup_marker
99
from ._div_range import div_range
1010
from ._find_bounds import find_bounds
11+
from ._hstrat_import_native import hstrat_import_native
1112
from ._is_nondecreasing import is_nondecreasing
1213
from ._is_nonincreasing import is_nonincreasing
1314
from ._launder_impl_modules import launder_impl_modules
@@ -25,6 +26,7 @@
2526
"caretup_marker",
2627
"div_range",
2728
"find_bounds",
29+
"hstrat_import_native",
2830
"is_nondecreasing",
2931
"is_nonincreasing",
3032
"launder_impl_modules",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from distutils.errors import CompileError
2+
import importlib
3+
import os
4+
import types
5+
import typing
6+
7+
8+
def hstrat_import_native(
9+
name: str,
10+
package: str,
11+
) -> typing.Optional[types.ModuleType]:
12+
"""Attempt importing a native hstrat module.
13+
14+
If the environment variable HSTRAT_CPPIMPORT_OPT_IN is not set,
15+
(potentially slow) module compilation will not be attempted. Use of native
16+
extensions will therefore require pre-existence of binaries.
17+
18+
If the environment variable HSTRAT_RERAISE_IMPORT_NATIVE_EXCEPTION is not
19+
set, a failed compilation or import will result in a return value of None.
20+
If it is set, any exceptions will be reaised prevent silent import failure.
21+
22+
The arguments 'name' and 'package are forwarded to importlib.import_module.
23+
The name argument specifies what module to import in absolute or relative
24+
terms (e.g. either pkg.mod or ..mod). If the name is specified in relative
25+
terms, then the package argument must be set to the name of the package
26+
which is to act as the anchor for resolving the package name (e.g.
27+
import_module('..mod', 'pkg.subpkg') will import pkg.mod).
28+
"""
29+
30+
try:
31+
if os.environ.get("HSTRAT_CPPIMPORT_OPT_IN", False):
32+
import cppimport.import_hook
33+
34+
return importlib.import_module(name, package)
35+
except (CompileError, ImportError, SystemExit) as e:
36+
if os.environ.get("HSTRAT_RERAISE_IMPORT_NATIVE_EXCEPTION", False):
37+
raise e
38+
else:
39+
return None
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
from distutils.errors import CompileError
1+
import opytional as opyt
22

3+
from ....._auxiliary_lib import hstrat_import_native
34
from ._PolicySpec import PolicySpec
45

56
impls = [
67
PolicySpec,
78
]
89

9-
try:
10-
import cppimport.import_hook
11-
12-
from ._PolicySpecNative import PolicySpecNative
13-
10+
PolicySpecNative = opyt.apply_if(
11+
hstrat_import_native("._PolicySpecNative", __name__),
12+
lambda x: x.PolicySpecNative,
13+
)
14+
if PolicySpecNative is not None:
1415
impls.append(PolicySpecNative)
15-
except (CompileError, ImportError, SystemExit):
16-
import os
17-
18-
os.environ["HSTRAT_NATIVE_ERROR"] = "1"
19-
pass
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from distutils.errors import CompileError
1+
import opytional as opyt
22

3+
from ......_auxiliary_lib import hstrat_import_native
34
from ._FromPredKeepRank import FromPredKeepRank
45
from ._GenDropRanks import GenDropRanks
56

@@ -8,14 +9,9 @@
89
GenDropRanks,
910
]
1011

11-
try:
12-
import cppimport.import_hook
13-
14-
from ._GenDropRanksNative import GenDropRanksNative
15-
12+
GenDropRanksNative = opyt.apply_if(
13+
hstrat_import_native("._GenDropRanksNative", __name__),
14+
lambda x: x.GenDropRanksNative,
15+
)
16+
if GenDropRanksNative is not None:
1617
impls.append(GenDropRanksNative)
17-
except (CompileError, ImportError, SystemExit):
18-
import os
19-
20-
os.environ["HSTRAT_NATIVE_ERROR"] = "1"
21-
pass

0 commit comments

Comments
 (0)