Skip to content

Commit

Permalink
Add a non-const overload of bytearray::data
Browse files Browse the repository at this point in the history
Unlike `bytes`, a `bytearray` is mutable. You are allowed to write to
it, and the underlying API function returns a `char*`

This adds an additional overload for the non-const version when called
on a non-const this
  • Loading branch information
tjstum committed Nov 4, 2024
1 parent 3528abf commit 7195850
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/api_core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,10 @@ Wrapper classes

Return the size in bytes.

.. cpp:function:: void * data()

Convert a Python ``bytearray`` object into a byte buffer of length :cpp:func:`bytearray::size()` bytes.

.. cpp:function:: const void * data() const

Convert a Python ``bytearray`` object into a byte buffer of length :cpp:func:`bytearray::size()` bytes.
Expand Down
3 changes: 2 additions & 1 deletion include/nanobind/nb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ class bytearray : public object {

const char *c_str() const { return PyByteArray_AsString(m_ptr); }

const void *data() const { return (const void *) PyByteArray_AsString(m_ptr); }
const void *data() const { return PyByteArray_AsString(m_ptr); }
void *data() { return PyByteArray_AsString(m_ptr); }

size_t size() const { return (size_t) PyByteArray_Size(m_ptr); }

Expand Down

0 comments on commit 7195850

Please sign in to comment.