Skip to content

Commit a115877

Browse files
authored
Merge pull request #3686 from davidhewitt/bound
make Bound and Borrowed types public API
2 parents 1fa47b0 + 4ac6a6b commit a115877

21 files changed

+577
-573
lines changed

newsfragments/3686.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `Bound<T>` and `Borrowed<T>` smart pointers as a new API for accessing Python objects.

src/err/mod.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::instance::Py2;
1+
use crate::instance::Bound;
22
use crate::panic::PanicException;
33
use crate::type_object::PyTypeInfo;
44
use crate::types::any::PyAnyMethods;
@@ -65,16 +65,16 @@ impl<'a> PyDowncastError<'a> {
6565

6666
/// Error that indicates a failure to convert a PyAny to a more specific Python type.
6767
#[derive(Debug)]
68-
pub(crate) struct PyDowncastError2<'a, 'py> {
69-
from: &'a Py2<'py, PyAny>,
68+
pub struct DowncastError<'a, 'py> {
69+
from: &'a Bound<'py, PyAny>,
7070
to: Cow<'static, str>,
7171
}
7272

73-
impl<'a, 'py> PyDowncastError2<'a, 'py> {
73+
impl<'a, 'py> DowncastError<'a, 'py> {
7474
/// Create a new `PyDowncastError` representing a failure to convert the object
7575
/// `from` into the type named in `to`.
76-
pub fn new(from: &'a Py2<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
77-
PyDowncastError2 {
76+
pub fn new(from: &'a Bound<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
77+
DowncastError {
7878
from,
7979
to: to.into(),
8080
}
@@ -83,16 +83,16 @@ impl<'a, 'py> PyDowncastError2<'a, 'py> {
8383

8484
/// Error that indicates a failure to convert a PyAny to a more specific Python type.
8585
#[derive(Debug)]
86-
pub(crate) struct PyDowncastIntoError<'py> {
87-
from: Py2<'py, PyAny>,
86+
pub struct DowncastIntoError<'py> {
87+
from: Bound<'py, PyAny>,
8888
to: Cow<'static, str>,
8989
}
9090

91-
impl<'py> PyDowncastIntoError<'py> {
92-
/// Create a new `PyDowncastIntoError` representing a failure to convert the object
91+
impl<'py> DowncastIntoError<'py> {
92+
/// Create a new `DowncastIntoError` representing a failure to convert the object
9393
/// `from` into the type named in `to`.
94-
pub fn new(from: Py2<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
95-
PyDowncastIntoError {
94+
pub fn new(from: Bound<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
95+
DowncastIntoError {
9696
from,
9797
to: to.into(),
9898
}
@@ -811,13 +811,13 @@ impl<'a> std::error::Error for PyDowncastError<'a> {}
811811

812812
impl<'a> std::fmt::Display for PyDowncastError<'a> {
813813
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
814-
display_downcast_error(f, Py2::borrowed_from_gil_ref(&self.from), &self.to)
814+
display_downcast_error(f, Bound::borrowed_from_gil_ref(&self.from), &self.to)
815815
}
816816
}
817817

818-
/// Convert `PyDowncastError2` to Python `TypeError`.
819-
impl std::convert::From<PyDowncastError2<'_, '_>> for PyErr {
820-
fn from(err: PyDowncastError2<'_, '_>) -> PyErr {
818+
/// Convert `DowncastError` to Python `TypeError`.
819+
impl std::convert::From<DowncastError<'_, '_>> for PyErr {
820+
fn from(err: DowncastError<'_, '_>) -> PyErr {
821821
let args = PyDowncastErrorArguments {
822822
from: err.from.get_type().into(),
823823
to: err.to,
@@ -827,17 +827,17 @@ impl std::convert::From<PyDowncastError2<'_, '_>> for PyErr {
827827
}
828828
}
829829

830-
impl std::error::Error for PyDowncastError2<'_, '_> {}
830+
impl std::error::Error for DowncastError<'_, '_> {}
831831

832-
impl std::fmt::Display for PyDowncastError2<'_, '_> {
832+
impl std::fmt::Display for DowncastError<'_, '_> {
833833
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
834834
display_downcast_error(f, self.from, &self.to)
835835
}
836836
}
837837

838-
/// Convert `PyDowncastIntoError` to Python `TypeError`.
839-
impl std::convert::From<PyDowncastIntoError<'_>> for PyErr {
840-
fn from(err: PyDowncastIntoError<'_>) -> PyErr {
838+
/// Convert `DowncastIntoError` to Python `TypeError`.
839+
impl std::convert::From<DowncastIntoError<'_>> for PyErr {
840+
fn from(err: DowncastIntoError<'_>) -> PyErr {
841841
let args = PyDowncastErrorArguments {
842842
from: err.from.get_type().into(),
843843
to: err.to,
@@ -847,17 +847,17 @@ impl std::convert::From<PyDowncastIntoError<'_>> for PyErr {
847847
}
848848
}
849849

850-
impl std::error::Error for PyDowncastIntoError<'_> {}
850+
impl std::error::Error for DowncastIntoError<'_> {}
851851

852-
impl std::fmt::Display for PyDowncastIntoError<'_> {
852+
impl std::fmt::Display for DowncastIntoError<'_> {
853853
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
854854
display_downcast_error(f, &self.from, &self.to)
855855
}
856856
}
857857

858858
fn display_downcast_error(
859859
f: &mut std::fmt::Formatter<'_>,
860-
from: &Py2<'_, PyAny>,
860+
from: &Bound<'_, PyAny>,
861861
to: &str,
862862
) -> std::fmt::Result {
863863
write!(

src/ffi_ptr_ext.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
ffi,
3-
instance::{Py2, Py2Borrowed},
3+
instance::{Borrowed, Bound},
44
PyAny, PyResult, Python,
55
};
66

@@ -15,65 +15,57 @@ mod sealed {
1515
use sealed::Sealed;
1616

1717
pub(crate) trait FfiPtrExt: Sealed {
18-
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Py2<'_, PyAny>>;
19-
unsafe fn assume_owned(self, py: Python<'_>) -> Py2<'_, PyAny>;
18+
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>>;
19+
unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny>;
2020

2121
/// Assumes this pointer is borrowed from a parent object.
2222
///
2323
/// Warning: the lifetime `'a` is not bounded by the function arguments; the caller is
2424
/// responsible to ensure this is tied to some appropriate lifetime.
25-
unsafe fn assume_borrowed_or_err<'a>(
26-
self,
27-
py: Python<'_>,
28-
) -> PyResult<Py2Borrowed<'a, '_, PyAny>>;
25+
unsafe fn assume_borrowed_or_err<'a>(self, py: Python<'_>)
26+
-> PyResult<Borrowed<'a, '_, PyAny>>;
2927

3028
/// Same as `assume_borrowed_or_err`, but doesn't fetch an error on NULL.
31-
unsafe fn assume_borrowed_or_opt<'a>(
32-
self,
33-
py: Python<'_>,
34-
) -> Option<Py2Borrowed<'a, '_, PyAny>>;
29+
unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>>;
3530

3631
/// Same as `assume_borrowed_or_err`, but panics on NULL.
37-
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny>;
32+
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>;
3833

3934
/// Same as `assume_borrowed_or_err`, but does not check for NULL.
40-
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny>;
35+
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>;
4136
}
4237

4338
impl FfiPtrExt for *mut ffi::PyObject {
4439
#[inline]
45-
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Py2<'_, PyAny>> {
46-
Py2::from_owned_ptr_or_err(py, self)
40+
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
41+
Bound::from_owned_ptr_or_err(py, self)
4742
}
4843

4944
#[inline]
50-
unsafe fn assume_owned(self, py: Python<'_>) -> Py2<'_, PyAny> {
51-
Py2::from_owned_ptr(py, self)
45+
unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny> {
46+
Bound::from_owned_ptr(py, self)
5247
}
5348

5449
#[inline]
5550
unsafe fn assume_borrowed_or_err<'a>(
5651
self,
5752
py: Python<'_>,
58-
) -> PyResult<Py2Borrowed<'a, '_, PyAny>> {
59-
Py2Borrowed::from_ptr_or_err(py, self)
53+
) -> PyResult<Borrowed<'a, '_, PyAny>> {
54+
Borrowed::from_ptr_or_err(py, self)
6055
}
6156

6257
#[inline]
63-
unsafe fn assume_borrowed_or_opt<'a>(
64-
self,
65-
py: Python<'_>,
66-
) -> Option<Py2Borrowed<'a, '_, PyAny>> {
67-
Py2Borrowed::from_ptr_or_opt(py, self)
58+
unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>> {
59+
Borrowed::from_ptr_or_opt(py, self)
6860
}
6961

7062
#[inline]
71-
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny> {
72-
Py2Borrowed::from_ptr(py, self)
63+
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
64+
Borrowed::from_ptr(py, self)
7365
}
7466

7567
#[inline]
76-
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny> {
77-
Py2Borrowed::from_ptr_unchecked(py, self)
68+
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
69+
Borrowed::from_ptr_unchecked(py, self)
7870
}
7971
}

0 commit comments

Comments
 (0)