-
Notifications
You must be signed in to change notification settings - Fork 901
Add BytesWriter
#5517
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
Draft
bschoenmaeckers
wants to merge
5
commits into
PyO3:main
Choose a base branch
from
bschoenmaeckers:byteswriter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add BytesWriter
#5517
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8e2c89
Add `BytesWriter`
bschoenmaeckers 0a38fa8
Support `abi3` using `Vec`
bschoenmaeckers fb5071b
Fix ffi-check
bschoenmaeckers 993eda8
Do not double free
bschoenmaeckers 89a3e09
Make `PyBytesWriter` -> `PyBytes` conversion infallible
bschoenmaeckers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add `BytesWriter` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_Create( | ||
| size: crate::Py_ssize_t, | ||
| ) -> *mut crate::PyBytesWriter { | ||
|
|
||
| if size < 0 { | ||
| crate::PyErr_SetString(crate::PyExc_ValueError, c_str!("size must be >= 0").as_ptr() as *const _); | ||
| return std::ptr::null_mut(); | ||
| } | ||
|
|
||
| let writer: *mut crate::PyBytesWriter = crate::PyMem_Malloc(std::mem::size_of::<crate::PyBytesWriter>()).cast(); | ||
| if writer.is_null() { | ||
| crate::PyErr_NoMemory(); | ||
| return std::ptr::null_mut(); | ||
| } | ||
|
|
||
| (*writer).obj = std::ptr::null_mut(); | ||
| (*writer).size = 0; | ||
|
|
||
| if size >=1 { | ||
| if _PyBytesWriter_Resize_impl(writer, size, 0) < 0 { | ||
| PyBytesWriter_Discard(writer); | ||
| return std::ptr::null_mut(); | ||
| } | ||
|
|
||
| (*writer).size = size; | ||
| } | ||
|
|
||
| writer | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_Discard(writer: *mut crate::PyBytesWriter) -> () { | ||
| if writer.is_null() { | ||
| return; | ||
| } | ||
|
|
||
| crate::Py_XDECREF((*writer).obj); | ||
| crate::PyMem_Free(writer.cast()); | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_Finish(writer: *mut crate::PyBytesWriter) -> *mut crate::PyObject { | ||
| PyBytesWriter_FinishWithSize(writer, (*writer).size) | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_FinishWithSize(writer: *mut crate::PyBytesWriter, size: crate::Py_ssize_t) -> *mut crate::PyObject { | ||
| let result = if size == 0 { | ||
| crate::PyBytes_FromStringAndSize(c_str!("").as_ptr(), 0) | ||
| } else if (*writer).obj.is_null() { | ||
| crate::PyBytes_FromStringAndSize((*writer).small_buffer.as_ptr(), size) | ||
| } else { | ||
| if size != crate::PyBytes_Size((*writer).obj) && crate::_PyBytes_Resize(&mut (*writer).obj, size) < 0{ | ||
| PyBytesWriter_Discard(writer); | ||
| return std::ptr::null_mut(); | ||
| } | ||
| std::mem::replace(&mut (*writer).obj, std::ptr::null_mut()) | ||
| }; | ||
|
|
||
| PyBytesWriter_Discard(writer); | ||
| result | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn _PyBytesWriter_GetAllocated(writer: *mut crate::PyBytesWriter) -> crate::Py_ssize_t { | ||
| if (*writer).obj.is_null() { | ||
| std::mem::size_of_val(&(*writer).small_buffer) as _ | ||
| } else { | ||
| crate::PyBytes_Size((*writer).obj) | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_GetData(writer: *mut crate::PyBytesWriter) -> *mut std::ffi::c_void { | ||
| if (*writer).obj.is_null() { | ||
| (*writer).small_buffer.as_ptr() as *mut _ | ||
| } else { | ||
| crate::PyBytes_AS_STRING((*writer).obj) as *mut _ | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_GetSize(writer: *mut crate::PyBytesWriter) -> crate::Py_ssize_t { | ||
| (*writer).size | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_WriteBytes(writer: *mut crate::PyBytesWriter, bytes: *const std::ffi::c_void, size: crate::Py_ssize_t) -> std::ffi::c_int { | ||
| let size = if size < 0 { | ||
| let len = libc::strlen(bytes as _); | ||
| if len > crate::PY_SSIZE_T_MAX as libc::size_t { | ||
| crate::PyErr_NoMemory(); | ||
| return -1; | ||
| } | ||
| len as crate::Py_ssize_t | ||
| } else { | ||
| size | ||
| }; | ||
|
|
||
| let pos = (*writer).size; | ||
| if PyBytesWriter_Grow(writer, size) < 0 { | ||
| return -1; | ||
| } | ||
|
|
||
| let buf = PyBytesWriter_GetData(writer); | ||
| std::ptr::copy_nonoverlapping(bytes, buf.add(pos as usize), size as usize); | ||
| 0 | ||
| } | ||
| ); | ||
|
|
||
| #[cfg(not(Py_LIMITED_API))] | ||
| compat_function!( | ||
| originally_defined_for(all(Py_3_15, not(Py_LIMITED_API))); | ||
|
|
||
| #[inline] | ||
| pub unsafe fn PyBytesWriter_Grow(writer: *mut crate::PyBytesWriter, size: crate::Py_ssize_t) -> std::ffi::c_int { | ||
| if size < 0 && (*writer).size + size < 0 { | ||
| crate::PyErr_SetString(crate::PyExc_ValueError, c_str!("invalid size").as_ptr()); | ||
| return -1; | ||
| } | ||
|
|
||
| if size > crate::PY_SSIZE_T_MAX - (*writer).size { | ||
| crate::PyErr_NoMemory(); | ||
| return -1; | ||
| } | ||
| let new_size = (*writer).size + size; | ||
|
|
||
| if _PyBytesWriter_Resize_impl(writer, new_size, 1) < 0 { | ||
| return -1; | ||
| } | ||
|
|
||
| (*writer).size = new_size; | ||
| 0 | ||
| } | ||
| ); | ||
|
|
||
| #[inline] | ||
| #[cfg(not(Py_LIMITED_API))] | ||
| unsafe fn _PyBytesWriter_Resize_impl( | ||
| writer: *mut crate::PyBytesWriter, | ||
| mut size: crate::Py_ssize_t, | ||
| resize: std::ffi::c_int, | ||
| ) -> std::ffi::c_int { | ||
| let overallocate = resize; | ||
| assert!(size >= 0); | ||
|
|
||
| if size <= _PyBytesWriter_GetAllocated(writer) { | ||
| return 0; | ||
| } | ||
|
|
||
| if overallocate > 0 { | ||
| #[cfg(windows)] | ||
| if size <= (crate::PY_SSIZE_T_MAX - size / 2) { | ||
| size += size / 2; | ||
| } | ||
|
|
||
| #[cfg(not(windows))] | ||
| if size <= (crate::PY_SSIZE_T_MAX - size / 4) { | ||
| size += size / 4; | ||
| } | ||
| } | ||
|
|
||
| if !(*writer).obj.is_null() { | ||
| if crate::_PyBytes_Resize(&mut (*writer).obj, size) > 0 { | ||
| return -1; | ||
| } | ||
| assert!(!(*writer).obj.is_null()) | ||
| } else { | ||
| (*writer).obj = crate::PyBytes_FromStringAndSize(std::ptr::null_mut(), size); | ||
| if (*writer).obj.is_null() { | ||
| return -1; | ||
| } | ||
|
|
||
| if resize > 0 { | ||
| assert!((size as usize) > std::mem::size_of_val(&(*writer).small_buffer)); | ||
|
|
||
| std::ptr::copy_nonoverlapping( | ||
| (*writer).small_buffer.as_ptr(), | ||
| crate::PyBytes_AS_STRING((*writer).obj) as *mut _, | ||
| std::mem::size_of_val(&(*writer).small_buffer), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| 0 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_Resizeis not available on the limited api. 😢There was a problem hiding this comment.
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 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hesitant to fallback to Vec for the same reason. But it seems fine to do it like this to make it work.