Skip to content

ENH: Allow broadcast to be called with zero arguments #13544

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
May 16, 2019

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented May 13, 2019

Follows on from gh-6905 which reduced the limit from 2 to 1. Let's go all the way to zero.

Just as for broadcast(broadcast(a), b) is interpreted as broadcast(a, b) , this change interprets
broadcast(broadcast(), a) as broadcast(a).

Also updates the test to prove that broadcast is allowed to be called on a single argument.

Follows on from numpygh-6905 which reduced the limit from 2 to 1. Let's go all the way to zero.

Just as for `broadcast(broadcast(a), b)` is interpreted as `broadcast(a, b)` , this change interprets
`broadcast(broadcast(), a)` as `broadcast(a)`.
@@ -1262,10 +1262,14 @@ PyArray_MultiIterFromObjects(PyObject **mps, int n, int nadd, ...)
int i, ntot, err=0;

ntot = n + nadd;
if (ntot < 1 || ntot > NPY_MAXARGS) {
if (ntot < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How does one hit the code path where ntot < 0? If there's a concise / practical way to do that, coverage shows this is never true so a test would be useful.

Conversely, if this can't be hit by any practical code path by calling NumPy public API, is the guard really needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the guard is needed because of C API exposure.

"n and nadd arguments must be non-negative", NPY_MAXARGS);
return NULL;
}
if (ntot > NPY_MAXARGS) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is never true in the coverage report either, though I suppose a unit test here might be slow if that max value is big.

Copy link
Member Author

@eric-wieser eric-wieser May 13, 2019

Choose a reason for hiding this comment

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

I think this entire function is probably untested, like most parts of the c API.

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

Looks like a nice cleanup. If you have time to comment please do. But it looks good, so may just merge in a bit.

PyErr_Format(PyExc_ValueError,
"Need at least 1 and at most %d "
"array objects.", NPY_MAXARGS);
"n and nadd arguments must be non-negative", NPY_MAXARGS);
Copy link
Member

Choose a reason for hiding this comment

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

Strange message, but not even sure how that should get floated to a user...

Copy link
Member Author

Choose a reason for hiding this comment

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

This is part of the c API, so python users will never see it.

if (PyErr_Occurred()) {
return NULL;
}
if (PyErr_Occurred()) {
Copy link
Member

Choose a reason for hiding this comment

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

OK, do you see why this is there? I do not see how there can possibly an error set at this stage. But... It was there before, so I guess we can keep it (and moving it a bit doesn't matter much).

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, isinstance can throw an exception in principle, when it returns -1

Copy link
Member

Choose a reason for hiding this comment

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

That seems pretty far fetched, but OK. This whole thing is trying to be minimal invasive. And I think that is successful enough for me to be OK with merging it in a bit. (so if anyone has any stomach aches about it, please comment).

The C-API testing would be better of course, so maybe will also see to add that before merging.

@seberg seberg self-assigned this May 14, 2019
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@mattip mattip merged commit b82869e into numpy:master May 16, 2019
@eric-wieser eric-wieser deleted the empty-broadcast branch June 1, 2019 20:59
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.

5 participants