Coverage for cuda/bindings/utils/__init__.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-05 01:11 +0000

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

2# SPDX-License-Identifier: Apache-2.0 

3from collections.abc import Callable 

4from typing import Any 

5 

6from ._envvar import envvar_bool 

7from ._nvvm_utils import check_nvvm_compiler_options 

8from ._ptx_utils import get_minimal_required_cuda_ver_from_ptx_ver, get_ptx_ver 

9from ._version_check import warn_if_cuda_major_version_mismatch 

10 

11_handle_getters: dict[type, Callable[[Any], int]] = {} 

12 

13 

14def _add_cuda_native_handle_getter(t: type, getter: Callable[[Any], int]) -> None: 

15 _handle_getters[t] = getter 

16 

17 

18def get_cuda_native_handle(obj: Any) -> int: 

19 """Returns the address of the provided CUDA Python object as a Python int. 

20 

21 Parameters 

22 ---------- 

23 obj : Any 

24 CUDA Python object 

25 

26 Returns 

27 ------- 

28 int : The object address. 

29 """ 

30 obj_type = type(obj) 1fghijklmnopqrstuvwbcde

31 try: 1fghijklmnopqrstuvwbcde

32 getter = _handle_getters[obj_type] 1fghijklmnopqrstuvwbcde

33 except KeyError: 1bcde

34 raise TypeError("Unknown type: " + str(obj_type)) from None 1bcde

35 # Deliberately outside the try: a KeyError raised by the getter itself is a 

36 # bug in that getter, not an unregistered type. 

37 return getter(obj) 1fghijklmnopqrstuvw