Skip to content

Commit ecb4ae2

Browse files
authored
Upgrade to pyo3 0.27 (#13708)
* Upgrade to pyo3 0.27 * 0.27.1 * fix for pypy
1 parent b580fcf commit ecb4ae2

File tree

16 files changed

+99
-75
lines changed

16 files changed

+99
-75
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ foreign-types-shared = "0.1"
3030
openssl = "0.10.74"
3131
openssl-sys = "0.9.108"
3232
pem = { version = "3", default-features = false }
33-
pyo3 = { version = "0.26", features = ["abi3"] }
34-
pyo3-build-config = { version = "0.26" }
33+
pyo3 = { version = "0.27.1", features = ["abi3"] }
34+
pyo3-build-config = { version = "0.27.1" }
3535
self_cell = "1"
3636

3737
[profile.release]

src/rust/src/asn1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(crate) fn py_oid_to_oid(
1414
py_oid: pyo3::Bound<'_, pyo3::PyAny>,
1515
) -> pyo3::PyResult<asn1::ObjectIdentifier> {
1616
Ok(py_oid
17-
.downcast::<crate::oid::ObjectIdentifier>()?
17+
.cast::<crate::oid::ObjectIdentifier>()?
1818
.get()
1919
.oid
2020
.clone())
@@ -85,8 +85,8 @@ pub(crate) fn py_uint_to_big_endian_bytes<'p>(
8585
.extract::<usize>()?
8686
/ 8
8787
+ 1;
88-
v.call_method1(pyo3::intern!(py, "to_bytes"), (n, "big"))?
89-
.extract()
88+
Ok(v.call_method1(pyo3::intern!(py, "to_bytes"), (n, "big"))?
89+
.extract()?)
9090
}
9191

9292
pub(crate) fn encode_der_data<'p>(

src/rust/src/backend/ciphers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,18 +592,18 @@ fn cipher_supported(
592592

593593
#[pyo3::pyfunction]
594594
fn _advance(ctx: pyo3::Bound<'_, pyo3::PyAny>, n: u64) {
595-
if let Ok(c) = ctx.downcast::<PyAEADEncryptionContext>() {
595+
if let Ok(c) = ctx.cast::<PyAEADEncryptionContext>() {
596596
c.borrow_mut().bytes_remaining -= n;
597-
} else if let Ok(c) = ctx.downcast::<PyAEADDecryptionContext>() {
597+
} else if let Ok(c) = ctx.cast::<PyAEADDecryptionContext>() {
598598
c.borrow_mut().bytes_remaining -= n;
599599
}
600600
}
601601

602602
#[pyo3::pyfunction]
603603
fn _advance_aad(ctx: pyo3::Bound<'_, pyo3::PyAny>, n: u64) {
604-
if let Ok(c) = ctx.downcast::<PyAEADEncryptionContext>() {
604+
if let Ok(c) = ctx.cast::<PyAEADEncryptionContext>() {
605605
c.borrow_mut().aad_bytes_remaining -= n;
606-
} else if let Ok(c) = ctx.downcast::<PyAEADDecryptionContext>() {
606+
} else if let Ok(c) = ctx.cast::<PyAEADDecryptionContext>() {
607607
c.borrow_mut().aad_bytes_remaining -= n;
608608
}
609609
}

src/rust/src/backend/dh.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ impl DHPrivateKey {
184184

185185
let parameter_numbers = DHParameterNumbers {
186186
p: py_p.extract()?,
187-
q: py_q.map(|q| q.extract()).transpose()?,
187+
q: py_q
188+
.map(|q| q.extract().map_err(CryptographyError::from))
189+
.transpose()?,
188190
g: py_g.extract()?,
189191
};
190192
let public_numbers = DHPublicNumbers {
@@ -291,7 +293,9 @@ impl DHPublicKey {
291293

292294
let parameter_numbers = DHParameterNumbers {
293295
p: py_p.extract()?,
294-
q: py_q.map(|q| q.extract()).transpose()?,
296+
q: py_q
297+
.map(|q| q.extract().map_err(CryptographyError::from))
298+
.transpose()?,
295299
g: py_g.extract()?,
296300
};
297301

@@ -331,7 +335,9 @@ impl DHParameters {
331335

332336
Ok(DHParameterNumbers {
333337
p: py_p.extract()?,
334-
q: py_q.map(|q| q.extract()).transpose()?,
338+
q: py_q
339+
.map(|q| q.extract().map_err(CryptographyError::from))
340+
.transpose()?,
335341
g: py_g.extract()?,
336342
})
337343
}

src/rust/src/buf.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use pyo3::types::PyAnyMethods;
1010
use crate::types;
1111

1212
// Common error message generation
13-
fn generate_non_convertible_buffer_error_msg(pyobj: &pyo3::Bound<'_, pyo3::PyAny>) -> String {
13+
fn generate_non_convertible_buffer_error_msg(
14+
pyobj: &pyo3::Borrowed<'_, '_, pyo3::PyAny>,
15+
) -> String {
1416
if pyobj.is_instance_of::<pyo3::types::PyString>() {
1517
format!(
1618
"Cannot convert \"{}\" instance to a buffer.\nDid you mean to pass a bytestring instead?",
@@ -26,7 +28,7 @@ fn generate_non_convertible_buffer_error_msg(pyobj: &pyo3::Bound<'_, pyo3::PyAny
2628

2729
#[cfg(Py_3_11)]
2830
fn _extract_buffer_length(
29-
pyobj: &pyo3::Bound<'_, pyo3::PyAny>,
31+
pyobj: &pyo3::Borrowed<'_, '_, pyo3::PyAny>,
3032
mutable: bool,
3133
) -> pyo3::PyResult<(Option<pyo3::buffer::PyBuffer<u8>>, usize, usize)> {
3234
let buf = pyo3::buffer::PyBuffer::<u8>::get(pyobj).map_err(|_| {
@@ -45,7 +47,7 @@ fn _extract_buffer_length(
4547

4648
#[cfg(not(Py_3_11))]
4749
fn _extract_buffer_length<'p>(
48-
pyobj: &pyo3::Bound<'p, pyo3::PyAny>,
50+
pyobj: &pyo3::Borrowed<'_, 'p, pyo3::PyAny>,
4951
mutable: bool,
5052
) -> pyo3::PyResult<(pyo3::Bound<'p, pyo3::PyAny>, usize, usize)> {
5153
let py = pyobj.py();
@@ -101,9 +103,11 @@ impl<'a> CffiBuf<'a> {
101103
}
102104
}
103105

104-
impl<'a> pyo3::conversion::FromPyObject<'a> for CffiBuf<'a> {
105-
fn extract_bound(pyobj: &pyo3::Bound<'a, pyo3::PyAny>) -> pyo3::PyResult<Self> {
106-
let (bufobj, ptrval, len) = _extract_buffer_length(pyobj, false)?;
106+
impl<'p> pyo3::conversion::FromPyObject<'_, 'p> for CffiBuf<'p> {
107+
type Error = pyo3::PyErr;
108+
109+
fn extract(pyobj: pyo3::Borrowed<'_, 'p, pyo3::PyAny>) -> pyo3::PyResult<Self> {
110+
let (bufobj, ptrval, len) = _extract_buffer_length(&pyobj, false)?;
107111
let buf = if len == 0 {
108112
&[]
109113
} else {
@@ -118,7 +122,7 @@ impl<'a> pyo3::conversion::FromPyObject<'a> for CffiBuf<'a> {
118122
unsafe { slice::from_raw_parts(ptrval as *const u8, len) }
119123
};
120124
Ok(CffiBuf {
121-
pyobj: pyobj.clone(),
125+
pyobj: pyobj.to_owned(),
122126
_bufobj: bufobj,
123127
buf,
124128
})
@@ -140,9 +144,11 @@ impl CffiMutBuf<'_> {
140144
}
141145
}
142146

143-
impl<'a> pyo3::conversion::FromPyObject<'a> for CffiMutBuf<'a> {
144-
fn extract_bound(pyobj: &pyo3::Bound<'a, pyo3::PyAny>) -> pyo3::PyResult<Self> {
145-
let (bufobj, ptrval, len) = _extract_buffer_length(pyobj, true)?;
147+
impl<'p> pyo3::conversion::FromPyObject<'_, 'p> for CffiMutBuf<'p> {
148+
type Error = pyo3::PyErr;
149+
150+
fn extract(pyobj: pyo3::Borrowed<'_, 'p, pyo3::PyAny>) -> pyo3::PyResult<Self> {
151+
let (bufobj, ptrval, len) = _extract_buffer_length(&pyobj, true)?;
146152
let buf = if len == 0 {
147153
&mut []
148154
} else {
@@ -157,7 +163,7 @@ impl<'a> pyo3::conversion::FromPyObject<'a> for CffiMutBuf<'a> {
157163
unsafe { slice::from_raw_parts_mut(ptrval as *mut u8, len) }
158164
};
159165
Ok(CffiMutBuf {
160-
_pyobj: pyobj.clone(),
166+
_pyobj: pyobj.to_owned(),
161167
_bufobj: bufobj,
162168
buf,
163169
})

src/rust/src/declarative_asn1/decode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn decode_pyint<'a>(
2727
) -> ParseResult<pyo3::Bound<'a, pyo3::types::PyInt>> {
2828
let value = parser.read_element::<asn1::BigInt<'a>>()?;
2929
let pyint =
30-
big_byte_slice_to_py_int(py, value.as_bytes())?.downcast_into::<pyo3::types::PyInt>()?;
30+
big_byte_slice_to_py_int(py, value.as_bytes())?.cast_into::<pyo3::types::PyInt>()?;
3131
Ok(pyint)
3232
}
3333

@@ -64,7 +64,7 @@ fn decode_utc_time<'a>(
6464
let dt = value.as_datetime();
6565

6666
let inner = crate::x509::datetime_to_py_utc(py, dt)?
67-
.downcast_into::<pyo3::types::PyDateTime>()?
67+
.cast_into::<pyo3::types::PyDateTime>()?
6868
.unbind();
6969

7070
Ok(pyo3::Bound::new(py, UtcTime { inner })?)
@@ -90,7 +90,7 @@ fn decode_generalized_time<'a>(
9090
};
9191

9292
let inner = crate::x509::datetime_to_py_utc_with_microseconds(py, dt, microseconds)?
93-
.downcast_into::<pyo3::types::PyDateTime>()?
93+
.cast_into::<pyo3::types::PyDateTime>()?
9494
.unbind();
9595

9696
Ok(pyo3::Bound::new(py, GeneralizedTime { inner })?)
@@ -121,7 +121,7 @@ pub(crate) fn decode_annotated_type<'a>(
121121
let kwargs = pyo3::types::PyDict::new(py);
122122
let fields = fields.bind(py);
123123
for (name, ann_type) in fields.into_iter() {
124-
let ann_type = ann_type.downcast::<AnnotatedType>()?;
124+
let ann_type = ann_type.cast::<AnnotatedType>()?;
125125
let value = decode_annotated_type(py, d, ann_type.get())?;
126126
kwargs.set_item(name, value)?;
127127
}

src/rust/src/declarative_asn1/encode.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ impl asn1::Asn1Writable for AnnotatedTypeObject<'_> {
4444
&asn1::SequenceWriter::new(&|w| {
4545
for (name, ann_type) in fields.bind(py).into_iter() {
4646
let name = name
47-
.downcast::<pyo3::types::PyString>()
47+
.cast::<pyo3::types::PyString>()
4848
.map_err(|_| asn1::WriteError::AllocationError)?;
4949
let ann_type = ann_type
50-
.downcast::<AnnotatedType>()
50+
.cast::<AnnotatedType>()
5151
.map_err(|_| asn1::WriteError::AllocationError)?;
5252
let object = AnnotatedTypeObject {
5353
annotated_type: ann_type.get(),
@@ -100,7 +100,7 @@ impl asn1::Asn1Writable for AnnotatedTypeObject<'_> {
100100
}
101101
Type::PrintableString() => {
102102
let val: &pyo3::Bound<'_, PrintableString> = value
103-
.downcast()
103+
.cast()
104104
.map_err(|_| asn1::WriteError::AllocationError)?;
105105
let inner_str = val
106106
.get()
@@ -114,7 +114,7 @@ impl asn1::Asn1Writable for AnnotatedTypeObject<'_> {
114114
}
115115
Type::UtcTime() => {
116116
let val: &pyo3::Bound<'_, UtcTime> = value
117-
.downcast()
117+
.cast()
118118
.map_err(|_| asn1::WriteError::AllocationError)?;
119119
let py_datetime = val.get().inner.clone_ref(py).into_bound(py);
120120
let datetime = crate::x509::py_to_datetime(py, py_datetime)
@@ -125,7 +125,7 @@ impl asn1::Asn1Writable for AnnotatedTypeObject<'_> {
125125
}
126126
Type::GeneralizedTime() => {
127127
let val: &pyo3::Bound<'_, GeneralizedTime> = value
128-
.downcast()
128+
.cast()
129129
.map_err(|_| asn1::WriteError::AllocationError)?;
130130
let py_datetime = val.get().inner.clone_ref(py).into_bound(py);
131131
let (datetime, microseconds) =

src/rust/src/declarative_asn1/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub(crate) fn python_class_to_annotated<'p>(
259259
) -> pyo3::PyResult<pyo3::Bound<'p, AnnotatedType>> {
260260
if let Ok(root) = class.getattr("__asn1_root__") {
261261
// Handle decorated classes
262-
Ok(root.downcast_into::<AnnotatedType>()?)
262+
Ok(root.cast_into::<AnnotatedType>()?)
263263
} else {
264264
// Handle builtin types
265265
pyo3::Bound::new(py, non_root_type_to_annotated(py, class)?)

src/rust/src/error.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ impl From<pyo3::PyErr> for CryptographyError {
3636
}
3737
}
3838

39-
impl From<pyo3::DowncastError<'_, '_>> for CryptographyError {
40-
fn from(e: pyo3::DowncastError<'_, '_>) -> CryptographyError {
39+
impl From<pyo3::CastError<'_, '_>> for CryptographyError {
40+
fn from(e: pyo3::CastError<'_, '_>) -> CryptographyError {
4141
CryptographyError::Py(e.into())
4242
}
4343
}
4444

45-
impl From<pyo3::DowncastIntoError<'_>> for CryptographyError {
46-
fn from(e: pyo3::DowncastIntoError<'_>) -> CryptographyError {
45+
impl From<pyo3::CastIntoError<'_>> for CryptographyError {
46+
fn from(e: pyo3::CastIntoError<'_>) -> CryptographyError {
4747
CryptographyError::Py(e.into())
4848
}
4949
}
@@ -305,6 +305,8 @@ pub(crate) fn capture_error_stack(
305305

306306
#[cfg(test)]
307307
mod tests {
308+
use pyo3::PyTypeInfo;
309+
308310
use super::CryptographyError;
309311

310312
#[test]
@@ -329,7 +331,11 @@ mod tests {
329331
let py_e: pyo3::PyErr = e.into();
330332
assert!(py_e.is_instance_of::<pyo3::exceptions::PyMemoryError>(py));
331333

332-
let e: CryptographyError = pyo3::DowncastError::new(py.None().bind(py), "abc").into();
334+
let e: CryptographyError = pyo3::CastError::new(
335+
py.None().bind(py).as_borrowed(),
336+
pyo3::types::PyString::type_object(py).into_any(),
337+
)
338+
.into();
333339
assert!(matches!(e, CryptographyError::Py(_)));
334340

335341
let e = cryptography_key_parsing::KeyParsingError::OpenSSL(
@@ -338,7 +344,11 @@ mod tests {
338344
.into();
339345
assert!(matches!(e, CryptographyError::OpenSSL(_)));
340346

341-
let e = pyo3::DowncastIntoError::new(py.None().into_bound(py), "abc").into();
347+
let e = pyo3::CastIntoError::new(
348+
py.None().into_bound(py),
349+
pyo3::types::PyString::type_object(py).into_any(),
350+
)
351+
.into();
342352
assert!(matches!(e, CryptographyError::Py(_)));
343353
})
344354
}

0 commit comments

Comments
 (0)