Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more pyo3 deprecated #907

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ unused_import_braces = "deny"
unreachable_code = "deny"
unreachable_patterns = "deny"
dead_code = "deny"
deprecated = "deny"
deprecated = "allow"
deprecated_in_future = "deny"
trivial_casts = "deny"
trivial_numeric_casts = "deny"
Expand Down
32 changes: 10 additions & 22 deletions crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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
let rust_sk = SecretKey::from_seed(data);
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();

Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
17 changes: 7 additions & 10 deletions crates/chia-bls/src/bls_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ impl BlsCache {
sig: &Signature,
) -> PyResult<bool> {
let pks = pks
.try_iter()?
.iter()?
.map(|item| item?.extract())
.collect::<PyResult<Vec<PublicKey>>>()?;

let msgs = msgs
.try_iter()?
.iter()?
.map(|item| item?.extract())
.collect::<PyResult<Vec<PyBackedBytes>>>()?;

Expand All @@ -184,21 +184,18 @@ impl BlsCache {
pub fn py_items(&self, py: pyo3::Python<'_>) -> PyResult<PyObject> {
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())
}

#[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<u8>, GTElement) = item?.extract()?;
c.put(
key.try_into()
Expand All @@ -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::<PyResult<Vec<PublicKey>>>()?;
let msgs = msgs
.try_iter()?
.iter()?
.map(|item| item?.extract())
.collect::<PyResult<Vec<PyBackedBytes>>>()?;
self.evict(pks.into_iter().zip(msgs));
Expand Down
5 changes: 1 addition & 4 deletions crates/chia-bls/src/gtelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ mod pybindings {
impl ToJsonDict for GTElement {
fn to_json_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
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))
}
}

Expand Down
22 changes: 8 additions & 14 deletions crates/chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,7 @@ mod pybindings {
impl ToJsonDict for PublicKey {
fn to_json_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
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))
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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::<PublicKey>();
let py_class = py.get_type_bound::<PublicKey>();
let pk2: PublicKey = PublicKey::from_json_dict(&py_class, py, string.bind(py))
.unwrap()
.extract(py)
Expand All @@ -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::<PublicKey>();
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::<PublicKey>();
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());
});
}
}
22 changes: 8 additions & 14 deletions crates/chia-bls/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,7 @@ mod pybindings {
impl ToJsonDict for SecretKey {
fn to_json_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
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))
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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::<SecretKey>();
let py_class = py.get_type_bound::<SecretKey>();
let sk2 = SecretKey::from_json_dict(&py_class, py, string.bind(py))
.unwrap()
.extract(py)
Expand Down Expand Up @@ -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::<SecretKey>();
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::<SecretKey>();
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());
});
}
}
22 changes: 8 additions & 14 deletions crates/chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,7 @@ mod pybindings {
impl ToJsonDict for Signature {
fn to_json_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
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))
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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::<Signature>();
let py_class = py.get_type_bound::<Signature>();
let sig2 = Signature::from_json_dict(&py_class, py, string.bind(py))
.unwrap()
.extract(py)
Expand All @@ -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::<Signature>();
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::<Signature>();
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());
});
}
}
2 changes: 1 addition & 1 deletion crates/chia-consensus/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/chia-protocol/src/block_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ impl BlockRecord {
}

fn sp_iters_impl(&self, py: Python<'_>, constants: &Bound<'_, PyAny>) -> PyResult<u64> {
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),
Expand All @@ -146,13 +146,13 @@ impl BlockRecord {
}

fn ip_iters_impl(&self, py: Python<'_>, constants: &Bound<'_, PyAny>) -> PyResult<u64> {
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),
Expand Down
Loading
Loading