comparison contrib/python-zstandard/c-ext/bufferutil.c @ 37495:b1fb341d8a61

zstandard: vendor python-zstandard 0.9.0 This was just released. It features a number of goodies. More info at https://gregoryszorc.com/blog/2018/04/09/release-of-python-zstandard-0.9/. The clang-format ignore list was updated to reflect the new source of files. The project contains a vendored copy of zstandard 1.3.4. The old version was 1.1.3. One of the changes between those versions is that zstandard is now dual licensed BSD + GPLv2 and the patent rights grant has been removed. Good riddance. The API should be backwards compatible. So no changes in core should be needed. However, there were a number of changes in the library that we'll want to adapt to. Those will be addressed in subsequent commits. Differential Revision: https://phab.mercurial-scm.org/D3198
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 09 Apr 2018 10:13:29 -0700
parents e0dc40530c5a
children e92ca942ddca
comparison
equal deleted inserted replaced
37494:1ce7a55b09d1 37495:b1fb341d8a61
81 PyErr_SetString(PyExc_ValueError, "segments buffer should be contiguous and have a single dimension"); 81 PyErr_SetString(PyExc_ValueError, "segments buffer should be contiguous and have a single dimension");
82 goto except; 82 goto except;
83 } 83 }
84 84
85 if (segments.len % sizeof(BufferSegment)) { 85 if (segments.len % sizeof(BufferSegment)) {
86 PyErr_Format(PyExc_ValueError, "segments array size is not a multiple of %lu", 86 PyErr_Format(PyExc_ValueError, "segments array size is not a multiple of %zu",
87 sizeof(BufferSegment)); 87 sizeof(BufferSegment));
88 goto except; 88 goto except;
89 } 89 }
90 90
91 segmentCount = segments.len / sizeof(BufferSegment); 91 segmentCount = segments.len / sizeof(BufferSegment);
121 121
122 except: 122 except:
123 PyBuffer_Release(&self->parent); 123 PyBuffer_Release(&self->parent);
124 PyBuffer_Release(&segments); 124 PyBuffer_Release(&segments);
125 return -1; 125 return -1;
126 }; 126 }
127 127
128 /** 128 /**
129 * Construct a BufferWithSegments from existing memory and offsets. 129 * Construct a BufferWithSegments from existing memory and offsets.
130 * 130 *
131 * Ownership of the backing memory and BufferSegments will be transferred to 131 * Ownership of the backing memory and BufferSegments will be transferred to
186 if (i >= self->segmentCount) { 186 if (i >= self->segmentCount) {
187 PyErr_Format(PyExc_IndexError, "offset must be less than %zd", self->segmentCount); 187 PyErr_Format(PyExc_IndexError, "offset must be less than %zd", self->segmentCount);
188 return NULL; 188 return NULL;
189 } 189 }
190 190
191 if (self->segments[i].length > PY_SSIZE_T_MAX) {
192 PyErr_Format(PyExc_ValueError,
193 "item at offset %zd is too large for this platform", i);
194 return NULL;
195 }
196
191 result = (ZstdBufferSegment*)PyObject_CallObject((PyObject*)&ZstdBufferSegmentType, NULL); 197 result = (ZstdBufferSegment*)PyObject_CallObject((PyObject*)&ZstdBufferSegmentType, NULL);
192 if (NULL == result) { 198 if (NULL == result) {
193 return NULL; 199 return NULL;
194 } 200 }
195 201
196 result->parent = (PyObject*)self; 202 result->parent = (PyObject*)self;
197 Py_INCREF(self); 203 Py_INCREF(self);
198 204
199 result->data = (char*)self->data + self->segments[i].offset; 205 result->data = (char*)self->data + self->segments[i].offset;
200 result->dataSize = self->segments[i].length; 206 result->dataSize = (Py_ssize_t)self->segments[i].length;
201 result->offset = self->segments[i].offset; 207 result->offset = self->segments[i].offset;
202 208
203 return result; 209 return result;
204 } 210 }
205 211
206 #if PY_MAJOR_VERSION >= 3 212 #if PY_MAJOR_VERSION >= 3
207 static int BufferWithSegments_getbuffer(ZstdBufferWithSegments* self, Py_buffer* view, int flags) { 213 static int BufferWithSegments_getbuffer(ZstdBufferWithSegments* self, Py_buffer* view, int flags) {
208 return PyBuffer_FillInfo(view, (PyObject*)self, self->data, self->dataSize, 1, flags); 214 if (self->dataSize > PY_SSIZE_T_MAX) {
215 view->obj = NULL;
216 PyErr_SetString(PyExc_BufferError, "buffer is too large for this platform");
217 return -1;
218 }
219
220 return PyBuffer_FillInfo(view, (PyObject*)self, self->data, (Py_ssize_t)self->dataSize, 1, flags);
209 } 221 }
210 #else 222 #else
211 static Py_ssize_t BufferWithSegments_getreadbuffer(ZstdBufferWithSegments* self, Py_ssize_t segment, void **ptrptr) { 223 static Py_ssize_t BufferWithSegments_getreadbuffer(ZstdBufferWithSegments* self, Py_ssize_t segment, void **ptrptr) {
212 if (segment != 0) { 224 if (segment != 0) {
213 PyErr_SetString(PyExc_ValueError, "segment number must be 0"); 225 PyErr_SetString(PyExc_ValueError, "segment number must be 0");
214 return -1; 226 return -1;
215 } 227 }
216 228
229 if (self->dataSize > PY_SSIZE_T_MAX) {
230 PyErr_SetString(PyExc_ValueError, "buffer is too large for this platform");
231 return -1;
232 }
233
217 *ptrptr = self->data; 234 *ptrptr = self->data;
218 return self->dataSize; 235 return (Py_ssize_t)self->dataSize;
219 } 236 }
220 237
221 static Py_ssize_t BufferWithSegments_getsegcount(ZstdBufferWithSegments* self, Py_ssize_t* len) { 238 static Py_ssize_t BufferWithSegments_getsegcount(ZstdBufferWithSegments* self, Py_ssize_t* len) {
222 if (len) { 239 if (len) {
223 *len = 1; 240 *len = 1;
230 PyDoc_STRVAR(BufferWithSegments_tobytes__doc__, 247 PyDoc_STRVAR(BufferWithSegments_tobytes__doc__,
231 "Obtain a bytes instance for this buffer.\n" 248 "Obtain a bytes instance for this buffer.\n"
232 ); 249 );
233 250
234 static PyObject* BufferWithSegments_tobytes(ZstdBufferWithSegments* self) { 251 static PyObject* BufferWithSegments_tobytes(ZstdBufferWithSegments* self) {
235 return PyBytes_FromStringAndSize(self->data, self->dataSize); 252 if (self->dataSize > PY_SSIZE_T_MAX) {
253 PyErr_SetString(PyExc_ValueError, "buffer is too large for this platform");
254 return NULL;
255 }
256
257 return PyBytes_FromStringAndSize(self->data, (Py_ssize_t)self->dataSize);
236 } 258 }
237 259
238 PyDoc_STRVAR(BufferWithSegments_segments__doc__, 260 PyDoc_STRVAR(BufferWithSegments_segments__doc__,
239 "Obtain a BufferSegments describing segments in this sintance.\n" 261 "Obtain a BufferSegments describing segments in this sintance.\n"
240 ); 262 );