Skip to content

Introduce mpl buffer functions to replace def_buffer() - #32576

Open
iccir wants to merge 5 commits into
matplotlib:mainfrom
iccir:pybind-mplbuffer
Open

iccir wants to merge 5 commits into
matplotlib:mainfrom
iccir:pybind-mplbuffer

Conversation

@iccir

@iccir iccir commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

PR summary

Important

This PR depends on #32532 and will appear larger than it really is until #32532 is merged. To view the actual changes in this PR, compare pybind-mplarray-initial → pybind-mplbuffer.

This PR replaces our use of pybind11's buffer_protocol() and def_buffer() with our own functions. This is needed as nanobind does not provide a buffer protocol.

For now, these functions will live in "py_buffer.h". In a future PR, there will be a variant that uses nanobind API and lives in some variant of: "mplutils.h", "mpl_buffer.h", or "mplbuffer.h". Both variants can exist as we adopt nanobind.

Usage

Replace py::buffer_protocol() with py::custom_type_setup(mpl::buffer_setup<TheClass>).

The wrapped class needs to implement a create_buffer_info() function which returns a new mpl::buffer_info:

// FT2Image::create_buffer_info
auto *create_buffer_info() const
{
    using BufferInfo = mpl::buffer_info<uint8_t, mpl::shape<-1, -1>>;
    return new BufferInfo(m_buffer, m_height, m_width);
}

Use set_owner() on the BufferInfo if another PyObject owns the raw buffer:

// FT2Font::create_buffer_info
auto *create_buffer_info() const
{
    using BufferInfo = mpl::buffer_info<uint8_t, mpl::shape<-1, -1>>;
    auto result = new BufferInfo(image.data(), image.shape(0), image.shape(1));
    result->set_owner(image.pyarray().ptr());
    return result;
}

AI Disclosure

  • I use AI for web search due to search engines becoming less reliable.
  • I used AI to review py_buffer.h

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [/] Plotting related features are demonstrated in an example
  • [/] New features and API changes have release notes
  • [/] Documentation complies with general and docstring guidelines

@iccir iccir added this to nanobind Nov 5, 2026
@github-project-automation github-project-automation Bot moved this to Todo in nanobind Nov 5, 2026
@iccir iccir moved this from Todo to In Progress in nanobind Nov 5, 2026
@iccir iccir moved this from In Progress to Blocked in nanobind Nov 5, 2026
@iccir
iccir marked this pull request as ready for review November 5, 2026 19:46
@iccir iccir mentioned this pull request Nov 6, 2026
@iccir iccir moved this from Blocked to In Progress in nanobind Nov 8, 2026
@iccir iccir moved this from In Progress to Blocked in nanobind Nov 8, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Blocked

Development

Successfully merging this pull request may close these issues.

1 participant