Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 34 additions & 121 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
openssl = { version = "0.10", features = ["vendored"] }
pyo3 = { version = "0.20", features = ["extension-module"] }
pyo3 = { version = "0.26", features = ["extension-module"] }


[features]
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use openssl::x509::{X509Ref, X509};
use pyo3::create_exception;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::types::{PyBytes, PyModule};
use pyo3::wrap_pymodule;

fn _sign(
Expand Down Expand Up @@ -108,7 +108,7 @@ fn material_from_sources(
std::fs::read(path).map_err(|err| CertificateError::new_err(err.to_string()))
}
(None, Some(obj)) => {
let obj = obj.as_ref(py);
let obj = obj.bind(py);

if let Ok(value) = obj.extract::<&str>() {
Ok(value.as_bytes().to_vec())
Expand All @@ -130,7 +130,7 @@ fn material_from_sources(
}

#[pymodule]
fn exceptions(py: Python, m: &PyModule) -> PyResult<()> {
fn exceptions(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("RsmimeError", py.get_type::<RsmimeError>())?;
m.add("CertificateError", py.get_type::<CertificateError>())?;
m.add(
Expand All @@ -143,14 +143,16 @@ fn exceptions(py: Python, m: &PyModule) -> PyResult<()> {
}

#[pymodule]
fn rsmime(py: Python, m: &PyModule) -> PyResult<()> {
fn rsmime(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
let exceptions = wrap_pymodule!(exceptions);

py.import("sys")?
.getattr("modules")?
.set_item("rsmime.exceptions", exceptions(py))?;

m.add_wrapped(exceptions)?;
let exc_py = exceptions(py);
let exc_bound = exc_py.bind(py);
m.add_submodule(&exc_bound)?;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Duplicate Module Instantiation Causes Identity Issues

The exceptions module is instantiated twice: once for sys.modules registration and again when added as a submodule. This creates two separate module instances, which can lead to inconsistencies or identity issues if Python code expects a single module object.

Fix in Cursor Fix in Web

m.add_class::<Rsmime>()?;

Ok(())
Expand Down Expand Up @@ -191,15 +193,15 @@ impl Rsmime {

#[staticmethod]
#[pyo3(signature = (message, *, raise_on_expired = false))]
fn verify(py_: Python<'_>, message: Vec<u8>, raise_on_expired: bool) -> PyResult<PyObject> {
fn verify(py_: Python<'_>, message: Vec<u8>, raise_on_expired: bool) -> PyResult<Py<PyAny>> {
match _verify(&message, raise_on_expired) {
Ok(data) => Ok(PyBytes::new(py_, &data).into()),
Err(err) => Err(err),
}
}

#[pyo3(signature = (message, *, detached = false))]
fn sign(self_: PyRef<'_, Self>, message: Vec<u8>, detached: bool) -> PyResult<PyObject> {
fn sign(self_: PyRef<'_, Self>, message: Vec<u8>, detached: bool) -> PyResult<Py<PyAny>> {
match _sign(
self_.stack.as_ref(),
self_.cert.as_ref(),
Expand Down