Merge pull request #647 from Alexander-N/bytes
Move PyBytes from string.rs into own file
This commit is contained in:
commit
59975f8dd8
103
src/types/bytes.rs
Normal file
103
src/types/bytes.rs
Normal file
|
@ -0,0 +1,103 @@
|
|||
use crate::conversion::FromPyObject;
|
||||
use crate::conversion::{PyTryFrom, ToPyObject};
|
||||
use crate::err::PyResult;
|
||||
use crate::object::PyObject;
|
||||
use crate::types::PyAny;
|
||||
use crate::AsPyPointer;
|
||||
use crate::Python;
|
||||
use crate::{ffi, FromPy};
|
||||
use std::ops::Index;
|
||||
use std::os::raw::c_char;
|
||||
use std::slice::SliceIndex;
|
||||
use std::str;
|
||||
|
||||
/// Represents a Python `bytes`.
|
||||
///
|
||||
/// This type is immutable
|
||||
#[repr(transparent)]
|
||||
pub struct PyBytes(PyObject);
|
||||
|
||||
pyobject_native_type!(
|
||||
PyBytes,
|
||||
ffi::PyBytes_Type,
|
||||
Some("builtins"),
|
||||
ffi::PyBytes_Check
|
||||
);
|
||||
|
||||
impl PyBytes {
|
||||
/// Creates a new Python byte string object.
|
||||
/// The byte string is initialized by copying the data from the `&[u8]`.
|
||||
///
|
||||
/// Panics if out of memory.
|
||||
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes {
|
||||
let ptr = s.as_ptr() as *const c_char;
|
||||
let len = s.len() as ffi::Py_ssize_t;
|
||||
unsafe { py.from_owned_ptr(ffi::PyBytes_FromStringAndSize(ptr, len)) }
|
||||
}
|
||||
|
||||
/// Creates a new Python byte string object from raw pointer.
|
||||
///
|
||||
/// Panics if out of memory.
|
||||
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
|
||||
py.from_owned_ptr(ffi::PyBytes_FromStringAndSize(
|
||||
ptr as *const _,
|
||||
len as isize,
|
||||
))
|
||||
}
|
||||
|
||||
/// Get the Python string as a byte slice.
|
||||
#[inline]
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
unsafe {
|
||||
let buffer = ffi::PyBytes_AsString(self.as_ptr()) as *const u8;
|
||||
let length = ffi::PyBytes_Size(self.as_ptr()) as usize;
|
||||
debug_assert!(!buffer.is_null());
|
||||
std::slice::from_raw_parts(buffer, length)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the same way [Vec] is indexed
|
||||
impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
|
||||
type Output = I::Output;
|
||||
|
||||
fn index(&self, index: I) -> &Self::Output {
|
||||
&self.as_bytes()[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromPy<&'a [u8]> for PyObject {
|
||||
fn from_py(other: &'a [u8], py: Python) -> Self {
|
||||
PyBytes::new(py, other).to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromPyObject<'a> for &'a [u8] {
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
Ok(<PyBytes as PyTryFrom>::try_from(obj)?.as_bytes())
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::PyBytes;
|
||||
use crate::FromPyObject;
|
||||
use crate::Python;
|
||||
|
||||
#[test]
|
||||
fn test_extract_bytes() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let py_bytes = py.eval("b'Hello Python'", None, None).unwrap();
|
||||
let bytes: &[u8] = FromPyObject::extract(py_bytes).unwrap();
|
||||
assert_eq!(bytes, b"Hello Python");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_index() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let bytes = PyBytes::new(py, b"Hello World");
|
||||
assert_eq!(bytes[1], b'e');
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
pub use self::any::PyAny;
|
||||
pub use self::boolobject::PyBool;
|
||||
pub use self::bytearray::PyByteArray;
|
||||
pub use self::bytes::PyBytes;
|
||||
pub use self::complex::PyComplex;
|
||||
pub use self::datetime::PyDeltaAccess;
|
||||
pub use self::datetime::{
|
||||
|
@ -20,7 +21,7 @@ pub use self::num::PyLong as PyInt;
|
|||
pub use self::sequence::PySequence;
|
||||
pub use self::set::{PyFrozenSet, PySet};
|
||||
pub use self::slice::{PySlice, PySliceIndices};
|
||||
pub use self::string::{PyBytes, PyString, PyString as PyUnicode};
|
||||
pub use self::string::{PyString, PyString as PyUnicode};
|
||||
pub use self::tuple::PyTuple;
|
||||
pub use self::typeobject::PyType;
|
||||
|
||||
|
@ -166,6 +167,7 @@ macro_rules! pyobject_native_type_convert(
|
|||
mod any;
|
||||
mod boolobject;
|
||||
mod bytearray;
|
||||
mod bytes;
|
||||
mod complex;
|
||||
mod datetime;
|
||||
mod dict;
|
||||
|
|
|
@ -13,10 +13,8 @@ use crate::Python;
|
|||
use crate::{ffi, FromPy};
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::CStr;
|
||||
use std::ops::Index;
|
||||
use std::os::raw::c_char;
|
||||
use std::ptr::NonNull;
|
||||
use std::slice::SliceIndex;
|
||||
use std::str;
|
||||
|
||||
/// Represents a Python `string`.
|
||||
|
@ -27,19 +25,6 @@ pub struct PyString(PyObject);
|
|||
|
||||
pyobject_native_type!(PyString, ffi::PyUnicode_Type, ffi::PyUnicode_Check);
|
||||
|
||||
/// Represents a Python `bytes`.
|
||||
///
|
||||
/// This type is immutable
|
||||
#[repr(transparent)]
|
||||
pub struct PyBytes(PyObject);
|
||||
|
||||
pyobject_native_type!(
|
||||
PyBytes,
|
||||
ffi::PyBytes_Type,
|
||||
Some("builtins"),
|
||||
ffi::PyBytes_Check
|
||||
);
|
||||
|
||||
impl PyString {
|
||||
/// Creates a new Python string object.
|
||||
///
|
||||
|
@ -116,48 +101,6 @@ impl PyString {
|
|||
}
|
||||
}
|
||||
|
||||
impl PyBytes {
|
||||
/// Creates a new Python byte string object.
|
||||
/// The byte string is initialized by copying the data from the `&[u8]`.
|
||||
///
|
||||
/// Panics if out of memory.
|
||||
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes {
|
||||
let ptr = s.as_ptr() as *const c_char;
|
||||
let len = s.len() as ffi::Py_ssize_t;
|
||||
unsafe { py.from_owned_ptr(ffi::PyBytes_FromStringAndSize(ptr, len)) }
|
||||
}
|
||||
|
||||
/// Creates a new Python byte string object from raw pointer.
|
||||
///
|
||||
/// Panics if out of memory.
|
||||
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
|
||||
py.from_owned_ptr(ffi::PyBytes_FromStringAndSize(
|
||||
ptr as *const _,
|
||||
len as isize,
|
||||
))
|
||||
}
|
||||
|
||||
/// Get the Python string as a byte slice.
|
||||
#[inline]
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
unsafe {
|
||||
let buffer = ffi::PyBytes_AsString(self.as_ptr()) as *const u8;
|
||||
let length = ffi::PyBytes_Size(self.as_ptr()) as usize;
|
||||
debug_assert!(!buffer.is_null());
|
||||
std::slice::from_raw_parts(buffer, length)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the same way [Vec] is indexed
|
||||
impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
|
||||
type Output = I::Output;
|
||||
|
||||
fn index(&self, index: I) -> &Self::Output {
|
||||
&self.as_bytes()[index]
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts Rust `str` to Python object.
|
||||
/// See `PyString::new` for details on the conversion.
|
||||
impl ToPyObject for str {
|
||||
|
@ -238,21 +181,9 @@ impl<'source> FromPyObject<'source> for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> FromPy<&'a [u8]> for PyObject {
|
||||
fn from_py(other: &'a [u8], py: Python) -> Self {
|
||||
PyBytes::new(py, other).to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromPyObject<'a> for &'a [u8] {
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
Ok(<PyBytes as PyTryFrom>::try_from(obj)?.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{PyBytes, PyString};
|
||||
use super::PyString;
|
||||
use crate::instance::AsPyRef;
|
||||
use crate::object::PyObject;
|
||||
use crate::Python;
|
||||
|
@ -279,16 +210,6 @@ mod test {
|
|||
assert_eq!(s, s2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_bytes() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let py_bytes = py.eval("b'Hello Python'", None, None).unwrap();
|
||||
let bytes: &[u8] = FromPyObject::extract(py_bytes).unwrap();
|
||||
assert_eq!(bytes, b"Hello Python");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_bytes() {
|
||||
let gil = Python::acquire_gil();
|
||||
|
@ -341,12 +262,4 @@ mod test {
|
|||
let py_string = <PyString as PyTryFrom>::try_from(obj.as_ref(py)).unwrap();
|
||||
assert_eq!(py_string.to_string_lossy(), "🐈 Hello <20><><EFBFBD>World");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_index() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let bytes = PyBytes::new(py, b"Hello World");
|
||||
assert_eq!(bytes[1], b'e');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue