Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/pypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
run: python setup.py develop

- name: Pytest
run: pytest --verbose test_snappy.py
run: pytest --verbose test_snappy.py test_formats.py
6 changes: 3 additions & 3 deletions .github/workflows/wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
run: pip install dist/python_snappy*.tar.gz

- name: Pytest
run: pytest --verbose test_snappy.py
run: pytest --verbose test_snappy.py test_formats.py

- name: Archive sdist
uses: actions/upload-artifact@v4
Expand All @@ -51,7 +51,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-14, windows-2022]
os: [ubuntu-22.04, macos-15-intel, windows-2022]
architecture: ["x64"]
linux_archs: ["auto s390x"]
include:
Expand All @@ -70,7 +70,7 @@ jobs:
name: Python ${{ matrix.os }}
env:
CIBW_TEST_REQUIRES: pytest
CIBW_TEST_COMMAND: "python -m pytest --verbose {project}/test_snappy.py"
CIBW_TEST_COMMAND: "python -m pytest --verbose {project}/test_snappy.py {project}/test_formats.py"
CIBW_BEFORE_ALL_LINUX: yum install -y snappy-devel
MACOSX_DEPLOYMENT_TARGET: "10.9"
CIBW_REPAIR_WHEEL_COMMAND_WINDOWS: "delvewheel repair -w {dest_dir} {wheel}"
Expand Down
3 changes: 3 additions & 0 deletions src/snappy/snappy_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def guess_format_by_header(fin):
def get_decompress_function(specified_format, fin):
if specified_format == "auto":
format, decompress_func = guess_format_by_header(fin)
if format in ("hadoop", "raw"):
# These probes seek to EOF; the decoder needs the complete stream.
fin.seek(0)
return decompress_func
return _DECOMPRESS_METHODS[specified_format]

Expand Down
44 changes: 44 additions & 0 deletions test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,50 @@ class TestFormatRawAuto(TestFormatBase):
decompress_format = "auto"
success = True

class TestAutoDecompress(TestCase):
data = bytes(range(256))

def compressed(self, name):
stream = io.BytesIO()
formats.get_compress_function(name)(io.BytesIO(self.data), stream)
stream.seek(0)
return stream

def test_seekable_formats(self):
for name in ("hadoop", "raw"):
with self.subTest(format=name):
compressed = self.compressed(name)
decompress = formats.get_decompress_function("auto", compressed)
output = io.BytesIO()
decompress(compressed, output)
self.assertEqual(output.getvalue(), self.data)

def test_cli_seekable_formats(self):
with tempfile.TemporaryDirectory() as tmpdir:
source = os.path.join(tmpdir, "input.snappy")
target = os.path.join(tmpdir, "output.bin")
for name in ("hadoop", "raw"):
with self.subTest(format=name):
with open(source, "wb") as stream:
stream.write(self.compressed(name).getvalue())
result = subprocess.run(
[sys.executable, "-m", "snappy", "-d", source, target],
capture_output=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
with open(target, "rb") as stream:
self.assertEqual(stream.read(), self.data)

def test_cli_framed_stdin(self):
result = subprocess.run(
[sys.executable, "-m", "snappy", "-d"],
input=self.compressed("framing").getvalue(),
capture_output=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout, self.data)


class TestDecompressFunction(TestCase):
def test_auto_returns_function(self):
data = b"hello world"
Expand Down
Loading