-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Raise a warning when assigning to a temporary (fix described, patch needed) #2802
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
Comments
Writing something like arr+1 never has anything to do with offsets in Compare to
This code is exactly the same as far as numpy can tell - the same The only thing I can think of is, we could teach np.copyto to issue a It looks like we could do the same for regular slice assignment too. I was
|
Of course, views complicate this picture... At a minimum we should check
|
I forgot about slicing notation and was trying to find an api call to make it work and got confused. Maybe the docs for |
Well, I think there is a real issue here, so I edited the bug description to describe a more general solution; perhaps someone will see it and be inspired to fix it :-). (The new description also has another plausible example of how this same basic issue can bite people in nasty ways, even if they aren't otherwise confused. And anyway I like software that I can successfully use even when I'm confused, since everyone gets confused sometimes...) |
Just to note, the functions I can think of right now that could use this warning are:
Since most functions in numpy (ie. ufuncs) return the output array, no warning is possible there. |
Doh, of course, that's a good point about out= arguments. And it'd be nice Still trying to work out in my mind how this actually works. Like you say,
I'm not sure why someone would write this; is there any situation where
so arguably a warning is still a good idea. But it might still be a useful On Fri, Dec 14, 2012 at 1:50 PM, seberg notifications@github.com wrote:
|
@njsmith didn't think enough about it maybe. It seems to me you are right, unless you want some very quirky memory order of the result (that cannot be specified with just |
Can we close this? It seems there are edge cases to the edge cases in the logic necessary to detect assigning to temporary variables |
No further response, closing. Feel free to reopen, but seems like this is not a useful issue anymore. |
This still strikes me as a handy feature to implement - and since we were able to implement elision of temporaries using refcounts, it certainly seems possible that we could pull off the same trick here to emit warnings. I'd argue we should reopen this, but it was certainly not a good first issue. |
It would be nice if numpy raised a warning when people assigned into temporary arrays, since it's rather easy to end up with these accidentally and not realize what's going on, so people keep barking their shins on this. The original report below is one example; another would be
which will usually modify
a
... but sometimes, depending ona
's memory layout, it may be a no-op instead. Quite sneaky.The way to do this would be to write a function like
PyArray_DefinitelyTemporary(arr)
that returned true iff all entries inarr
's.base
chain fulfilled the properties:Then in the Python entry points for numpy assignments --
np.copyto
,__setitem__
, ufuncout=
parameters, etc. -- have some code like(NB: I say Python entry points above, because any numpy C API entry points can legitimately take borrowed references to non-temporary arrays that will have reference count 1. So we need to be careful that these checks are done before we hit code that could be reached by C API users.)
Original report:
I know that I should be using slice notation instead of
np.copyto
, but I also feel like case #1 should throw an error.The text was updated successfully, but these errors were encountered: