Skip to content

Commit

Permalink
Convert src/x509/sct.rs to new pyo3 APIs (#10713)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Apr 4, 2024
1 parent 88004e9 commit 69e7e5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
17 changes: 10 additions & 7 deletions src/rust/src/x509/sct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<pyo3::Bound<'p, pyo3::PyAny>> {
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::<Option<pyo3::PyAny>>)?;

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]
Expand Down Expand Up @@ -222,7 +223,7 @@ pub(crate) fn parse_scts(
) -> Result<pyo3::PyObject, CryptographyError> {
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();
Expand Down Expand Up @@ -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::<Sct>()?;

Ok(())
Expand Down

0 comments on commit 69e7e5b

Please sign in to comment.