diff --git a/Cargo.lock b/Cargo.lock index bb3bb2601..4e029bfe6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1801,9 +1801,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e484fd2c8b4cb67ab05a318f1fd6fa8f199fcc30819f08f07d200809dba26c15" +checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884" dependencies = [ "cfg-if", "indoc", @@ -1821,9 +1821,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc0e0469a84f208e20044b98965e1561028180219e35352a2afaf2b942beff3b" +checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38" dependencies = [ "once_cell", "target-lexicon", @@ -1831,9 +1831,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb1547a7f9966f6f1a0f0227564a9945fe36b90da5a93b3933fc3dc03fae372d" +checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636" dependencies = [ "libc", "pyo3-build-config", @@ -1841,9 +1841,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb6da8ec6fa5cedd1626c886fc8749bdcbb09424a86461eb8cdf096b7c33257" +checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -1853,9 +1853,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38a385202ff5a92791168b1136afae5059d3ac118457bb7bc304c197c2d33e7d" +checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 3b4b8dd17..2e5821ef1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -133,7 +133,7 @@ sha2 = "0.10.8" hkdf = "0.12.0" hex = "0.4.3" thiserror = "1.0.69" -pyo3 = "0.23.3" +pyo3 = "0.22.6" arbitrary = "1.4.1" rand = "0.8.5" criterion = "0.5.1" diff --git a/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs b/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs index 3d4f385c4..37beb1a60 100644 --- a/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs +++ b/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs @@ -35,7 +35,7 @@ fuzz_target!(|data: &[u8]| { print(sys.executable) "#, None, None).unwrap(); */ - let blspy = py.import("blspy").unwrap(); + let blspy = py.import_bound("blspy").unwrap(); let aug = blspy.getattr("AugSchemeMPL").unwrap(); // Generate key pair from seed @@ -43,7 +43,7 @@ fuzz_target!(|data: &[u8]| { let py_sk = aug .call_method1( "key_gen", - PyTuple::new(py, [PyBytes::new(py, data)]).unwrap(), + PyTuple::new_bound(py, [PyBytes::new_bound(py, data)]), ) .unwrap(); @@ -66,14 +66,10 @@ fuzz_target!(|data: &[u8]| { let py_sk1 = aug .call_method1( "derive_child_sk_unhardened", - PyTuple::new( + PyTuple::new_bound( py, - [ - py_sk.clone(), - idx.into_pyobject(py).unwrap().clone().into_any(), - ], - ) - .unwrap(), + [py_sk.clone(), idx.to_object(py).bind(py).clone().into_any()], + ), ) .unwrap(); assert_eq!(to_bytes(&py_sk1), rust_sk1.to_bytes()); @@ -82,11 +78,7 @@ fuzz_target!(|data: &[u8]| { let py_pk1 = aug .call_method1( "derive_child_pk_unhardened", - PyTuple::new( - py, - [py_pk, idx.into_pyobject(py).unwrap().clone().into_any()], - ) - .unwrap(), + PyTuple::new_bound(py, [py_pk, idx.to_object(py).bind(py).clone().into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_pk1), rust_pk1.to_bytes()); @@ -96,7 +88,7 @@ fuzz_target!(|data: &[u8]| { let py_sig1 = aug .call_method1( "sign", - PyTuple::new(py, [py_sk1, PyBytes::new(py, data).into_any()]).unwrap(), + PyTuple::new_bound(py, [py_sk1, PyBytes::new_bound(py, data).into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_sig1), rust_sig1.to_bytes()); @@ -107,11 +99,7 @@ fuzz_target!(|data: &[u8]| { let py_sk2 = aug .call_method1( "derive_child_sk", - PyTuple::new( - py, - [py_sk, idx.into_pyobject(py).unwrap().clone().into_any()], - ) - .unwrap(), + PyTuple::new_bound(py, [py_sk, idx.to_object(py).bind(py).clone().into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_sk2), rust_sk2.to_bytes()); @@ -121,7 +109,7 @@ fuzz_target!(|data: &[u8]| { let py_sig2 = aug .call_method1( "sign", - PyTuple::new(py, [py_sk2, PyBytes::new(py, data).into_any()]).unwrap(), + PyTuple::new_bound(py, [py_sk2, PyBytes::new_bound(py, data).into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_sig2), rust_sig2.to_bytes()); @@ -131,7 +119,7 @@ fuzz_target!(|data: &[u8]| { let py_agg = aug .call_method1( "aggregate", - PyTuple::new(py, [PyList::new(py, [py_sig1, py_sig2]).unwrap()]).unwrap(), + PyTuple::new_bound(py, [PyList::new_bound(py, [py_sig1, py_sig2])]), ) .unwrap(); assert_eq!(to_bytes(&py_agg), rust_agg.to_bytes()); diff --git a/crates/chia-bls/src/bls_cache.rs b/crates/chia-bls/src/bls_cache.rs index 101256b98..188acc183 100644 --- a/crates/chia-bls/src/bls_cache.rs +++ b/crates/chia-bls/src/bls_cache.rs @@ -163,12 +163,12 @@ impl BlsCache { sig: &Signature, ) -> PyResult { let pks = pks - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; let msgs = msgs - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; @@ -184,13 +184,10 @@ impl BlsCache { pub fn py_items(&self, py: pyo3::Python<'_>) -> PyResult { use pyo3::prelude::*; use pyo3::types::PyBytes; - let ret = PyList::empty(py); + let ret = PyList::empty_bound(py); let c = self.cache.lock().expect("cache"); for (key, value) in &c.items { - ret.append(( - PyBytes::new(py, key), - value.clone().into_pyobject(py)?.into_any(), - ))?; + ret.append((PyBytes::new_bound(py, key), value.clone().into_py(py)))?; } Ok(ret.into()) } @@ -198,7 +195,7 @@ impl BlsCache { #[pyo3(name = "update")] pub fn py_update(&self, other: &Bound<'_, PySequence>) -> PyResult<()> { let mut c = self.cache.lock().expect("cache"); - for item in other.borrow().try_iter()? { + for item in other.borrow().iter()? { let (key, value): (Vec, GTElement) = item?.extract()?; c.put( key.try_into() @@ -212,11 +209,11 @@ impl BlsCache { #[pyo3(name = "evict")] pub fn py_evict(&self, pks: &Bound<'_, PyList>, msgs: &Bound<'_, PyList>) -> PyResult<()> { let pks = pks - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; let msgs = msgs - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; self.evict(pks.into_iter().zip(msgs)); diff --git a/crates/chia-bls/src/gtelement.rs b/crates/chia-bls/src/gtelement.rs index 30dad4d63..373890c2f 100644 --- a/crates/chia-bls/src/gtelement.rs +++ b/crates/chia-bls/src/gtelement.rs @@ -148,10 +148,7 @@ mod pybindings { impl ToJsonDict for GTElement { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } diff --git a/crates/chia-bls/src/public_key.rs b/crates/chia-bls/src/public_key.rs index 8d1eed6e9..8c6140083 100644 --- a/crates/chia-bls/src/public_key.rs +++ b/crates/chia-bls/src/public_key.rs @@ -371,10 +371,7 @@ mod pybindings { impl ToJsonDict for PublicKey { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } @@ -737,7 +734,7 @@ mod tests { mod pytests { use super::*; use crate::SecretKey; - use pyo3::Python; + use pyo3::{IntoPy, Python}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rstest::rstest; @@ -753,7 +750,7 @@ mod pytests { let pk = sk.public_key(); Python::with_gil(|py| { let string = pk.to_json_dict(py).expect("to_json_dict"); - let py_class = py.get_type::(); + let py_class = py.get_type_bound::(); let pk2: PublicKey = PublicKey::from_json_dict(&py_class, py, string.bind(py)) .unwrap() .extract(py) @@ -772,14 +769,11 @@ mod pytests { fn test_json_dict(#[case] input: &str, #[case] msg: &str) { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let py_class = py.get_type::(); - let err = PublicKey::from_json_dict( - &py_class, - py, - &input.to_string().into_pyobject(py).unwrap().into_any(), - ) - .unwrap_err(); - assert_eq!(err.value(py).to_string(), msg.to_string()); + let py_class = py.get_type_bound::(); + let err = + PublicKey::from_json_dict(&py_class, py, input.to_string().into_py(py).bind(py)) + .unwrap_err(); + assert_eq!(err.value_bound(py).to_string(), msg.to_string()); }); } } diff --git a/crates/chia-bls/src/secret_key.rs b/crates/chia-bls/src/secret_key.rs index 935af33a9..9c9998122 100644 --- a/crates/chia-bls/src/secret_key.rs +++ b/crates/chia-bls/src/secret_key.rs @@ -311,10 +311,7 @@ mod pybindings { impl ToJsonDict for SecretKey { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } @@ -559,7 +556,7 @@ mod tests { #[cfg(feature = "py-bindings")] mod pytests { use super::*; - use pyo3::Python; + use pyo3::{IntoPy, Python}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rstest::rstest; @@ -574,7 +571,7 @@ mod pytests { let sk = SecretKey::from_seed(&data); Python::with_gil(|py| { let string = sk.to_json_dict(py).expect("to_json_dict"); - let py_class = py.get_type::(); + let py_class = py.get_type_bound::(); let sk2 = SecretKey::from_json_dict(&py_class, py, string.bind(py)) .unwrap() .extract(py) @@ -609,14 +606,11 @@ mod pytests { fn test_json_dict(#[case] input: &str, #[case] msg: &str) { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let py_class = py.get_type::(); - let err = SecretKey::from_json_dict( - &py_class, - py, - &input.to_string().into_pyobject(py).unwrap().into_any(), - ) - .unwrap_err(); - assert_eq!(err.value(py).to_string(), msg.to_string()); + let py_class = py.get_type_bound::(); + let err = + SecretKey::from_json_dict(&py_class, py, input.to_string().into_py(py).bind(py)) + .unwrap_err(); + assert_eq!(err.value_bound(py).to_string(), msg.to_string()); }); } } diff --git a/crates/chia-bls/src/signature.rs b/crates/chia-bls/src/signature.rs index 5000c1b3a..01eed3b9b 100644 --- a/crates/chia-bls/src/signature.rs +++ b/crates/chia-bls/src/signature.rs @@ -536,10 +536,7 @@ mod pybindings { impl ToJsonDict for Signature { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } @@ -1257,7 +1254,7 @@ mod tests { mod pytests { use super::*; - use pyo3::Python; + use pyo3::{IntoPy, Python}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rstest::rstest; @@ -1275,7 +1272,7 @@ mod pytests { let sig = sign(&sk, msg); Python::with_gil(|py| { let string = sig.to_json_dict(py).expect("to_json_dict"); - let py_class = py.get_type::(); + let py_class = py.get_type_bound::(); let sig2 = Signature::from_json_dict(&py_class, py, string.bind(py)) .unwrap() .extract(py) @@ -1294,14 +1291,11 @@ mod pytests { fn test_json_dict(#[case] input: &str, #[case] msg: &str) { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let py_class = py.get_type::(); - let err = Signature::from_json_dict( - &py_class, - py, - &input.to_string().into_pyobject(py).unwrap().into_any(), - ) - .unwrap_err(); - assert_eq!(err.value(py).to_string(), msg.to_string()); + let py_class = py.get_type_bound::(); + let err = + Signature::from_json_dict(&py_class, py, input.to_string().into_py(py).bind(py)) + .unwrap_err(); + assert_eq!(err.value_bound(py).to_string(), msg.to_string()); }); } } diff --git a/crates/chia-consensus/src/merkle_tree.rs b/crates/chia-consensus/src/merkle_tree.rs index e031d5c8a..52a3d53b3 100644 --- a/crates/chia-consensus/src/merkle_tree.rs +++ b/crates/chia-consensus/src/merkle_tree.rs @@ -368,7 +368,7 @@ impl MerkleSet { included_leaf: [u8; 32], ) -> PyResult<(bool, PyObject)> { match self.generate_proof(&included_leaf) { - Ok((included, proof)) => Ok((included, PyBytes::new(py, &proof).into())), + Ok((included, proof)) => Ok((included, PyBytes::new_bound(py, &proof).into())), Err(_) => Err(PyValueError::new_err("invalid proof")), } } diff --git a/crates/chia-protocol/src/block_record.rs b/crates/chia-protocol/src/block_record.rs index c90e0d77b..2478fbe21 100644 --- a/crates/chia-protocol/src/block_record.rs +++ b/crates/chia-protocol/src/block_record.rs @@ -132,12 +132,12 @@ impl BlockRecord { } fn sp_iters_impl(&self, py: Python<'_>, constants: &Bound<'_, PyAny>) -> PyResult { - let ctx = PyDict::new(py); + let ctx = PyDict::new_bound(py); ctx.set_item("sub_slot_iters", self.sub_slot_iters)?; ctx.set_item("signage_point_index", self.signage_point_index)?; ctx.set_item("constants", constants)?; - py.run( - c"from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ + py.run_bound( + "from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ ret = calculate_sp_iters(constants, sub_slot_iters, signage_point_index)\n", None, Some(&ctx), @@ -146,13 +146,13 @@ impl BlockRecord { } fn ip_iters_impl(&self, py: Python<'_>, constants: &Bound<'_, PyAny>) -> PyResult { - let ctx = PyDict::new(py); + let ctx = PyDict::new_bound(py); ctx.set_item("sub_slot_iters", self.sub_slot_iters)?; ctx.set_item("signage_point_index", self.signage_point_index)?; ctx.set_item("required_iters", self.required_iters)?; ctx.set_item("constants", constants)?; - py.run( - c"from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ + py.run_bound( + "from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ ret = calculate_ip_iters(constants, sub_slot_iters, signage_point_index, required_iters)\n", None, Some(&ctx), diff --git a/crates/chia-protocol/src/bytes.rs b/crates/chia-protocol/src/bytes.rs index 74f567829..74f5c7962 100644 --- a/crates/chia-protocol/src/bytes.rs +++ b/crates/chia-protocol/src/bytes.rs @@ -86,7 +86,7 @@ impl Streamable for Bytes { #[cfg(feature = "py-bindings")] impl ToJsonDict for Bytes { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - Ok(format!("0x{self}").into_pyobject(py)?.into_any().unbind()) + Ok(format!("0x{self}").to_object(py)) } } @@ -226,7 +226,7 @@ impl Streamable for BytesImpl { #[cfg(feature = "py-bindings")] impl ToJsonDict for BytesImpl { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - Ok(format!("0x{self}").into_pyobject(py)?.into_any().unbind()) + Ok(format!("0x{self}").to_object(py)) } } @@ -390,13 +390,16 @@ impl From for Bytes32 { } #[cfg(feature = "py-bindings")] -impl<'py, const N: usize> IntoPyObject<'py> for BytesImpl { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; +impl ToPyObject for BytesImpl { + fn to_object(&self, py: Python<'_>) -> PyObject { + ChiaToPython::to_python(self, py).unwrap().into() + } +} - fn into_pyobject(self, py: Python<'py>) -> Result { - ChiaToPython::to_python(&self, py) +#[cfg(feature = "py-bindings")] +impl IntoPy for BytesImpl { + fn into_py(self, py: Python<'_>) -> PyObject { + ChiaToPython::to_python(&self, py).unwrap().into() } } @@ -404,15 +407,15 @@ impl<'py, const N: usize> IntoPyObject<'py> for BytesImpl { impl ChiaToPython for BytesImpl { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { if N == 32 { - let bytes_module = PyModule::import(py, "chia_rs.sized_bytes")?; + let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?; let ty = bytes_module.getattr("bytes32")?; - ty.call1((self.0.into_pyobject(py)?,)) + ty.call1((self.0.into_py(py),)) } else if N == 48 { - let bytes_module = PyModule::import(py, "chia_rs.sized_bytes")?; + let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?; let ty = bytes_module.getattr("bytes48")?; - ty.call1((self.0.into_pyobject(py)?,)) + ty.call1((self.0.into_py(py),)) } else { - Ok(PyBytes::new(py, &self.0).into_any()) + Ok(PyBytes::new_bound(py, &self.0).into_any()) } } } @@ -428,20 +431,23 @@ impl<'py, const N: usize> FromPyObject<'py> for BytesImpl { } #[cfg(feature = "py-bindings")] -impl<'py> IntoPyObject<'py> for Bytes { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = std::convert::Infallible; +impl ToPyObject for Bytes { + fn to_object(&self, py: Python<'_>) -> PyObject { + PyBytes::new_bound(py, &self.0).into() + } +} - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyBytes::new(py, &self.0).into_any()) +#[cfg(feature = "py-bindings")] +impl IntoPy for Bytes { + fn into_py(self, py: Python<'_>) -> PyObject { + PyBytes::new_bound(py, &self.0).into() } } #[cfg(feature = "py-bindings")] impl ChiaToPython for Bytes { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyBytes::new(py, &self.0).into_any()) + Ok(PyBytes::new_bound(py, &self.0).into_any()) } } diff --git a/crates/chia-protocol/src/chia_protocol.rs b/crates/chia-protocol/src/chia_protocol.rs index 655a03e92..a24fe53b8 100644 --- a/crates/chia-protocol/src/chia_protocol.rs +++ b/crates/chia-protocol/src/chia_protocol.rs @@ -146,9 +146,7 @@ pub enum ProtocolMessageTypes { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for ProtocolMessageTypes { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone().into_any()) } } @@ -173,9 +171,7 @@ pub enum NodeType { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for NodeType { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone().into_any()) } } diff --git a/crates/chia-protocol/src/coin_spend.rs b/crates/chia-protocol/src/coin_spend.rs index 0cfdd3910..ffbd3c9c1 100644 --- a/crates/chia-protocol/src/coin_spend.rs +++ b/crates/chia-protocol/src/coin_spend.rs @@ -23,6 +23,6 @@ impl CoinSpend { // Convert result into potential child class let instance = cls.call1((cs.coin, cs.puzzle_reveal, cs.solution))?; - Ok(instance.into_pyobject(py)?.unbind()) + Ok(instance.into_py(py)) } } diff --git a/crates/chia-protocol/src/lazy_node.rs b/crates/chia-protocol/src/lazy_node.rs index 8bde2f037..59b9824e4 100644 --- a/crates/chia-protocol/src/lazy_node.rs +++ b/crates/chia-protocol/src/lazy_node.rs @@ -1,6 +1,6 @@ use clvmr::{allocator::NodePtr, allocator::SExp, Allocator}; use pyo3::prelude::*; -use pyo3::types::PyBytes; +use pyo3::types::{PyBytes, PyTuple}; use std::rc::Rc; #[pyclass(subclass, unsendable, frozen)] @@ -10,6 +10,12 @@ pub struct LazyNode { node: NodePtr, } +impl ToPyObject for LazyNode { + fn to_object(&self, py: Python<'_>) -> PyObject { + Bound::new(py, self.clone()).unwrap().to_object(py) + } +} + #[pymethods] impl LazyNode { #[getter(pair)] @@ -18,7 +24,7 @@ impl LazyNode { SExp::Pair(p1, p2) => { let r1 = Self::new(self.allocator.clone(), *p1); let r2 = Self::new(self.allocator.clone(), *p2); - let v = (r1, r2).into_pyobject(py)?; + let v = PyTuple::new_bound(py, &[r1, r2]); Ok(Some(v.into())) } SExp::Atom => Ok(None), @@ -28,7 +34,9 @@ impl LazyNode { #[getter(atom)] pub fn atom(&self, py: Python<'_>) -> Option { match &self.allocator.sexp(self.node) { - SExp::Atom => Some(PyBytes::new(py, self.allocator.atom(self.node).as_ref()).into()), + SExp::Atom => { + Some(PyBytes::new_bound(py, self.allocator.atom(self.node).as_ref()).into()) + } SExp::Pair(..) => None, } } diff --git a/crates/chia-protocol/src/program.rs b/crates/chia-protocol/src/program.rs index 71e5787eb..45067a97a 100644 --- a/crates/chia-protocol/src/program.rs +++ b/crates/chia-protocol/src/program.rs @@ -297,9 +297,9 @@ fn clvm_serialize(a: &mut Allocator, o: &Bound<'_, PyAny>) -> PyResult #[cfg(feature = "py-bindings")] fn to_program(py: Python<'_>, node: LazyNode) -> PyResult> { - let int_module = PyModule::import(py, "chia.types.blockchain_format.program")?; + let int_module = PyModule::import_bound(py, "chia.types.blockchain_format.program")?; let ty = int_module.getattr("Program")?; - ty.call1((node.into_pyobject(py)?,)) + ty.call1((node.into_py(py),)) } #[cfg(feature = "py-bindings")] diff --git a/crates/chia-protocol/src/spend_bundle.rs b/crates/chia-protocol/src/spend_bundle.rs index ffa29c4ad..b5ab9dc9b 100644 --- a/crates/chia-protocol/src/spend_bundle.rs +++ b/crates/chia-protocol/src/spend_bundle.rs @@ -102,10 +102,10 @@ impl SpendBundle { ) -> PyResult { let aggregated = Bound::new(py, Self::aggregate(&spend_bundles))?; if aggregated.is_exact_instance(cls) { - Ok(aggregated.into_pyobject(py)?.unbind().into_any()) + Ok(aggregated.into_py(py)) } else { - let instance = cls.call_method1("from_parent", (aggregated.into_pyobject(py)?,))?; - Ok(instance.into_pyobject(py)?.unbind().into_any()) + let instance = cls.call_method1("from_parent", (aggregated.into_py(py),))?; + Ok(instance.into_py(py)) } } @@ -122,7 +122,7 @@ impl SpendBundle { None, )?; - Ok(instance.into_pyobject(py)?.unbind()) + Ok(instance.into_py(py)) } #[pyo3(name = "name")] diff --git a/crates/chia-protocol/src/wallet_protocol.rs b/crates/chia-protocol/src/wallet_protocol.rs index 29c5425d4..731edc490 100644 --- a/crates/chia-protocol/src/wallet_protocol.rs +++ b/crates/chia-protocol/src/wallet_protocol.rs @@ -300,9 +300,7 @@ pub enum RejectStateReason { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for RejectStateReason { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone()) } } @@ -320,9 +318,7 @@ pub enum MempoolRemoveReason { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for MempoolRemoveReason { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone()) } } diff --git a/crates/chia-traits/src/from_json_dict.rs b/crates/chia-traits/src/from_json_dict.rs index 0bb601ffe..e15f78289 100644 --- a/crates/chia-traits/src/from_json_dict.rs +++ b/crates/chia-traits/src/from_json_dict.rs @@ -51,7 +51,7 @@ where { fn from_json_dict(o: &Bound<'_, PyAny>) -> PyResult { let mut ret = Vec::::new(); - for v in o.try_iter()? { + for v in o.iter()? { ret.push(::from_json_dict(&v?)?); } Ok(ret) diff --git a/crates/chia-traits/src/int.rs b/crates/chia-traits/src/int.rs index 7c710a2c7..2b49e155c 100644 --- a/crates/chia-traits/src/int.rs +++ b/crates/chia-traits/src/int.rs @@ -10,9 +10,9 @@ macro_rules! primitive_int { ($t:ty, $name:expr) => { impl ChiaToPython for $t { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - let int_module = PyModule::import(py, "chia_rs.sized_ints")?; + let int_module = PyModule::import_bound(py, "chia_rs.sized_ints")?; let ty = int_module.getattr($name)?; - ty.call1((self.into_pyobject(py)?.into_any(),)) + ty.call1((self.into_py(py),)) } } }; @@ -40,7 +40,7 @@ impl ChiaToPython for Option { impl ChiaToPython for Vec { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - let ret = PyList::empty(py); + let ret = PyList::empty_bound(py); for v in self { ret.append(v.to_python(py)?)?; } @@ -50,32 +50,32 @@ impl ChiaToPython for Vec { impl ChiaToPython for bool { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyBool::new(py, *self).as_any().clone()) + Ok(PyBool::new_bound(py, *self).as_any().clone()) } } impl ChiaToPython for String { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyString::new(py, self.as_str()).into_any()) + Ok(PyString::new_bound(py, self.as_str()).into_any()) } } impl ChiaToPython for (T, U) { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyTuple::new(py, [self.0.to_python(py)?, self.1.to_python(py)?])?.into_any()) + Ok(PyTuple::new_bound(py, [self.0.to_python(py)?, self.1.to_python(py)?]).into_any()) } } impl ChiaToPython for (T, U, V) { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyTuple::new( + Ok(PyTuple::new_bound( py, [ self.0.to_python(py)?, self.1.to_python(py)?, self.2.to_python(py)?, ], - )? + ) .into_any()) } } diff --git a/crates/chia-traits/src/to_json_dict.rs b/crates/chia-traits/src/to_json_dict.rs index 789ef80ac..48e60a0b4 100644 --- a/crates/chia-traits/src/to_json_dict.rs +++ b/crates/chia-traits/src/to_json_dict.rs @@ -1,6 +1,5 @@ use pyo3::prelude::*; use pyo3::types::PyList; -use pyo3::BoundObject; pub trait ToJsonDict { fn to_json_dict(&self, py: Python<'_>) -> PyResult; @@ -10,7 +9,7 @@ macro_rules! to_json_primitive { ($t:ty) => { impl $crate::to_json_dict::ToJsonDict for $t { fn to_json_dict(&self, py: Python<'_>) -> pyo3::PyResult { - Ok(self.into_pyobject(py)?.into_any().unbind()) + Ok(self.to_object(py)) } } }; @@ -31,7 +30,7 @@ to_json_primitive!(String); impl ToJsonDict for Vec { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - let list = PyList::empty(py); + let list = PyList::empty_bound(py); for v in self { list.append(v.to_json_dict(py)?)?; } @@ -51,7 +50,7 @@ impl ToJsonDict for Option { // if we need more of these, we should probably make a macro impl ToJsonDict for (T, U) { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - let list = PyList::empty(py); + let list = PyList::empty_bound(py); list.append(self.0.to_json_dict(py)?)?; list.append(self.1.to_json_dict(py)?)?; Ok(list.into()) @@ -60,7 +59,7 @@ impl ToJsonDict for (T, U) { impl ToJsonDict for (T, U, W) { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - let list = PyList::empty(py); + let list = PyList::empty_bound(py); list.append(self.0.to_json_dict(py)?)?; list.append(self.1.to_json_dict(py)?)?; list.append(self.2.to_json_dict(py)?)?; diff --git a/wheel/src/api.rs b/wheel/src/api.rs index af7a27ca4..e0a149f54 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -88,7 +88,10 @@ pub fn compute_merkle_set_root<'p>( use pyo3::types::PyBytesMethods; buffer.push(b.as_bytes().try_into()?); } - Ok(PyBytes::new(py, &compute_merkle_root_impl(&mut buffer))) + Ok(PyBytes::new_bound( + py, + &compute_merkle_root_impl(&mut buffer), + )) } #[pyfunction] @@ -175,8 +178,8 @@ pub fn get_puzzle_and_solution_for_coin<'a>( }; */ Ok(( - PyBytes::new(py, &serialize(&allocator, puzzle)?), - PyBytes::new(py, &serialize(&allocator, solution)?), + PyBytes::new_bound(py, &serialize(&allocator, puzzle)?), + PyBytes::new_bound(py, &serialize(&allocator, solution)?), )) } @@ -235,7 +238,7 @@ type CoinSpendRef = (Coin, PyBackedBytes, PyBackedBytes); fn convert_list_of_tuples(spends: &Bound<'_, PyAny>) -> PyResult> { let mut native_spends = Vec::::new(); - for s in spends.try_iter()? { + for s in spends.iter()? { let s = s?; let tuple = s.downcast::()?; let coin = tuple.get_item(0)?.extract::()?; @@ -252,7 +255,7 @@ fn solution_generator<'p>( spends: &Bound<'_, PyAny>, ) -> PyResult> { let spends = convert_list_of_tuples(spends)?; - Ok(PyBytes::new(py, &native_solution_generator(spends)?)) + Ok(PyBytes::new_bound(py, &native_solution_generator(spends)?)) } #[pyfunction] @@ -261,7 +264,7 @@ fn solution_generator_backrefs<'p>( spends: &Bound<'_, PyAny>, ) -> PyResult> { let spends = convert_list_of_tuples(spends)?; - Ok(PyBytes::new( + Ok(PyBytes::new_bound( py, &native_solution_generator_backrefs(spends)?, )) @@ -397,7 +400,7 @@ fn fast_forward_singleton<'p>( let solution = node_from_bytes(&mut a, spend.solution.as_slice())?; let new_solution = native_ff(&mut a, puzzle, solution, &spend.coin, new_coin, new_parent)?; - Ok(PyBytes::new( + Ok(PyBytes::new_bound( py, node_to_bytes(&a, new_solution)?.as_slice(), ))