diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index abb29d4..b6edc07 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,7 +61,7 @@ jobs: - "3.15" - "3.15t" - "pypy-3.11" - - "pypy-3.11" + - "pypy-3.12" os: ["ubuntu-latest"] include: - os: "macos-latest" # For m1 macos @@ -156,7 +156,75 @@ jobs: env: PYTHON_ZLIB_NG_LINK_DYNAMIC: True - deploy: + # Run the tests on CPython builds with AddressSanitizer and ThreadSanitizer + # from https://github.com/nascheme/cpython_sanity. + test-sanitizers: + runs-on: ubuntu-latest + # A crash under ThreadSanitizer can hang instead of exiting. + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + include: + - sanitizer: address + image: ghcr.io/nascheme/cpython-asan:3.14 + # The free-threaded build runs Python threads in parallel. + - sanitizer: thread + image: ghcr.io/nascheme/cpython-tsan:3.14t + container: + image: ${{ matrix.image }} + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Install build dependencies + run: apt-get update && apt-get install -y cmake + - name: Install python-isal with ${{ matrix.sanitizer }} sanitizer + # setuptools-scm needs git, and the container user does not own the + # repository. + run: | + git config --global --add safe.directory "$GITHUB_WORKSPACE" + python -m pip install . pytest pytest-timeout + env: + # The extension modules get -fsanitize from CPython. CFLAGS adds it + # to the C code of ISA-L. + CFLAGS: -fsanitize=${{ matrix.sanitizer }} -fno-omit-frame-pointer -g + - name: Run tests + # Do not set PYTHONDEVMODE. Its debug allocator pads each allocation, + # so AddressSanitizer misses small overflows. --capture=sys prints + # sanitizer reports from C code even when they end the process, and + # -v prints the name of the test before its report. + run: python -m pytest -v tests --capture=sys + env: + # Exit at the first data race. A crash after a race can hang. + TSAN_OPTIONS: halt_on_error=1 + + make_sdist: + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') + needs: + - lint + - package-checks + - test-static + - test-dynamic + - test-arch + - test-sanitizers + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + with: + fetch-depth: 0 # Optional, use if you use setuptools_scm + submodules: true # Optional, use if you have submodules + persist-credentials: false # Highly recommended as a good security practice to not store credentials in disk, unless you need this for your specific use case + + - name: Build SDist + run: pipx run build --sdist + + - uses: actions/upload-artifact@v4 + with: + name: cibw-sdist + path: dist/*.tar.gz + + build_wheels: if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') runs-on: ${{ matrix.os }} needs: @@ -165,6 +233,7 @@ jobs: - test-static - test-dynamic - test-arch + - test-sanitizers strategy: matrix: os: @@ -233,25 +302,35 @@ jobs: PYTHON_ZLIB_NG_BUILD_CACHE=True PYTHON_ZLIB_NG_BUILD_CACHE_FILE=${{ runner.temp }}/build_cache CFLAGS="-O3 -DNDEBUG" - - name: Build sdist - if: ${{runner.os == 'Linux' && matrix.cibw_archs_linux == 'x86_64'}} - run: - python -m build --sdist - uses: actions/upload-artifact@v7 with: - name: "dist-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cibw_archs_linux }}" + name: "cibw-${{ runner.os }}-${{ runner.arch }}-${{ matrix.cibw_archs_linux }}" path: "dist/" - - name: Publish package to TestPyPI - # pypa/gh-action-pypi-publish@master does not work on OSX + + upload_to_pypi: + needs: [ build_wheels, make_sdist ] + environment: pypi + permissions: + id-token: write + runs-on: ubuntu-latest + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') + steps: + - uses: actions/download-artifact@v5 + with: + pattern: cibw-* + path: dist + merge-multiple: true + + - name: Publish to test PyPI # Alpha, Beta and dev releases contain a - in the tag. - if: contains(github.ref, '-') && startsWith(github.ref, 'refs/tags') - run: twine upload --skip-existing -r testpypi dist/* - env: - TWINE_USERNAME: __token__ - TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }} - - name: Publish package to PyPI + if: "contains(github.ref, '-') && startsWith(github.ref, 'refs/tags')" + uses: pypa/gh-action-pypi-publish@release/v1 + with: + repository-url: https://test.pypi.org/legacy/ + skip-existing: true + + - name: Publish to PyPI if: "!contains(github.ref, '-') && startsWith(github.ref, 'refs/tags')" - run: twine upload --skip-existing dist/* - env: - TWINE_USERNAME: __token__ - TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} + uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true diff --git a/.readthedocs.yml b/.readthedocs.yml index cb71cd8..1a83c66 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -14,6 +14,8 @@ build: os: "ubuntu-22.04" tools: python: "3" + apt_packages: + - cmake # Make sure zlib-ng is cloned alongside submodules: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e30b5ea..5aabc60 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,9 +7,13 @@ Changelog .. This document is user facing. Please word the changes in such a way .. that users understand how the changes affect the new version. -version 1.0.1-dev +version 1.1.0 ----------------- ++ Python 3.10 and PyPy 3.10 are no longer supported. Added support for PyPy + 3.12 and Python 3.15. + Update included zlib-ng distribution to 2.3.3. ++ Fix BrokenPipeError when piping ``python -m zlib_ng.gzip_ng`` into ``head``, + ``less`` or similar programs. + Fix ``gzip_ng_threaded`` writers that waited forever when writing to the underlying file failed, for example, because the disk was full. A later call to ``write()``, ``flush()``, or ``close()`` raises the error. After an error, diff --git a/pyproject.toml b/pyproject.toml index 531017a..65409e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,11 +15,11 @@ requires-python = ">=3.9" # Because of setuptools version classifiers = [ "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: 3.15", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Programming Language :: C", diff --git a/src/zlib_ng/gzip_ng.py b/src/zlib_ng/gzip_ng.py index f24bdbe..c2afec2 100644 --- a/src/zlib_ng/gzip_ng.py +++ b/src/zlib_ng/gzip_ng.py @@ -304,6 +304,9 @@ def main(): READ_BUFFER_SIZE = args.buffer_size try: shutil.copyfileobj(in_file, out_file, args.buffer_size) + except BrokenPipeError: + # Do not throw errors when a pipe to head or less closes. + pass finally: if in_file is not sys.stdin.buffer: in_file.close() diff --git a/src/zlib_ng/zlib_ngmodule.c b/src/zlib_ng/zlib_ngmodule.c index 59233e5..d2b5e79 100644 --- a/src/zlib_ng/zlib_ngmodule.c +++ b/src/zlib_ng/zlib_ngmodule.c @@ -1183,8 +1183,10 @@ decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length) self->avail_in_real += self->zst.avail_in; - if (_PyBytes_Resize(&return_value, self->zst.next_out - - (uint8_t *)PyBytes_AS_STRING(return_value)) != 0) { + /* Prevent empty string object from being resized. */ + if (PyBytes_GET_SIZE(return_value) != 0 && + _PyBytes_Resize(&return_value, self->zst.next_out - + (uint8_t *)PyBytes_AS_STRING(return_value)) != 0) { goto error; } diff --git a/tests/test_gzip_ng.py b/tests/test_gzip_ng.py index 171bd84..41eb954 100644 --- a/tests/test_gzip_ng.py +++ b/tests/test_gzip_ng.py @@ -15,6 +15,7 @@ import re import shutil import struct +import subprocess import sys import tempfile import zlib @@ -496,3 +497,14 @@ def test_adverserial_header(): gzip_file.read() finally: os.remove(tmpfile.name) + + +@pytest.mark.skipif(sys.platform != "linux", reason="bash required") +def test_no_broken_pipe(): + subprocess.run( + [ + "bash", "-c", "-e", "-o", "pipefail", + f"{sys.executable} -m zlib_ng.gzip_ng -cd {TEST_FILE} | head -n 10" + ], + check=True + ) diff --git a/tox.ini b/tox.ini index dfefd90..1eb5fe9 100644 --- a/tox.ini +++ b/tox.ini @@ -21,6 +21,8 @@ commands = coverage xml -i [testenv:asan] +# Use debug python as default python gives address sanitization errors. +basepython=python3d setenv= PYTHONDEVMODE=1 PYTHONUNBUFFERED=1