test_frame_9.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import array
  2. import os
  3. import io
  4. import pickle
  5. import sys
  6. import lz4.frame
  7. import pytest
  8. def test_issue_172_1():
  9. """Test reproducer for issue 172
  10. Issue 172 is a reported failure occurring on Windows 10 only. This bug was
  11. due to incorrect handling of Py_ssize_t types when doing comparisons and
  12. using them as a size when allocating memory.
  13. """
  14. input_data = 8 * os.urandom(1024)
  15. with lz4.frame.open('testfile_small', 'wb') as fp:
  16. bytes_written = fp.write(input_data) # noqa: F841
  17. with lz4.frame.open('testfile_small', 'rb') as fp:
  18. data = fp.read(10)
  19. assert len(data) == 10
  20. def test_issue_172_2():
  21. input_data = 9 * os.urandom(1024)
  22. with lz4.frame.open('testfile_small', 'w') as fp:
  23. bytes_written = fp.write(input_data) # noqa: F841
  24. with lz4.frame.open('testfile_small', 'r') as fp:
  25. data = fp.read(10)
  26. assert len(data) == 10
  27. def test_issue_172_3():
  28. input_data = 9 * os.urandom(1024)
  29. with lz4.frame.open('testfile_small', 'wb') as fp:
  30. bytes_written = fp.write(input_data) # noqa: F841
  31. with lz4.frame.open('testfile_small', 'rb') as fp:
  32. data = fp.read(10)
  33. assert len(data) == 10
  34. with lz4.frame.open('testfile_small', 'rb') as fp:
  35. data = fp.read(16 * 1024 - 1)
  36. assert len(data) == 9 * 1024
  37. assert data == input_data
  38. def test_issue_227_1():
  39. q = array.array('Q', [1, 2, 3, 4, 5])
  40. LENGTH = len(q) * q.itemsize
  41. with lz4.frame.open(io.BytesIO(), 'w') as f:
  42. assert f.write(q) == LENGTH
  43. assert f.tell() == LENGTH
  44. @pytest.mark.skipif(
  45. sys.version_info < (3, 8),
  46. reason="PickleBuffer only availiable in Python 3.8 or greater"
  47. )
  48. def test_issue_227_2():
  49. q = array.array('Q', [1, 2, 3, 4, 5])
  50. c = lz4.frame.compress(q)
  51. d = lz4.frame.LZ4FrameDecompressor().decompress(pickle.PickleBuffer(c))
  52. assert memoryview(q).tobytes() == d