Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Work around past and present PEP3118 issues in ctypes #11277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2018

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Jun 8, 2018

Fixes gh-10528
Fixes gh-10978
Fixes gh-11150

It seems we already have some ctype -> dtype code that we can fall back on in the frequent cases where ctypes give the wrong pep3118 buffer.


This is kind of unfortunate - I was hoping to implement ctype -> dtype in terms of the PEP3118 buffer code (#10533 (comment)), but it seems that we have to fall back on the reverse here

@eric-wieser eric-wieser added this to the 1.15.0 release milestone Jun 8, 2018
@eric-wieser eric-wieser force-pushed the fix-older-ctypes-compat branch 2 times, most recently from 3f2c622 to d86c1ad Compare June 8, 2018 07:37
@eric-wieser eric-wieser changed the title TST: Add a test for gh-11150 BUG: Work around past and present PEP3118 issues in ctypes Jun 8, 2018
@eric-wieser eric-wieser force-pushed the fix-older-ctypes-compat branch 3 times, most recently from 0457b33 to ae4b7af Compare June 8, 2018 08:59
# determine if an object comes from ctypes, in order to work around
# a bug in the buffer protocol for those objects, bpo-10746
try:
return 'ctypes' in type(obj).__mro__[-2].__module__
Copy link
Member

Choose a reason for hiding this comment

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

__mro__ is only available for new style classes, might be a problem in Python 2.7.

Copy link
Member

Choose a reason for hiding this comment

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

Although I suspect anything that inherits from ctypes is going to be a new style class, so this should be OK. Maybe a note?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, it seemed that ctypes classes are always new-style

try:
return 'ctypes' in type(obj).__mro__[-2].__module__
except Exception:
return False
Copy link
Member

Choose a reason for hiding this comment

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

File needs newline at end.

}
}
else {
descr = PyArray_DescrNewFromType(NPY_STRING);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe note on string assumption.

@@ -1315,27 +1356,73 @@ _array_from_buffer_3118(PyObject *memoryview)
npy_intp shape[NPY_MAXDIMS], strides[NPY_MAXDIMS];

view = PyMemoryView_GET_BUFFER(memoryview);
Copy link
Member

@charris charris Jun 8, 2018

Choose a reason for hiding this comment

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

Is it safe to assume that memoryview is actually a memoryview?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, because this function is only ever called with one, I think

if (descr == NULL) {
return NULL;
}
if (descr->elsize != view->len) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused here as view->len should be the total size of the memory. What am I missing?

Copy link
Member Author

Choose a reason for hiding this comment

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

We're creating an 0d array below with one (compound) element - so the dtype should span the total size of the memory

Copy link
Member

Choose a reason for hiding this comment

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

A comment somewhere would help, as that is a bit unexpected. At least, I didn't expect it :)

@charris
Copy link
Member

charris commented Jun 8, 2018

Couple of nits and a question.

@eric-wieser eric-wieser force-pushed the fix-older-ctypes-compat branch from ae4b7af to e85efc7 Compare June 8, 2018 18:25
…ype(ctype_obj))

Fixes numpygh-10528
Fixes numpygh-10978
Fixes numpygh-11150

A warning is thrown when ctypes misbehaves, in order to not hide issues that need fixing upstream
@eric-wieser eric-wieser force-pushed the fix-older-ctypes-compat branch from e85efc7 to 1e5df66 Compare June 8, 2018 18:27
@eric-wieser
Copy link
Member Author

Comments improved

@charris charris merged commit 71650e3 into numpy:master Jun 8, 2018
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.

2 participants