From 2801b4ad3d4cfb988b8a88b66368eb328f3771f3 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 4 Apr 2024 18:24:46 -0400 Subject: [PATCH] Convert `src/x509/sct.rs` to new pyo3 APIs --- src/rust/src/lib.rs | 2 +- src/rust/src/x509/sct.rs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index 8ea8709c6e11..022c78eaf515 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -106,7 +106,7 @@ fn _rust(py: pyo3::Python<'_>, m: &pyo3::types::PyModule) -> pyo3::PyResult<()> crate::x509::common::add_to_module(x509_mod)?; crate::x509::crl::add_to_module(x509_mod)?; crate::x509::csr::add_to_module(x509_mod)?; - crate::x509::sct::add_to_module(x509_mod)?; + crate::x509::sct::add_to_module(&x509_mod.as_borrowed())?; crate::x509::verify::add_to_module(x509_mod)?; m.add_submodule(x509_mod)?; diff --git a/src/rust/src/x509/sct.rs b/src/rust/src/x509/sct.rs index b7cce3ff4036..a7bfbb5eb472 100644 --- a/src/rust/src/x509/sct.rs +++ b/src/rust/src/x509/sct.rs @@ -5,6 +5,7 @@ use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; +use pyo3::prelude::{PyAnyMethods, PyDictMethods, PyListMethods, PyModuleMethods}; use pyo3::ToPyObject; use crate::error::CryptographyError; @@ -163,20 +164,20 @@ impl Sct { } #[getter] - fn timestamp<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<&'p pyo3::PyAny> { + fn timestamp<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult> { let utc = types::DATETIME_TIMEZONE_UTC.get(py)?; - let kwargs = pyo3::types::PyDict::new(py); + let kwargs = pyo3::types::PyDict::new_bound(py); kwargs.set_item("microsecond", self.timestamp % 1000 * 1000)?; kwargs.set_item("tzinfo", None::>)?; types::DATETIME_DATETIME - .get(py)? + .get_bound(py)? .call_method1( pyo3::intern!(py, "fromtimestamp"), (self.timestamp / 1000, utc), )? - .call_method("replace", (), Some(kwargs)) + .call_method("replace", (), Some(&kwargs)) } #[getter] @@ -222,7 +223,7 @@ pub(crate) fn parse_scts( ) -> Result { let mut reader = TLSReader::new(data).read_length_prefixed()?; - let py_scts = pyo3::types::PyList::empty(py); + let py_scts = pyo3::types::PyList::empty_bound(py); while !reader.is_empty() { let mut sct_data = reader.read_length_prefixed()?; let raw_sct_data = sct_data.data.to_vec(); @@ -250,12 +251,14 @@ pub(crate) fn parse_scts( extension_bytes, sct_data: raw_sct_data, }; - py_scts.append(pyo3::PyCell::new(py, sct)?)?; + py_scts.append(pyo3::Bound::new(py, sct)?)?; } Ok(py_scts.to_object(py)) } -pub(crate) fn add_to_module(module: &pyo3::prelude::PyModule) -> pyo3::PyResult<()> { +pub(crate) fn add_to_module( + module: &pyo3::Bound<'_, pyo3::prelude::PyModule>, +) -> pyo3::PyResult<()> { module.add_class::()?; Ok(())