Skip to content

Commit 59d4a7d

Browse files
authored
Use pyo3 warning helper (#7433)
Much simpler than importing the warnings module
1 parent b744346 commit 59d4a7d

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

src/rust/src/x509/certificate.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,11 @@ impl Certificate {
351351
let cryptography_warning = py
352352
.import("cryptography.utils")?
353353
.getattr(crate::intern!(py, "DeprecatedIn35"))?;
354-
let warnings = py.import("warnings")?;
355-
warnings.call_method1(
356-
"warn",
357-
(
358-
"This version of cryptography contains a temporary pyOpenSSL fallback path. Upgrade pyOpenSSL now.",
359-
cryptography_warning,
360-
),
354+
pyo3::PyErr::warn(
355+
py,
356+
cryptography_warning,
357+
"This version of cryptography contains a temporary pyOpenSSL fallback path. Upgrade pyOpenSSL now.",
358+
1
361359
)?;
362360
let backend = py
363361
.import("cryptography.hazmat.backends.openssl.backend")?
@@ -415,13 +413,11 @@ fn warn_if_negative_serial(py: pyo3::Python<'_>, bytes: &'_ [u8]) -> pyo3::PyRes
415413
let cryptography_warning = py
416414
.import("cryptography.utils")?
417415
.getattr(crate::intern!(py, "DeprecatedIn36"))?;
418-
let warnings = py.import("warnings")?;
419-
warnings.call_method1(
420-
"warn",
421-
(
422-
"Parsed a negative serial number, which is disallowed by RFC 5280.",
423-
cryptography_warning,
424-
),
416+
pyo3::PyErr::warn(
417+
py,
418+
cryptography_warning,
419+
"Parsed a negative serial number, which is disallowed by RFC 5280.",
420+
1,
425421
)?;
426422
}
427423
Ok(())

src/rust/src/x509/crl.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,11 @@ impl CertificateRevocationList {
407407
let cryptography_warning = py
408408
.import("cryptography.utils")?
409409
.getattr(crate::intern!(py, "DeprecatedIn35"))?;
410-
let warnings = py.import("warnings")?;
411-
warnings.call_method1(
412-
"warn",
413-
(
414-
"This version of cryptography contains a temporary pyOpenSSL fallback path. Upgrade pyOpenSSL now.",
415-
cryptography_warning,
416-
),
410+
pyo3::PyErr::warn(
411+
py,
412+
cryptography_warning,
413+
"This version of cryptography contains a temporary pyOpenSSL fallback path. Upgrade pyOpenSSL now.",
414+
1
417415
)?;
418416
let backend = py
419417
.import("cryptography.hazmat.backends.openssl.backend")?

src/rust/src/x509/csr.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,11 @@ impl CertificateSigningRequest {
207207
let cryptography_warning = py
208208
.import("cryptography.utils")?
209209
.getattr(crate::intern!(py, "DeprecatedIn36"))?;
210-
let warnings = py.import("warnings")?;
211-
warnings.call_method1(
212-
"warn",
213-
(
214-
"CertificateSigningRequest.get_attribute_for_oid has been deprecated. Please switch to request.attributes.get_attribute_for_oid.",
215-
cryptography_warning,
216-
),
210+
pyo3::PyErr::warn(
211+
py,
212+
cryptography_warning,
213+
"CertificateSigningRequest.get_attribute_for_oid has been deprecated. Please switch to request.attributes.get_attribute_for_oid.",
214+
1,
217215
)?;
218216
let rust_oid = py_oid_to_oid(oid)?;
219217
for attribute in self
@@ -309,13 +307,11 @@ impl CertificateSigningRequest {
309307
let cryptography_warning = py
310308
.import("cryptography.utils")?
311309
.getattr(crate::intern!(py, "DeprecatedIn35"))?;
312-
let warnings = py.import("warnings")?;
313-
warnings.call_method1(
314-
"warn",
315-
(
316-
"This version of cryptography contains a temporary pyOpenSSL fallback path. Upgrade pyOpenSSL now.",
317-
cryptography_warning,
318-
),
310+
pyo3::PyErr::warn(
311+
py,
312+
cryptography_warning,
313+
"This version of cryptography contains a temporary pyOpenSSL fallback path. Upgrade pyOpenSSL now.",
314+
1,
319315
)?;
320316
let backend = py
321317
.import("cryptography.hazmat.backends.openssl.backend")?

src/rust/src/x509/sign.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ fn identify_hash_type(
101101
let cryptography_warning = py
102102
.import("cryptography.utils")?
103103
.getattr(crate::intern!(py, "DeprecatedIn38"))?;
104-
let warnings = py.import("warnings")?;
105-
warnings.call_method1(
106-
"warn",
107-
(
108-
"MD5 signatures are deprecated and support for them will be removed in the next version.",
109-
cryptography_warning,
110-
),
104+
pyo3::PyErr::warn(
105+
py,
106+
cryptography_warning,
107+
"MD5 signatures are deprecated and support for them will be removed in the next version.",
108+
1
111109
)?;
112110

113111
Ok(HashType::Md5)
@@ -116,14 +114,13 @@ fn identify_hash_type(
116114
let cryptography_warning = py
117115
.import("cryptography.utils")?
118116
.getattr(crate::intern!(py, "DeprecatedIn38"))?;
119-
let warnings = py.import("warnings")?;
120-
warnings.call_method1(
121-
"warn",
122-
(
123-
"SHA1 signatures are deprecated and support for them will be removed in the next version.",
124-
cryptography_warning,
125-
),
117+
pyo3::PyErr::warn(
118+
py,
119+
cryptography_warning,
120+
"SHA1 signatures are deprecated and support for them will be removed in the next version.",
121+
1
126122
)?;
123+
127124
Ok(HashType::Sha1)
128125
}
129126
"sha224" => Ok(HashType::Sha224),

0 commit comments

Comments
 (0)