comparison contrib/python-zstandard/c-ext/decompressobj.c @ 30435:b86a448a2965

zstd: vendor python-zstandard 0.5.0 As the commit message for the previous changeset says, we wish for zstd to be a 1st class citizen in Mercurial. To make that happen, we need to enable Python to talk to the zstd C API. And that requires bindings. This commit vendors a copy of existing Python bindings. Why do we need to vendor? As the commit message of the previous commit says, relying on systems in the wild to have the bindings or zstd present is a losing proposition. By distributing the zstd and bindings with Mercurial, we significantly increase our chances that zstd will work. Since zstd will deliver a better end-user experience by achieving better performance, this benefits our users. Another reason is that the Python bindings still aren't stable and the API is somewhat fluid. While Mercurial could be coded to target multiple versions of the Python bindings, it is safer to bundle an explicit, known working version. The added Python bindings are mostly a fully-featured interface to the zstd C API. They allow one-shot operations, streaming, reading and writing from objects implements the file object protocol, dictionary compression, control over low-level compression parameters, and more. The Python bindings work on Python 2.6, 2.7, and 3.3+ and have been tested on Linux and Windows. There are CFFI bindings, but they are lacking compared to the C extension. Upstream work will be needed before we can support zstd with PyPy. But it will be possible. The files added in this commit come from Git commit e637c1b214d5f869cf8116c550dcae23ec13b677 from https://github.com/indygreg/python-zstandard and are added without modifications. Some files from the upstream repository have been omitted, namely files related to continuous integration. In the spirit of full disclosure, I'm the maintainer of the "python-zstandard" project and have authored 100% of the code added in this commit. Unfortunately, the Python bindings have not been formally code reviewed by anyone. While I've tested much of the code thoroughly (I even have tests that fuzz APIs), there's a good chance there are bugs, memory leaks, not well thought out APIs, etc. If someone wants to review the code and send feedback to the GitHub project, it would be greatly appreciated. Despite my involvement with both projects, my opinions of code style differ from Mercurial's. The code in this commit introduces numerous code style violations in Mercurial's linters. So, the code is excluded from most lints. However, some violations I agree with. These have been added to the known violations ignore list for now.
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 10 Nov 2016 22:15:58 -0800
parents
children c32454d69b85
comparison
equal deleted inserted replaced
30434:2e484bdea8c4 30435:b86a448a2965
1 /**
2 * Copyright (c) 2016-present, Gregory Szorc
3 * All rights reserved.
4 *
5 * This software may be modified and distributed under the terms
6 * of the BSD license. See the LICENSE file for details.
7 */
8
9 #include "python-zstandard.h"
10
11 extern PyObject* ZstdError;
12
13 PyDoc_STRVAR(DecompressionObj__doc__,
14 "Perform decompression using a standard library compatible API.\n"
15 );
16
17 static void DecompressionObj_dealloc(ZstdDecompressionObj* self) {
18 if (self->dstream) {
19 ZSTD_freeDStream(self->dstream);
20 self->dstream = NULL;
21 }
22
23 Py_XDECREF(self->decompressor);
24
25 PyObject_Del(self);
26 }
27
28 static PyObject* DecompressionObj_decompress(ZstdDecompressionObj* self, PyObject* args) {
29 const char* source;
30 Py_ssize_t sourceSize;
31 size_t zresult;
32 ZSTD_inBuffer input;
33 ZSTD_outBuffer output;
34 size_t outSize = ZSTD_DStreamOutSize();
35 PyObject* result = NULL;
36 Py_ssize_t resultSize = 0;
37
38 if (self->finished) {
39 PyErr_SetString(ZstdError, "cannot use a decompressobj multiple times");
40 return NULL;
41 }
42
43 #if PY_MAJOR_VERSION >= 3
44 if (!PyArg_ParseTuple(args, "y#",
45 #else
46 if (!PyArg_ParseTuple(args, "s#",
47 #endif
48 &source, &sourceSize)) {
49 return NULL;
50 }
51
52 input.src = source;
53 input.size = sourceSize;
54 input.pos = 0;
55
56 output.dst = PyMem_Malloc(outSize);
57 if (!output.dst) {
58 PyErr_NoMemory();
59 return NULL;
60 }
61 output.size = outSize;
62 output.pos = 0;
63
64 /* Read input until exhausted. */
65 while (input.pos < input.size) {
66 Py_BEGIN_ALLOW_THREADS
67 zresult = ZSTD_decompressStream(self->dstream, &output, &input);
68 Py_END_ALLOW_THREADS
69
70 if (ZSTD_isError(zresult)) {
71 PyErr_Format(ZstdError, "zstd decompressor error: %s",
72 ZSTD_getErrorName(zresult));
73 result = NULL;
74 goto finally;
75 }
76
77 if (0 == zresult) {
78 self->finished = 1;
79 }
80
81 if (output.pos) {
82 if (result) {
83 resultSize = PyBytes_GET_SIZE(result);
84 if (-1 == _PyBytes_Resize(&result, resultSize + output.pos)) {
85 goto except;
86 }
87
88 memcpy(PyBytes_AS_STRING(result) + resultSize,
89 output.dst, output.pos);
90 }
91 else {
92 result = PyBytes_FromStringAndSize(output.dst, output.pos);
93 if (!result) {
94 goto except;
95 }
96 }
97
98 output.pos = 0;
99 }
100 }
101
102 if (!result) {
103 result = PyBytes_FromString("");
104 }
105
106 goto finally;
107
108 except:
109 Py_DecRef(result);
110 result = NULL;
111
112 finally:
113 PyMem_Free(output.dst);
114
115 return result;
116 }
117
118 static PyMethodDef DecompressionObj_methods[] = {
119 { "decompress", (PyCFunction)DecompressionObj_decompress,
120 METH_VARARGS, PyDoc_STR("decompress data") },
121 { NULL, NULL }
122 };
123
124 PyTypeObject ZstdDecompressionObjType = {
125 PyVarObject_HEAD_INIT(NULL, 0)
126 "zstd.ZstdDecompressionObj", /* tp_name */
127 sizeof(ZstdDecompressionObj), /* tp_basicsize */
128 0, /* tp_itemsize */
129 (destructor)DecompressionObj_dealloc, /* tp_dealloc */
130 0, /* tp_print */
131 0, /* tp_getattr */
132 0, /* tp_setattr */
133 0, /* tp_compare */
134 0, /* tp_repr */
135 0, /* tp_as_number */
136 0, /* tp_as_sequence */
137 0, /* tp_as_mapping */
138 0, /* tp_hash */
139 0, /* tp_call */
140 0, /* tp_str */
141 0, /* tp_getattro */
142 0, /* tp_setattro */
143 0, /* tp_as_buffer */
144 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
145 DecompressionObj__doc__, /* tp_doc */
146 0, /* tp_traverse */
147 0, /* tp_clear */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
150 0, /* tp_iter */
151 0, /* tp_iternext */
152 DecompressionObj_methods, /* tp_methods */
153 0, /* tp_members */
154 0, /* tp_getset */
155 0, /* tp_base */
156 0, /* tp_dict */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
160 0, /* tp_init */
161 0, /* tp_alloc */
162 PyType_GenericNew, /* tp_new */
163 };
164
165 void decompressobj_module_init(PyObject* module) {
166 Py_TYPE(&ZstdDecompressionObjType) = &PyType_Type;
167 if (PyType_Ready(&ZstdDecompressionObjType) < 0) {
168 return;
169 }
170 }