8000 Fix uses of PyObject_IsTrue. by anntzer · Pull Request #14491 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix uses of PyObject_IsTrue. #14491

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
Jun 8, 2019
Merged

Fix uses of PyObject_IsTrue. #14491

merged 1 commit into from
Jun 8, 2019

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Jun 8, 2019

PyObject_IsTrue can return -1 if an exception occurred when trying to
convert a Python object to a bool (a typical case is for numpy arrays,
which cannot be converted to bools). Failure to handle this correctly
results e.g. in

l, = plt.plot([1, 2]); l.get_path().should_simplify = np.array([1, 2])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/matplotlib/backends/backend_qt5.py", line 501, in _draw_idle
<elided>
  File "/usr/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 146, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
SystemError: PyEval_EvalFrameEx returned a result with an error set

whereas after this PR, the ValueError gets correctly propagated out.

No tests, because one really needs to go out of their way to trigger the
faulty cases anyways...

PR Summary

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

PyObject_IsTrue can return -1 if an exception occurred when trying to
convert a Python object to a bool (a typical case is for numpy arrays,
which cannot be converted to bools).  Failure to handle this correctly
results e.g. in
```
l, = plt.plot([1, 2]); l.get_path().should_simplify = np.array([1, 2])
```
```
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/matplotlib/backends/backend_qt5.py", line 501, in _draw_idle
<elided>
  File "/usr/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 146, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
SystemError: PyEval_EvalFrameEx returned a result with an error set
```
whereas after this PR, the ValueError gets correctly propagated out.

No tests, because one really needs to go out of their way to trigger the
faulty cases anyways...
@WeatherGod
Copy link
Member

This should probably get backported?

@anntzer
Copy link
Contributor Author
anntzer commented Jun 8, 2019

Up to you :)

@WeatherGod WeatherGod modified the milestones: v2.2.5, v3.1.1 Jun 8, 2019
@WeatherGod WeatherGod merged commit ad2607b into 8000 matplotlib:master Jun 8, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jun 8, 2019
@anntzer anntzer deleted the istrue branch June 8, 2019 15:08
timhoffm added a commit that referenced this pull request Jun 8, 2019
…491-on-v3.1.x

Backport PR #14491 on branch v3.1.x (Fix uses of PyObject_IsTrue.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0