Skip to content

Commit

Permalink
fix deprecated gil refs in function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Mar 20, 2024
1 parent 7c8bf45 commit 0854cab
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 144 deletions.
3 changes: 2 additions & 1 deletion pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,11 @@ fn impl_simple_enum(
fn __pyo3__richcmp__(
&self,
py: #pyo3_path::Python,
other: &#pyo3_path::PyAny,
other: &#pyo3_path::Bound<'_, #pyo3_path::PyAny>,
op: #pyo3_path::basic::CompareOp
) -> #pyo3_path::PyResult<#pyo3_path::PyObject> {
use #pyo3_path::conversion::ToPyObject;
use #pyo3_path::types::PyAnyMethods;
use ::core::result::Result::*;
match op {
#pyo3_path::basic::CompareOp::Eq => {
Expand Down
25 changes: 18 additions & 7 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,9 @@ impl Ty {
extract_error_mode,
holders,
&name_str,
quote! { #ident },ctx
quote! { #ident },
arg.ty.span(),
ctx
),
Ty::MaybeNullObject => extract_object(
extract_error_mode,
Expand All @@ -996,32 +998,40 @@ impl Ty {
} else {
#ident
}
},ctx
},
arg.ty.span(),
ctx
),
Ty::NonNullObject => extract_object(
extract_error_mode,
holders,
&name_str,
quote! { #ident.as_ptr() },ctx
quote! { #ident.as_ptr() },
arg.ty.span(),
ctx
),
Ty::IPowModulo => extract_object(
extract_error_mode,
holders,
&name_str,
quote! { #ident.as_ptr() },ctx
quote! { #ident.as_ptr() },
arg.ty.span(),
ctx
),
Ty::CompareOp => extract_error_mode.handle_error(
quote! {
#pyo3_path::class::basic::CompareOp::from_raw(#ident)
.ok_or_else(|| #pyo3_path::exceptions::PyValueError::new_err("invalid comparison operator"))
},ctx
},
ctx
),
Ty::PySsizeT => {
let ty = arg.ty;
extract_error_mode.handle_error(
quote! {
::std::convert::TryInto::<#ty>::try_into(#ident).map_err(|e| #pyo3_path::exceptions::PyValueError::new_err(e.to_string()))
},ctx
},
ctx
)
}
// Just pass other types through unmodified
Expand All @@ -1035,11 +1045,12 @@ fn extract_object(
holders: &mut Holders,
name: &str,
source_ptr: TokenStream,
span: Span,
ctx: &Ctx,
) -> TokenStream {
let Ctx { pyo3_path } = ctx;
let holder = holders.push_holder(Span::call_site());
let gil_refs_checker = holders.push_gil_refs_checker(Span::call_site());
let gil_refs_checker = holders.push_gil_refs_checker(span);
let extracted = extract_error_mode.handle_error(
quote! {
#pyo3_path::impl_::extract_argument::extract_argument(
Expand Down
4 changes: 2 additions & 2 deletions src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
exceptions::{PyAttributeError, PyRuntimeError, PyStopIteration},
panic::PanicException,
types::{string::PyStringMethods, PyIterator, PyString},
IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python,
Bound, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python,
};

pub(crate) mod cancel;
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Coroutine {
}
}

fn send(&mut self, py: Python<'_>, _value: &PyAny) -> PyResult<PyObject> {
fn send(&mut self, py: Python<'_>, _value: &Bound<'_, PyAny>) -> PyResult<PyObject> {
self.poll(py, None)
}

Expand Down
2 changes: 1 addition & 1 deletion src/coroutine/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl LoopAndFuture {
/// Future can be cancelled by the event loop before being waken.
/// See <https://github.com/python/cpython/blob/main/Lib/asyncio/tasks.py#L452C5-L452C5>
#[pyfunction(crate = "crate")]
fn release_waiter(future: &PyAny) -> PyResult<()> {
fn release_waiter(future: &Bound<'_, PyAny>) -> PyResult<()> {
let done = future.call_method0(intern!(future.py(), "done"))?;
if !done.extract::<bool>()? {
future.call_method1(intern!(future.py(), "set_result"), (future.py().None(),))?;
Expand Down
2 changes: 1 addition & 1 deletion src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@
//!
//! It is better to write that function like this:
//! ```rust
//! # #![allow(deprecated)]
//! # use pyo3::prelude::*;
//! # #[pyclass]
//! # pub struct Number {
//! # inner: u32,
//! # }
//! # #[allow(deprecated)]
//! #[pyfunction]
//! fn swap_numbers(a: &PyCell<Number>, b: &PyCell<Number>) {
//! // Check that the pointers are unequal
Expand Down
52 changes: 31 additions & 21 deletions src/tests/hygiene/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Dummy {
}

fn __format__(&self, format_spec: ::std::string::String) -> ::std::string::String {
::std::panic!("unimplemented isn't hygienic before 1.50")
::std::unimplemented!()
}

fn __lt__(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -63,12 +63,12 @@ impl Dummy {
// Customizing attribute access
//////////////////////

fn __getattr__(&self, name: ::std::string::String) -> &crate::PyAny {
::std::panic!("unimplemented isn't hygienic before 1.50")
fn __getattr__(&self, name: ::std::string::String) -> &crate::Bound<'_, crate::PyAny> {
::std::unimplemented!()
}

fn __getattribute__(&self, name: ::std::string::String) -> &crate::PyAny {
::std::panic!("unimplemented isn't hygienic before 1.50")
fn __getattribute__(&self, name: ::std::string::String) -> &crate::Bound<'_, crate::PyAny> {
::std::unimplemented!()
}

fn __setattr__(&mut self, name: ::std::string::String, value: ::std::string::String) {}
Expand All @@ -85,17 +85,27 @@ impl Dummy {

fn __get__(
&self,
instance: &crate::PyAny,
owner: &crate::PyAny,
) -> crate::PyResult<&crate::PyAny> {
::std::panic!("unimplemented isn't hygienic before 1.50")
instance: &crate::Bound<'_, crate::PyAny>,
owner: &crate::Bound<'_, crate::PyAny>,
) -> crate::PyResult<&crate::Bound<'_, crate::PyAny>> {
::std::unimplemented!()
}

fn __set__(&self, instance: &crate::PyAny, owner: &crate::PyAny) {}
fn __set__(
&self,
instance: &crate::Bound<'_, crate::PyAny>,
owner: &crate::Bound<'_, crate::PyAny>,
) {
}

fn __delete__(&self, instance: &crate::PyAny) {}
fn __delete__(&self, instance: &crate::Bound<'_, crate::PyAny>) {}

fn __set_name__(&self, owner: &crate::PyAny, name: &crate::PyAny) {}
fn __set_name__(
&self,
owner: &crate::Bound<'_, crate::PyAny>,
name: &crate::Bound<'_, crate::PyAny>,
) {
}

//////////////////////
// Implementing Descriptors
Expand Down Expand Up @@ -323,9 +333,9 @@ impl Dummy {

fn __exit__(
&mut self,
exc_type: &crate::PyAny,
exc_value: &crate::PyAny,
traceback: &crate::PyAny,
exc_type: &crate::Bound<'_, crate::PyAny>,
exc_value: &crate::Bound<'_, crate::PyAny>,
traceback: &crate::Bound<'_, crate::PyAny>,
) {
}

Expand Down Expand Up @@ -361,9 +371,9 @@ impl Dummy {

fn __aexit__(
&mut self,
exc_type: &crate::PyAny,
exc_value: &crate::PyAny,
traceback: &crate::PyAny,
exc_type: &crate::Bound<'_, crate::PyAny>,
exc_value: &crate::Bound<'_, crate::PyAny>,
traceback: &crate::Bound<'_, crate::PyAny>,
) {
}

Expand All @@ -378,10 +388,10 @@ impl Dummy {
#[pyo3(signature = (*_args, **_kwds))]
fn __call__(
&self,
_args: &crate::types::PyTuple,
_kwds: ::std::option::Option<&crate::types::PyDict>,
_args: &crate::Bound<'_, crate::types::PyTuple>,
_kwds: ::std::option::Option<&crate::Bound<'_, crate::types::PyDict>>,
) -> crate::PyResult<i32> {
::std::panic!("unimplemented isn't hygienic before 1.50")
::std::unimplemented!()
}
#[new]
fn new(a: u8) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions src/types/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl PyByteArray {
/// use pyo3::types::PyByteArray;
///
/// #[pyfunction]
/// fn a_valid_function(bytes: &PyByteArray) -> PyResult<()> {
/// fn a_valid_function(bytes: &Bound<'_, PyByteArray>) -> PyResult<()> {
/// let section = {
/// // SAFETY: We promise to not let the interpreter regain control
/// // or invoke any PyO3 APIs while using the slice.
Expand Down Expand Up @@ -224,7 +224,7 @@ impl PyByteArray {
///
/// # #[allow(dead_code)]
/// #[pyfunction]
/// fn bug(py: Python<'_>, bytes: &PyByteArray) {
/// fn bug(py: Python<'_>, bytes: &Bound<'_, PyByteArray>) {
/// let slice = unsafe { bytes.as_bytes() };
///
/// // This explicitly yields control back to the Python interpreter...
Expand Down Expand Up @@ -333,7 +333,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
/// use pyo3::types::PyByteArray;
///
/// #[pyfunction]
/// fn a_valid_function(bytes: &PyByteArray) -> PyResult<()> {
/// fn a_valid_function(bytes: &Bound<'_, PyByteArray>) -> PyResult<()> {
/// let section = {
/// // SAFETY: We promise to not let the interpreter regain control
/// // or invoke any PyO3 APIs while using the slice.
Expand Down Expand Up @@ -386,7 +386,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
///
/// # #[allow(dead_code)]
/// #[pyfunction]
/// fn bug(py: Python<'_>, bytes: &PyByteArray) {
/// fn bug(py: Python<'_>, bytes: &Bound<'_, PyByteArray>) {
/// let slice = unsafe { bytes.as_bytes() };
///
/// // This explicitly yields control back to the Python interpreter...
Expand Down
Loading

0 comments on commit 0854cab

Please sign in to comment.