|
| 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 | +} |
0 commit comments