Skip to content

Commit 2ee7c32

Browse files
Reintroduce constructors for PyByteArray, PyComplex, PyFloat, PyEllipsis, PyFrozenSet (#4409)
* Reintroduce `bytearray` constructors * Reintroduce `PyComplex` constructors * Reintroduce `PyFloat` constructors * Reintroduce `PyEllipsis` constructors * Reintroduce `PyFrozenSet` constructors
1 parent ef0f544 commit 2ee7c32

File tree

13 files changed

+153
-84
lines changed

13 files changed

+153
-84
lines changed

guide/src/class/numeric.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Number {
171171
}
172172

173173
fn __complex__<'py>(&self, py: Python<'py>) -> Bound<'py, PyComplex> {
174-
PyComplex::from_doubles_bound(py, self.0 as f64, 0.0)
174+
PyComplex::from_doubles(py, self.0 as f64, 0.0)
175175
}
176176
}
177177
```
@@ -321,7 +321,7 @@ impl Number {
321321
}
322322

323323
fn __complex__<'py>(&self, py: Python<'py>) -> Bound<'py, PyComplex> {
324-
PyComplex::from_doubles_bound(py, self.0 as f64, 0.0)
324+
PyComplex::from_doubles(py, self.0 as f64, 0.0)
325325
}
326326
}
327327

src/conversions/hashbrown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ mod tests {
185185
let hash_set: hashbrown::HashSet<usize> = set.extract().unwrap();
186186
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());
187187

188-
let set = PyFrozenSet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
188+
let set = PyFrozenSet::new(py, &[1, 2, 3, 4, 5]).unwrap();
189189
let hash_set: hashbrown::HashSet<usize> = set.extract().unwrap();
190190
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());
191191
});

src/conversions/num_complex.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
//! #
6060
//! # module.add_function(&wrap_pyfunction!(get_eigenvalues, module)?)?;
6161
//! #
62-
//! # let m11 = PyComplex::from_doubles_bound(py, 0_f64, -1_f64);
63-
//! # let m12 = PyComplex::from_doubles_bound(py, 1_f64, 0_f64);
64-
//! # let m21 = PyComplex::from_doubles_bound(py, 2_f64, -1_f64);
65-
//! # let m22 = PyComplex::from_doubles_bound(py, -1_f64, 0_f64);
62+
//! # let m11 = PyComplex::from_doubles(py, 0_f64, -1_f64);
63+
//! # let m12 = PyComplex::from_doubles(py, 1_f64, 0_f64);
64+
//! # let m21 = PyComplex::from_doubles(py, 2_f64, -1_f64);
65+
//! # let m22 = PyComplex::from_doubles(py, -1_f64, 0_f64);
6666
//! #
6767
//! # let result = module
6868
//! # .getattr("get_eigenvalues")?

src/conversions/std/set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ mod tests {
127127
let hash_set: HashSet<usize> = set.extract().unwrap();
128128
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());
129129

130-
let set = PyFrozenSet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
130+
let set = PyFrozenSet::new(py, &[1, 2, 3, 4, 5]).unwrap();
131131
let hash_set: HashSet<usize> = set.extract().unwrap();
132132
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());
133133
});
@@ -140,7 +140,7 @@ mod tests {
140140
let hash_set: BTreeSet<usize> = set.extract().unwrap();
141141
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());
142142

143-
let set = PyFrozenSet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
143+
let set = PyFrozenSet::new(py, &[1, 2, 3, 4, 5]).unwrap();
144144
let hash_set: BTreeSet<usize> = set.extract().unwrap();
145145
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());
146146
});

src/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl<'py> Python<'py> {
677677
#[allow(non_snake_case)] // the Python keyword starts with uppercase
678678
#[inline]
679679
pub fn Ellipsis(self) -> PyObject {
680-
PyEllipsis::get_bound(self).into_py(self)
680+
PyEllipsis::get(self).into_py(self)
681681
}
682682

683683
/// Gets the Python builtin value `NotImplemented`.

src/pybacked.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ mod test {
382382
#[test]
383383
fn py_backed_bytes_from_bytearray() {
384384
Python::with_gil(|py| {
385-
let b = PyByteArray::new_bound(py, b"abcde");
385+
let b = PyByteArray::new(py, b"abcde");
386386
let py_backed_bytes = PyBackedBytes::from(b);
387387
assert_eq!(&*py_backed_bytes, b"abcde");
388388
});
@@ -401,7 +401,7 @@ mod test {
401401
#[test]
402402
fn rust_backed_bytes_into_py() {
403403
Python::with_gil(|py| {
404-
let orig_bytes = PyByteArray::new_bound(py, b"abcde");
404+
let orig_bytes = PyByteArray::new(py, b"abcde");
405405
let rust_backed_bytes = PyBackedBytes::from(orig_bytes);
406406
assert!(matches!(
407407
rust_backed_bytes.storage,
@@ -508,7 +508,7 @@ mod test {
508508
#[test]
509509
fn test_backed_bytes_from_bytearray_clone() {
510510
Python::with_gil(|py| {
511-
let b1: PyBackedBytes = PyByteArray::new_bound(py, b"abcde").into();
511+
let b1: PyBackedBytes = PyByteArray::new(py, b"abcde").into();
512512
let b2 = b1.clone();
513513
assert_eq!(b1, b2);
514514

@@ -521,7 +521,7 @@ mod test {
521521
fn test_backed_bytes_eq() {
522522
Python::with_gil(|py| {
523523
let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into();
524-
let b2: PyBackedBytes = PyByteArray::new_bound(py, b"abcde").into();
524+
let b2: PyBackedBytes = PyByteArray::new(py, b"abcde").into();
525525

526526
assert_eq!(b1, b"abcde");
527527
assert_eq!(b1, b2);
@@ -548,7 +548,7 @@ mod test {
548548
hasher.finish()
549549
};
550550

551-
let b2: PyBackedBytes = PyByteArray::new_bound(py, b"abcde").into();
551+
let b2: PyBackedBytes = PyByteArray::new(py, b"abcde").into();
552552
let h2 = {
553553
let mut hasher = DefaultHasher::new();
554554
b2.hash(&mut hasher);

src/tests/hygiene/pymethods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl Dummy {
291291
&self,
292292
py: crate::Python<'py>,
293293
) -> crate::Bound<'py, crate::types::PyComplex> {
294-
crate::types::PyComplex::from_doubles_bound(py, 0.0, 0.0)
294+
crate::types::PyComplex::from_doubles(py, 0.0, 0.0)
295295
}
296296

297297
fn __int__(&self) -> u32 {

src/types/any.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
176176
///
177177
/// # fn main() -> PyResult<()> {
178178
/// Python::with_gil(|py| -> PyResult<()> {
179-
/// let a = PyFloat::new_bound(py, 0_f64);
180-
/// let b = PyFloat::new_bound(py, 42_f64);
179+
/// let a = PyFloat::new(py, 0_f64);
180+
/// let b = PyFloat::new(py, 42_f64);
181181
/// assert_eq!(a.compare(b)?, Ordering::Less);
182182
/// Ok(())
183183
/// })?;
@@ -192,7 +192,7 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
192192
///
193193
/// # fn main() -> PyResult<()> {
194194
/// Python::with_gil(|py| -> PyResult<()> {
195-
/// let a = PyFloat::new_bound(py, 0_f64);
195+
/// let a = PyFloat::new(py, 0_f64);
196196
/// let b = PyString::new_bound(py, "zero");
197197
/// assert!(a.compare(b).is_err());
198198
/// Ok(())

src/types/bytearray.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl PyByteArray {
2222
/// Creates a new Python bytearray object.
2323
///
2424
/// The byte string is initialized by copying the data from the `&[u8]`.
25-
pub fn new_bound<'py>(py: Python<'py>, src: &[u8]) -> Bound<'py, PyByteArray> {
25+
pub fn new<'py>(py: Python<'py>, src: &[u8]) -> Bound<'py, PyByteArray> {
2626
let ptr = src.as_ptr().cast();
2727
let len = src.len() as ffi::Py_ssize_t;
2828
unsafe {
@@ -32,6 +32,13 @@ impl PyByteArray {
3232
}
3333
}
3434

35+
/// Deprecated name for [`PyByteArray::new`].
36+
#[deprecated(since = "0.23.0", note = "renamed to `PyByteArray::new`")]
37+
#[inline]
38+
pub fn new_bound<'py>(py: Python<'py>, src: &[u8]) -> Bound<'py, PyByteArray> {
39+
Self::new(py, src)
40+
}
41+
3542
/// Creates a new Python `bytearray` object with an `init` closure to write its contents.
3643
/// Before calling `init` the bytearray is zero-initialised.
3744
/// * If Python raises a MemoryError on the allocation, `new_with` will return
@@ -46,7 +53,7 @@ impl PyByteArray {
4653
///
4754
/// # fn main() -> PyResult<()> {
4855
/// Python::with_gil(|py| -> PyResult<()> {
49-
/// let py_bytearray = PyByteArray::new_bound_with(py, 10, |bytes: &mut [u8]| {
56+
/// let py_bytearray = PyByteArray::new_with(py, 10, |bytes: &mut [u8]| {
5057
/// bytes.copy_from_slice(b"Hello Rust");
5158
/// Ok(())
5259
/// })?;
@@ -56,11 +63,7 @@ impl PyByteArray {
5663
/// })
5764
/// # }
5865
/// ```
59-
pub fn new_bound_with<F>(
60-
py: Python<'_>,
61-
len: usize,
62-
init: F,
63-
) -> PyResult<Bound<'_, PyByteArray>>
66+
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<Bound<'_, PyByteArray>>
6467
where
6568
F: FnOnce(&mut [u8]) -> PyResult<()>,
6669
{
@@ -81,15 +84,36 @@ impl PyByteArray {
8184
}
8285
}
8386

87+
/// Deprecated name for [`PyByteArray::new_with`].
88+
#[deprecated(since = "0.23.0", note = "renamed to `PyByteArray::new_with`")]
89+
#[inline]
90+
pub fn new_bound_with<F>(
91+
py: Python<'_>,
92+
len: usize,
93+
init: F,
94+
) -> PyResult<Bound<'_, PyByteArray>>
95+
where
96+
F: FnOnce(&mut [u8]) -> PyResult<()>,
97+
{
98+
Self::new_with(py, len, init)
99+
}
100+
84101
/// Creates a new Python `bytearray` object from another Python object that
85102
/// implements the buffer protocol.
86-
pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> {
103+
pub fn from<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> {
87104
unsafe {
88105
ffi::PyByteArray_FromObject(src.as_ptr())
89106
.assume_owned_or_err(src.py())
90107
.downcast_into_unchecked()
91108
}
92109
}
110+
111+
///Deprecated name for [`PyByteArray::from`].
112+
#[deprecated(since = "0.23.0", note = "renamed to `PyByteArray::from`")]
113+
#[inline]
114+
pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> {
115+
Self::from(src)
116+
}
93117
}
94118

95119
/// Implementation of functionality for [`PyByteArray`].
@@ -227,7 +251,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
227251
/// # use pyo3::prelude::*;
228252
/// # use pyo3::types::PyByteArray;
229253
/// # Python::with_gil(|py| {
230-
/// let bytearray = PyByteArray::new_bound(py, b"Hello World.");
254+
/// let bytearray = PyByteArray::new(py, b"Hello World.");
231255
/// let mut copied_message = bytearray.to_vec();
232256
/// assert_eq!(b"Hello World.", copied_message.as_slice());
233257
///
@@ -308,7 +332,7 @@ impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyByteArray> {
308332
/// Creates a new Python `bytearray` object from another Python object that
309333
/// implements the buffer protocol.
310334
fn try_from(value: &Bound<'py, PyAny>) -> Result<Self, Self::Error> {
311-
PyByteArray::from_bound(value)
335+
PyByteArray::from(value)
312336
}
313337
}
314338

@@ -321,7 +345,7 @@ mod tests {
321345
fn test_len() {
322346
Python::with_gil(|py| {
323347
let src = b"Hello Python";
324-
let bytearray = PyByteArray::new_bound(py, src);
348+
let bytearray = PyByteArray::new(py, src);
325349
assert_eq!(src.len(), bytearray.len());
326350
});
327351
}
@@ -330,7 +354,7 @@ mod tests {
330354
fn test_as_bytes() {
331355
Python::with_gil(|py| {
332356
let src = b"Hello Python";
333-
let bytearray = PyByteArray::new_bound(py, src);
357+
let bytearray = PyByteArray::new(py, src);
334358

335359
let slice = unsafe { bytearray.as_bytes() };
336360
assert_eq!(src, slice);
@@ -342,7 +366,7 @@ mod tests {
342366
fn test_as_bytes_mut() {
343367
Python::with_gil(|py| {
344368
let src = b"Hello Python";
345-
let bytearray = PyByteArray::new_bound(py, src);
369+
let bytearray = PyByteArray::new(py, src);
346370

347371
let slice = unsafe { bytearray.as_bytes_mut() };
348372
assert_eq!(src, slice);
@@ -358,7 +382,7 @@ mod tests {
358382
fn test_to_vec() {
359383
Python::with_gil(|py| {
360384
let src = b"Hello Python";
361-
let bytearray = PyByteArray::new_bound(py, src);
385+
let bytearray = PyByteArray::new(py, src);
362386

363387
let vec = bytearray.to_vec();
364388
assert_eq!(src, vec.as_slice());
@@ -369,10 +393,10 @@ mod tests {
369393
fn test_from() {
370394
Python::with_gil(|py| {
371395
let src = b"Hello Python";
372-
let bytearray = PyByteArray::new_bound(py, src);
396+
let bytearray = PyByteArray::new(py, src);
373397

374398
let ba: PyObject = bytearray.into();
375-
let bytearray = PyByteArray::from_bound(ba.bind(py)).unwrap();
399+
let bytearray = PyByteArray::from(ba.bind(py)).unwrap();
376400

377401
assert_eq!(src, unsafe { bytearray.as_bytes() });
378402
});
@@ -381,7 +405,7 @@ mod tests {
381405
#[test]
382406
fn test_from_err() {
383407
Python::with_gil(|py| {
384-
if let Err(err) = PyByteArray::from_bound(py.None().bind(py)) {
408+
if let Err(err) = PyByteArray::from(py.None().bind(py)) {
385409
assert!(err.is_instance_of::<exceptions::PyTypeError>(py));
386410
} else {
387411
panic!("error");
@@ -393,7 +417,7 @@ mod tests {
393417
fn test_try_from() {
394418
Python::with_gil(|py| {
395419
let src = b"Hello Python";
396-
let bytearray: &Bound<'_, PyAny> = &PyByteArray::new_bound(py, src);
420+
let bytearray: &Bound<'_, PyAny> = &PyByteArray::new(py, src);
397421
let bytearray: Bound<'_, PyByteArray> = TryInto::try_into(bytearray).unwrap();
398422

399423
assert_eq!(src, unsafe { bytearray.as_bytes() });
@@ -404,7 +428,7 @@ mod tests {
404428
fn test_resize() {
405429
Python::with_gil(|py| {
406430
let src = b"Hello Python";
407-
let bytearray = PyByteArray::new_bound(py, src);
431+
let bytearray = PyByteArray::new(py, src);
408432

409433
bytearray.resize(20).unwrap();
410434
assert_eq!(20, bytearray.len());
@@ -414,7 +438,7 @@ mod tests {
414438
#[test]
415439
fn test_byte_array_new_with() -> super::PyResult<()> {
416440
Python::with_gil(|py| -> super::PyResult<()> {
417-
let py_bytearray = PyByteArray::new_bound_with(py, 10, |b: &mut [u8]| {
441+
let py_bytearray = PyByteArray::new_with(py, 10, |b: &mut [u8]| {
418442
b.copy_from_slice(b"Hello Rust");
419443
Ok(())
420444
})?;
@@ -427,7 +451,7 @@ mod tests {
427451
#[test]
428452
fn test_byte_array_new_with_zero_initialised() -> super::PyResult<()> {
429453
Python::with_gil(|py| -> super::PyResult<()> {
430-
let py_bytearray = PyByteArray::new_bound_with(py, 10, |_b: &mut [u8]| Ok(()))?;
454+
let py_bytearray = PyByteArray::new_with(py, 10, |_b: &mut [u8]| Ok(()))?;
431455
let bytearray: &[u8] = unsafe { py_bytearray.as_bytes() };
432456
assert_eq!(bytearray, &[0; 10]);
433457
Ok(())
@@ -438,7 +462,7 @@ mod tests {
438462
fn test_byte_array_new_with_error() {
439463
use crate::exceptions::PyValueError;
440464
Python::with_gil(|py| {
441-
let py_bytearray_result = PyByteArray::new_bound_with(py, 10, |_b: &mut [u8]| {
465+
let py_bytearray_result = PyByteArray::new_with(py, 10, |_b: &mut [u8]| {
442466
Err(PyValueError::new_err("Hello Crustaceans!"))
443467
});
444468
assert!(py_bytearray_result.is_err());

0 commit comments

Comments
 (0)