Skip to content

Commit 4828262

Browse files
committed
Add __repr__ to core API classes and consolidate object protocol tests
- Add __repr__ methods to Stream, Event, Context, Buffer, ObjectCode, Kernel - Create test_object_protocols.py with comprehensive tests for: - Weak references (weakref.ref, WeakValueDict, WeakKeyDict, WeakSet) - Hash consistency, distinctness, and small value guards - Equality (reflexive, cross-type, same-type distinct objects) - Collection usage (dict, set, weak collections) - repr format validation with regex patterns - Remove test_hashable.py, test_comparable.py, test_weakref.py (consolidated)
1 parent c5d1417 commit 4828262

9 files changed

Lines changed: 338 additions & 526 deletions

File tree

cuda_core/cuda/core/_context.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -48,6 +48,9 @@ cdef class Context:
4848
def __hash__(self) -> int:
4949
return hash(as_intptr(self._h_context))
5050

51+
def __repr__(self) -> str:
52+
return f"Context(handle={as_intptr(self._h_context):#x}, device={self._device_id})"
53+
5154

5255
@dataclass
5356
class ContextOptions:

cuda_core/cuda/core/_event.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -178,6 +178,9 @@ cdef class Event:
178178
cdef Event _other = <Event>other
179179
return as_intptr(self._h_event) == as_intptr(_other._h_event)
180180

181+
def __repr__(self) -> str:
182+
return f"Event(handle={as_intptr(self._h_event):#x})"
183+
181184
def get_ipc_descriptor(self) -> IPCEventDescriptor:
182185
"""Export an event allocated for sharing between processes."""
183186
if self._ipc_descriptor is not None:

cuda_core/cuda/core/_memory/_buffer.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -334,6 +334,9 @@ cdef class Buffer:
334334
def __hash__(self) -> int:
335335
return hash((as_intptr(self._h_ptr), self._size))
336336

337+
def __repr__(self) -> str:
338+
return f"Buffer(ptr={as_intptr(self._h_ptr):#x}, size={self._size})"
339+
337340
@property
338341
def is_device_accessible(self) -> bool:
339342
"""Return True if this buffer can be accessed by the GPU, otherwise False."""

cuda_core/cuda/core/_module.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -536,6 +536,9 @@ def __eq__(self, other) -> bool:
536536
def __hash__(self) -> int:
537537
return hash(int(self._handle))
538538

539+
def __repr__(self) -> str:
540+
return f"Kernel(handle={int(self._handle):#x})"
541+
539542

540543
CodeTypeT = bytes | bytearray | str
541544

@@ -775,3 +778,7 @@ def __eq__(self, other) -> bool:
775778
def __hash__(self) -> int:
776779
# Trigger lazy load to get the handle
777780
return hash(int(self.handle))
781+
782+
def __repr__(self) -> str:
783+
# Trigger lazy load to get the handle
784+
return f"ObjectCode(handle={int(self.handle):#x}, code_type='{self._code_type}')"

cuda_core/cuda/core/_stream.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -221,6 +221,9 @@ cdef class Stream:
221221
return NotImplemented
222222
return as_intptr(self._h_stream) == as_intptr((<Stream>other)._h_stream)
223223

224+
def __repr__(self) -> str:
225+
return f"Stream(handle={as_intptr(self._h_stream):#x})"
226+
224227
@property
225228
def handle(self) -> cuda.bindings.driver.CUstream:
226229
"""Return the underlying ``CUstream`` object.

cuda_core/tests/test_comparable.py

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)