Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump pyo3 version #7432

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
92 changes: 48 additions & 44 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions redwood/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0"
pyo3 = { version = "0.18.0", features = ["extension-module"] }
pyo3 = { version = "0.23.4", features = ["extension-module"] }
sequoia-openpgp = { version = "1.21.1", default-features = false, features = ["crypto-openssl", "compression"]}
thiserror = "1.0.31"

[dev-dependencies]
tempfile = "3.3.0"

[lints.rust]
# can be removed once we upgrade to pyo3 >= 0.23
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(addr_of)'] }
11 changes: 7 additions & 4 deletions redwood/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ const KEY_CREATION_SECONDS_FROM_EPOCH: u64 = 1368507600;

/// A Python module implemented in Rust.
#[pymodule]
fn redwood(py: Python, m: &PyModule) -> PyResult<()> {
fn redwood(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(generate_source_key_pair, m)?)?;
m.add_function(wrap_pyfunction!(is_valid_public_key, m)?)?;
m.add_function(wrap_pyfunction!(is_valid_secret_key, m)?)?;
m.add_function(wrap_pyfunction!(encrypt_message, m)?)?;
m.add_function(wrap_pyfunction!(encrypt_stream, m)?)?;
m.add_function(wrap_pyfunction!(decrypt, m)?)?;
m.add("RedwoodError", py.get_type::<RedwoodError>())?;
m.add("RedwoodError", m.py().get_type::<RedwoodError>())?;
Ok(())
}

Expand Down Expand Up @@ -124,7 +124,10 @@ pub fn is_valid_secret_key(input: &str, passphrase: String) -> Result<String> {
/// Encrypt a message (text) for the specified recipients. The list of
/// recipients is a set of PGP public keys. The encrypted message will
/// be written to `destination`.
#[pyfunction]
///
/// This macro signature addresses a new pyo3 warning about default values...
/// https://github.com/jcmgray/cotengrust/issues/2
#[pyfunction(signature = (recipients, plaintext, destination, armor=None))]
pub fn encrypt_message(
recipients: Vec<String>,
plaintext: String,
Expand All @@ -141,7 +144,7 @@ pub fn encrypt_message(
#[pyfunction]
pub fn encrypt_stream(
recipients: Vec<String>,
plaintext: &PyAny,
plaintext: Bound<PyAny>,
destination: PathBuf,
) -> Result<()> {
let stream = stream::Stream { reader: plaintext };
Expand Down
20 changes: 12 additions & 8 deletions redwood/src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use pyo3::types::PyBytes;
use pyo3::{intern, PyAny, PyResult};
//! redwood/stream.rs

use pyo3::types::{PyAnyMethods, PyBytes, PyTuple};
use pyo3::{intern, Bound, PyAny, PyResult};
use std::io::{self, ErrorKind, Read, Write};

/// Wrapper to implement the `Read` trait around a Python
/// object that contains a `.read()` function.
pub(crate) struct Stream<'a> {
pub(crate) reader: &'a PyAny,
pub(crate) reader: Bound<'a, PyAny>,
}

impl Stream<'_> {
/// Read the specified number of bytes out of the object
fn read_bytes(&self, len: usize) -> PyResult<&PyBytes> {
let func = self.reader.getattr(intern!(self.reader.py(), "read"))?;
fn read_bytes(&self, len: usize) -> PyResult<Vec<u8>> {
let func: Bound<'_, PyAny> =
self.reader.getattr(intern!(self.reader.py(), "read"))?;
// In Python this is effectively calling `reader.read(len)`
let bytes = func.call1((len,))?;
let args: Bound<PyTuple> = PyTuple::new(self.reader.py(), vec![len])?;
let bytes: Bound<PyAny> = func.call1(args)?;
let bytes = bytes.downcast::<PyBytes>()?;
Ok(bytes)
bytes.extract()
}
}

Expand All @@ -27,6 +31,6 @@ impl Read for Stream<'_> {
// all of them as "other" for simplicity.
io::Error::new(ErrorKind::Other, err.to_string())
})?;
buf.write(bytes.as_bytes())
buf.write(bytes.as_slice())
}
}