8000 Don't confuse uintptr_t and Py_ssize_t. by anntzer · Pull Request #12569 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Don't confuse uintptr_t and Py_ssize_t. #12569

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 1 commit into from
Oct 22, 2018
Merged

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Oct 19, 2018

PR Summary

Should hopefully close #12567.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer added this to the v3.0.x milestone Oct 19, 2018
@jklymak jklymak added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Oct 19, 2018
int convert_voidptr(PyObject *obj, void *p)
{
*(void **)p = PyLong_AsVoidPtr(obj);
return !PyErr_Occurred();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can get a maybe get a speed improvement here by checking this only if *(void **)p == NULL

Copy link
Contributor Author
@anntzer anntzer Oct 21, 2018

Choose a reason for hiding this comment

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

I strongly doubt this would have any measurable effect on speed, so I'd rather just avoid a conditional path to keep things simple.

Copy link
Contributor
@eric-wieser eric-wieser Oct 21, 2018

Choose a reason for hiding this comment

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

PyErr_Occured calls PyThreadState_GET, which involves atomic operations. This is just like using the common pattern of ret == -1 && PyErr_Occurred() when converting integers. It may be unnecessary, but it does seem to be typical within CPython.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed it...

Copy link
Member

Choose a reason for hiding this comment

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

It's not actually clear to me from the docs that the result would necessarily be NULL when an error occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://docs.python.org/3/c-api/long.html#c.PyLong_AsVoidPtr
Returns NULL on error. Use PyErr_Occurred() to disambiguate.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I was looking at 3.5 docs which didn't seem to have that line.

{
void **val = (void **)p;
*val = PyLong_AsVoidPtr(obj);
return *val != NULL ? 1 : !PyErr_Occurred();
Copy link
Contributor
@eric-wieser eric-wieser Oct 21, 2018

Choose a reason for hiding this comment

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

nit: more readable as val == NULL && PyErr_Occurred() ? 0 : 1 or !(val == NULL && PyErr_Occurred())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's exactly why I wanted to leave it at the old version: I don't think this discussion really helps (and I think my version is more readable: if it's non-null, PyLong_AsVoidPtr succeeded and we don't need to check for an error, otherwise do check).
I could even write the whole thing as return ((void **)p = PyLong_AsVoidPtr(obj)) ? 1 : !PyErr_Occurred(); but that's just silly.

@tacaswell tacaswell merged commit e02f2c5 into matplotlib:master Oct 22, 2018
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Oct 22, 2018
@anntzer anntzer deleted the uintptr_t branch October 22, 2018 21:23
dstansby added a commit that referenced this pull request Oct 25, 2018
…569-on-v3.0.x

Backport PR #12569 on branch v3.0.x (Don't confuse uintptr_t and Py_ssize_t.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling pyplot.show() with TkAgg backend on x86 machine raises OverflowError.
5 participants
0