Skip to content
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

buffer: tidy up exceptions #1534

Merged
merged 1 commit into from Apr 12, 2021
Merged

Conversation

davidhewitt
Copy link
Member

Motivated by #1128 (comment)

Copy link
Member

@kngwyu kngwyu left a comment

Choose a reason for hiding this comment

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

Thanks, looks reasonable for me.

src/buffer.rs Outdated Show resolved Hide resolved
src/buffer.rs Outdated Show resolved Hide resolved
unsafe {
let mut buf = Box::pin(ffi::Py_buffer::new());
// TODO: use nightly API Box::new_uninit() once stable
let mut buf = Box::new(mem::MaybeUninit::uninit());
Copy link
Member

Choose a reason for hiding this comment

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

If you take this approach, then you can remove Py_buffer::new().
Though I'm a bit doubtful about how effective MaybeUninit is (I mean that this is effective only when we raise an error, but then buf is immediately dropped...).

Copy link
Member Author

Choose a reason for hiding this comment

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

In the future I would like to remove Py_buffer::new(), but I think to do so right now would be inconvenient for users because PyBufferProtocol basically forces you to handle the raw ffi pointer (which probably points to uninitialized memory which C created).

I chose MaybeUninit here because that's pretty much identical to what C would do: we create a slab of memory and pass its location to PyObject_GetBuffer to initialize the contents.

@kngwyu
Copy link
Member

kngwyu commented Apr 2, 2021

Could you please add some tests where construction fails?

@davidhewitt davidhewitt force-pushed the buffer-errors branch 3 times, most recently from dc7c2fc to aa0b5d8 Compare April 12, 2021 07:43
@davidhewitt davidhewitt merged commit b4636e6 into PyO3:main Apr 12, 2021
@davidhewitt davidhewitt deleted the buffer-errors branch April 12, 2021 08:11
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.

None yet

2 participants