Skip to content
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
7 changes: 7 additions & 0 deletions .github/workflows/external-zstd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ jobs:

- name: Build
run: |
# Homebrew's prefix (/opt/homebrew on Apple Silicon) isn't on the
# default compiler search paths, and its libzstd is arm64 only, so
# don't build the universal2 binary the Python framework asks for.
ZSTD_PREFIX="$(brew --prefix zstd)"
export CFLAGS="-I${ZSTD_PREFIX}/include"
export LDFLAGS="-L${ZSTD_PREFIX}/lib"
export ARCHFLAGS="-arch arm64"
python -m pip install --config-settings=--global-option=--system-zstd .
52 changes: 10 additions & 42 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 @@ -26,5 +26,5 @@ version = "2.0.16"
features = ["experimental", "legacy", "zstdmt"]

[dependencies.pyo3]
version = "0.25.1"
version = "0.29.3"
features = ["extension-module"]
8 changes: 6 additions & 2 deletions ci/conda/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{% set data = load_setup_py_data() %}
# Don't use load_setup_py_data(): it runs setup.py inside the conda-build
# process, and setup.py's sys.path.insert(0, ".") then makes the unbuilt
# zstandard package in the source tree shadow the zstandard bindings that
# conda-package-handling needs for .conda support.
{% set version_match = load_file_regex(load_file="../../c-ext/python-zstandard.h", regex_pattern='PYTHON_ZSTANDARD_VERSION "([^"]+)"', from_recipe_dir=True) %}

package:
name: zstandard
version: "{{ data.get('version') }}"
version: "{{ version_match.group(1) }}"

source:
path: ../../
Expand Down
5 changes: 5 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Backwards Compatibility Notes
* cibuildwheel upgrade required us to drop generating the CPython 3.13 free-threaded
wheels.

Changes
-------

* PyO3 Rust crate upgraded from 0.25 to 0.29.

0.25.0 (released 2025-09-14)
============================

Expand Down
12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ requires = [
build-backend = "setuptools.build_meta"

[dependency-groups]
dev = [
# Dependencies needed to run the test suite. cibuildwheel installs this group
# unlocked inside its build containers, so keep it free of packages (like mypy)
# without wheels for every platform we build.
test = [
"cffi~=1.17; platform_python_implementation != 'PyPy' and python_version < '3.14'",
"cffi>=2.0.0b; platform_python_implementation != 'PyPy' and python_version >= '3.14'",
"hypothesis==6.111.0",
"mypy>=1.17.1",
"pytest>=8.4.1",
"pytest-xdist>=3.8.0",
]
dev = [
{ include-group = "test" },
"mypy>=1.17.1",
]

[tool.pytest.ini_options]
testpaths = ["tests"]
Expand All @@ -71,7 +77,7 @@ test-command = [
"pytest -v --numprocesses=auto",
"PYTHON_ZSTANDARD_IMPORT_POLICY=cffi pytest -v --numprocesses=auto",
]
test-groups = ["dev"]
test-groups = ["test"]
test-sources = ["tests"]

[tool.cibuildwheel.linux]
Expand Down
37 changes: 18 additions & 19 deletions rust-ext/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) struct BufferSegment {
#[pyclass(module = "zstandard.backend_rust", name = "BufferSegment")]
pub struct ZstdBufferSegment {
/// The object backing storage. For reference counting.
_parent: PyObject,
_parent: Py<PyAny>,
/// PyBuffer into parent object.
buffer: PyBuffer<u8>,
/// Offset of segment within data.
Expand All @@ -53,7 +53,7 @@ impl ZstdBufferSegment {
}

// PyBufferProtocol.
fn bf_getbuffer(slf: PyRefMut<Self>, view: PyObject, flags: i32) -> PyResult<()> {
fn bf_getbuffer(slf: PyRefMut<Self>, view: Py<PyAny>, flags: i32) -> PyResult<()> {
let slice = slf.as_slice();

if unsafe {
Expand All @@ -74,7 +74,7 @@ impl ZstdBufferSegment {
}

#[allow(unused_variables)]
fn bf_releasebuffer(slf: PyRefMut<Self>, view: PyObject) {}
fn bf_releasebuffer(slf: PyRefMut<Self>, view: Py<PyAny>) {}

// Our methods.

Expand All @@ -90,18 +90,18 @@ impl ZstdBufferSegment {

#[pyclass(module = "zstandard.backend_rust", name = "BufferSegments")]
pub struct ZstdBufferSegments {
parent: PyObject,
parent: Py<PyAny>,
}

unsafe impl Sync for ZstdBufferSegments {}

#[pymethods]
impl ZstdBufferSegments {
// PyBufferProtocol.
fn bf_getbuffer(slf: PyRefMut<Self>, view: PyObject, flags: i32) -> PyResult<()> {
fn bf_getbuffer(slf: PyRefMut<Self>, view: Py<PyAny>, flags: i32) -> PyResult<()> {
let py = slf.py();

let parent = slf.parent.downcast_bound::<ZstdBufferWithSegments>(py)?;
let parent = slf.parent.cast_bound::<ZstdBufferWithSegments>(py)?;

if unsafe {
pyo3::ffi::PyBuffer_FillInfo(
Expand All @@ -121,12 +121,12 @@ impl ZstdBufferSegments {
}

#[allow(unused_variables)]
fn bf_releasebuffer(slf: PyRefMut<Self>, view: PyObject) {}
fn bf_releasebuffer(slf: PyRefMut<Self>, view: Py<PyAny>) {}
}

#[pyclass(module = "zstandard.backend_rust", name = "BufferWithSegments")]
pub struct ZstdBufferWithSegments {
source: PyObject,
source: Py<PyAny>,
pub(crate) buffer: PyBuffer<u8>,
pub(crate) segments: Vec<BufferSegment>,
}
Expand Down Expand Up @@ -161,7 +161,7 @@ impl ZstdBufferWithSegments {
}

fn __getitem__(&self, key: isize) -> PyResult<ZstdBufferSegment> {
let py = unsafe { Python::assume_gil_acquired() };
let py = unsafe { Python::assume_attached() };

if key < 0 {
return Err(PyIndexError::new_err("offset must be non-negative"));
Expand All @@ -180,14 +180,14 @@ impl ZstdBufferWithSegments {

Ok(ZstdBufferSegment {
_parent: self.source.clone_ref(py),
buffer: PyBuffer::get(self.source.downcast_bound(py)?)?,
buffer: PyBuffer::get(self.source.cast_bound(py)?)?,
offset: segment.offset as _,
len: segment.length as _,
})
}

// PyBufferProtocol.
fn bf_getbuffer(slf: PyRefMut<Self>, view: PyObject, flags: i32) -> PyResult<()> {
fn bf_getbuffer(slf: PyRefMut<Self>, view: Py<PyAny>, flags: i32) -> PyResult<()> {
if unsafe {
pyo3::ffi::PyBuffer_FillInfo(
view.as_ptr() as *mut Py_buffer,
Expand All @@ -206,7 +206,7 @@ impl ZstdBufferWithSegments {
}

#[allow(unused_variables)]
fn bf_releasebuffer(slf: PyRefMut<Self>, view: PyObject) {}
fn bf_releasebuffer(slf: PyRefMut<Self>, view: Py<PyAny>) {}

// Our methods.

Expand Down Expand Up @@ -256,8 +256,7 @@ impl ZstdBufferWithSegments {

fn segments(slf: PyRef<Self>, py: Python) -> PyResult<ZstdBufferSegments> {
Ok(ZstdBufferSegments {
// TODO surely there is a better way to cast self to PyObject?
parent: unsafe { Py::from_borrowed_ptr(py, slf.as_ptr()) },
parent: slf.into_pyobject(py)?.into_any().unbind(),
})
}

Expand All @@ -272,7 +271,7 @@ impl ZstdBufferWithSegments {
)]
pub struct ZstdBufferWithSegmentsCollection {
// Py<ZstdBufferWithSegments>.
pub(crate) buffers: Vec<PyObject>,
pub(crate) buffers: Vec<Py<PyAny>>,
first_elements: Vec<usize>,
}

Expand All @@ -287,7 +286,7 @@ impl ZstdBufferWithSegmentsCollection {
}

fn __getitem__(&self, key: isize) -> PyResult<ZstdBufferSegment> {
let py = unsafe { Python::assume_gil_acquired() };
let py = unsafe { Python::assume_attached() };

if key < 0 {
return Err(PyIndexError::new_err("offset must be non-negative"));
Expand All @@ -309,7 +308,7 @@ impl ZstdBufferWithSegmentsCollection {
offset = self.first_elements[buffer_index - 1];
}

let item = segment.downcast_bound::<ZstdBufferWithSegments>(py)?;
let item = segment.cast_bound::<ZstdBufferWithSegments>(py)?;

return item.borrow().__getitem__((key - offset) as isize);
}
Expand All @@ -332,7 +331,7 @@ impl ZstdBufferWithSegmentsCollection {
let mut offset = 0;

for item in py_args {
let item = item.downcast::<ZstdBufferWithSegments>().map_err(|_| {
let item = item.cast::<ZstdBufferWithSegments>().map_err(|_| {
PyTypeError::new_err("arguments must be BufferWithSegments instances")
})?;
let segment = item.borrow();
Expand All @@ -359,7 +358,7 @@ impl ZstdBufferWithSegmentsCollection {
let mut size = 0;

for buffer in &self.buffers {
let item = buffer.downcast_bound::<ZstdBufferWithSegments>(py)?;
let item = buffer.cast_bound::<ZstdBufferWithSegments>(py)?;

for segment in &item.borrow().segments {
size += segment.length as usize;
Expand Down
4 changes: 2 additions & 2 deletions rust-ext/src/compression_chunker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ impl ZstdCompressionChunkerIterator {
slf
}

fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyObject>> {
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<Py<PyAny>>> {
if slf.finished {
return Ok(None);
}

let py = unsafe { Python::assume_gil_acquired() };
let py = unsafe { Python::assume_attached() };

// Consume any data left in the input.
while let Some(mut in_buffer) = slf.source.input_buffer(py)? {
Expand Down
4 changes: 2 additions & 2 deletions rust-ext/src/compression_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fn train_dictionary(
// validated to be PyBytes.
for sample in samples.iter() {
let bytes = sample
.downcast::<PyBytes>()
.cast::<PyBytes>()
.or_else(|_| Err(PyValueError::new_err("samples must be bytes")))?;

samples_len += bytes.as_bytes().len();
Expand All @@ -267,7 +267,7 @@ fn train_dictionary(
let mut sample_sizes: Vec<libc::size_t> = Vec::with_capacity(samples.len());

for sample in samples.iter() {
let bytes = sample.downcast::<PyBytes>()?;
let bytes = sample.cast::<PyBytes>()?;
let data = bytes.as_bytes();
sample_sizes.push(data.len());
samples_buffer.extend_from_slice(data);
Expand Down
Loading