Coverage for cuda / bindings / _internal / utils.pyx: 67.50%
80 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
5cimport cpython
6from libc.stdint cimport intptr_t
7from libcpp.utility cimport move
8from cython.operator cimport dereference as deref
11cdef extern from *:
12 """
13 #if defined(__clang__)
14 #define _COMPILER_VERSION ("Clang " __clang_version__)
15 #elif defined(__GNUC__) || defined(__GNUG__)
16 #define _COMPILER_VERSION ("GCC " __VERSION__)
17 #elif defined(_MSC_VER)
18 #define _COMPILER_VERSION ("MSVC " Py_STRINGIFY(_MSC_VER))
19 #else
20 #define _COMPILER_VERSION ("Unknown Compiler")
21 #endif
22 """
23 cdef char *_COMPILER_VERSION
26cpdef str get_c_compiler():
27 """
28 Returns a string describing the C compiler used to build cuda.bindings
29 """
30 return _COMPILER_VERSION.decode() 2eb
33cdef bint is_nested_sequence(data):
34 if not cpython.PySequence_Check(data): 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
35 return False
36 else:
37 for i in data: 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
38 if not cpython.PySequence_Check(i): 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
39 return False
40 else:
41 return True 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
44cdef void* get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except*:
45 """The caller must ensure ``buf`` is alive when the returned pointer is in use."""
46 cdef void* bufPtr
47 cdef int flags = cpython.PyBUF_ANY_CONTIGUOUS 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m cbdbn o p q ! # $ r s % t u abbb* D E + F G
48 if not readonly: 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m cbdbn o p q ! # $ r s % t u abbb* D E + F G
49 flags |= cpython.PyBUF_WRITABLE 2a H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb
50 cdef int status = -1 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m cbdbn o p q ! # $ r s % t u abbb* D E + F G
51 cdef cpython.Py_buffer view
53 if isinstance(buf, int): 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m cbdbn o p q ! # $ r s % t u abbb* D E + F G
54 bufPtr = <void*><intptr_t>buf
55 else: # try buffer protocol
56 try: 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m cbdbn o p q ! # $ r s % t u abbb* D E + F G
57 status = cpython.PyObject_GetBuffer(buf, &view, flags) 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m cbdbn o p q ! # $ r s % t u abbb* D E + F G
58 # when the caller does not provide a size, it is set to -1 at generate-time by cybind
59 if size != -1: 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb* D E + F G
60 assert view.len == size 1aHIJKLMNO'(PQRSTUVWXYZ0123456789vwxybcdefghijklmnopq!#$rs%tu*DE+FG
61 assert view.ndim == 1 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb* D E + F G
62 except Exception as e: 2cbdb
63 adj = "writable " if not readonly else "" 2cbdb
64 raise ValueError( 2cbdb
65 "buf must be either a Python int representing the pointer " 2cbdb
66 f"address to a valid buffer, or a 1D contiguous {adj}" 2cbdb
67 "buffer, of size bytes") from e 2cbdb
68 else:
69 bufPtr = view.buf 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb* D E + F G
70 finally:
71 if status == 0: 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb* D E + F G
72 cpython.PyBuffer_Release(&view) 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb* D E + F G
74 return bufPtr 2a H I J K L M N O ' ( P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ) v w x y z A B C b c d e f g h i j k l m n o p q ! # $ r s % t u abbb* D E + F G
77# Cython can't infer the ResT overload when it is wrapped in nullable_unique_ptr,
78# so we need a dummy (__unused) input argument to help it
79cdef int get_resource_ptr(nullable_unique_ptr[vector[ResT]] &in_out_ptr, object obj, ResT* __unused) except 1:
80 if cpython.PySequence_Check(obj):
81 vec = new vector[ResT](len(obj))
82 # set the ownership immediately to avoid leaking the `vec` memory in
83 # case of exception in the following loop
84 in_out_ptr.reset(vec, True)
85 for i in range(len(obj)):
86 deref(vec)[i] = obj[i]
87 else:
88 in_out_ptr.reset(<vector[ResT]*><intptr_t>obj, False)
89 return 0
92cdef int get_resource_ptrs(nullable_unique_ptr[ vector[PtrT*] ] &in_out_ptr, object obj, PtrT* __unused) except 1:
93 if cpython.PySequence_Check(obj):
94 vec = new vector[PtrT*](len(obj))
95 # set the ownership immediately to avoid leaking the `vec` memory in
96 # case of exception in the following loop
97 in_out_ptr.reset(vec, True)
98 for i in range(len(obj)):
99 deref(vec)[i] = <PtrT*><intptr_t>(obj[i])
100 else:
101 in_out_ptr.reset(<vector[PtrT*]*><intptr_t>obj, False)
102 return 0
105cdef int get_nested_resource_ptr(nested_resource[ResT] &in_out_ptr, object obj, ResT* __unused) except 1:
106 cdef nullable_unique_ptr[ vector[intptr_t] ] nested_ptr
107 cdef nullable_unique_ptr[ vector[vector[ResT]] ] nested_res_ptr
108 cdef vector[intptr_t]* nested_vec = NULL 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
109 cdef vector[vector[ResT]]* nested_res_vec = NULL 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
110 cdef size_t i = 0, length = 0 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
111 cdef intptr_t addr
113 if is_nested_sequence(obj): 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
114 length = len(obj) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
115 nested_res_vec = new vector[vector[ResT]](length) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
116 nested_vec = new vector[intptr_t](length) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
117 # set the ownership immediately to avoid leaking memory in case of
118 # exception in the following loop
119 nested_res_ptr.reset(nested_res_vec, True) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
120 nested_ptr.reset(nested_vec, True) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
121 for i, obj_i in enumerate(obj): 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
122 if ResT is char:
123 obj_i_bytes = (<str?>(obj_i)).encode() 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
124 str_len = <size_t>(len(obj_i_bytes)) + 1 # including null termination 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
125 deref(nested_res_vec)[i].resize(str_len) 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
126 obj_i_ptr = <char*>(obj_i_bytes) 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
127 # cast to size_t explicitly to work around a potentially Cython bug
128 deref(nested_res_vec)[i].assign(obj_i_ptr, obj_i_ptr + <size_t>str_len) 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
129 else:
130 deref(nested_res_vec)[i] = obj_i
131 deref(nested_vec)[i] = <intptr_t>(deref(nested_res_vec)[i].data()) 1a,vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopqrstuDEFG
132 elif cpython.PySequence_Check(obj):
133 length = len(obj)
134 nested_vec = new vector[intptr_t](length)
135 nested_ptr.reset(nested_vec, True)
136 for i, addr in enumerate(obj):
137 deref(nested_vec)[i] = addr
138 nested_res_ptr.reset(NULL, False)
139 else:
140 # obj is an int (ResT**)
141 nested_res_ptr.reset(NULL, False)
142 nested_ptr.reset(<vector[intptr_t]*><intptr_t>obj, False)
144 in_out_ptr.ptrs = move(nested_ptr) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
145 in_out_ptr.nested_resource_ptr = move(nested_res_ptr) 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
146 return 0 1aHIJKLMNO'(PQRSTUVWXYZ0123456789|),vwxy-./:;=?@[]^_zABCbcdefghijklm`{nopq!#}~$rs%tu*DE+FG
149class FunctionNotFoundError(RuntimeError): pass
151class NotSupportedError(RuntimeError): pass