Skip to content

Commit

Permalink
Add back PyBytes::new (PyO3#4387)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers authored Jul 31, 2024
1 parent a5dd124 commit 6caefd1
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 33 deletions.
2 changes: 1 addition & 1 deletion guide/src/conversions/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ enum RustyEnum<'py> {
# }
#
# {
# let thing = PyBytes::new_bound(py, b"text");
# let thing = PyBytes::new(py, b"text");
# let rust_thing: RustyEnum<'_> = thing.extract()?;
#
# assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/buf_and_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl BytesExtractor {

#[pyfunction]
fn return_memoryview(py: Python<'_>) -> PyResult<Bound<'_, PyMemoryView>> {
let bytes = PyBytes::new_bound(py, b"hello world");
let bytes = PyBytes::new(py, b"hello world");
PyMemoryView::from_bound(&bytes)
}

Expand Down
2 changes: 1 addition & 1 deletion src/conversions/anyhow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
//! let res = Python::with_gil(|py| {
//! let zlib = PyModule::import_bound(py, "zlib")?;
//! let decompress = zlib.getattr("decompress")?;
//! let bytes = PyBytes::new_bound(py, bytes);
//! let bytes = PyBytes::new(py, bytes);
//! let value = decompress.call1((bytes,))?;
//! value.extract::<Vec<u8>>()
//! })?;
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/eyre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
//! let res = Python::with_gil(|py| {
//! let zlib = PyModule::import_bound(py, "zlib")?;
//! let decompress = zlib.getattr("decompress")?;
//! let bytes = PyBytes::new_bound(py, bytes);
//! let bytes = PyBytes::new(py, bytes);
//! let value = decompress.call1((bytes,))?;
//! value.extract::<Vec<u8>>()
//! })?;
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ macro_rules! bigint_conversion {
#[cfg(Py_LIMITED_API)]
fn to_object(&self, py: Python<'_>) -> PyObject {
let bytes = $to_bytes(self);
let bytes_obj = PyBytes::new_bound(py, &bytes);
let bytes_obj = PyBytes::new(py, &bytes);
let kwargs = if $is_signed {
let kwargs = crate::types::PyDict::new_bound(py);
kwargs.set_item(crate::intern!(py, "signed"), true).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/conversions/std/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{

impl<'a> IntoPy<PyObject> for &'a [u8] {
fn into_py(self, py: Python<'_>) -> PyObject {
PyBytes::new_bound(py, self).unbind().into()
PyBytes::new(py, self).unbind().into()
}

#[cfg(feature = "experimental-inspect")]
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<'a> crate::conversion::FromPyObjectBound<'a, '_> for Cow<'a, [u8]> {

impl ToPyObject for Cow<'_, [u8]> {
fn to_object(&self, py: Python<'_>) -> Py<PyAny> {
PyBytes::new_bound(py, self.as_ref()).into()
PyBytes::new(py, self.as_ref()).into()
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/pybacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl ToPyObject for PyBackedBytes {
fn to_object(&self, py: Python<'_>) -> Py<PyAny> {
match &self.storage {
PyBackedBytesStorage::Python(bytes) => bytes.to_object(py),
PyBackedBytesStorage::Rust(bytes) => PyBytes::new_bound(py, bytes).into_any().unbind(),
PyBackedBytesStorage::Rust(bytes) => PyBytes::new(py, bytes).into_any().unbind(),
}
}
}
Expand All @@ -216,7 +216,7 @@ impl IntoPy<Py<PyAny>> for PyBackedBytes {
fn into_py(self, py: Python<'_>) -> Py<PyAny> {
match self.storage {
PyBackedBytesStorage::Python(bytes) => bytes.into_any(),
PyBackedBytesStorage::Rust(bytes) => PyBytes::new_bound(py, &bytes).into_any().unbind(),
PyBackedBytesStorage::Rust(bytes) => PyBytes::new(py, &bytes).into_any().unbind(),
}
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ mod test {
#[test]
fn py_backed_bytes_empty() {
Python::with_gil(|py| {
let b = PyBytes::new_bound(py, b"");
let b = PyBytes::new(py, b"");
let py_backed_bytes = b.extract::<PyBackedBytes>().unwrap();
assert_eq!(&*py_backed_bytes, b"");
});
Expand All @@ -364,7 +364,7 @@ mod test {
#[test]
fn py_backed_bytes() {
Python::with_gil(|py| {
let b = PyBytes::new_bound(py, b"abcde");
let b = PyBytes::new(py, b"abcde");
let py_backed_bytes = b.extract::<PyBackedBytes>().unwrap();
assert_eq!(&*py_backed_bytes, b"abcde");
});
Expand All @@ -373,7 +373,7 @@ mod test {
#[test]
fn py_backed_bytes_from_bytes() {
Python::with_gil(|py| {
let b = PyBytes::new_bound(py, b"abcde");
let b = PyBytes::new(py, b"abcde");
let py_backed_bytes = PyBackedBytes::from(b);
assert_eq!(&*py_backed_bytes, b"abcde");
});
Expand All @@ -391,7 +391,7 @@ mod test {
#[test]
fn py_backed_bytes_into_py() {
Python::with_gil(|py| {
let orig_bytes = PyBytes::new_bound(py, b"abcde");
let orig_bytes = PyBytes::new(py, b"abcde");
let py_backed_bytes = PyBackedBytes::from(orig_bytes.clone());
assert!(py_backed_bytes.to_object(py).is(&orig_bytes));
assert!(py_backed_bytes.into_py(py).is(&orig_bytes));
Expand Down Expand Up @@ -495,7 +495,7 @@ mod test {
#[test]
fn test_backed_bytes_from_bytes_clone() {
Python::with_gil(|py| {
let b1: PyBackedBytes = PyBytes::new_bound(py, b"abcde").into();
let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into();
let b2 = b1.clone();
assert_eq!(b1, b2);

Expand All @@ -520,13 +520,13 @@ mod test {
#[test]
fn test_backed_bytes_eq() {
Python::with_gil(|py| {
let b1: PyBackedBytes = PyBytes::new_bound(py, b"abcde").into();
let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into();
let b2: PyBackedBytes = PyByteArray::new_bound(py, b"abcde").into();

assert_eq!(b1, b"abcde");
assert_eq!(b1, b2);

let b3: PyBackedBytes = PyBytes::new_bound(py, b"hello").into();
let b3: PyBackedBytes = PyBytes::new(py, b"hello").into();
assert_eq!(b"hello", b3);
assert_ne!(b1, b3);
});
Expand All @@ -541,7 +541,7 @@ mod test {
hasher.finish()
};

let b1: PyBackedBytes = PyBytes::new_bound(py, b"abcde").into();
let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into();
let h1 = {
let mut hasher = DefaultHasher::new();
b1.hash(&mut hasher);
Expand All @@ -566,7 +566,7 @@ mod test {
let mut a = vec![b"a", b"c", b"d", b"b", b"f", b"g", b"e"];
let mut b = a
.iter()
.map(|&b| PyBytes::new_bound(py, b).into())
.map(|&b| PyBytes::new(py, b).into())
.collect::<Vec<PyBackedBytes>>();

a.sort();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/hygiene/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Dummy {
}

fn __bytes__<'py>(&self, py: crate::Python<'py>) -> crate::Bound<'py, crate::types::PyBytes> {
crate::types::PyBytes::new_bound(py, &[0])
crate::types::PyBytes::new(py, &[0])
}

fn __format__(&self, format_spec: ::std::string::String) -> ::std::string::String {
Expand Down
55 changes: 43 additions & 12 deletions src/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ use std::str;
/// use pyo3::types::PyBytes;
///
/// # Python::with_gil(|py| {
/// let py_bytes = PyBytes::new_bound(py, b"foo".as_slice());
/// let py_bytes = PyBytes::new(py, b"foo".as_slice());
/// // via PartialEq<[u8]>
/// assert_eq!(py_bytes, b"foo".as_slice());
///
/// // via Python equality
/// let other = PyBytes::new_bound(py, b"foo".as_slice());
/// let other = PyBytes::new(py, b"foo".as_slice());
/// assert!(py_bytes.as_any().eq(other).unwrap());
///
/// // Note that `eq` will convert it's argument to Python using `ToPyObject`,
Expand All @@ -54,7 +54,7 @@ impl PyBytes {
/// The bytestring is initialized by copying the data from the `&[u8]`.
///
/// Panics if out of memory.
pub fn new_bound<'p>(py: Python<'p>, s: &[u8]) -> Bound<'p, PyBytes> {
pub fn new<'p>(py: Python<'p>, s: &[u8]) -> Bound<'p, PyBytes> {
let ptr = s.as_ptr().cast();
let len = s.len() as ffi::Py_ssize_t;
unsafe {
Expand All @@ -64,6 +64,13 @@ impl PyBytes {
}
}

/// Deprecated name for [`PyBytes::new`].
#[deprecated(since = "0.23.0", note = "renamed to `PyBytes::new`")]
#[inline]
pub fn new_bound<'p>(py: Python<'p>, s: &[u8]) -> Bound<'p, PyBytes> {
Self::new(py, s)
}

/// Creates a new Python `bytes` object with an `init` closure to write its contents.
/// Before calling `init` the bytes' contents are zero-initialised.
/// * If Python raises a MemoryError on the allocation, `new_with` will return
Expand All @@ -78,7 +85,7 @@ impl PyBytes {
///
/// # fn main() -> PyResult<()> {
/// Python::with_gil(|py| -> PyResult<()> {
/// let py_bytes = PyBytes::new_bound_with(py, 10, |bytes: &mut [u8]| {
/// let py_bytes = PyBytes::new_with(py, 10, |bytes: &mut [u8]| {
/// bytes.copy_from_slice(b"Hello Rust");
/// Ok(())
/// })?;
Expand All @@ -88,7 +95,7 @@ impl PyBytes {
/// })
/// # }
/// ```
pub fn new_bound_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<Bound<'_, PyBytes>>
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<Bound<'_, PyBytes>>
where
F: FnOnce(&mut [u8]) -> PyResult<()>,
{
Expand All @@ -106,6 +113,16 @@ impl PyBytes {
}
}

/// Deprecated name for [`PyBytes::new_with`].
#[deprecated(since = "0.23.0", note = "renamed to `PyBytes::new_with`")]
#[inline]
pub fn new_bound_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<Bound<'_, PyBytes>>
where
F: FnOnce(&mut [u8]) -> PyResult<()>,
{
Self::new_with(py, len, init)
}

/// Creates a new Python byte string object from a raw pointer and length.
///
/// Panics if out of memory.
Expand All @@ -116,11 +133,25 @@ impl PyBytes {
/// leading pointer of a slice of length `len`. [As with
/// `std::slice::from_raw_parts`, this is
/// unsafe](https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety).
pub unsafe fn bound_from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> Bound<'_, PyBytes> {
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> Bound<'_, PyBytes> {
ffi::PyBytes_FromStringAndSize(ptr.cast(), len as isize)
.assume_owned(py)
.downcast_into_unchecked()
}

/// Deprecated name for [`PyBytes::from_ptr`].
///
/// # Safety
///
/// This function dereferences the raw pointer `ptr` as the
/// leading pointer of a slice of length `len`. [As with
/// `std::slice::from_raw_parts`, this is
/// unsafe](https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety).
#[deprecated(since = "0.23.0", note = "renamed to `PyBytes::from_ptr`")]
#[inline]
pub unsafe fn bound_from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> Bound<'_, PyBytes> {
Self::from_ptr(py, ptr, len)
}
}

/// Implementation of functionality for [`PyBytes`].
Expand Down Expand Up @@ -279,15 +310,15 @@ mod tests {
#[test]
fn test_bytes_index() {
Python::with_gil(|py| {
let bytes = PyBytes::new_bound(py, b"Hello World");
let bytes = PyBytes::new(py, b"Hello World");
assert_eq!(bytes[1], b'e');
});
}

#[test]
fn test_bound_bytes_index() {
Python::with_gil(|py| {
let bytes = PyBytes::new_bound(py, b"Hello World");
let bytes = PyBytes::new(py, b"Hello World");
assert_eq!(bytes[1], b'e');

let bytes = &bytes;
Expand All @@ -298,7 +329,7 @@ mod tests {
#[test]
fn test_bytes_new_with() -> super::PyResult<()> {
Python::with_gil(|py| -> super::PyResult<()> {
let py_bytes = PyBytes::new_bound_with(py, 10, |b: &mut [u8]| {
let py_bytes = PyBytes::new_with(py, 10, |b: &mut [u8]| {
b.copy_from_slice(b"Hello Rust");
Ok(())
})?;
Expand All @@ -311,7 +342,7 @@ mod tests {
#[test]
fn test_bytes_new_with_zero_initialised() -> super::PyResult<()> {
Python::with_gil(|py| -> super::PyResult<()> {
let py_bytes = PyBytes::new_bound_with(py, 10, |_b: &mut [u8]| Ok(()))?;
let py_bytes = PyBytes::new_with(py, 10, |_b: &mut [u8]| Ok(()))?;
let bytes: &[u8] = py_bytes.extract()?;
assert_eq!(bytes, &[0; 10]);
Ok(())
Expand All @@ -322,7 +353,7 @@ mod tests {
fn test_bytes_new_with_error() {
use crate::exceptions::PyValueError;
Python::with_gil(|py| {
let py_bytes_result = PyBytes::new_bound_with(py, 10, |_b: &mut [u8]| {
let py_bytes_result = PyBytes::new_with(py, 10, |_b: &mut [u8]| {
Err(PyValueError::new_err("Hello Crustaceans!"))
});
assert!(py_bytes_result.is_err());
Expand All @@ -337,7 +368,7 @@ mod tests {
fn test_comparisons() {
Python::with_gil(|py| {
let b = b"hello, world".as_slice();
let py_bytes = PyBytes::new_bound(py, b);
let py_bytes = PyBytes::new(py, b);

assert_eq!(py_bytes, b"hello, world".as_slice());

Expand Down
4 changes: 2 additions & 2 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_pybytes_bytes_conversion() {

#[pyfunction]
fn bytes_vec_conversion(py: Python<'_>, bytes: Vec<u8>) -> Bound<'_, PyBytes> {
PyBytes::new_bound(py, bytes.as_slice())
PyBytes::new(py, bytes.as_slice())
}

#[test]
Expand All @@ -43,7 +43,7 @@ fn test_bytearray_vec_conversion() {
#[test]
fn test_py_as_bytes() {
let pyobj: pyo3::Py<pyo3::types::PyBytes> =
Python::with_gil(|py| pyo3::types::PyBytes::new_bound(py, b"abc").unbind());
Python::with_gil(|py| pyo3::types::PyBytes::new(py, b"abc").unbind());

let data = Python::with_gil(|py| pyobj.as_bytes(py));

Expand Down

0 comments on commit 6caefd1

Please sign in to comment.