Skip to content

Commit

Permalink
Update PyO3 to 0.23 (#200)
Browse files Browse the repository at this point in the history
* Update PyO3 to 0.23

* add upper bound to hypothesis version
  • Loading branch information
ngoldbaum authored Feb 14, 2025
1 parent 61564e7 commit 1ff8aab
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ wasm32-compat = ["libcramjam/wasm32-compat"]


[dependencies]
pyo3 = { version = "^0.22", default-features = false, features = ["macros"] }
pyo3 = { version = "^0.23", default-features = false, features = ["macros"] }
libcramjam = { version = "^0.6", default-features = false }

[build-dependencies]
pyo3-build-config = "^0.22"
pyo3-build-config = "^0.23"

[profile.release]
strip = true
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ dev = [
"pytest>=5.30",
"pytest-xdist",
"pytest-benchmark",
"hypothesis"
"hypothesis<6.123.0"
]
4 changes: 2 additions & 2 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,14 @@ fn write<W: Write>(input: &mut BytesType, output: &mut W) -> std::io::Result<u64

fn read<'a, R: Read>(reader: &mut R, py: Python<'a>, n_bytes: Option<usize>) -> PyResult<Bound<'a, PyBytes>> {
match n_bytes {
Some(n) => PyBytes::new_bound_with(py, n, |buf| {
Some(n) => PyBytes::new_with(py, n, |buf| {
reader.read(buf)?;
Ok(())
}),
None => {
let mut buf = vec![];
reader.read_to_end(&mut buf)?;
Ok(PyBytes::new_bound(py, buf.as_slice()))
Ok(PyBytes::new(py, buf.as_slice()))
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/lz4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod lz4 {
use pyo3::prelude::*;
use pyo3::PyResult;
use std::io::Cursor;
use std::sync::Mutex;

const DEFAULT_COMPRESSION_LEVEL: u32 = 4;

Expand Down Expand Up @@ -204,7 +205,7 @@ pub mod lz4 {
/// lz4 Compressor object for streaming compression
#[pyclass]
pub struct Compressor {
inner: Option<libcramjam::lz4::lz4::Encoder<Cursor<Vec<u8>>>>,
inner: Mutex<Option<libcramjam::lz4::lz4::Encoder<Cursor<Vec<u8>>>>>,
}

#[pymethods]
Expand All @@ -229,18 +230,20 @@ pub mod lz4 {
_ => BlockMode::Linked,
})
.build(Cursor::new(vec![]))?;
Ok(Self { inner: Some(inner) })
Ok(Self {
inner: Mutex::new(Some(inner)),
})
}

/// Compress input into the current compressor's stream.
pub fn compress(&mut self, input: &[u8]) -> PyResult<usize> {
crate::io::stream_compress(&mut self.inner, input)
crate::io::stream_compress(&mut self.inner.lock().unwrap(), input)
}

/// Flush and return current compressed stream
#[allow(mutable_transmutes)] // TODO: feature req to lz4 to get mut ref to writer
pub fn flush(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_flush(&mut self.inner, |e| {
crate::io::stream_flush(&mut self.inner.lock().unwrap(), |e| {
let writer = e.writer();
// no other mutations to buf b/c it'll be truncated and return immediately after this
unsafe { std::mem::transmute::<&Cursor<Vec<u8>>, &mut Cursor<Vec<u8>>>(writer) }
Expand All @@ -250,7 +253,7 @@ pub mod lz4 {
/// Consume the current compressor state and return the compressed stream
/// **NB** The compressor will not be usable after this method is called.
pub fn finish(&mut self) -> PyResult<RustyBuffer> {
crate::io::stream_finish(&mut self.inner, |inner| {
crate::io::stream_finish(&mut self.inner.lock().unwrap(), |inner| {
let (cursor, result) = inner.finish();
result.map(|_| cursor.into_inner())
})
Expand Down

0 comments on commit 1ff8aab

Please # to comment.