-
Notifications
You must be signed in to change notification settings - Fork 120
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
Fix signature of PyArray::new or better yet replace it by PyArray::uninit #217
Comments
Ah, it probably is not that simple. In contrast to |
@davidhewitt I am having some trouble trying to figure out the correct API here. The above does not work as just returning I think the main issue is that as soon as So it seems I need some type
Does something like this already exist in PyO3? Is there a different idiom I should use to delay registration with the release pool? (Of course this all means the underlying NumPy array is leaked if the initialization is never completed, but at least this would be safe.) |
Or maybe I want to return MaybeUninit<Py<PyArray>> to avoid registration? |
Yes, NumPy seems to use @kngwyu Since we do not have proper API to do mostly safe element-wise initialized either, I would propose to remove the creation of uninitialized |
Hmm not sure why the github timestamp for my previous comment is in the future? Never seen that before! Sounds like something like |
The problem is that we do not really have API to enable the user to properly initialize that array, i.e. get access to raw data pointers and such. Additionally, that |
Agreed we would need to add Regarding it being a "bomb" - true, but only in the Either way, if the method to create it is Removing |
EDIT: This message has an incorrect timestamp (maybe 1 hour delayed?) not sure what GitHub did here. This seems like a tricky one... because presumably we do want to free the underlying allocation on drop, so we do need numpy's destructor to run? To clarify, we're in a thorny situation where numpy will free the underlying array elements on drop, so we need to be sure they're valid to drop before calling the numpy destructor. I guess this is only a problem with I'm not aware of prior art, so we might need to get creative. |
I think I found a useful idiom in #216 for keeping the uninitialized array alive, I just do let ref _ = mem::ManuallyDrop(array.to_object(py)); before initialization to elevate its reference count and do let _ = mem::ManuallyDrop::into_inner(ref_); afterwards to release the spurious reference. (I added a bit of convoluted test which does seem to verify that this works as intended.)
I wonder whether keeping the signature as-is (i.e. leaving out the In any case, I think we should first make sure #216 is in good shape before starting on the public API as it forces us to figure this internally already. |
Looking at my test case again, I wonder whether we should just make " |
(Assuming |
Could also add a |
I don't think |
Ah right, I see 👍 |
As discussed in #216 (comment),
PyArray::new
creates an uninitialized array and should therefore be markedunsafe
as it is the callers responsibility to not access (and of non-copy element types not even drop) the array until all elements are initialized.However, we also do not seem to offer a good API for doing that element-wise initialization post facto. I think instead of just marking the method
unsafe
, we should rename itPyArray::uninit
and keep it safe but change its return type toPyArray<MaybeUninit<A>, D>>
and add an additionalassume_init
method for turning this intoPyArray<A, D>
. This would be similar to the approach taken byndarray
.The text was updated successfully, but these errors were encountered: