Skip to content

Commit 6ccfeb0

Browse files
committed
Make namespace hygiene easier in cuda.core.__init__.py
1 parent d818a75 commit 6ccfeb0

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

cuda_core/cuda/core/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44

55
from cuda.core._version import __version__
66

7-
try:
7+
8+
def _import_versioned_module():
9+
import importlib
10+
811
from cuda import bindings
9-
except ImportError:
10-
raise ImportError("cuda.bindings 12.x or 13.x must be installed") from None
11-
else:
12-
cuda_major, cuda_minor = bindings.__version__.split(".")[:2]
12+
13+
cuda_major, _ = bindings.__version__.split(".")[:2]
1314
if cuda_major not in ("12", "13"):
1415
raise ImportError("cuda.bindings 12.x or 13.x must be installed")
1516

16-
import importlib
17+
subdir = f"cu{cuda_major}"
18+
try:
19+
versioned_mod = importlib.import_module(f".{subdir}", __package__)
20+
# Import all symbols from the module
21+
globals().update(versioned_mod.__dict__)
22+
except ImportError:
23+
# This is not a wheel build, but a conda or local build, do nothing
24+
pass
25+
26+
27+
_import_versioned_module()
28+
del _import_versioned_module
1729

18-
subdir = f"cu{cuda_major}"
19-
try:
20-
versioned_mod = importlib.import_module(f".{subdir}", __package__)
21-
# Import all symbols from the module
22-
globals().update(versioned_mod.__dict__)
23-
except ImportError:
24-
# This is not a wheel build, but a conda or local build, do nothing
25-
pass
26-
else:
27-
del versioned_mod
28-
finally:
29-
del bindings, importlib, subdir, cuda_major, cuda_minor
3030

3131
from cuda.core import system, utils
3232
from cuda.core._device import Device

0 commit comments

Comments
 (0)