Skip to content

Commit f137596

Browse files
authored
Update to pyo3-0.23 (#11954)
* WIP: Update to pyo3-0.23 * update Cargo.toml * fix lifetime error * avoid unnecessary allocations constructing warning messages * point at 0.23 on crates.io * add _str_ref_to_cstr_ref helper for constructing warnings * use null-terminated strings * fix inline null typos * add cstr_from_literal macro for constructing warnings
1 parent 466eea7 commit f137596

38 files changed

+507
-533
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ rust-version = "1.65.0"
2020

2121
[workspace.dependencies]
2222
asn1 = { version = "0.18.0", default-features = false }
23-
pyo3 = { version = "0.22.6", features = ["abi3"] }
23+
pyo3 = { version = "0.23.0", features = ["abi3"] }
2424

2525
[profile.release]
2626
overflow-checks = true

src/rust/src/asn1.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cryptography_x509::common::{DssSignature, SubjectPublicKeyInfo};
66
use pyo3::pybacked::PyBackedBytes;
77
use pyo3::types::IntoPyDict;
88
use pyo3::types::PyAnyMethods;
9-
use pyo3::ToPyObject;
9+
use pyo3::IntoPyObject;
1010

1111
use crate::error::{CryptographyError, CryptographyResult};
1212
use crate::types;
@@ -38,7 +38,7 @@ fn parse_spki_for_data<'p>(
3838
return Err(pyo3::exceptions::PyValueError::new_err("Invalid public key encoding").into());
3939
}
4040

41-
Ok(pyo3::types::PyBytes::new_bound(
41+
Ok(pyo3::types::PyBytes::new(
4242
py,
4343
spki.subject_public_key.as_bytes(),
4444
))
@@ -48,8 +48,8 @@ pub(crate) fn big_byte_slice_to_py_int<'p>(
4848
py: pyo3::Python<'p>,
4949
v: &'_ [u8],
5050
) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::PyAny>> {
51-
let int_type = py.get_type_bound::<pyo3::types::PyLong>();
52-
let kwargs = [("signed", true)].into_py_dict_bound(py);
51+
let int_type = py.get_type::<pyo3::types::PyInt>();
52+
let kwargs = [("signed", true)].into_py_dict(py)?;
5353
int_type.call_method(pyo3::intern!(py, "from_bytes"), (v, "big"), Some(&kwargs))
5454
}
5555

@@ -64,12 +64,14 @@ fn decode_dss_signature(
6464
big_byte_slice_to_py_int(py, sig.r.as_bytes())?,
6565
big_byte_slice_to_py_int(py, sig.s.as_bytes())?,
6666
)
67-
.to_object(py))
67+
.into_pyobject(py)?
68+
.into_any()
69+
.unbind())
6870
}
6971

7072
pub(crate) fn py_uint_to_big_endian_bytes<'p>(
7173
py: pyo3::Python<'p>,
72-
v: pyo3::Bound<'p, pyo3::types::PyLong>,
74+
v: pyo3::Bound<'p, pyo3::types::PyInt>,
7375
) -> pyo3::PyResult<PyBackedBytes> {
7476
if v.lt(0)? {
7577
return Err(pyo3::exceptions::PyValueError::new_err(
@@ -96,9 +98,9 @@ pub(crate) fn encode_der_data<'p>(
9698
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
9799
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
98100
if encoding.is(&types::ENCODING_DER.get(py)?) {
99-
Ok(pyo3::types::PyBytes::new_bound(py, &data))
101+
Ok(pyo3::types::PyBytes::new(py, &data))
100102
} else if encoding.is(&types::ENCODING_PEM.get(py)?) {
101-
Ok(pyo3::types::PyBytes::new_bound(
103+
Ok(pyo3::types::PyBytes::new(
102104
py,
103105
&pem::encode_config(
104106
&pem::Pem::new(pem_tag, data),
@@ -117,8 +119,8 @@ pub(crate) fn encode_der_data<'p>(
117119
#[pyo3::pyfunction]
118120
fn encode_dss_signature<'p>(
119121
py: pyo3::Python<'p>,
120-
r: pyo3::Bound<'_, pyo3::types::PyLong>,
121-
s: pyo3::Bound<'_, pyo3::types::PyLong>,
122+
r: pyo3::Bound<'_, pyo3::types::PyInt>,
123+
s: pyo3::Bound<'_, pyo3::types::PyInt>,
122124
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
123125
let r_bytes = py_uint_to_big_endian_bytes(py, r)?;
124126
let s_bytes = py_uint_to_big_endian_bytes(py, s)?;
@@ -127,7 +129,7 @@ fn encode_dss_signature<'p>(
127129
s: asn1::BigUint::new(&s_bytes).unwrap(),
128130
};
129131
let result = asn1::write_single(&sig)?;
130-
Ok(pyo3::types::PyBytes::new_bound(py, &result))
132+
Ok(pyo3::types::PyBytes::new(py, &result))
131133
}
132134

133135
#[pyo3::pymodule]

src/rust/src/backend/aead.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl EvpCipherAead {
172172

173173
Self::process_aad(&mut ctx, aad)?;
174174

175-
Ok(pyo3::types::PyBytes::new_bound_with(
175+
Ok(pyo3::types::PyBytes::new_with(
176176
py,
177177
plaintext.len() + tag_len,
178178
|b| {
@@ -254,7 +254,7 @@ impl EvpCipherAead {
254254

255255
Self::process_aad(&mut ctx, aad)?;
256256

257-
Ok(pyo3::types::PyBytes::new_bound_with(
257+
Ok(pyo3::types::PyBytes::new_with(
258258
py,
259259
ciphertext_data.len(),
260260
|b| {
@@ -399,7 +399,7 @@ impl EvpAead {
399399
assert!(aad.is_none());
400400
b""
401401
};
402-
Ok(pyo3::types::PyBytes::new_bound_with(
402+
Ok(pyo3::types::PyBytes::new_with(
403403
py,
404404
plaintext.len() + self.tag_len,
405405
|b| {
@@ -430,7 +430,7 @@ impl EvpAead {
430430
b""
431431
};
432432

433-
Ok(pyo3::types::PyBytes::new_bound_with(
433+
Ok(pyo3::types::PyBytes::new_with(
434434
py,
435435
ciphertext.len() - self.tag_len,
436436
|b| {

src/rust/src/backend/ciphers.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::error::{CryptographyError, CryptographyResult};
88
use crate::exceptions;
99
use crate::types;
1010
use pyo3::types::PyAnyMethods;
11-
use pyo3::IntoPy;
11+
use pyo3::IntoPyObject;
1212

1313
pub(crate) struct CipherContext {
1414
ctx: openssl::cipher_ctx::CipherCtx,
@@ -160,7 +160,7 @@ impl CipherContext {
160160
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
161161
let mut buf = vec![0; data.len() + self.ctx.block_size()];
162162
let n = self.update_into(py, data, &mut buf)?;
163-
Ok(pyo3::types::PyBytes::new_bound(py, &buf[..n]))
163+
Ok(pyo3::types::PyBytes::new(py, &buf[..n]))
164164
}
165165

166166
pub(crate) fn update_into(
@@ -224,7 +224,7 @@ impl CipherContext {
224224
),
225225
))
226226
})?;
227-
Ok(pyo3::types::PyBytes::new_bound(py, &out_buf[..n]))
227+
Ok(pyo3::types::PyBytes::new(py, &out_buf[..n]))
228228
}
229229
}
230230

@@ -359,7 +359,7 @@ impl PyAEADEncryptionContext {
359359
let result = ctx.finalize(py)?;
360360

361361
// XXX: do not hard code 16
362-
let tag = pyo3::types::PyBytes::new_bound_with(py, 16, |t| {
362+
let tag = pyo3::types::PyBytes::new_with(py, 16, |t| {
363363
ctx.ctx.tag(t).map_err(CryptographyError::from)?;
364364
Ok(())
365365
})?;
@@ -539,9 +539,14 @@ fn create_encryption_ctx(
539539
.getattr(pyo3::intern!(py, "_MAX_AAD_BYTES"))?
540540
.extract()?,
541541
}
542-
.into_py(py))
542+
.into_pyobject(py)?
543+
.into_any()
544+
.unbind())
543545
} else {
544-
Ok(PyCipherContext { ctx: Some(ctx) }.into_py(py))
546+
Ok(PyCipherContext { ctx: Some(ctx) }
547+
.into_pyobject(py)?
548+
.into_any()
549+
.unbind())
545550
}
546551
}
547552

@@ -571,9 +576,14 @@ fn create_decryption_ctx(
571576
.getattr(pyo3::intern!(py, "_MAX_AAD_BYTES"))?
572577
.extract()?,
573578
}
574-
.into_py(py))
579+
.into_pyobject(py)?
580+
.into_any()
581+
.unbind())
575582
} else {
576-
Ok(PyCipherContext { ctx: Some(ctx) }.into_py(py))
583+
Ok(PyCipherContext { ctx: Some(ctx) }
584+
.into_pyobject(py)?
585+
.into_any()
586+
.unbind())
577587
}
578588
}
579589

src/rust/src/backend/cmac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Cmac {
7777
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
7878
let data = self.get_mut_ctx()?.finish()?;
7979
self.ctx = None;
80-
Ok(pyo3::types::PyBytes::new_bound(py, &data))
80+
Ok(pyo3::types::PyBytes::new(py, &data))
8181
}
8282

8383
fn verify(&mut self, py: pyo3::Python<'_>, signature: &[u8]) -> CryptographyResult<()> {

src/rust/src/backend/dh.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl DHPrivateKey {
149149
.map_err(|_| pyo3::exceptions::PyValueError::new_err("Error computing shared key."))?;
150150

151151
let len = deriver.len()?;
152-
Ok(pyo3::types::PyBytes::new_bound_with(py, len, |b| {
152+
Ok(pyo3::types::PyBytes::new_with(py, len, |b| {
153153
let n = deriver.derive(b).unwrap();
154154

155155
let pad = b.len() - n;
@@ -363,34 +363,34 @@ impl DHParameters {
363363
#[pyo3::pyclass(frozen, module = "cryptography.hazmat.primitives.asymmetric.dh")]
364364
struct DHPrivateNumbers {
365365
#[pyo3(get)]
366-
x: pyo3::Py<pyo3::types::PyLong>,
366+
x: pyo3::Py<pyo3::types::PyInt>,
367367
#[pyo3(get)]
368368
public_numbers: pyo3::Py<DHPublicNumbers>,
369369
}
370370

371371
#[pyo3::pyclass(frozen, module = "cryptography.hazmat.primitives.asymmetric.dh")]
372372
struct DHPublicNumbers {
373373
#[pyo3(get)]
374-
y: pyo3::Py<pyo3::types::PyLong>,
374+
y: pyo3::Py<pyo3::types::PyInt>,
375375
#[pyo3(get)]
376376
parameter_numbers: pyo3::Py<DHParameterNumbers>,
377377
}
378378

379379
#[pyo3::pyclass(frozen, module = "cryptography.hazmat.primitives.asymmetric.dh")]
380380
struct DHParameterNumbers {
381381
#[pyo3(get)]
382-
p: pyo3::Py<pyo3::types::PyLong>,
382+
p: pyo3::Py<pyo3::types::PyInt>,
383383
#[pyo3(get)]
384-
g: pyo3::Py<pyo3::types::PyLong>,
384+
g: pyo3::Py<pyo3::types::PyInt>,
385385
#[pyo3(get)]
386-
q: Option<pyo3::Py<pyo3::types::PyLong>>,
386+
q: Option<pyo3::Py<pyo3::types::PyInt>>,
387387
}
388388

389389
#[pyo3::pymethods]
390390
impl DHPrivateNumbers {
391391
#[new]
392392
fn new(
393-
x: pyo3::Py<pyo3::types::PyLong>,
393+
x: pyo3::Py<pyo3::types::PyInt>,
394394
public_numbers: pyo3::Py<DHPublicNumbers>,
395395
) -> DHPrivateNumbers {
396396
DHPrivateNumbers { x, public_numbers }
@@ -428,7 +428,7 @@ impl DHPrivateNumbers {
428428
py: pyo3::Python<'_>,
429429
other: pyo3::PyRef<'_, Self>,
430430
) -> CryptographyResult<bool> {
431-
Ok(self.x.bind(py).eq(other.x.bind(py))?
431+
Ok((**self.x.bind(py)).eq(other.x.bind(py))?
432432
&& self
433433
.public_numbers
434434
.bind(py)
@@ -440,7 +440,7 @@ impl DHPrivateNumbers {
440440
impl DHPublicNumbers {
441441
#[new]
442442
fn new(
443-
y: pyo3::Py<pyo3::types::PyLong>,
443+
y: pyo3::Py<pyo3::types::PyInt>,
444444
parameter_numbers: pyo3::Py<DHParameterNumbers>,
445445
) -> DHPublicNumbers {
446446
DHPublicNumbers {
@@ -472,7 +472,7 @@ impl DHPublicNumbers {
472472
py: pyo3::Python<'_>,
473473
other: pyo3::PyRef<'_, Self>,
474474
) -> CryptographyResult<bool> {
475-
Ok(self.y.bind(py).eq(other.y.bind(py))?
475+
Ok((**self.y.bind(py)).eq(other.y.bind(py))?
476476
&& self
477477
.parameter_numbers
478478
.bind(py)
@@ -486,9 +486,9 @@ impl DHParameterNumbers {
486486
#[pyo3(signature = (p, g, q=None))]
487487
fn new(
488488
py: pyo3::Python<'_>,
489-
p: pyo3::Py<pyo3::types::PyLong>,
490-
g: pyo3::Py<pyo3::types::PyLong>,
491-
q: Option<pyo3::Py<pyo3::types::PyLong>>,
489+
p: pyo3::Py<pyo3::types::PyInt>,
490+
g: pyo3::Py<pyo3::types::PyInt>,
491+
q: Option<pyo3::Py<pyo3::types::PyInt>>,
492492
) -> CryptographyResult<DHParameterNumbers> {
493493
if g.bind(py).lt(2)? {
494494
return Err(CryptographyError::from(
@@ -528,12 +528,12 @@ impl DHParameterNumbers {
528528
other: pyo3::PyRef<'_, Self>,
529529
) -> CryptographyResult<bool> {
530530
let q_equal = match (self.q.as_ref(), other.q.as_ref()) {
531-
(Some(self_q), Some(other_q)) => self_q.bind(py).eq(other_q.bind(py))?,
531+
(Some(self_q), Some(other_q)) => (**self_q.bind(py)).eq(other_q.bind(py))?,
532532
(None, None) => true,
533533
_ => false,
534534
};
535-
Ok(self.p.bind(py).eq(other.p.bind(py))?
536-
&& self.g.bind(py).eq(other.g.bind(py))?
535+
Ok((**self.p.bind(py)).eq(other.p.bind(py))?
536+
&& (**self.g.bind(py)).eq(other.g.bind(py))?
537537
&& q_equal)
538538
}
539539
}

0 commit comments

Comments
 (0)