Skip to content

Handle exceptions in numpy::array_view<...>::set(). #25850

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 3, 2023

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented May 10, 2023

set() can throw an exception; we must check for that to properly get that exception propagated to the python side; otherwise we get a SystemError ("method ... returned a result with an exception set"). Example repro:

from pylab import *
gca().add_collection(mpl.collections.LineCollection(rand(2, 2, 2), array=0))

(Here the C extension method receives a single tuple (rgba) color rather than an array of tuple (rgba) colors.)

I'd rather not explicitly test for the exception being raised (a ValueError) because if switching to pybind11 one naturally gets a broadcast of the scalar value into a correctly dimensionalized array; indeed mplcairo handles the above example just fine.

Also fix an unrelated (harmless) typo in _tkagg.cpp: use a logical or instead of a bitwise or.

PR summary

PR checklist

set() can throw an exception; we must check for that to properly get
that exception propagated to the python side; otherwise we get a
SystemError ("method ... returned a result with an exception set").
Example repro:
```
from pylab import *
gca().add_collection(mpl.collections.LineCollection(rand(2, 2, 2), array=0))
```
(Here the C extension method receives a single tuple (rgba) color rather
than an array of tuple (rgba) colors.)

I'd rather not explicitly test for the exception being raised (a
ValueError) because if switching to pybind11 one naturally gets a
broadcast of the scalar value into a correctly dimensionalized array;
indeed mplcairo handles the above example just fine.
@ksunden ksunden added this to the v3.7.2 milestone Jun 3, 2023
@ksunden ksunden merged commit f394597 into matplotlib:main Jun 3, 2023
@lumberbot-app
Copy link

lumberbot-app bot commented Jun 3, 2023

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.7.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 f394597f687acb0bc8bbbbab00f459e1bd48b28b
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #25850: Handle exceptions in numpy::array_view<...>::set().'
  1. Push to a named branch:
git push YOURFORK v3.7.x:auto-backport-of-pr-25850-on-v3.7.x
  1. Create a PR against branch v3.7.x, I would have named this PR:

"Backport PR #25850 on branch v3.7.x (Handle exceptions in numpy::array_view<...>::set().)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@ksunden
Copy link
Member

ksunden commented Jun 3, 2023

I'll argue it's not worth a manual backport, the code is significantly enough different.

Example conflict (4 very similar such diffs):

<<<<<<< HEAD

    points->set(obj);

    if (points->size() == 0) {
        return 1;
    }

    if (points->dim(1) != 2) {
        PyErr_Format(PyExc_ValueError,
                     "Points must be Nx2 array, got %" NPY_INTP_FMT "x%" NPY_INTP_FMT,
                     points->dim(0), points->dim(1));
=======
    if (!points->set(obj)
        || points->size() && !check_trailing_shape(*points, "points", 2)) {
>>>>>>> f394597f68 (Merge pull request #25850 from anntzer/exc)

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