Skip to content

Conversation

@bschoenmaeckers
Copy link
Member

@bschoenmaeckers bschoenmaeckers commented Oct 15, 2025

Adds a new constructor on PyBytes that allows creating a python bytes object using a std::io::Write writer. I've exposed the writer as &mut dyn Write dyn object so we do not have to expose the internal writer implementation. We can change this to &mut PyBytesWriter at a later stage if that seems necessary.

closes #4003

}

if !(*writer).obj.is_null() {
if crate::_PyBytes_Resize(&mut (*writer).obj, size) > 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

It’s a shame that _PyBytes_Resize is not available on the limited api. 😢

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it's viable to just use a Vec<u8> until calling .finish(). This might defeat the point of the API for users, but at the same time it would let us support it cleanly on all versions 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if it's viable to just use a Vec<u8> until calling .finish(). This might defeat the point of the API for users, but at the same time it would let us support it cleanly on all versions 🤔

I was hesitant to fallback to Vec for the same reason. But it seems fine to do it like this to make it work.

@bschoenmaeckers bschoenmaeckers force-pushed the byteswriter branch 5 times, most recently from de94a5a to 89a3e09 Compare October 17, 2025 13:20
@bschoenmaeckers
Copy link
Member Author

How can I make pyo3-ffi-check happy? It is complaining that PyBytesWriter does not exist on pre-1.15. This is expected but I cannot let it point to the existing _PyBytesWriter because the layout is different.

  error[E0412]: cannot find type `PyBytesWriter` in module `bindings`
    --> src\main.rs:70:5
     |
  70 |     pyo3_ffi_check_macro::for_all_structs!(check_struct);
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in `bindings`

@bschoenmaeckers bschoenmaeckers force-pushed the byteswriter branch 3 times, most recently from 244df75 to 5318908 Compare January 16, 2026 14:04
@bschoenmaeckers bschoenmaeckers marked this pull request as ready for review January 16, 2026 14:43
@bschoenmaeckers bschoenmaeckers changed the title Add BytesWriter Add PyBytes::new_with_writer Jan 16, 2026
@bschoenmaeckers bschoenmaeckers force-pushed the byteswriter branch 8 times, most recently from 85b55d9 to 7bfa6f4 Compare January 16, 2026 16:25
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.

Support resize on PyBytes when initialised via with_new

2 participants