| 1 |
import array |
| 2 |
import os |
| 3 |
import io |
| 4 |
import pickle |
| 5 |
import sys |
| 6 |
import threading |
| 7 |
import lz4.frame |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
def test_issue_172_1(tmp_path): |
| 12 |
"""Test reproducer for issue 172 |
| 13 |
|
| 14 |
Issue 172 is a reported failure occurring on Windows 10 only. This bug was |
| 15 |
due to incorrect handling of Py_ssize_t types when doing comparisons and |
| 16 |
using them as a size when allocating memory. |
| 17 |
|
| 18 |
""" |
| 19 |
input_data = 8 * os.urandom(1024) |
| 20 |
thread_id = threading.get_native_id() |
| 21 |
|
| 22 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'wb') as fp: |
| 23 |
bytes_written = fp.write(input_data) |
| 24 |
|
| 25 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'rb') as fp: |
| 26 |
data = fp.read(10) |
| 27 |
assert len(data) == 10 |
| 28 |
|
| 29 |
|
| 30 |
def test_issue_172_2(tmp_path): |
| 31 |
input_data = 9 * os.urandom(1024) |
| 32 |
thread_id = threading.get_native_id() |
| 33 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'w') as fp: |
| 34 |
bytes_written = fp.write(input_data) |
| 35 |
|
| 36 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'r') as fp: |
| 37 |
data = fp.read(10) |
| 38 |
assert len(data) == 10 |
| 39 |
|
| 40 |
|
| 41 |
def test_issue_172_3(tmp_path): |
| 42 |
input_data = 9 * os.urandom(1024) |
| 43 |
thread_id = threading.get_native_id() |
| 44 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'wb') as fp: |
| 45 |
bytes_written = fp.write(input_data) |
| 46 |
|
| 47 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'rb') as fp: |
| 48 |
data = fp.read(10) |
| 49 |
assert len(data) == 10 |
| 50 |
|
| 51 |
with lz4.frame.open(tmp_path / f'testfile_small_{thread_id}', 'rb') as fp: |
| 52 |
data = fp.read(16 * 1024 - 1) |
| 53 |
assert len(data) == 9 * 1024 |
| 54 |
assert data == input_data |
| 55 |
|
| 56 |
|
| 57 |
def test_issue_227_1(): |
| 58 |
q = array.array('Q', [1, 2, 3, 4, 5]) |
| 59 |
LENGTH = len(q) * q.itemsize |
| 60 |
|
| 61 |
with lz4.frame.open(io.BytesIO(), 'w') as f: |
| 62 |
assert f.write(q) == LENGTH |
| 63 |
assert f.tell() == LENGTH |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.skipif( |
| 67 |
sys.version_info < (3, 8), |
| 68 |
reason="PickleBuffer only available in Python 3.8 or greater" |
| 69 |
) |
| 70 |
def test_issue_227_2(): |
| 71 |
q = array.array('Q', [1, 2, 3, 4, 5]) |
| 72 |
|
| 73 |
c = lz4.frame.compress(q) |
| 74 |
d = lz4.frame.LZ4FrameDecompressor().decompress(pickle.PickleBuffer(c)) |
| 75 |
|
| 76 |
assert memoryview(q).tobytes() == d |