Skip to content

Commit d17668b

Browse files
authored
Add __repr__ to core API classes and consolidate object protocol tests (#1538)
* 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) * Update __repr__ to use angle-bracket format for consistency Change repr format from ClassName(...) to <ClassName ...> with space-separated attributes for Stream, Event, Context, Buffer, ObjectCode, and Kernel. This aligns with Device's existing format. - Stream: add context handle to repr - Buffer: show is_mapped=True for IPC-imported buffers - Update test patterns to match new format - Add comments to hash distinctness tests explaining rationale * Add test for equality of objects created via from_handle Verify that Stream, Buffer, and Kernel objects created via from_handle compare equal (and have equal hashes) when wrapping the same underlying handle. This addresses the review comment about testing same-handle equality. * Remove test_hash_not_small and explicit assert messages Address review feedback: - Remove test_hash_not_small (not adding much value per cpcloud/rwgk) - Remove explicit assert messages to let pytest provide better output * Empty commit to retrigger CI
1 parent e5e30e8 commit d17668b

22 files changed

Lines changed: 365 additions & 539 deletions

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/_cpp/resource_handles.cpp

Lines changed: 1 addition & 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

cuda_core/cuda/core/_cpp/resource_handles.hpp

Lines changed: 1 addition & 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

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/_launch_config.pyx

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

cuda_core/cuda/core/_launcher.pyx

Lines changed: 1 addition & 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

cuda_core/cuda/core/_linker.py

Lines changed: 1 addition & 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

cuda_core/cuda/core/_memory/_buffer.pyx

Lines changed: 5 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,10 @@ 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+
maybe_is_mapped = " is_mapped=True" if self.is_mapped else ""
339+
return f"<Buffer ptr={as_intptr(self._h_ptr):#x} size={self._size}{maybe_is_mapped}>"
340+
337341
@property
338342
def is_device_accessible(self) -> bool:
339343
"""Return True if this buffer can be accessed by the GPU, otherwise False."""

cuda_core/cuda/core/_memory/_memory_pool.pxd

Lines changed: 1 addition & 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

cuda_core/cuda/core/_memory/_memory_pool.pyx

Lines changed: 1 addition & 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

0 commit comments

Comments
 (0)