-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
__version__ = "0.0.0" | ||
|
||
import sys | ||
import zlib as zlib_original | ||
|
||
from . import zlib_adapter as best_zlib | ||
|
||
|
||
def enable() -> None: | ||
"""Enable the adapter.""" | ||
sys.modules["zlib"] = best_zlib | ||
|
||
|
||
def disable() -> None: | ||
"""Disable the adapter restore the original zlib.""" | ||
sys.modules["zlib"] = zlib_original |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from typing import Any, Optional | ||
|
||
from isal import isal_zlib | ||
from isal.isal_zlib import ( | ||
DEF_BUF_SIZE, | ||
DEF_MEM_LEVEL, | ||
DEFLATED, | ||
MAX_WBITS, | ||
Z_BEST_COMPRESSION, | ||
Z_BEST_SPEED, | ||
Z_DEFAULT_COMPRESSION, | ||
Z_DEFAULT_STRATEGY, | ||
Z_FILTERED, | ||
Z_FINISH, | ||
Z_FIXED, | ||
Z_FULL_FLUSH, | ||
Z_HUFFMAN_ONLY, | ||
Z_NO_FLUSH, | ||
Z_RLE, | ||
Z_SYNC_FLUSH, | ||
Compress, | ||
Decompress, | ||
adler32, | ||
compress, | ||
crc32, | ||
crc32_combine, | ||
decompress, | ||
decompressobj, | ||
error, | ||
) | ||
|
||
|
||
def compressobj( | ||
level: int = isal_zlib.Z_DEFAULT_COMPRESSION, | ||
method: int = isal_zlib.DEFLATED, | ||
wbits: int = isal_zlib.MAX_WBITS, | ||
memLevel: int = isal_zlib.DEF_MEM_LEVEL, | ||
strategy: int = isal_zlib.Z_DEFAULT_STRATEGY, | ||
zdict: Optional[Any] = None, | ||
) -> isal_zlib.Compress: | ||
"""Compressobj adapter to convert zlib level to isal compression level.""" | ||
if level < 0 or level > 9: | ||
raise ValueError(f"Invalid compression level: {level}") | ||
|
||
if level <= 3: | ||
level = isal_zlib.Z_BEST_SPEED | ||
elif level <= 6: | ||
level = isal_zlib.Z_DEFAULT_COMPRESSION | ||
else: | ||
level = isal_zlib.Z_BEST_COMPRESSION | ||
|
||
if zdict is not None: | ||
return isal_zlib.compressobj( | ||
level, | ||
method, | ||
wbits, | ||
memLevel, | ||
strategy, | ||
zdict, | ||
) | ||
|
||
return isal_zlib.compressobj( | ||
level, | ||
method, | ||
wbits, | ||
memLevel, | ||
strategy, | ||
) | ||
|
||
|
||
__all__ = ( | ||
"DEF_BUF_SIZE", | ||
"DEF_MEM_LEVEL", | ||
"DEFLATED", | ||
"MAX_WBITS", | ||
"Z_BEST_COMPRESSION", | ||
"Z_BEST_SPEED", | ||
"Z_DEFAULT_COMPRESSION", | ||
"Z_DEFAULT_STRATEGY", | ||
"Z_FILTERED", | ||
"Z_FINISH", | ||
"Z_FIXED", | ||
"Z_FULL_FLUSH", | ||
"Z_HUFFMAN_ONLY", | ||
"Z_NO_FLUSH", | ||
"Z_RLE", | ||
"Z_SYNC_FLUSH", | ||
"Compress", | ||
"Decompress", | ||
"adler32", | ||
"compress", | ||
"crc32", | ||
"crc32_combine", | ||
"decompress", | ||
"decompressobj", | ||
"error", | ||
"compressobj", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import zlib as zlib_original | ||
|
||
from zlib_fast import disable, enable | ||
from zlib_fast import zlib_adapter as best_zlib | ||
|
||
|
||
def test_enable_disable(): | ||
"""Test enable/disable.""" | ||
import zlib | ||
|
||
assert zlib is zlib_original | ||
enable() | ||
import zlib | ||
|
||
assert zlib is best_zlib | ||
disable() | ||
import zlib | ||
|
||
assert zlib is zlib_original |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
|
||
from zlib_fast import zlib_adapter | ||
|
||
|
||
def test_compressobj(): | ||
assert zlib_adapter.compressobj() is not None | ||
assert zlib_adapter.compressobj(zdict=b"") is not None | ||
with pytest.raises(ValueError, match="Invalid compression level: -1"): | ||
zlib_adapter.compressobj(level=-1) | ||
for level in range(10): | ||
zlib_adapter.compressobj(level=level) | ||
|
||
|
||
def test_decompressobj(): | ||
assert zlib_adapter.decompressobj() is not None |