-
Notifications
You must be signed in to change notification settings - Fork 763
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
Added Rust initialisation of Python-allocated bytes #1074
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
98b9f1c
Added Rust initialisation of Python-allocated bytes
juntyr 20d27ff
Added unsafe PyBytes::new_with_unit constructor
juntyr 533c105
Added examples to PyBytes::new_with and PyBytes::new_with_uninit (now…
juntyr 35d2ef1
Fixed doc test imports for PyBytes::new_with and PyBytes::new_with_un…
juntyr f4a5a22
Fixed clippy error in PyBytes::new_with_uninit test
juntyr 0a47af7
Added PyByteArray::new_with and Removed PyBytes::new_with_uninit
juntyr cac2d8a
Small doc fixes + FnOnce init closure for PyBytes::new_with and PyByt…
juntyr cb07c67
Fixed where clause formatting in PyBytes::new_with and PyByteArray::n…
juntyr 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 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 |
---|---|---|
|
@@ -26,6 +26,21 @@ impl PyBytes { | |
unsafe { py.from_owned_ptr(ffi::PyBytes_FromStringAndSize(ptr, len)) } | ||
} | ||
|
||
/// Creates a new Python bytestring object. | ||
/// The uninitialised bytestring must be initialised in the `init` closure. | ||
/// | ||
/// Panics if out of memory. | ||
pub fn new_with<F: Fn(&mut [u8])>(py: Python<'_>, len: usize, init: F) -> &PyBytes { | ||
juntyr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let length = len as ffi::Py_ssize_t; | ||
let pyptr = unsafe { ffi::PyBytes_FromStringAndSize(std::ptr::null(), length) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this and the next 4 lines all contain unsafe, could perhaps just refactor to be a single unsafe block? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 20d27ff |
||
// Iff pyptr is null, py.from_owned_ptr(pyptr) will panic | ||
let pybytes = unsafe { py.from_owned_ptr(pyptr) }; | ||
let buffer = unsafe { ffi::PyBytes_AsString(pyptr) } as *mut u8; | ||
debug_assert!(!buffer.is_null()); | ||
init(unsafe { std::slice::from_raw_parts_mut(buffer, len) }); | ||
pybytes | ||
} | ||
|
||
/// Creates a new Python byte string object from a raw pointer and length. | ||
/// | ||
/// Panics if out of memory. | ||
|
@@ -91,4 +106,15 @@ mod test { | |
let bytes = PyBytes::new(py, b"Hello World"); | ||
assert_eq!(bytes[1], b'e'); | ||
} | ||
|
||
#[test] | ||
fn test_bytes_new_with() { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
let py_bytes = PyBytes::new_with(py, 10, |b: &mut [u8]| { | ||
b.copy_from_slice(b"Hello Rust"); | ||
}); | ||
let bytes: &[u8] = FromPyObject::extract(py_bytes).unwrap(); | ||
assert_eq!(bytes, b"Hello Rust"); | ||
} | ||
} |
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.
Maybe specifically say the entire uninitialised bytestring?
Also, if you're up for it, adding a little example would be very much appreciated.
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.
Where and in what format should I add an example?
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've added examples in 533c105