Skip to content

Conversation

@youknowone
Copy link

standard_b64encode is inherently an unsafe function. Meanwhile, standard_b64encode_impl uses unsafe operations internally, but it is intended to be a function where the programmer guarantees its safety. By separating these two functions, we can ensure that the truly safe parts of standard_b64encode_impl remain outside the unsafe block.

BorrowedBuffer::from_object is actually safe as long as the pointer to the object remains valid. In this case, it seems better to require the caller to ensure pointer validity rather than hiding that requirement inside the function itself.

@youknowone youknowone force-pushed the standard_b64encode_impl branch from 8bb35a3 to ed31a89 Compare November 18, 2025 05:44
@youknowone youknowone mentioned this pull request Nov 18, 2025
Copy link

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

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

Thank you!
That looks much better.

@youknowone youknowone force-pushed the standard_b64encode_impl branch from ed31a89 to 5d45c51 Compare November 19, 2025 04:48
fn from_object(obj: &PyObject) -> Result<Self, ()> {
let mut view = MaybeUninit::<Py_buffer>::uninit();
if unsafe { PyObject_GetBuffer(obj, view.as_mut_ptr(), PYBUF_SIMPLE) } != 0 {
if unsafe { PyObject_GetBuffer(obj as *const _ as *mut _, view.as_mut_ptr(), PYBUF_SIMPLE) } != 0 {
Copy link
Author

Choose a reason for hiding this comment

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

Passing Rc::into_raw() is the right interface. But this patch doesn't include Rc

Copy link
Author

Choose a reason for hiding this comment

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

The new interface guarantees there is no mutating to PyObject on Rust side. mutating in C side is okay as before.

Copy link
Owner

Choose a reason for hiding this comment

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

I think you could expose .raw_get() from the inner UnsafeCell to get a *mut _object, but I guess that won't work as I assume PyObject_GetBuffer expects a *mut PyObject, right?

Copy link
Author

Choose a reason for hiding this comment

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

That's right. But the idea using .raw() and encapsulating it from PyObject is also a better idea.

Copy link
Author

Choose a reason for hiding this comment

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

I added PyObject::as_raw(). Please give it a better name.

Copy link
Author

Choose a reason for hiding this comment

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

@emmatyping is there more issues to fix?

@emmatyping emmatyping merged commit c66a76e into emmatyping:rust-in-cpython Nov 24, 2025
3 checks passed
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