Skip to content

Commit a8dc5cb

Browse files
committed
use PyBuffer_FillInfo
1 parent e08fdd9 commit a8dc5cb

2 files changed

Lines changed: 6 additions & 15 deletions

File tree

python/rapidsmpf/rapidsmpf/memory/buffer.pxd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ cdef extern from "<rapidsmpf/memory/buffer.hpp>" namespace "rapidsmpf" nogil:
1313

1414
cdef cppclass cpp_Buffer "rapidsmpf::Buffer":
1515
size_t size
16-
const char* data() except +
16+
# data() actually returns const std::byte*, declared as const void* here
17+
# because const std::byte* -> const void* is an implicit C++ conversion.
18+
const void* data() except +
1719
MemoryType mem_type() noexcept
1820

1921

python/rapidsmpf/rapidsmpf/memory/buffer.pyx

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

4+
from cpython.buffer cimport PyBuffer_FillInfo
45
from cython.operator cimport dereference as deref
5-
from libc.stddef cimport size_t
66
from libcpp.utility cimport move
77

88

@@ -42,19 +42,8 @@ cdef class Buffer:
4242
raise TypeError(
4343
"buffer protocol is only supported for PINNED_HOST buffers"
4444
)
45-
cdef size_t nbytes = deref(self._handle).size
46-
cdef const char* ptr = <const char*>deref(self._handle).data()
47-
view.buf = <void*>ptr
48-
view.len = nbytes
49-
view.readonly = 0
50-
view.format = 'B'
51-
view.ndim = 1
52-
view.shape = &view.len
53-
view.strides = NULL
54-
view.suboffsets = NULL
55-
view.itemsize = 1
56-
view.internal = NULL
57-
view.obj = self
45+
cdef void* ptr = <void*><const void*>deref(self._handle).data()
46+
PyBuffer_FillInfo(view, self, ptr, deref(self._handle).size, False, flags)
5847

5948
def __releasebuffer__(self, Py_buffer* view):
6049
pass

0 commit comments

Comments
 (0)