Skip to content

Commit

Permalink
Allow use without numpy (#57)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
milesgranger authored May 16, 2021
1 parent a15fda8 commit 4042d45
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cramjam"
version = "2.3.0"
version = "2.3.1"
authors = ["Miles Granger <miles59923@gmail.com>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RustyFile>),
/// [`cramjam.Buffer`](io/struct.RustyBuffer.html)
#[pyo3(transparent, annotation = "Buffer")]
RustyBuffer(&'a PyCell<RustyBuffer>),
/// `numpy.array` with `dtype=np.uint8`
#[pyo3(transparent, annotation = "numpy")]
NumpyArray(RustyNumpyArray<'a>)
}

impl<'a> AsBytes for BytesType<'a> {
Expand Down
23 changes: 23 additions & 0 deletions tests/test_no_numpy.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 4042d45

Please sign in to comment.