Skip to content

ENH: Removed remnants of Python 2 buffer protocol from NumPy internals - #31176

Open
Anushreebasics wants to merge 2 commits into
numpy:mainfrom
Anushreebasics:internals
Open

Anushreebasics wants to merge 2 commits into
numpy:mainfrom
Anushreebasics:internals

Conversation

@Anushreebasics

Copy link
Copy Markdown

fixes #30813

First time committer introduction

Hi NumPy team, I’m a first-time contributor. I use NumPy regularly for array-heavy Python work, mainly for data processing and numerical experimentation. While working with NumPy internals/C-API docs, I noticed issue #30813 around legacy Python 2 buffer-protocol language and wanted to help modernize and clarify that path. This contribution is my attempt to reduce confusion for future contributors and keep the API/docs aligned with current behavior.

AI Disclosure

no AI tools were used

@Anushreebasics

Copy link
Copy Markdown
Author

Summary

It makes three main changes:

  • Adds a [DeprecationWarning] when [PyArray_BufferConverter] is used.

  • Moves internal ndarray construction to a private [_PyArray_Chunk] typedef so internal code no longer depends on the public alias.

  • Updates the C-API reference docs for [PyArray_BufferConverter] and [PyArray_Chunk] to describe the modern buffer-protocol behavior instead of the old single-segment/Python 2 framing.

@ngoldbaum @jorenham please review

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments. FWIW it's OK if you used AI for PRs like this, you just need to disclose it.

npy_intp len;
int flags;
} _PyArray_Chunk;
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this indirection if you're not going to deprecate using the struct or change it? I'd just keep it around and use the public version internally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also figure out a way to signal to users that this struct is deprecated. In that case it makes sense to add the indirection.

PyArray_BufferConverter(PyObject *obj, _PyArray_Chunk *buf)
{
if (DEPRECATE("PyArray_BufferConverter is deprecated and will be removed in a future version. Use the standard Python buffer protocol instead.") < 0) {
return NPY_FAIL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a test for this deprecation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not looking earlier, if you are up for an iteration.

It would be good to add a (Deprecated NumPy 2.5) as well as a /* Deprecated NumPy 2.5, 2026-04 */ style comment to this (breaking the long line would be nice also).

The deprecation test-case is nice. And I don't mind keeping it where it is, although it would be nice to cover the error path by making sure DeprecationWarning is raised an error.
(That is part of why we have a test_deprecated, moving the test there may be the nicest solution.)

Either way, all of these are indeed more nits and we could probably merge without as well.

@Anushreebasics

Copy link
Copy Markdown
Author

@ngoldbaum please review the changes

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I marked it for the 2.5 release. Before this can be merged it needs a release note though. See doc/release/upcoming_changes/README.rst for instructions.

@Anushreebasics

Copy link
Copy Markdown
Author

@ngoldbaum I have added the release notes, please review the changes

@ngoldbaum

Copy link
Copy Markdown
Member

I think this is OK to merge. Maybe @seberg could take a look and hit the merge button though?

@charris charris modified the milestones: 2.5.0 Release, 2.6.0 Release May 24, 2026
@ngoldbaum

Copy link
Copy Markdown
Member

@Anushreebasics sorry for dropping this - it looks like there's a merge conflict now. Would you mind fixing that?

@seberg

seberg commented May 29, 2026

Copy link
Copy Markdown
Member

I think I commented on a resolved convo, so it got lost. But it would be good to test add /* Deprecated NumPy 2.6, 2026-05 */ or so (and maybe also (Deprecated NumPy 2.6) in the message as well as breaking the long line.
It would also be nice for the test to exercise the error path, but it's simple enough.

Either way, both are smaller things if Nathan prefers to just go ahead that is fine with me, but if there is a conflict to resolve...

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

None yet

Development

Successfully merging this pull request may close these issues.

ENH: Remove remnants of Python 2 buffer protocol from NumPy internals

4 participants