FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add memoryview support to c-api by bschoenmaeckers · Pull Request #8240 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions crates/capi/src/lib.rs
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
Expand Up @@ -22,6 +22,7 @@ pub mod genericaliasobject;
pub mod import;
pub mod listobject;
pub mod longobject;
pub mod memoryobject;
pub mod methodobject;
pub mod moduleobject;
pub mod object;
Expand Down
37 changes: 37 additions & 0 deletions crates/capi/src/memoryobject.rs
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,37 @@
use crate::object::define_py_check;
use crate::{PyObject, pystate::with_vm};
use rustpython_vm::PyPayload;
use rustpython_vm::builtins::PyMemoryView;

define_py_check!(fn PyMemoryView_Check, types.memoryview_type);

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyMemoryView_FromObject(obj: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let obj = unsafe { &*obj };
Ok(PyMemoryView::from_object(obj, vm)?.into_ref(&vm.ctx))
})
}

#[cfg(test)]
mod tests {
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyMemoryView};

#[test]
fn memoryview_from_bytes() {
Python::attach(|py| {
let bytes = PyBytes::new(py, b"hello");
let view = PyMemoryView::from(&bytes).unwrap();

assert!(view.is_instance_of::<PyMemoryView>());

let copied = view
.call_method1("tobytes", ())
.unwrap()
.cast_into::<PyBytes>()
.unwrap();
assert_eq!(copied.as_bytes(), b"hello");
})
}
}
Loading

Back | FazBrowse Home | New Git URL