Skip to content

Commit

Permalink
Upgrade to pyo3 0.16
Browse files Browse the repository at this point in the history
Rebased-by: H. Vetinari <h.vetinari@gmx.com>
  • Loading branch information
alex authored and h-vetinari committed Apr 3, 2022
1 parent df06889 commit 30f878d
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 165 deletions.
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ classifiers =
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
features=(
[]
if platform.python_implementation() == "PyPy"
else ["pyo3/abi3-py36"]
else ["pyo3/abi3-py37"]
),
rust_version=">=1.41.0",
)
Expand Down
72 changes: 22 additions & 50 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
lazy_static = "1"
pyo3 = { version = "0.15.1" }
pyo3 = { version = "0.16" }
asn1 = { version = "0.8.7", default-features = false, features = ["derive"] }
pem = "1.0"
chrono = { version = "0.4", default-features = false, features = ["alloc", "clock"] }
Expand Down
17 changes: 7 additions & 10 deletions src/rust/src/x509/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ pub(crate) struct Certificate {
pub(crate) cached_extensions: Option<pyo3::PyObject>,
}

#[pyo3::prelude::pyproto]
impl pyo3::PyObjectProtocol for Certificate {
#[pyo3::prelude::pymethods]
impl Certificate {
fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.raw.borrow_value().hash(&mut hasher);
Expand All @@ -92,7 +92,7 @@ impl pyo3::PyObjectProtocol for Certificate {

fn __richcmp__(
&self,
other: pyo3::PyRef<Certificate>,
other: pyo3::PyRef<'_, Certificate>,
op: pyo3::basic::CompareOp,
) -> pyo3::PyResult<bool> {
match op {
Expand All @@ -112,10 +112,7 @@ impl pyo3::PyObjectProtocol for Certificate {
let subject_repr = subject.repr()?.extract::<&str>()?;
Ok(format!("<Certificate(subject={}, ...)>", subject_repr))
}
}

#[pyo3::prelude::pymethods]
impl Certificate {
fn __deepcopy__(slf: pyo3::PyRef<'_, Self>, _memo: pyo3::PyObject) -> pyo3::PyRef<'_, Self> {
slf
}
Expand Down Expand Up @@ -157,9 +154,9 @@ impl Certificate {
.getattr("Encoding")?;

let result = asn1::write_single(self.raw.borrow_value());
if encoding == encoding_class.getattr("DER")? {
if encoding.is(encoding_class.getattr("DER")?) {
Ok(pyo3::types::PyBytes::new(py, &result))
} else if encoding == encoding_class.getattr("PEM")? {
} else if encoding.is(encoding_class.getattr("PEM")?) {
let pem = pem::encode_config(
&pem::Pem {
tag: "CERTIFICATE".to_string(),
Expand Down Expand Up @@ -253,7 +250,7 @@ impl Certificate {
let hash_alg = sig_oids_to_hash.get_item(self.signature_algorithm_oid(py)?);
match hash_alg {
Ok(data) => Ok(data),
Err(_) => Err(PyAsn1Error::from(pyo3::PyErr::from_instance(
Err(_) => Err(PyAsn1Error::from(pyo3::PyErr::from_value(
py.import("cryptography.exceptions")?.call_method1(
"UnsupportedAlgorithm",
(format!(
Expand Down Expand Up @@ -326,7 +323,7 @@ fn cert_version(py: pyo3::Python<'_>, version: u8) -> Result<&pyo3::PyAny, PyAsn
match version {
0 => Ok(x509_module.getattr("Version")?.get_item("v1")?),
2 => Ok(x509_module.getattr("Version")?.get_item("v3")?),
_ => Err(PyAsn1Error::from(pyo3::PyErr::from_instance(
_ => Err(PyAsn1Error::from(pyo3::PyErr::from_value(
x509_module
.getattr("InvalidVersion")?
.call1((format!("{} is not a valid X509 version", version), version))?,
Expand Down
24 changes: 12 additions & 12 deletions src/rust/src/x509/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ pub(crate) fn encode_name_entry<'p>(

let attr_type = py_name_entry.getattr("_type")?;
let tag = attr_type.getattr("value")?.extract::<u8>()?;
let encoding = if attr_type == asn1_type.getattr("BMPString")? {
let encoding = if attr_type.is(asn1_type.getattr("BMPString")?) {
"utf_16_be"
} else if attr_type == asn1_type.getattr("UniversalString")? {
} else if attr_type.is(asn1_type.getattr("UniversalString")?) {
"utf_32_be"
} else {
"utf8"
Expand Down Expand Up @@ -226,18 +226,18 @@ pub(crate) fn encode_general_name<'a>(
let gn_module = py.import("cryptography.x509.general_name")?;
let gn_type = gn.get_type().as_ref();
let gn_value = gn.getattr("value")?;
if gn_type == gn_module.getattr("DNSName")? {
if gn_type.is(gn_module.getattr("DNSName")?) {
Ok(GeneralName::DNSName(UnvalidatedIA5String(
gn_value.extract::<&str>()?,
)))
} else if gn_type == gn_module.getattr("RFC822Name")? {
} else if gn_type.is(gn_module.getattr("RFC822Name")?) {
Ok(GeneralName::RFC822Name(UnvalidatedIA5String(
gn_value.extract::<&str>()?,
)))
} else if gn_type == gn_module.getattr("DirectoryName")? {
} else if gn_type.is(gn_module.getattr("DirectoryName")?) {
let name = encode_name(py, gn_value)?;
Ok(GeneralName::DirectoryName(name))
} else if gn_type == gn_module.getattr("OtherName")? {
} else if gn_type.is(gn_module.getattr("OtherName")?) {
Ok(GeneralName::OtherName(OtherName {
type_id: asn1::ObjectIdentifier::from_string(
gn.getattr("type_id")?
Expand All @@ -247,15 +247,15 @@ pub(crate) fn encode_general_name<'a>(
.unwrap(),
value: asn1::parse_single(gn_value.extract::<&[u8]>()?)?,
}))
} else if gn_type == gn_module.getattr("UniformResourceIdentifier")? {
} else if gn_type.is(gn_module.getattr("UniformResourceIdentifier")?) {
Ok(GeneralName::UniformResourceIdentifier(
UnvalidatedIA5String(gn_value.extract::<&str>()?),
))
} else if gn_type == gn_module.getattr("IPAddress")? {
} else if gn_type.is(gn_module.getattr("IPAddress")?) {
Ok(GeneralName::IPAddress(
gn.call_method0("_packed")?.extract::<&[u8]>()?,
))
} else if gn_type == gn_module.getattr("RegisteredID")? {
} else if gn_type.is(gn_module.getattr("RegisteredID")?) {
let oid = asn1::ObjectIdentifier::from_string(
gn_value.getattr("dotted_string")?.extract::<&str>()?,
)
Expand Down Expand Up @@ -458,7 +458,7 @@ pub(crate) fn parse_general_name(
.to_object(py)
}
_ => {
return Err(PyAsn1Error::from(pyo3::PyErr::from_instance(
return Err(PyAsn1Error::from(pyo3::PyErr::from_value(
x509_module.call_method1(
"UnsupportedGeneralNameType",
("x400Address/EDIPartyName are not supported types",),
Expand Down Expand Up @@ -556,7 +556,7 @@ pub(crate) fn parse_and_cache_extensions<
x509_module.call_method1("ObjectIdentifier", (raw_ext.extn_id.to_string(),))?;

if seen_oids.contains(&raw_ext.extn_id) {
return Err(pyo3::PyErr::from_instance(x509_module.call_method1(
return Err(pyo3::PyErr::from_value(x509_module.call_method1(
"DuplicateExtension",
(
format!("Duplicate {} extension found", raw_ext.extn_id),
Expand Down Expand Up @@ -608,7 +608,7 @@ pub(crate) fn encode_extensions<
.unwrap();

let ext_val = py_ext.getattr("value")?;
if unrecognized_extension_type.is_instance(ext_val)? {
if ext_val.is_instance(unrecognized_extension_type)? {
exts.push(Extension {
extn_id: oid,
critical: py_ext.getattr("critical")?.extract()?,
Expand Down
Loading

0 comments on commit 30f878d

Please sign in to comment.