diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..be006de9 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,13 @@ +# Keep GitHub Actions up to date with GitHub's Dependabot... +# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot +# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + groups: + github-actions: + patterns: + - "*" # Group all Actions updates into a single larger pull request + schedule: + interval: weekly diff --git a/.github/workflows/build_dist.yml b/.github/workflows/build_dist.yml index 1e815c18..d33b3520 100644 --- a/.github/workflows/build_dist.yml +++ b/.github/workflows/build_dist.yml @@ -19,19 +19,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Check out repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 with: - fetch-depth: 0 # To ensure tags are retrieved to enabe setuptools_scm to work + fetch-depth: 0 # To ensure tags are retrieved to enable setuptools_scm to work - name: Install Python 3.x - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: 3.x - - name: Install setuptools - run: pip install setuptools - name: Build sdist - run: python setup.py sdist + run: pipx run build --sdist - name: Save sdist - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: name: cibw-sdist.tar.gz path: dist/*.tar.gz @@ -47,17 +45,17 @@ jobs: matrix: os: - ubuntu-latest - - macos-13 # x86 + - macos-15-intel # x86 - macos-latest # arm - windows-latest - cibw_build: [cp39-*, cp310-*, cp311-*, cp312-*, cp313-*] + cibw_build: [cp39-*, cp310-*, cp311-*, cp312-*, cp313-*, cp313t-*, cp314-*, cp314t-*] steps: - name: Check out repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 with: - fetch-depth: 0 # To ensure tags are retrieved to enabe setuptools_scm to work + fetch-depth: 0 # To ensure tags are retrieved to enable setuptools_scm to work - name: Install Python 3.x - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: 3.x - name: Set up QEMU # Needed to build aarch64 wheels @@ -65,34 +63,89 @@ jobs: uses: docker/setup-qemu-action@v3 with: platforms: all + + - name: Setup free-threading variables + if: ${{ endsWith(matrix.cibw_build, 't-*') }} + shell: bash -l {0} + run: | + echo "CIBW_BEFORE_TEST=pip install pytest pytest-run-parallel" >> "$GITHUB_ENV" + echo "CIBW_ENVIRONMENT=PYLZ4_USE_SYSTEM_LZ4=False PYTEST_ADDOPTS=--parallel-threads=4" >> "$GITHUB_ENV" + echo "CIBW_TEST_COMMAND=tox -x testenv.deps+=pytest-run-parallel -x testenv.pass_env+=PYTEST_ADDOPTS -c {project}" >> "$GITHUB_ENV" + - name: Setup environment + if: ${{ !endsWith(matrix.cibw_build, 't-*') }} + shell: bash -l {0} + run: | + echo "CIBW_ENVIRONMENT=PYLZ4_USE_SYSTEM_LZ4=False" >> "$GITHUB_ENV" + echo "CIBW_TEST_COMMAND=tox -c {project}" >> "$GITHUB_ENV" - name: Build wheels - uses: pypa/cibuildwheel@v2.21 + uses: pypa/cibuildwheel@v3.3.0 env: - CIBW_ENVIRONMENT: PYLZ4_USE_SYSTEM_LZ4="False" - CIBW_ARCHS_LINUX: "x86_64 i686 aarch64" + # CIBW_ARCHS_LINUX: "x86_64 i686 aarch64" + CIBW_ARCHS_LINUX: "x86_64 i686" CIBW_ARCHS_MACOS: "auto64" # since we have both runner arches - CIBW_ARCHS_WINDOWS: "AMD64 x86" + CIBW_ARCHS_WINDOWS: "AMD64 x86 ARM64" + CIBW_ENABLE: cpython-freethreading + CIBW_BUILD: ${{ matrix.cibw_build }} + CIBW_SKIP: "cp*-musllinux*" + CIBW_TEST_SKIP: "*-macosx_arm64 *-macosx_universal2:arm64 *-*linux_{ppc64le,s390x} *-win_arm64" + CIBW_BEFORE_BUILD: "python -m pip install -U pip && python -m pip install tox" + - name: Save wheels + uses: actions/upload-artifact@v5 + with: + name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }} + path: ./wheelhouse/*.whl + + wheels_linux_arm: + name: Build wheels on ${{ matrix.os }} CIBW_BUILD=${{ matrix.cibw_build }} + runs-on: ${{ matrix.os }} + strategy: + # since multiple builds run at the same time, cancelling them all when one + # fails is wasteful and forces handling build problems one by one instead + # of showing a "full picture" + fail-fast: false + matrix: + os: + - ubuntu-24.04-arm + cibw_build: [cp39-*, cp310-*, cp311-*, cp312-*, cp313-*, cp313t-*, cp314-*, cp314t-*] + steps: + - name: Check out repository + uses: actions/checkout@v6 + with: + fetch-depth: 0 # To ensure tags are retrieved to enable setuptools_scm to work + - name: Install Python 3.x + uses: actions/setup-python@v6 + with: + python-version: 3.x + - name: Setup environment + shell: bash -l {0} + run: | + echo "CIBW_ENVIRONMENT=PYLZ4_USE_SYSTEM_LZ4=False" >> "$GITHUB_ENV" + echo "CIBW_TEST_COMMAND=tox -c {project}" >> "$GITHUB_ENV" + - name: Build wheels + uses: pypa/cibuildwheel@v3.3.0 + env: + CIBW_ARCHS_LINUX: "aarch64" CIBW_BUILD: ${{ matrix.cibw_build }} CIBW_SKIP: "cp*-musllinux*" - CIBW_TEST_COMMAND: "tox -c {project}" - CIBW_TEST_SKIP: "*-macosx_arm64 *-macosx_universal2:arm64 *-*linux_{aarch64,ppc64le,s390x}" + CIBW_ENABLE: cpython-freethreading CIBW_BEFORE_BUILD: "python -m pip install -U pip && python -m pip install tox" - name: Save wheels - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: - name: cibw-wheels-${{ strategy.job-index }}.whl - path: wheelhouse/*.whl + name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }} + path: ./wheelhouse/*.whl upload_pypi: name: Upload to PyPI - needs: [sdist, wheels] + needs: [sdist, wheels, wheels_linux_arm] runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/') steps: - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@v6 with: pattern: cibw-* path: dist + merge-multiple: true - uses: pypa/gh-action-pypi-publish@release/v1 with: password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/lz4/_version.c b/lz4/_version.c index c611f0b3..af606abe 100644 --- a/lz4/_version.c +++ b/lz4/_version.c @@ -113,5 +113,9 @@ PyInit__version(void) if (module == NULL) return NULL; + #ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED); + #endif + return module; } diff --git a/lz4/block/_block.c b/lz4/block/_block.c index 3e904a03..993cc44c 100644 --- a/lz4/block/_block.c +++ b/lz4/block/_block.c @@ -518,5 +518,9 @@ PyInit__block(void) Py_INCREF(LZ4BlockError); PyModule_AddObject(module, "LZ4BlockError", LZ4BlockError); + #ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED); + #endif + return module; } diff --git a/lz4/frame/__init__.py b/lz4/frame/__init__.py index 00f3e64a..2a82cd08 100644 --- a/lz4/frame/__init__.py +++ b/lz4/frame/__init__.py @@ -25,9 +25,9 @@ __doc__ = _doc try: - import _compression # Python 3.6 and later + import compression._common._streams as _compression # Python 3.14 except ImportError: - from . import _compression + import _compression # Python 3.9 - 3.13 BLOCKSIZE_DEFAULT = _BLOCKSIZE_DEFAULT @@ -268,7 +268,7 @@ def flush(self): This returns a ``bytes`` or ``bytearray`` object containing any data stored in the compressor's internal buffers and a frame footer. - The LZ4FrameCompressor instance may be re-used after this method has + The LZ4FrameCompressor instance may be reused after this method has been called to create a new frame of compressed data. Returns: @@ -287,7 +287,7 @@ def flush(self): def reset(self): """Reset the `LZ4FrameCompressor` instance. - This allows the `LZ4FrameCompression` instance to be re-used after an + This allows the `LZ4FrameCompression` instance to be reused after an error. """ @@ -360,7 +360,7 @@ def __exit__(self, exception_type, exception, traceback): def reset(self): """Reset the decompressor state. - This is useful after an error occurs, allowing re-use of the instance. + This is useful after an error occurs, allowing reuse of the instance. """ reset_decompression_context(self._context) diff --git a/lz4/frame/_frame.c b/lz4/frame/_frame.c index 34606653..440b0b50 100644 --- a/lz4/frame/_frame.c +++ b/lz4/frame/_frame.c @@ -1330,7 +1330,7 @@ PyDoc_STRVAR( ); #define COMPRESS_KWARGS_DOCSTRING \ - " block_size (int): Sepcifies the maximum blocksize to use.\n" \ + " block_size (int): Specifies the maximum blocksize to use.\n" \ " Options:\n\n" \ " - `lz4.frame.BLOCKSIZE_DEFAULT`: the lz4 library default\n" \ " - `lz4.frame.BLOCKSIZE_MAX64KB`: 64 kB\n" \ @@ -1466,7 +1466,7 @@ PyDoc_STRVAR "data will also be included in the returned data.\n" \ "\n" \ "If the ``end_frame`` argument is ``True``, the compression context will be\n" \ - "reset and can be re-used.\n" \ + "reset and can be reused.\n" \ "\n" \ "Args:\n" \ " context (cCtx): Compression context\n" \ @@ -1677,5 +1677,9 @@ PyInit__frame(void) PyModule_AddIntConstant (module, "BLOCKSIZE_MAX1MB", LZ4F_max1MB); PyModule_AddIntConstant (module, "BLOCKSIZE_MAX4MB", LZ4F_max4MB); + #ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED); + #endif + return module; } diff --git a/lz4/stream/__init__.py b/lz4/stream/__init__.py index 4b007464..58ce298a 100644 --- a/lz4/stream/__init__.py +++ b/lz4/stream/__init__.py @@ -32,7 +32,7 @@ def __init__(self, strategy, buffer_size, return_bytearray=False, store_comp_siz perform decompression using this initial dictionary. Raises: - Exceptions occuring during the context initialization. + Exceptions occurring during the context initialization. OverflowError: raised if the ``dictionary`` parameter is too large for the LZ4 context. @@ -73,7 +73,7 @@ def decompress(self, chunk): bytes or bytearray: Decompressed data. Raises: - Exceptions occuring during decompression. + Exceptions occurring during decompression. ValueError: raised if the source is inconsistent with a finite LZ4 stream block chain. @@ -96,7 +96,7 @@ def get_block(self, stream): bytes or bytearray: LZ4 compressed data block. Raises: - Exceptions occuring while getting the first block from ``stream``. + Exceptions occurring while getting the first block from ``stream``. BufferError: raised if the function cannot return a complete LZ4 compressed block from the stream (i.e. the stream does not hold @@ -150,7 +150,7 @@ def __init__(self, strategy, buffer_size, mode="default", acceleration=True, com perform compression using this initial dictionary. Raises: - Exceptions occuring during the context initialization. + Exceptions occurring during the context initialization. OverflowError: raised if the ``dictionary`` parameter is too large for the LZ4 context. @@ -194,7 +194,7 @@ def compress(self, chunk): bytes or bytearray: Compressed data. Raises: - Exceptions occuring during compression. + Exceptions occurring during compression. OverflowError: raised if the source is too large for being compressed in the given context. diff --git a/lz4/stream/_stream.c b/lz4/stream/_stream.c index 522fdeda..4c51d89d 100644 --- a/lz4/stream/_stream.c +++ b/lz4/stream/_stream.c @@ -931,7 +931,7 @@ _create_context (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwds) context->output.len = buffer_size; total_size = context->output.len; - /* Here we cannot assert the maximal theorical decompressed chunk length + /* Here we cannot assert the maximal theoretical decompressed chunk length * will fit in one page of the double_buffer, i.e.: * assert( !(double_buffer.page_size < _LZ4_inputBound(store_max_size)) ) * @@ -1649,5 +1649,9 @@ PyInit__stream(void) Py_INCREF (LZ4StreamError); PyModule_AddObject (module, "LZ4StreamError", LZ4StreamError); + #ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED); + #endif + return module; } diff --git a/setup.py b/setup.py index 9bc91f01..eb9030fa 100644 --- a/setup.py +++ b/setup.py @@ -121,6 +121,8 @@ def pkgconfig_installed_check(lib, required_version, default): '-Wall', '-Wundef' ] + # strip debug symbols from libraries to reduce wheel size + extension_kwargs['extra_link_args'] = ['-s'] else: print('Unrecognized compiler: {0}'.format(compiler)) sys.exit(1) @@ -180,6 +182,7 @@ def pkgconfig_installed_check(lib, required_version, default): long_description=open('README.rst', 'r').read(), author='Jonathan Underwood', author_email='jonathan.underwood@gmail.com', + license='BSD-3-Clause', url='https://github.com/python-lz4/python-lz4', packages=packages, ext_modules=ext_modules, @@ -196,7 +199,6 @@ def pkgconfig_installed_check(lib, required_version, default): }, classifiers=[ 'Development Status :: 5 - Production/Stable', - 'License :: OSI Approved :: BSD License', 'Intended Audience :: Developers', 'Programming Language :: C', 'Programming Language :: Python', @@ -205,5 +207,7 @@ def pkgconfig_installed_check(lib, required_version, default): 'Programming Language :: Python :: 3.11', 'Programming Language :: Python :: 3.12', 'Programming Language :: Python :: 3.13', + 'Programming Language :: Python :: 3.14', + 'Programming Language :: Python :: Free Threading', ], ) diff --git a/tests/block/test_block_0.py b/tests/block/test_block_0.py index 8fc0f488..f39da18a 100644 --- a/tests/block/test_block_0.py +++ b/tests/block/test_block_0.py @@ -1,6 +1,8 @@ import lz4.block from multiprocessing.pool import ThreadPool import sys +import copy +import pytest from functools import partial if sys.version_info <= (3, 2): import struct @@ -68,6 +70,13 @@ def setup_kwargs(mode, store_size, c_return_bytearray=None, d_return_bytearray=N # Test single threaded usage with all valid variations of input def test_1(data, mode, store_size, c_return_bytearray, d_return_bytearray, dictionary): + if isinstance(data, memoryview): + data = memoryview(copy.deepcopy(data.obj)) + elif isinstance(data, bytearray): + data_x = bytearray() + data_x[:] = data + data = data_x + (c_kwargs, d_kwargs) = setup_kwargs( mode, store_size, c_return_bytearray, d_return_bytearray) @@ -79,10 +88,24 @@ def test_1(data, mode, store_size, c_return_bytearray, d_return_bytearray, dicti # Test multi threaded usage with all valid variations of input +@pytest.mark.thread_unsafe( + reason=("Test is already explicitly multithreaded, should " + "not be run in a thread pool.") +) def test_2(data, mode, store_size, dictionary): (c_kwargs, d_kwargs) = setup_kwargs(mode, store_size) - data_in = [data for i in range(32)] + def copy_buf(data): + if isinstance(data, memoryview): + data_x = memoryview(copy.deepcopy(data.obj)) + elif isinstance(data, bytearray): + data_x = bytearray() + data_x[:] = data + else: + data_x = data + return data_x + + data_in = [copy_buf(data) for i in range(32)] pool = ThreadPool(2) rt = partial(roundtrip, c_kwargs=c_kwargs, diff --git a/tests/block/test_block_2.py b/tests/block/test_block_2.py index a2aea4da..a75e92f0 100644 --- a/tests/block/test_block_2.py +++ b/tests/block/test_block_2.py @@ -19,6 +19,10 @@ # fragile. +@pytest.mark.thread_unsafe( + reason=("Large multithreaded allocations will likely exhaust " + "system memory.") +) @pytest.mark.skipif( os.environ.get('TRAVIS') is not None, reason='Skipping test on Travis due to insufficient memory' diff --git a/tests/block/test_block_3.py b/tests/block/test_block_3.py index 3fcb175b..86bd9933 100644 --- a/tests/block/test_block_3.py +++ b/tests/block/test_block_3.py @@ -18,6 +18,11 @@ def data(request): return request.param +# coverage might allocate and cause this test to fail +@pytest.mark.no_cover +@pytest.mark.thread_unsafe( + reason="tracemalloc captures global snapshots" +) def test_block_decompress_mem_usage(data): tracemalloc = pytest.importorskip('tracemalloc') diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..0fefd8e0 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,13 @@ +try: + import pytest_run_parallel # noqa: F401 + PARALLEL_RUN_AVALIABLE = True +except ModuleNotFoundError: + PARALLEL_RUN_AVALIABLE = False + + +def pytest_configure(config): + if not PARALLEL_RUN_AVALIABLE: + config.addinivalue_line( + "markers", + "thread_unsafe: mark the test function as single-threaded", + ) diff --git a/tests/frame/test_frame_2.py b/tests/frame/test_frame_2.py index 80b44b87..230867e6 100644 --- a/tests/frame/test_frame_2.py +++ b/tests/frame/test_frame_2.py @@ -1,6 +1,7 @@ import lz4.frame as lz4frame import pytest import os +import copy import sys from . helpers import ( get_chunked, @@ -41,6 +42,13 @@ def test_roundtrip_chunked(data, block_size, block_linked, data, c_chunks, d_chunks = data + if isinstance(data, memoryview): + data = memoryview(copy.deepcopy(data.obj)) + elif isinstance(data, bytearray): + data_2 = bytearray() + data_2[:] = data + data = data_2 + c_context = lz4frame.create_compression_context() kwargs = {} diff --git a/tests/frame/test_frame_5.py b/tests/frame/test_frame_5.py index 05daf283..29e7765a 100644 --- a/tests/frame/test_frame_5.py +++ b/tests/frame/test_frame_5.py @@ -19,6 +19,11 @@ def data(request): return request.param +# coverage might allocate and cause this test to fail +@pytest.mark.no_cover +@pytest.mark.thread_unsafe( + reason="tracemalloc captures global snapshots" +) def test_frame_decompress_mem_usage(data): tracemalloc = pytest.importorskip('tracemalloc') @@ -41,6 +46,11 @@ def test_frame_decompress_mem_usage(data): prev_snapshot = snapshot +# coverage might allocate and cause this test to fail +@pytest.mark.no_cover +@pytest.mark.thread_unsafe( + reason="tracemalloc captures global snapshots" +) def test_frame_decompress_chunk_mem_usage(data): tracemalloc = pytest.importorskip('tracemalloc') tracemalloc.start() @@ -66,6 +76,11 @@ def test_frame_decompress_chunk_mem_usage(data): prev_snapshot = snapshot +# coverage might allocate and cause this test to fail +@pytest.mark.no_cover +@pytest.mark.thread_unsafe( + reason="tracemalloc captures global snapshots" +) def test_frame_open_decompress_mem_usage(data): tracemalloc = pytest.importorskip('tracemalloc') tracemalloc.start() diff --git a/tests/frame/test_frame_6.py b/tests/frame/test_frame_6.py index c20a4f31..4f4185ee 100644 --- a/tests/frame/test_frame_6.py +++ b/tests/frame/test_frame_6.py @@ -1,5 +1,6 @@ import os import pytest +import threading import lz4.frame as lz4frame test_data = [ @@ -33,40 +34,45 @@ def compression_level(request): return request.param -def test_lz4frame_open_write(data): - with lz4frame.open('testfile', mode='wb') as fp: +def test_lz4frame_open_write(tmp_path, data): + thread_id = threading.get_native_id() + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wb') as fp: fp.write(data) -def test_lz4frame_open_write_read_defaults(data): - with lz4frame.open('testfile', mode='wb') as fp: +def test_lz4frame_open_write_read_defaults(tmp_path, data): + thread_id = threading.get_native_id() + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wb') as fp: fp.write(data) - with lz4frame.open('testfile', mode='r') as fp: + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='r') as fp: data_out = fp.read() assert data_out == data -def test_lz4frame_open_write_read_text(): +def test_lz4frame_open_write_read_text(tmp_path): data = u'This is a test string' - with lz4frame.open('testfile', mode='wt') as fp: + thread_id = threading.get_native_id() + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wt') as fp: fp.write(data) - with lz4frame.open('testfile', mode='rt') as fp: + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='rt') as fp: data_out = fp.read() assert data_out == data -def test_lz4frame_open_write_read_text_iter(): +def test_lz4frame_open_write_read_text_iter(tmp_path): data = u'This is a test string' - with lz4frame.open('testfile', mode='wt') as fp: + thread_id = threading.get_native_id() + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wt') as fp: fp.write(data) data_out = '' - with lz4frame.open('testfile', mode='rt') as fp: + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='rt') as fp: for line in fp: data_out += line assert data_out == data def test_lz4frame_open_write_read( + tmp_path, data, compression_level, block_linked, @@ -91,29 +97,31 @@ def test_lz4frame_open_write_read( kwargs['return_bytearray'] = return_bytearray kwargs['mode'] = 'wb' - with lz4frame.open('testfile', **kwargs) as fp: + thread_id = threading.get_native_id() + with lz4frame.open(tmp_path / f'testfile_{thread_id}', **kwargs) as fp: fp.write(data) - with lz4frame.open('testfile', mode='r') as fp: + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='r') as fp: data_out = fp.read() assert data_out == data -def test_lz4frame_flush(): +def test_lz4frame_flush(tmp_path): data_1 = b"This is a..." data_2 = b" test string!" + thread_id = threading.get_native_id() - with lz4frame.open("testfile", mode="w") as fp_write: + with lz4frame.open(tmp_path / f"testfile_{thread_id}", mode="w") as fp_write: fp_write.write(data_1) fp_write.flush() fp_write.write(data_2) - with lz4frame.open("testfile", mode="r") as fp_read: + with lz4frame.open(tmp_path / f"testfile_{thread_id}", mode="r") as fp_read: assert fp_read.read() == data_1 fp_write.flush() - with lz4frame.open("testfile", mode="r") as fp_read: + with lz4frame.open(tmp_path / f"testfile_{thread_id}", mode="r") as fp_read: assert fp_read.read() == data_1 + data_2 diff --git a/tests/frame/test_frame_8.py b/tests/frame/test_frame_8.py index 159534ae..cfaeaace 100644 --- a/tests/frame/test_frame_8.py +++ b/tests/frame/test_frame_8.py @@ -1,12 +1,14 @@ +import threading import lz4.frame as lz4frame -def test_lz4frame_open_write_read_text_iter(): +def test_lz4frame_open_write_read_text_iter(tmp_path): data = u'This is a test string' - with lz4frame.open('testfile', mode='wt') as fp: + thread_id = threading.get_native_id() + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='wt') as fp: fp.write(data) data_out = '' - with lz4frame.open('testfile', mode='rt') as fp: + with lz4frame.open(tmp_path / f'testfile_{thread_id}', mode='rt') as fp: for line in fp: data_out += line assert data_out == data diff --git a/tests/frame/test_frame_9.py b/tests/frame/test_frame_9.py index 51433934..6f7fc0db 100644 --- a/tests/frame/test_frame_9.py +++ b/tests/frame/test_frame_9.py @@ -3,11 +3,12 @@ import io import pickle import sys +import threading import lz4.frame import pytest -def test_issue_172_1(): +def test_issue_172_1(tmp_path): """Test reproducer for issue 172 Issue 172 is a reported failure occurring on Windows 10 only. This bug was @@ -16,34 +17,38 @@ def test_issue_172_1(): """ input_data = 8 * os.urandom(1024) - with lz4.frame.open('testfile_small', 'wb') as fp: + thread_id = threading.get_native_id() + + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'wb') as fp: bytes_written = fp.write(input_data) # noqa: F841 - with lz4.frame.open('testfile_small', 'rb') as fp: + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'rb') as fp: data = fp.read(10) assert len(data) == 10 -def test_issue_172_2(): +def test_issue_172_2(tmp_path): input_data = 9 * os.urandom(1024) - with lz4.frame.open('testfile_small', 'w') as fp: + thread_id = threading.get_native_id() + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'w') as fp: bytes_written = fp.write(input_data) # noqa: F841 - with lz4.frame.open('testfile_small', 'r') as fp: + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'r') as fp: data = fp.read(10) assert len(data) == 10 -def test_issue_172_3(): +def test_issue_172_3(tmp_path): input_data = 9 * os.urandom(1024) - with lz4.frame.open('testfile_small', 'wb') as fp: + thread_id = threading.get_native_id() + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'wb') as fp: bytes_written = fp.write(input_data) # noqa: F841 - with lz4.frame.open('testfile_small', 'rb') as fp: + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'rb') as fp: data = fp.read(10) assert len(data) == 10 - with lz4.frame.open('testfile_small', 'rb') as fp: + with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'rb') as fp: data = fp.read(16 * 1024 - 1) assert len(data) == 9 * 1024 assert data == input_data @@ -60,7 +65,7 @@ def test_issue_227_1(): @pytest.mark.skipif( sys.version_info < (3, 8), - reason="PickleBuffer only availiable in Python 3.8 or greater" + reason="PickleBuffer only available in Python 3.8 or greater" ) def test_issue_227_2(): q = array.array('Q', [1, 2, 3, 4, 5]) diff --git a/tests/stream/test_stream_0.py b/tests/stream/test_stream_0.py index 03b19f3f..c7db76ee 100644 --- a/tests/stream/test_stream_0.py +++ b/tests/stream/test_stream_0.py @@ -96,6 +96,10 @@ def setup_kwargs(strategy, mode, buffer_size, store_comp_size, # Test single threaded usage with all valid variations of input +@pytest.mark.thread_unsafe( + reason=("test modifies fixtures that would be shared, see " + "https://github.com/Quansight-Labs/pytest-run-parallel/issues/14") +) def test_1(data, strategy, mode, buffer_size, store_comp_size, c_return_bytearray, d_return_bytearray, dictionary): if buffer_size >= (1 << (8 * store_comp_size['store_comp_size'])): @@ -111,6 +115,6 @@ def test_1(data, strategy, mode, buffer_size, store_comp_size, # Test multi threaded: # Not relevant in the lz4.stream case (the process is highly sequential, -# and re-use/share the same context from one input chunk to the next one). +# and reuse/share the same context from one input chunk to the next one). def test_2(data, strategy, mode, buffer_size, store_comp_size, dictionary): # noqa pass diff --git a/tests/stream/test_stream_1.py b/tests/stream/test_stream_1.py index 6b49267e..481de8aa 100644 --- a/tests/stream/test_stream_1.py +++ b/tests/stream/test_stream_1.py @@ -136,7 +136,7 @@ def test_invalid_config_c_4(store_comp_size): c_kwargs.update(store_comp_size) if store_comp_size['store_comp_size'] >= 4: - # No need for skiping this test case, since arguments check is + # No need for skipping this test case, since arguments check is # expecting to raise an error. # Make sure the page size is larger than what the input bound will be, @@ -169,7 +169,7 @@ def test_invalid_config_d_4(store_comp_size): # but still fit in 4 bytes d_kwargs['buffer_size'] -= 1 - # No failure expected during instanciation/initialization + # No failure expected during instantiation/initialization lz4.stream.LZ4StreamDecompressor(**d_kwargs) @@ -199,7 +199,7 @@ def test_invalid_config_d_5(): d_kwargs = {} d_kwargs['strategy'] = "double_buffer" - # No failure expected during instanciation/initialization + # No failure expected during instantiation/initialization d_kwargs['buffer_size'] = lz4.stream.LZ4_MAX_INPUT_SIZE if sys.maxsize < 0xffffffff: @@ -207,7 +207,7 @@ def test_invalid_config_d_5(): lz4.stream.LZ4StreamDecompressor(**d_kwargs) - # No failure expected during instanciation/initialization + # No failure expected during instantiation/initialization d_kwargs['buffer_size'] = lz4.stream.LZ4_MAX_INPUT_SIZE + 1 if sys.maxsize < 0xffffffff: @@ -215,7 +215,7 @@ def test_invalid_config_d_5(): lz4.stream.LZ4StreamDecompressor(**d_kwargs) - # No failure expected during instanciation/initialization + # No failure expected during instantiation/initialization d_kwargs['buffer_size'] = _4GB - 1 # 4GB - 1 (to fit in 4 bytes) if sys.maxsize < 0xffffffff: diff --git a/tests/stream/test_stream_3.py b/tests/stream/test_stream_3.py index 2b52d6b5..a18ed561 100644 --- a/tests/stream/test_stream_3.py +++ b/tests/stream/test_stream_3.py @@ -71,6 +71,11 @@ def data(request): return request.param +# coverage might allocate and cause this test to fail +@pytest.mark.no_cover +@pytest.mark.thread_unsafe( + reason="tracemalloc captures global snapshots" +) def test_block_decompress_mem_usage(data, buffer_size): kwargs = { 'strategy': "double_buffer",