Skip to content

gh-57060: populate Py_buffer strides field for ctypes arrays. - #4714

Open
vanossj wants to merge 6 commits into
python:mainfrom
vanossj:fix-issue-12851
Open

gh-57060: populate Py_buffer strides field for ctypes arrays.#4714
vanossj wants to merge 6 commits into
python:mainfrom
vanossj:fix-issue-12851

Conversation

@vanossj

@vanossj vanossj commented Dec 5, 2017

Copy link
Copy Markdown

bpo-12851: populate Py_buffer strides field for ctypes arrays.

PEP 3118 specifies that if PyBUF_STIDES is set in the flags argument for PyCData_NewGetBuffer,

The returned buffer must provide strides information (i.e. the strides cannot be NULL).

However it also says that the strides field of Py_buffer:

...will be filled with a pointer to an array of Py_ssize_t of length ndims (or NULL if ndims is 0)

This patch follows the latter, setting strides for ctypes arrays (ndim>0) but not for other ctypes types

https://bugs.python.org/issue12851

@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@vanossj vanossj changed the title Fix issue 12851 bpo-12851: populate Py_buffer strides field for ctypes arrays. Dec 5, 2017
@vanossj

vanossj commented Apr 9, 2018

Copy link
Copy Markdown
Author

@skrah would you be willing to review these changes? Thanks.

@MaxwellDupre MaxwellDupre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran ok:
Ran 3 tests in 0.002s
OK

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 13, 2022
# Conflicts:
#	Lib/test/test_ctypes/test_pep3118.py
#	Modules/_ctypes/_ctypes.c
#	Modules/_ctypes/_ctypes_test.c
#	Modules/_ctypes/ctypes.h
#	Modules/_ctypes/stgdict.c
PyBUF_SIMPLE requests crashed on a multidimensional array: ndim > 1
implies shape != NULL (see PyBuffer_IsContiguous()).  The buffer is C
contiguous, so it can be exposed as flat.

Also, only a multidimensional array is not Fortran contiguous, and
BufferError is more appropriate than TypeError.

Clear the strides when the shape which they point into is freed.
_testbuffer.ndarray already exposes the raw Py_buffer, and it can also
request particular flags, which the buffer_info() helper could not.
@serhiy-storchaka serhiy-storchaka removed the stale Stale PR or inactive for long period of time. label Aug 11, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

I have merged the current main into this and made a few changes on top:

  • PyBUF_SIMPLE requests crashed on a multidimensional array — ndim > 1 implies shape != NULL, so it is now exposed as flat.
  • The tests use _testbuffer.ndarray instead of a new helper in _ctypes_test; it can also request particular flags.
  • The NEWS entry was renamed to the GitHub issue number.

Thank you for the patch, and sorry that it took so long.

@serhiy-storchaka serhiy-storchaka changed the title bpo-12851: populate Py_buffer strides field for ctypes arrays. gh-57060: populate Py_buffer strides field for ctypes arrays. Aug 12, 2026
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.

6 participants