Skip to content
Snippets Groups Projects
Commit 9b0e738a authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Use ValueError on insufficient space in output buffer

Following mmap.write(), and (more roughly) memoryview, ctypes buffers &
numpy.

Also don't automatically error for 0 size output buffer
parent 3f26e9a8
No related branches found
No related tags found
No related merge requests found
Pipeline #163021 passed
...@@ -72,11 +72,6 @@ compress_into(PyObject *module, PyObject *args, PyObject *kwargs) { ...@@ -72,11 +72,6 @@ compress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
return NULL; return NULL;
} }
if (output.len <= 0) {
PyErr_SetString(PyExc_ValueError, "Output buffer may not be 0 size");
goto done;
}
zst.opaque = NULL; zst.opaque = NULL;
zst.zalloc = PyZlib_Malloc; zst.zalloc = PyZlib_Malloc;
zst.zfree = PyZlib_Free; zst.zfree = PyZlib_Free;
...@@ -113,7 +108,7 @@ compress_into(PyObject *module, PyObject *args, PyObject *kwargs) { ...@@ -113,7 +108,7 @@ compress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
case Z_OK: case Z_OK:
case Z_BUF_ERROR: case Z_BUF_ERROR:
deflateEnd(&zst); deflateEnd(&zst);
PyErr_SetString(PyExc_BufferError, "Not enough space in output buffer"); PyErr_SetString(PyExc_ValueError, "Not enough space in output buffer");
goto done; goto done;
default: default:
deflateEnd(&zst); deflateEnd(&zst);
...@@ -150,11 +145,6 @@ decompress_into(PyObject *module, PyObject *args, PyObject *kwargs) { ...@@ -150,11 +145,6 @@ decompress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
return NULL; return NULL;
} }
if (output.len <= 0) {
PyErr_SetString(PyExc_ValueError, "Output buffer may not be 0 size");
goto done;
}
zst.opaque = NULL; zst.opaque = NULL;
zst.zalloc = PyZlib_Malloc; zst.zalloc = PyZlib_Malloc;
zst.zfree = PyZlib_Free; zst.zfree = PyZlib_Free;
...@@ -187,7 +177,7 @@ decompress_into(PyObject *module, PyObject *args, PyObject *kwargs) { ...@@ -187,7 +177,7 @@ decompress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
case Z_OK: case Z_OK:
case Z_BUF_ERROR: case Z_BUF_ERROR:
deflateEnd(&zst); deflateEnd(&zst);
PyErr_SetString(PyExc_BufferError, "Not enough space in output buffer"); PyErr_SetString(PyExc_ValueError, "Not enough space in output buffer");
goto done; goto done;
default: default:
deflateEnd(&zst); deflateEnd(&zst);
......
...@@ -30,7 +30,7 @@ def test_compress_into_err(): ...@@ -30,7 +30,7 @@ def test_compress_into_err():
with pytest.raises(TypeError): with pytest.raises(TypeError):
compress_into(data_in, memoryview(buf).toreadonly()) # Output not writable compress_into(data_in, memoryview(buf).toreadonly()) # Output not writable
with pytest.raises(BufferError): with pytest.raises(ValueError):
compress_into(data_in, buf[:10]) # Output too small compress_into(data_in, buf[:10]) # Output too small
...@@ -54,9 +54,15 @@ def test_decompress_into(): ...@@ -54,9 +54,15 @@ def test_decompress_into():
# Not enough space, by 1 byte # Not enough space, by 1 byte
buf3 = bytearray(len(expanded_data) - 1) buf3 = bytearray(len(expanded_data) - 1)
with pytest.raises(BufferError): with pytest.raises(ValueError):
decompress_into(compressed_data, buf3) decompress_into(compressed_data, buf3)
# Corner case: decompress 0 bytes
compressed_nothing = zlib.compress(b'')
buf_size_0 = bytearray(0)
n_bytes_out = decompress_into(compressed_nothing, buf_size_0)
assert n_bytes_out == 0
def test_decompress_into_err(): def test_decompress_into_err():
expanded_data = b'abcde' * 5000 expanded_data = b'abcde' * 5000
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment