cpython icon indicating copy to clipboard operation
cpython copied to clipboard

bpo-32782: PEP3118 itemsize of an empty ctypes array should not be 0

Open eric-wieser opened this issue 7 years ago • 24 comments

The itemsize should be computed from the item type, not by dividing the total size by the length and assuming that the length is not zero.

https://bugs.python.org/issue32782

(fixes #76963)

eric-wieser avatar Feb 07 '18 07:02 eric-wieser

@pv: Since you've made PEP3118 patches to cpython before, would you mind reviewing this?

eric-wieser avatar Mar 27 '18 23:03 eric-wieser

LGTM

the test failures do not seem related to this pull request

mattip avatar May 20 '18 07:05 mattip

@mattip: Can you approve via github so that this goes through the process automation that's set up?

eric-wieser avatar May 21 '18 03:05 eric-wieser

Thanks @mattip. If you've more ctypes appetite left, there's also #5561

eric-wieser avatar May 21 '18 03:05 eric-wieser

Re-opening to restart the tests

eric-wieser avatar Aug 08 '18 05:08 eric-wieser

Tests for empty arrays of floats and doubles are added.

My naive question is this: The PEP suggests that itemsize should cache the return from PyBuffer_SizeFromFormat. Was that function actual added? Can it be used?

terryjreedy avatar Apr 14 '19 20:04 terryjreedy

I can't find an reference to PyBuffer_SizeFromFormat in the source code other than a prototype, and the docs say it's unimplemented.

At any rate, ctypes knows the size without having to resort to string parsing

eric-wieser avatar Apr 14 '19 21:04 eric-wieser

@terryjreedy No, PyBuffer_SizeFromFormat() has never been added. struct.calcsize() works for simple formats but cannot handle records.

To implement that function properly for nested types, you need some sort of a type tree.

skrah avatar Apr 15 '19 08:04 skrah

@eric-wieser, please respond to @skrah's review. Thanks!

csabella avatar May 13 '19 12:05 csabella

@skrah: Updated the existing commit to fix PEP7, and made a follow-up to use the approach you suggested. It's been over a year since I authored this, so I no longer remember how to run the tests on my setup - I'll leave that to CI.

eric-wieser avatar Jun 06 '19 04:06 eric-wieser

Ci fails on the second commit - an assertion error checking that stgdict.itemsize was populated by the time get_buffer is called.

There are way too many places that construct stgdict objects fieldwise, and while I tried to find all of them to populate the itemsize member. I seems to have missed some. At any rate, storing information in two places seems like a recipe for it to become out of sync, and I don't think we care about whatever marginal speed boost caching gives here - memoryviews are probably used mostly on large buffers, and the overhead of my original approach scales with number of dimensions (rarely more than 3) not array size.

@skrah, are you happy with the patch of the first commit by itself?

eric-wieser avatar Jun 06 '19 08:06 eric-wieser

@eric-wieser Thanks for trying out the other approach, if it's too much work I agree that we should not spend too much time on it. The principle of f4e3ca9 looks good to me, I haven't really reviewed it yet though.

skrah avatar Jun 06 '19 16:06 skrah

I'm at a loss here. Tests pass on my machine (WSL on windows 10), but fail on CI with:

Re-running test_ctypes in verbose mode
Fatal Python error: Floating point exception

I'm miles away from floating point here, so I have no clue what is happening.

Edit: https://github.com/python/cpython/pull/12660/files#r291465946 introduced a new bug in the year since I submitted this.

eric-wieser avatar Jun 07 '19 06:06 eric-wieser

Restart tests now that #13881 is in

eric-wieser avatar Jun 08 '19 02:06 eric-wieser

@skrah: All passing - although probably best to backport #13881 to 2.7 first so that this passes when backported

eric-wieser avatar Jun 08 '19 04:06 eric-wieser

Worth tagging this as wanting a backport to 2.7 as well?

eric-wieser avatar Jun 08 '19 21:06 eric-wieser

@skrah: Mind taking another look?

eric-wieser avatar Jun 23 '19 23:06 eric-wieser

@csabella, do you have suggestions for finding a core reviewer with time to review this?

eric-wieser avatar Apr 22 '20 16:04 eric-wieser

I have not looked at this closely, but it seems like @skrah has reviewed it, and this has been looked at by a few people involved in heavier buffer protocol users (i.e. NumPy). Can we somehow help moving issues like this ahead, by reviewing it, or is this hinging too much at specific python core devs? In the back of my mind, the buffer protocol came as much from NumPy as from Python itself, which maybe adds to making it difficult to push these type of issues over the finish line?

seberg avatar Jul 17 '20 16:07 seberg

I've only reviewed the principle. The buffer protocol is maintained, it is ctypes that hardly anyone wants to touch.

The buffer protocol part in the abstract looks correct, I don't have time or inclination for a deep ctypes review.

skrah avatar Jul 17 '20 16:07 skrah

This missed the boat for inclusion in Python 3.9 which accepts security fixes only as of today.

ambv avatar May 17 '22 16:05 ambv

Is there anything I can do to help prevent this forever being pushed to a later release?

eric-wieser avatar May 17 '22 21:05 eric-wieser

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

github-actions[bot] avatar Aug 14 '22 00:08 github-actions[bot]

The CI failure here looks completely unrelated to the change; is there anything I can do about it?

eric-wieser avatar Aug 14 '22 07:08 eric-wieser

Deploy Preview for python-cpython-preview canceled.

Name Link
Latest commit accc969bef59e488ca0ae12d4df855ba9700df7c
Latest deploy log https://app.netlify.com/sites/python-cpython-preview/deploys/639bb2ef300d190009b736b6

netlify[bot] avatar Dec 15 '22 01:12 netlify[bot]

@mdickinson, I see you recently reviewed some ctypes PRs; would you mind taking a quick look at this one?

eric-wieser avatar Dec 15 '22 01:12 eric-wieser

@eric-wieser Will do. It might take me a few days to get to it.

mdickinson avatar Dec 15 '22 19:12 mdickinson

:robot: New build scheduled with the buildbot fleet by @mdickinson for commit accc969bef59e488ca0ae12d4df855ba9700df7c :robot:

If you want to schedule another build, you need to add the :hammer: test-with-refleak-buildbots label again.

bedevere-bot avatar Dec 20 '22 11:12 bedevere-bot

Thanks for testing!

Given this file has 52 asserts (11 on std_dict) and no Py_FatalErrors, I'd be inclined to declare that change out of scope.

Is that ok?

eric-wieser avatar Dec 22 '22 15:12 eric-wieser

~~Oops, just realized that I probably clobbered the buildbot status by merging main; github was giving me a red cross so I clicked the update button without looking carefully enough...~~

Edit: nevermind, results are here

eric-wieser avatar Dec 22 '22 15:12 eric-wieser