Coverage for cuda / pathfinder / _optional_cuda_import.py: 100.00%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-08 01:07 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# SPDX-License-Identifier: Apache-2.0 

3 

4from __future__ import annotations 

5 

6import importlib 

7from collections.abc import Callable 

8from types import ModuleType 

9 

10from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError 

11 

12 

13def optional_cuda_import( 

14 fully_qualified_modname: str, 

15 *, 

16 probe_function: Callable[[ModuleType], object] | None = None, 

17) -> ModuleType | None: 

18 """Import an optional CUDA module without masking unrelated import bugs. 

19 

20 Returns: 

21 The imported module if available and the optional probe succeeds, 

22 otherwise ``None`` when the requested module is unavailable. 

23 

24 Raises: 

25 ModuleNotFoundError: If the import fails because a dependency of the 

26 target module is missing (instead of the target module itself). 

27 Exception: Any exception raised by ``probe_function`` except 

28 :class:`DynamicLibNotFoundError`, which is treated as "unavailable". 

29 """ 

30 try: 1adcfeb

31 module = importlib.import_module(fully_qualified_modname) 1adcfeb

32 except ModuleNotFoundError as err: 1de

33 if err.name != fully_qualified_modname: 1de

34 raise 1d

35 return None 1e

36 

37 if probe_function is not None: 1acfb

38 try: 1acb

39 probe_function(module) 1acb

40 except DynamicLibNotFoundError: 1cb

41 return None 1b

42 

43 return module 1af