Skip to content

Commit 05cec26

Browse files
Add bytesarray support to the c-api
1 parent 885cf5c commit 05cec26

4 files changed

Lines changed: 130 additions & 1 deletion

File tree

‎crates/capi/src/bytearrayobject.rs‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use crate::PyObject;
2+
use crate::object::define_py_check;
3+
use crate::pystate::with_vm;
4+
use core::ffi::c_char;
5+
use rustpython_vm::builtins::PyByteArray;
6+
use rustpython_vm::byte::bytes_from_object;
7+
8+
define_py_check!(fn PyByteArray_Check, types.bytearray_type);
9+
10+
#[unsafe(no_mangle)]
11+
#[allow(clippy::uninit_vec)]
12+
pub unsafe extern "C" fn PyByteArray_FromStringAndSize(
13+
bytes: *const c_char,
14+
len: isize,
15+
) -> *mut PyObject {
16+
with_vm(|vm| {
17+
let len: usize = len.try_into().map_err(|_| {
18+
vm.new_system_error("Negative size passed to PyByteArray_FromStringAndSize")
19+
})?;
20+
21+
let data = if bytes.is_null() {
22+
let mut data = Vec::with_capacity(len);
23+
unsafe { data.set_len(len) };
24+
data
25+
} else {
26+
unsafe { core::slice::from_raw_parts(bytes.cast::<u8>(), len) }.to_vec()
27+
};
28+
29+
Ok(vm.ctx.new_bytearray(data))
30+
})
31+
}
32+
33+
#[unsafe(no_mangle)]
34+
pub unsafe extern "C" fn PyByteArray_FromObject(obj: *mut PyObject) -> *mut PyObject {
35+
with_vm(|vm| {
36+
let obj = unsafe { &*obj };
37+
let data = bytes_from_object(vm, obj)?;
38+
Ok(vm.ctx.new_bytearray(data))
39+
})
40+
}
41+
42+
#[unsafe(no_mangle)]
43+
pub unsafe extern "C" fn PyByteArray_Size(bytearray: *mut PyObject) -> isize {
44+
with_vm(|vm| {
45+
let bytearray = unsafe { &*bytearray }.try_downcast_ref::<PyByteArray>(vm)?;
46+
Ok(bytearray.borrow_buf().len())
47+
})
48+
}
49+
50+
#[unsafe(no_mangle)]
51+
pub unsafe extern "C" fn PyByteArray_AsString(bytearray: *mut PyObject) -> *mut c_char {
52+
with_vm(|vm| {
53+
let bytearray = unsafe { &*bytearray }.try_downcast_ref::<PyByteArray>(vm)?;
54+
Ok(bytearray.borrow_buf_mut().as_mut_ptr())
55+
})
56+
}
57+
58+
#[unsafe(no_mangle)]
59+
pub unsafe extern "C" fn PyByteArray_Resize(bytearray: *mut PyObject, len: isize) -> i32 {
60+
with_vm(|vm| {
61+
let bytearray = unsafe { &*bytearray }.try_downcast_ref::<PyByteArray>(vm)?;
62+
bytearray.resize(len, vm)?;
63+
Ok(())
64+
})
65+
}
66+
67+
#[cfg(false)]
68+
mod tests {
69+
use pyo3::prelude::*;
70+
use pyo3::types::{PyByteArray, PyBytes};
71+
72+
#[test]
73+
fn bytearray_size() {
74+
Python::attach(|py| {
75+
let bytearray = PyByteArray::new(py, b"abc");
76+
assert_eq!(bytearray.len(), 3);
77+
})
78+
}
79+
80+
#[test]
81+
fn bytearray_resize() {
82+
Python::attach(|py| {
83+
let bytearray = PyByteArray::new(py, b"abcde");
84+
bytearray.resize(3).unwrap();
85+
assert_eq!(bytearray.len(), 3);
86+
assert_eq!(bytearray.to_vec(), b"abc");
87+
})
88+
}
89+
90+
#[test]
91+
fn bytearray_from_string_and_size() {
92+
Python::attach(|py| {
93+
let bytearray = PyByteArray::new(py, b"hello");
94+
assert_eq!(bytearray.len(), 5);
95+
assert_eq!(bytearray.to_vec(), b"hello");
96+
})
97+
}
98+
99+
#[test]
100+
fn bytearray_new_with_zero_initialized() {
101+
Python::attach(|py| {
102+
let bytearray = PyByteArray::new_with(py, 4, |bytes| {
103+
bytes[..2].copy_from_slice(b"hi");
104+
Ok(())
105+
})
106+
.unwrap();
107+
assert_eq!(bytearray.len(), 4);
108+
assert_eq!(bytearray.to_vec(), b"hi\0\0");
109+
})
110+
}
111+
112+
#[test]
113+
fn bytearray_from_object() {
114+
Python::attach(|py| {
115+
let source = PyBytes::new(py, b"ABC");
116+
let bytearray = PyByteArray::from(&source).unwrap();
117+
assert_eq!(bytearray.to_vec(), b"ABC");
118+
})
119+
}
120+
}

‎crates/capi/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern crate alloc;
1010

1111
pub mod abstract_;
1212
pub mod boolobject;
13+
pub mod bytearrayobject;
1314
pub mod bytesobject;
1415
pub mod ceval;
1516
pub mod complexobject;

‎crates/capi/src/util.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ impl FfiResult<*mut c_char> for *const u8 {
7676
}
7777
}
7878

79+
impl FfiResult<*mut c_char> for *mut u8 {
80+
const ERR_VALUE: *mut c_char = core::ptr::null_mut();
81+
82+
fn into_output(self, _vm: &VirtualMachine) -> *mut c_char {
83+
self.cast()
84+
}
85+
}
86+
7987
impl FfiResult for *const c_char {
8088
const ERR_VALUE: *const c_char = core::ptr::null_mut();
8189

‎crates/vm/src/builtins/bytearray.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ impl PyByteArray {
548548
}
549549

550550
#[pymethod]
551-
fn resize(&self, size: isize, vm: &VirtualMachine) -> PyResult<()> {
551+
pub fn resize(&self, size: isize, vm: &VirtualMachine) -> PyResult<()> {
552552
if size < 0 {
553553
return Err(vm.new_value_error("bytearray.resize(): new size must be >= 0"));
554554
}

0 commit comments

Comments
 (0)