From 4042d45bc40ceac5c97e5281ba5d0a446a2c2167 Mon Sep 17 00:00:00 2001 From: Miles Granger Date: Sun, 16 May 2021 21:00:34 +0200 Subject: [PATCH] Allow use without numpy (#57) Conversion of input is evaluated in order of variants declared in `BytesType`. If numpy is not installed, it would error that numpy was not installed if the input was a Buffer b/c Buffer was evaluated after a potential numpy input. The patch fixes this behavior by moving the numpy variant as the last variant evaluated. Thus wiill only raise an error if it's a numpy input/output and numpy is not installed. --- .github/workflows/CI.yml | 5 +++++ Cargo.toml | 2 +- src/lib.rs | 6 +++--- tests/test_no_numpy.py | 23 +++++++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 tests/test_no_numpy.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 5427405a..e758f6d6 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -138,6 +138,11 @@ jobs: pip install cramjam --no-index --find-links dist --force-reinstall pip install -r dev-requirements.txt make test + - name: Test no numpy installed works + if: matrix.target == 'x86_64-unknown-linux-gnu' + run: | + pip uninstall numpy -y + python -m pytest tests/test_no_numpy.py - name: Upload wheels uses: actions/upload-artifact@v2 with: diff --git a/Cargo.toml b/Cargo.toml index 8a383973..c27901c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cramjam" -version = "2.3.0" +version = "2.3.1" authors = ["Miles Granger "] edition = "2018" license-file = "LICENSE" diff --git a/src/lib.rs b/src/lib.rs index 1304cc79..6e74c69f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,15 +81,15 @@ pub enum BytesType<'a> { /// `bytearray` #[pyo3(transparent, annotation = "bytearray")] ByteArray(RustyPyByteArray<'a>), - /// `numpy.array` with `dtype=np.uint8` - #[pyo3(transparent, annotation = "numpy")] - NumpyArray(RustyNumpyArray<'a>), /// [`cramjam.File`](io/struct.RustyFile.html) #[pyo3(transparent, annotation = "File")] RustyFile(&'a PyCell), /// [`cramjam.Buffer`](io/struct.RustyBuffer.html) #[pyo3(transparent, annotation = "Buffer")] RustyBuffer(&'a PyCell), + /// `numpy.array` with `dtype=np.uint8` + #[pyo3(transparent, annotation = "numpy")] + NumpyArray(RustyNumpyArray<'a>) } impl<'a> AsBytes for BytesType<'a> { diff --git a/tests/test_no_numpy.py b/tests/test_no_numpy.py new file mode 100644 index 00000000..82ed9e51 --- /dev/null +++ b/tests/test_no_numpy.py @@ -0,0 +1,23 @@ +import pytest +import cramjam + + +@pytest.mark.parametrize("obj", (bytes, bytearray, cramjam.Buffer, cramjam.File)) +@pytest.mark.parametrize( + "variant_str", ("snappy", "brotli", "lz4", "gzip", "deflate", "zstd") +) +def test_no_numpy_installed(tmpdir, obj, variant_str): + """ + These operations should work even when numpy is not installed + """ + if cramjam.File == obj: + data = obj(str(tmpdir.join("tmp.txt"))) + data.write(b"data") + data.seek(0) + else: + data = obj(b"data") + + variant = getattr(cramjam, variant_str) + compressed = variant.compress(data) + decompressed = variant.decompress(compressed) + assert decompressed.read() == b"data"