Skip to content

Commit

Permalink
global: remove check for C contiguous single dimension buffer
Browse files Browse the repository at this point in the history
The former is redundant with what Python does behind the scenes
and the latter isn't needed in practice.

I believe all removed instances were for PyBuffer obtained via
Python's argument parser using `y*` or `w*`.

Closes #124.
  • Loading branch information
indygreg committed Dec 25, 2020
1 parent 6b0a6d8 commit 4ce9dce
Show file tree
Hide file tree
Showing 18 changed files with 5 additions and 131 deletions.
5 changes: 5 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ Bug Fixes
Changes
-------

* ``PyBuffer`` instances are no longer checked to be C contiguous and
have a single dimension. The former was redundant with what
``PyArg_ParseTuple()`` already did and the latter is not necessary
in practice because very few extension modules create buffers with
more than 1 dimension. (#124)
* Added Python typing stub file for the ``zstandard`` module. (#120)
* The ``make_cffi.py`` script should now respect the ``CC`` environment
variable for locating the compiler. (#103)
Expand Down
10 changes: 0 additions & 10 deletions c-ext/bufferutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ static int BufferWithSegments_init(ZstdBufferWithSegments* self, PyObject* args,
return -1;
}

if (!PyBuffer_IsContiguous(&self->parent, 'C') || self->parent.ndim > 1) {
PyErr_SetString(PyExc_ValueError, "data buffer should be contiguous and have a single dimension");
goto except;
}

if (!PyBuffer_IsContiguous(&segments, 'C') || segments.ndim > 1) {
PyErr_SetString(PyExc_ValueError, "segments buffer should be contiguous and have a single dimension");
goto except;
}

if (segments.len % sizeof(BufferSegment)) {
PyErr_Format(PyExc_ValueError, "segments array size is not a multiple of %zu",
sizeof(BufferSegment));
Expand Down
7 changes: 0 additions & 7 deletions c-ext/compressionchunker.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ static ZstdCompressionChunkerIterator* ZstdCompressionChunker_compress(ZstdCompr
return NULL;
}

if (!PyBuffer_IsContiguous(&self->inBuffer, 'C') || self->inBuffer.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
PyBuffer_Release(&self->inBuffer);
return NULL;
}

result = (ZstdCompressionChunkerIterator*)PyObject_CallObject((PyObject*)&ZstdCompressionChunkerIteratorType, NULL);
if (!result) {
PyBuffer_Release(&self->inBuffer);
Expand Down
6 changes: 0 additions & 6 deletions c-ext/compressiondict.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,6 @@ static int ZstdCompressionDict_init(ZstdCompressionDict* self, PyObject* args, P
return -1;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

if (dictType != ZSTD_dct_auto && dictType != ZSTD_dct_rawContent
&& dictType != ZSTD_dct_fullDict) {
PyErr_Format(PyExc_ValueError,
Expand Down
12 changes: 0 additions & 12 deletions c-ext/compressionreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,6 @@ static PyObject* compressionreader_readinto(ZstdCompressionReader* self, PyObjec
return NULL;
}

if (!PyBuffer_IsContiguous(&dest, 'C') || dest.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"destination buffer should be contiguous and have at most one dimension");
goto finally;
}

output.dst = dest.buf;
output.size = dest.len;
output.pos = 0;
Expand Down Expand Up @@ -631,12 +625,6 @@ static PyObject* compressionreader_readinto1(ZstdCompressionReader* self, PyObje
return NULL;
}

if (!PyBuffer_IsContiguous(&dest, 'C') || dest.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"destination buffer should be contiguous and have at most one dimension");
goto finally;
}

output.dst = dest.buf;
output.size = dest.len;
output.pos = 0;
Expand Down
6 changes: 0 additions & 6 deletions c-ext/compressionwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ static PyObject* ZstdCompressionWriter_write(ZstdCompressionWriter* self, PyObje
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

if (self->closed) {
PyErr_SetString(PyExc_ValueError, "stream is closed");
return NULL;
Expand Down
6 changes: 0 additions & 6 deletions c-ext/compressobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ static PyObject* ZstdCompressionObj_compress(ZstdCompressionObj* self, PyObject*
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

input.src = source.buf;
input.size = source.len;
input.pos = 0;
Expand Down
6 changes: 0 additions & 6 deletions c-ext/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,6 @@ static PyObject* ZstdCompressor_compress(ZstdCompressor* self, PyObject* args, P
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only);

destSize = ZSTD_compressBound(source.len);
Expand Down
12 changes: 0 additions & 12 deletions c-ext/decompressionreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,6 @@ static PyObject* decompressionreader_readinto(ZstdDecompressionReader* self, PyO
return NULL;
}

if (!PyBuffer_IsContiguous(&dest, 'C') || dest.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"destination buffer should be contiguous and have at most one dimension");
goto finally;
}

output.dst = dest.buf;
output.size = dest.len;
output.pos = 0;
Expand Down Expand Up @@ -487,12 +481,6 @@ static PyObject* decompressionreader_readinto1(ZstdDecompressionReader* self, Py
return NULL;
}

if (!PyBuffer_IsContiguous(&dest, 'C') || dest.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"destination buffer should be contiguous and have at most one dimension");
goto finally;
}

output.dst = dest.buf;
output.size = dest.len;
output.pos = 0;
Expand Down
6 changes: 0 additions & 6 deletions c-ext/decompressionwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ static PyObject* ZstdDecompressionWriter_write(ZstdDecompressionWriter* self, Py
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

if (self->closed) {
PyErr_SetString(PyExc_ValueError, "stream is closed");
return NULL;
Expand Down
6 changes: 0 additions & 6 deletions c-ext/decompressobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ static PyObject* DecompressionObj_decompress(ZstdDecompressionObj* self, PyObjec
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

/* Special case of empty input. Output will always be empty. */
if (source.len == 0) {
result = PyBytes_FromString("");
Expand Down
11 changes: 0 additions & 11 deletions c-ext/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,6 @@ PyObject* Decompressor_decompress(ZstdDecompressor* self, PyObject* args, PyObje
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

if (ensure_dctx(self, 1)) {
goto finally;
}
Expand Down Expand Up @@ -1533,11 +1527,6 @@ static ZstdBufferWithSegmentsCollection* Decompressor_multi_decompress_to_buffer
}

if (frameSizes.buf) {
if (!PyBuffer_IsContiguous(&frameSizes, 'C') || frameSizes.ndim > 1) {
PyErr_SetString(PyExc_ValueError, "decompressed_sizes buffer should be contiguous and have a single dimension");
goto finally;
}

frameSizesP = (unsigned long long*)frameSizes.buf;
}

Expand Down
6 changes: 0 additions & 6 deletions c-ext/frameparams.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ FrameParametersObject* get_frame_parameters(PyObject* self, PyObject* args, PyOb
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

zresult = ZSTD_getFrameHeader(&header, source.buf, source.len);

if (ZSTD_isError(zresult)) {
Expand Down
4 changes: 0 additions & 4 deletions docs/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ Many Python types implement the buffer protocol. These include ``bytes``
(``str`` on Python 2), ``bytearray``, ``array.array``, ``io.BytesIO``,
``mmap.mmap``, and ``memoryview``.

``python-zstandard`` APIs that accept objects conforming to the buffer
protocol require that the buffer is *C contiguous*. This is usually the
case.

Requiring Output Sizes for Non-Streaming Decompression APIs
===========================================================

Expand Down
7 changes: 0 additions & 7 deletions rust-ext/src/compression_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ impl ZstdCompressionDict {
fn new_impl(py: Python, data: PyObject, dict_type: Option<u32>) -> PyResult<PyObject> {
let buffer = PyBuffer::get(py, &data)?;

if !buffer.is_c_contiguous() || buffer.dimensions() > 1 {
return Err(PyErr::new::<ValueError, _>(
py,
"data buffer should be contiguous and have at most one dimension",
));
}

let dict_type = if dict_type == Some(zstd_sys::ZSTD_dictContentType_e::ZSTD_dct_auto as u32)
{
Ok(zstd_sys::ZSTD_dictContentType_e::ZSTD_dct_auto)
Expand Down
7 changes: 0 additions & 7 deletions rust-ext/src/compressionobj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ impl ZstdCompressionObj {

let buffer = PyBuffer::get(py, &data)?;

if !buffer.is_c_contiguous() || buffer.dimensions() > 1 {
return Err(PyErr::new::<ValueError, _>(
py,
"data buffer should be contiguous and have at most one dimension",
));
}

let mut source = unsafe {
std::slice::from_raw_parts::<u8>(buffer.buf_ptr() as *const _, buffer.len_bytes())
};
Expand Down
7 changes: 0 additions & 7 deletions rust-ext/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,6 @@ impl ZstdCompressor {

let buffer = PyBuffer::get(py, &data)?;

if !buffer.is_c_contiguous() || buffer.dimensions() > 1 {
return Err(ZstdError::from_message(
py,
"data buffer should be contiguous and have at most one dimension",
));
}

let source: &[u8] =
unsafe { std::slice::from_raw_parts(buffer.buf_ptr() as *const _, buffer.len_bytes()) };

Expand Down
12 changes: 0 additions & 12 deletions zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ static PyObject* frame_content_size(PyObject* self, PyObject* args, PyObject* kw
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

size = ZSTD_getFrameContentSize(source.buf, source.len);

if (size == ZSTD_CONTENTSIZE_ERROR) {
Expand Down Expand Up @@ -113,12 +107,6 @@ static PyObject* frame_header_size(PyObject* self, PyObject* args, PyObject* kwa
return NULL;
}

if (!PyBuffer_IsContiguous(&source, 'C') || source.ndim > 1) {
PyErr_SetString(PyExc_ValueError,
"data buffer should be contiguous and have at most one dimension");
goto finally;
}

zresult = ZSTD_frameHeaderSize(source.buf, source.len);
if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "could not determine frame header size: %s",
Expand Down

0 comments on commit 4ce9dce

Please sign in to comment.