Skip to content

Commit 22f960f

Browse files
committed
use Borrowed instead of newtype
1 parent 6badf8e commit 22f960f

File tree

8 files changed

+71
-132
lines changed

8 files changed

+71
-132
lines changed

guide/src/class.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a
12531253
type Holder = ::std::option::Option<pyo3::PyRef<'py, MyClass>>;
12541254

12551255
#[inline]
1256-
fn extract(obj: pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
1256+
fn extract(obj: &'a pyo3::Bound<'py, PyAny>, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
12571257
pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
12581258
}
12591259
}
@@ -1263,7 +1263,7 @@ impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a
12631263
type Holder = ::std::option::Option<pyo3::PyRefMut<'py, MyClass>>;
12641264

12651265
#[inline]
1266-
fn extract(obj: pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
1266+
fn extract(obj: &'a pyo3::Bound<'py, PyAny>, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
12671267
pyo3::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder)
12681268
}
12691269
}

pyo3-macros-backend/src/method.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,11 @@ impl SelfType {
196196
holders.push(quote_spanned! { *span =>
197197
#[allow(clippy::let_unit_value)]
198198
let mut #holder = _pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT;
199+
let mut #slf = _pyo3::Borrowed::from_ptr(#py, #slf);
199200
});
200201
error_mode.handle_error(quote_spanned! { *span =>
201202
_pyo3::impl_::extract_argument::#method::<#cls>(
202-
_pyo3::impl_::extract_argument::PyArg::from_ptr(#py, #slf),
203+
&#slf,
203204
&mut #holder,
204205
)
205206
})

pyo3-macros-backend/src/params.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub fn impl_arg_params(
4444
.collect::<Result<_>>()?;
4545
return Ok((
4646
quote! {
47-
let _args = _pyo3::impl_::extract_argument::PyArg::from_ptr(py, _args);
48-
let _kwargs = _pyo3::impl_::extract_argument::PyArg::from_ptr_or_opt(py, _kwargs);
47+
let _args = _pyo3::Borrowed::from_ptr(py, _args);
48+
let _kwargs = _pyo3::Borrowed::from_ptr_or_opt(py, _kwargs);
4949
},
5050
arg_convert,
5151
));
@@ -180,7 +180,7 @@ fn impl_arg_param(
180180
let holder = push_holder();
181181
return Ok(quote_arg_span! {
182182
_pyo3::impl_::extract_argument::extract_argument(
183-
_pyo3::impl_::extract_argument::PyArg::from_borrowed(_args.as_borrowed()),
183+
&_args,
184184
&mut #holder,
185185
#name_str
186186
)?
@@ -193,7 +193,7 @@ fn impl_arg_param(
193193
let holder = push_holder();
194194
return Ok(quote_arg_span! {
195195
_pyo3::impl_::extract_argument::extract_optional_argument(
196-
_kwargs.as_ref().map(|kwargs| _pyo3::impl_::extract_argument::PyArg::from_borrowed(kwargs.as_borrowed())),
196+
_kwargs.as_deref(),
197197
&mut #holder,
198198
#name_str,
199199
|| ::std::option::Option::None
@@ -217,7 +217,7 @@ fn impl_arg_param(
217217
quote_arg_span! {
218218
#[allow(clippy::redundant_closure)]
219219
_pyo3::impl_::extract_argument::from_py_with_with_default(
220-
#arg_value.map(_pyo3::impl_::extract_argument::PyArg::as_borrowed).as_deref(),
220+
#arg_value.as_deref(),
221221
#name_str,
222222
#expr_path as fn(_) -> _,
223223
|| #default
@@ -226,7 +226,7 @@ fn impl_arg_param(
226226
} else {
227227
quote_arg_span! {
228228
_pyo3::impl_::extract_argument::from_py_with(
229-
&_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value).as_borrowed(),
229+
&_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value),
230230
#name_str,
231231
#expr_path as fn(_) -> _,
232232
)?
@@ -237,7 +237,7 @@ fn impl_arg_param(
237237
quote_arg_span! {
238238
#[allow(clippy::redundant_closure)]
239239
_pyo3::impl_::extract_argument::extract_optional_argument(
240-
#arg_value,
240+
#arg_value.as_deref(),
241241
&mut #holder,
242242
#name_str,
243243
|| #default
@@ -248,7 +248,7 @@ fn impl_arg_param(
248248
quote_arg_span! {
249249
#[allow(clippy::redundant_closure)]
250250
_pyo3::impl_::extract_argument::extract_argument_with_default(
251-
#arg_value,
251+
#arg_value.as_deref(),
252252
&mut #holder,
253253
#name_str,
254254
|| #default
@@ -258,7 +258,7 @@ fn impl_arg_param(
258258
let holder = push_holder();
259259
quote_arg_span! {
260260
_pyo3::impl_::extract_argument::extract_argument(
261-
_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value),
261+
&_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value),
262262
&mut #holder,
263263
#name_str
264264
)?

pyo3-macros-backend/src/pyclass.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ impl<'a> PyClassImplsBuilder<'a> {
13701370
type Holder = ::std::option::Option<_pyo3::PyRef<'py, #cls>>;
13711371

13721372
#[inline]
1373-
fn extract(obj: _pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
1373+
fn extract(obj: &'a _pyo3::Bound<'py, _pyo3::PyAny>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
13741374
_pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
13751375
}
13761376
}
@@ -1382,7 +1382,7 @@ impl<'a> PyClassImplsBuilder<'a> {
13821382
type Holder = ::std::option::Option<_pyo3::PyRef<'py, #cls>>;
13831383

13841384
#[inline]
1385-
fn extract(obj: _pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
1385+
fn extract(obj: &'a _pyo3::Bound<'py, _pyo3::PyAny>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
13861386
_pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
13871387
}
13881388
}
@@ -1392,7 +1392,7 @@ impl<'a> PyClassImplsBuilder<'a> {
13921392
type Holder = ::std::option::Option<_pyo3::PyRefMut<'py, #cls>>;
13931393

13941394
#[inline]
1395-
fn extract(obj: _pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
1395+
fn extract(obj: &'a _pyo3::Bound<'py, _pyo3::PyAny>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
13961396
_pyo3::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder)
13971397
}
13981398
}

pyo3-macros-backend/src/pymethod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ fn extract_object(
990990
});
991991
extract_error_mode.handle_error(quote! {
992992
_pyo3::impl_::extract_argument::extract_argument(
993-
_pyo3::impl_::extract_argument::PyArg::from_ptr(py, #source_ptr),
993+
&_pyo3::Borrowed::from_ptr(py, #source_ptr),
994994
&mut #holder,
995995
#name
996996
)

src/err/mod.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
exceptions::{self, PyBaseException},
88
ffi,
99
};
10-
use crate::{Borrowed, IntoPy, Py, PyAny, PyNativeType, PyObject, Python, ToPyObject};
10+
use crate::{IntoPy, Py, PyAny, PyNativeType, PyObject, Python, ToPyObject};
1111
use std::borrow::Cow;
1212
use std::cell::UnsafeCell;
1313
use std::ffi::CString;
@@ -46,50 +46,43 @@ unsafe impl Sync for PyErr {}
4646
pub type PyResult<T> = Result<T, PyErr>;
4747

4848
/// Error that indicates a failure to convert a PyAny to a more specific Python type.
49-
pub struct PyDowncastError<'py>(DowncastError<'py, 'py>);
49+
#[derive(Debug)]
50+
pub struct PyDowncastError<'a> {
51+
from: &'a PyAny,
52+
to: Cow<'static, str>,
53+
}
5054

5155
impl<'a> PyDowncastError<'a> {
5256
/// Create a new `PyDowncastError` representing a failure to convert the object
5357
/// `from` into the type named in `to`.
5458
pub fn new(from: &'a PyAny, to: impl Into<Cow<'static, str>>) -> Self {
55-
PyDowncastError(DowncastError::new_from_borrowed(from.as_borrowed(), to))
59+
PyDowncastError {
60+
from,
61+
to: to.into(),
62+
}
5663
}
5764

5865
/// Compatibility API to convert the Bound variant `DowncastError` into the
5966
/// gil-ref variant
60-
pub(crate) fn from_downcast_err(err: DowncastError<'a, 'a>) -> Self {
61-
Self(err)
62-
}
63-
}
64-
65-
impl std::fmt::Debug for PyDowncastError<'_> {
66-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67-
f.debug_struct("PyDowncastError")
68-
.field("from", &self.0.from)
69-
.field("to", &self.0.to)
70-
.finish()
67+
pub(crate) fn from_downcast_err(DowncastError { from, to }: DowncastError<'a, 'a>) -> Self {
68+
Self {
69+
from: from.as_gil_ref(),
70+
to,
71+
}
7172
}
7273
}
7374

7475
/// Error that indicates a failure to convert a PyAny to a more specific Python type.
7576
#[derive(Debug)]
7677
pub struct DowncastError<'a, 'py> {
77-
from: Borrowed<'a, 'py, PyAny>,
78+
from: &'a Bound<'py, PyAny>,
7879
to: Cow<'static, str>,
7980
}
8081

8182
impl<'a, 'py> DowncastError<'a, 'py> {
8283
/// Create a new `PyDowncastError` representing a failure to convert the object
8384
/// `from` into the type named in `to`.
8485
pub fn new(from: &'a Bound<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
85-
Self::new_from_borrowed(from.as_borrowed(), to)
86-
}
87-
88-
/// Similar to [`DowncastError::new`], but from a `Borrowed` instead of a `Bound`.
89-
pub(crate) fn new_from_borrowed(
90-
from: Borrowed<'a, 'py, PyAny>,
91-
to: impl Into<Cow<'static, str>>,
92-
) -> Self {
9386
DowncastError {
9487
from,
9588
to: to.into(),
@@ -977,15 +970,20 @@ impl PyErrArguments for PyDowncastErrorArguments {
977970
/// Convert `PyDowncastError` to Python `TypeError`.
978971
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
979972
fn from(err: PyDowncastError<'_>) -> PyErr {
980-
PyErr::from(err.0)
973+
let args = PyDowncastErrorArguments {
974+
from: err.from.get_type().into(),
975+
to: err.to,
976+
};
977+
978+
exceptions::PyTypeError::new_err(args)
981979
}
982980
}
983981

984982
impl<'a> std::error::Error for PyDowncastError<'a> {}
985983

986984
impl<'a> std::fmt::Display for PyDowncastError<'a> {
987985
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
988-
self.0.fmt(f)
986+
display_downcast_error(f, &self.from.as_borrowed(), &self.to)
989987
}
990988
}
991989

@@ -1025,13 +1023,13 @@ impl std::error::Error for DowncastIntoError<'_> {}
10251023

10261024
impl std::fmt::Display for DowncastIntoError<'_> {
10271025
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
1028-
display_downcast_error(f, self.from.as_borrowed(), &self.to)
1026+
display_downcast_error(f, &self.from, &self.to)
10291027
}
10301028
}
10311029

10321030
fn display_downcast_error(
10331031
f: &mut std::fmt::Formatter<'_>,
1034-
from: Borrowed<'_, '_, PyAny>,
1032+
from: &Bound<'_, PyAny>,
10351033
to: &str,
10361034
) -> std::fmt::Result {
10371035
write!(

0 commit comments

Comments
 (0)