From 418febef01b2d9c385620a2396623d55eff2eb6f Mon Sep 17 00:00:00 2001 From: Tim Stumbaugh Date: Mon, 4 Nov 2024 18:20:40 -0700 Subject: [PATCH] Add a non-const overload of bytearray::data (#771) 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 --- docs/api_core.rst | 4 ++++ include/nanobind/nb_types.h | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/api_core.rst b/docs/api_core.rst index 3ef27967..7cf15947 100644 --- a/docs/api_core.rst +++ b/docs/api_core.rst @@ -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. diff --git a/include/nanobind/nb_types.h b/include/nanobind/nb_types.h index d2645593..1a8c35e4 100644 --- a/include/nanobind/nb_types.h +++ b/include/nanobind/nb_types.h @@ -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); }