Skip to content

Commit

Permalink
Simplify rust code with new pyo3 0.20 features (#9716)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Oct 12, 2023
1 parent 519a9fe commit 5963480
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 152 deletions.
12 changes: 2 additions & 10 deletions src/rust/src/backend/dh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,8 @@ impl DHPublicKey {
.call1((py_pub_key, parameter_numbers))?)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, DHPublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,8 @@ impl DsaPublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, false)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, DsaPublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,8 @@ impl ECPublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, false)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, ECPublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,8 @@ impl Ed25519PublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, true)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, Ed25519PublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/ed448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,8 @@ impl Ed448PublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, true)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, Ed448PublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,16 +557,8 @@ impl RsaPublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, false)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, RsaPublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,8 @@ impl X25519PublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, false, true)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, X25519PublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
12 changes: 2 additions & 10 deletions src/rust/src/backend/x448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,8 @@ impl X448PublicKey {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, false, true)
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, X448PublicKey>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.pkey.public_eq(&other.pkey)),
pyo3::basic::CompareOp::Ne => Ok(!self.pkey.public_eq(&other.pkey)),
_ => Err(pyo3::exceptions::PyTypeError::new_err("Cannot be ordered")),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}

fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
Expand Down
14 changes: 2 additions & 12 deletions src/rust/src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,8 @@ impl ObjectIdentifier {
))
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, ObjectIdentifier>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.oid == other.oid),
pyo3::basic::CompareOp::Ne => Ok(self.oid != other.oid),
_ => Err(pyo3::exceptions::PyTypeError::new_err(
"ObjectIdentifiers cannot be ordered",
)),
}
fn __eq__(&self, other: pyo3::PyRef<'_, ObjectIdentifier>) -> bool {
self.oid == other.oid
}

fn __hash__(&self) -> u64 {
Expand Down
18 changes: 2 additions & 16 deletions src/rust/src/x509/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,8 @@ impl Certificate {
hasher.finish()
}

fn __richcmp__(
&self,
other: pyo3::PyRef<'_, Certificate>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => {
Ok(self.raw.borrow_dependent() == other.raw.borrow_dependent())
}
pyo3::basic::CompareOp::Ne => {
Ok(self.raw.borrow_dependent() != other.raw.borrow_dependent())
}
_ => Err(pyo3::exceptions::PyTypeError::new_err(
"Certificates cannot be ordered",
)),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Certificate>) -> bool {
self.raw.borrow_dependent() == other.raw.borrow_dependent()
}

fn __repr__(&self, py: pyo3::Python<'_>) -> pyo3::PyResult<String> {
Expand Down
18 changes: 2 additions & 16 deletions src/rust/src/x509/crl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,8 @@ impl CertificateRevocationList {

#[pyo3::prelude::pymethods]
impl CertificateRevocationList {
fn __richcmp__(
&self,
other: pyo3::PyRef<'_, CertificateRevocationList>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => {
Ok(self.owned.borrow_dependent() == other.owned.borrow_dependent())
}
pyo3::basic::CompareOp::Ne => {
Ok(self.owned.borrow_dependent() != other.owned.borrow_dependent())
}
_ => Err(pyo3::exceptions::PyTypeError::new_err(
"CRLs cannot be ordered",
)),
}
fn __eq__(&self, other: pyo3::PyRef<'_, CertificateRevocationList>) -> bool {
self.owned.borrow_dependent() == other.owned.borrow_dependent()
}

fn __len__(&self) -> usize {
Expand Down
17 changes: 3 additions & 14 deletions src/rust/src/x509/csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,12 @@ impl CertificateSigningRequest {
hasher.finish()
}

fn __richcmp__(
fn __eq__(
&self,
py: pyo3::Python<'_>,
other: pyo3::PyRef<'_, CertificateSigningRequest>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => {
Ok(self.raw.borrow_owner().as_bytes(py) == other.raw.borrow_owner().as_bytes(py))
}
pyo3::basic::CompareOp::Ne => {
Ok(self.raw.borrow_owner().as_bytes(py) != other.raw.borrow_owner().as_bytes(py))
}
_ => Err(pyo3::exceptions::PyTypeError::new_err(
"CSRs cannot be ordered",
)),
}
) -> bool {
self.raw.borrow_owner().as_bytes(py) == other.raw.borrow_owner().as_bytes(py)
}

fn public_key<'p>(&self, py: pyo3::Python<'p>) -> CryptographyResult<&'p pyo3::PyAny> {
Expand Down
14 changes: 2 additions & 12 deletions src/rust/src/x509/sct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,8 @@ pub(crate) struct Sct {

#[pyo3::prelude::pymethods]
impl Sct {
fn __richcmp__(
&self,
other: pyo3::PyRef<'_, Sct>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
pyo3::basic::CompareOp::Eq => Ok(self.sct_data == other.sct_data),
pyo3::basic::CompareOp::Ne => Ok(self.sct_data != other.sct_data),
_ => Err(pyo3::exceptions::PyTypeError::new_err(
"SCTs cannot be ordered",
)),
}
fn __eq__(&self, other: pyo3::PyRef<'_, Sct>) -> bool {
self.sct_data == other.sct_data
}

fn __hash__(&self) -> u64 {
Expand Down
4 changes: 2 additions & 2 deletions tests/x509/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ def test_ordering_unsupported(self, backend):
os.path.join("x509", "custom", "post2000utctime.pem"),
x509.load_pem_x509_certificate,
)
with pytest.raises(TypeError, match="cannot be ordered"):
with pytest.raises(TypeError, match="'>' not supported"):
cert > cert2 # type: ignore[operator]

def test_hash(self, backend):
Expand Down Expand Up @@ -2164,7 +2164,7 @@ def test_ordering_unsupported(self, backend):
os.path.join("x509", "requests", "rsa_sha256.pem"),
x509.load_pem_x509_csr,
)
with pytest.raises(TypeError, match="cannot be ordered"):
with pytest.raises(TypeError, match="'>' not supported"):
csr > csr2 # type: ignore[operator]

def test_hash(self, backend):
Expand Down

0 comments on commit 5963480

Please sign in to comment.