Skip to content

Commit f73d256

Browse files
authored
Merge pull request #6 from rhpvorderman/tests
Add compliance tests
2 parents d521e03 + e4b7f16 commit f73d256

File tree

8 files changed

+2074
-198
lines changed

8 files changed

+2074
-198
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Changelog
99
1010
version 0.1.0-dev
1111
-----------------
12+
+ Add compliance tests from CPython to ensure isal_zlib and igzip are validated
13+
to the same standards as the zlib and gzip modules.
1214
+ Added a working gzip app using ``python -m isal.igzip``
1315
+ Add test suite that tests all possible settings for functions on the
1416
isal_zlib module.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from setuptools import Extension, find_packages, setup
2525

26-
EXTENSION_OPTS = dict(compiler_directives={})
26+
EXTENSION_OPTS = dict()
2727

2828
# Make sure conda prefix is loaded if installed in a conda environment.
2929
CONDA_PREFIX = os.environ.get("CONDA_PREFIX")

src/isal/igzip.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
import os
2929

3030
import _compression
31+
import sys
3132

3233
from . import isal_zlib
3334

34-
__all__ = ["IGzipFile", "open", "compress", "decompress"]
35+
__all__ = ["IGzipFile", "open", "compress", "decompress", "BadGzipFile"]
3536

3637
_COMPRESS_LEVEL_FAST = isal_zlib.ISAL_BEST_SPEED
3738
_COMPRESS_LEVEL_TRADEOFF = isal_zlib.ISAL_DEFAULT_COMPRESSION
@@ -40,6 +41,9 @@
4041

4142
BUFFER_SIZE = _compression.BUFFER_SIZE
4243

44+
class BadGzipFile(OSError):
45+
pass
46+
4347

4448
# The open method was copied from the python source with minor adjustments.
4549
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
@@ -225,7 +229,7 @@ def main():
225229
parser.description = (
226230
"A simple command line interface for the igzip module. "
227231
"Acts like igzip.")
228-
parser.add_argument("file")
232+
parser.add_argument("file", nargs="?")
229233
compress_group = parser.add_mutually_exclusive_group()
230234
compress_group.add_argument(
231235
"-0", "--fast", action="store_const", dest="compresslevel",
@@ -251,24 +255,37 @@ def main():
251255

252256
compresslevel = args.compresslevel or _COMPRESS_LEVEL_TRADEOFF
253257

254-
if args.compress:
255-
out_filename = args.file + ".gz"
256-
out_open = functools.partial(open, compresslevel=compresslevel)
257-
in_open = io.open
258+
if args.file is None:
259+
if args.compress:
260+
in_file = sys.stdin.buffer
261+
out_file = IGzipFile(mode="wb", compresslevel=compresslevel,
262+
fileobj=sys.stdout.buffer)
263+
else:
264+
in_file = IGzipFile(mode="rb", fileobj=sys.stdin.buffer)
265+
out_file = sys.stdout.buffer
258266
else:
259-
base, extension = os.path.splitext(args.file)
260-
if extension != ".gz":
261-
raise ValueError("Can only decompress files with a .gz extension")
262-
out_filename = base
263-
out_open = io.open
264-
in_open = open
265-
with in_open(args.file, "rb") as in_file:
266-
with out_open(out_filename, "wb") as out_file:
267-
while True:
268-
block = in_file.read(_BLOCK_SIZE)
269-
if block == b"":
270-
break
271-
out_file.write(block)
267+
if args.compress:
268+
in_file = io.open(args.file, mode="rb")
269+
out_file = open(args.file + ".gz", mode="wb",
270+
compresslevel=compresslevel)
271+
else:
272+
base, extension = os.path.splitext(args.file)
273+
if extension != ".gz":
274+
print(f"filename doesn't end in .gz: {args.file}")
275+
return
276+
in_file = open(args.file, "rb")
277+
out_file = io.open(base, "wb")
278+
try:
279+
while True:
280+
block = in_file.read(_BLOCK_SIZE)
281+
if block == b"":
282+
break
283+
out_file.write(block)
284+
finally:
285+
if in_file is not sys.stdin.buffer:
286+
in_file.close()
287+
if out_file is not sys.stdout.buffer:
288+
out_file.close()
272289

273290

274291
if __name__ == "__main__":

0 commit comments

Comments
 (0)