blob: bb52fdc5c5110217aebd9cf1e98ccffee091ce7e [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Guido van Rossumfb221561997-04-29 15:38:09 +00006
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "Python.h"
8#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +00009
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000010#ifdef WITH_THREAD
11#include "pythread.h"
12
13/* #defs ripped off from _tkinter.c, even though the situation here is much
14 simpler, because we don't have to worry about waiting for Tcl
15 events! And, since zlib itself is threadsafe, we don't need to worry
16 about re-entering zlib functions.
17
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018 N.B.
19
20 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
21 that modify the components of preexisting de/compress objects, it
22 could prove to be a performance gain on multiprocessor machines if
23 there was an de/compress object-specific lock. However, for the
24 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
25 de/compress objects.
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000026 */
27
28static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
29
30#define ENTER_ZLIB \
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000031 Py_BEGIN_ALLOW_THREADS \
32 PyThread_acquire_lock(zlib_lock, 1); \
33 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000034
35#define LEAVE_ZLIB \
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000036 PyThread_release_lock(zlib_lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000037
38#else
39
40#define ENTER_ZLIB
41#define LEAVE_ZLIB
42
43#endif
44
Guido van Rossumfb221561997-04-29 15:38:09 +000045/* The following parameters are copied from zutil.h, version 0.95 */
46#define DEFLATED 8
47#if MAX_MEM_LEVEL >= 8
48# define DEF_MEM_LEVEL 8
49#else
50# define DEF_MEM_LEVEL MAX_MEM_LEVEL
51#endif
52#define DEF_WBITS MAX_WBITS
53
Guido van Rossumb729a1d1999-04-07 20:23:17 +000054/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
55#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000056#define PyInit_zlib initzlib
57
Jeremy Hylton938ace62002-07-17 16:30:39 +000058static PyTypeObject Comptype;
59static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000060
61static PyObject *ZlibError;
62
Tim Peters977e5402001-10-17 03:57:20 +000063typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000064{
Jeremy Hylton9714f992001-10-16 21:19:45 +000065 PyObject_HEAD
66 z_stream zst;
67 PyObject *unused_data;
68 PyObject *unconsumed_tail;
69 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000070} compobject;
71
Jeremy Hylton0965e082001-10-16 21:56:09 +000072static void
73zlib_error(z_stream zst, int err, char *msg)
74{
75 if (zst.msg == Z_NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000076 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000077 else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000078 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079}
80
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000082"compressobj([level]) -- Return a compressor object.\n"
83"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000086PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000087"decompressobj([wbits]) -- Return a decompressor object.\n"
88"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000089"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000097 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000098 self->is_initialised = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +000099 self->unused_data = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000100 if (self->unused_data == NULL) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000101 Py_DECREF(self);
102 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000103 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000104 self->unconsumed_tail = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 if (self->unconsumed_tail == NULL) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000106 Py_DECREF(self);
107 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 }
109 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000110}
111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000113"compress(string[, level]) -- Returned compressed string.\n"
114"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000116
Guido van Rossumfb221561997-04-29 15:38:09 +0000117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000119{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 PyObject *ReturnVal = NULL;
121 Byte *input, *output;
122 int length, level=Z_DEFAULT_COMPRESSION, err;
123 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000124
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 /* require Python string object, optional 'level' arg */
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000126 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000127 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000128
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000130
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000131 output = (Byte*)malloc(zst.avail_out);
132 if (output == NULL) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000133 PyErr_SetString(PyExc_MemoryError,
134 "Can't allocate memory to compress data");
135 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000136 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000137
Jeremy Hylton9714f992001-10-16 21:19:45 +0000138 /* Past the point of no return. From here on out, we need to make sure
139 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000140
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000141 zst.zalloc = (alloc_func)NULL;
142 zst.zfree = (free_func)Z_NULL;
143 zst.next_out = (Byte *)output;
144 zst.next_in = (Byte *)input;
145 zst.avail_in = length;
146 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000147
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000148 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000149 case(Z_OK):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000150 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000151 case(Z_MEM_ERROR):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000152 PyErr_SetString(PyExc_MemoryError,
153 "Out of memory while compressing data");
154 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000155 case(Z_STREAM_ERROR):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000156 PyErr_SetString(ZlibError,
157 "Bad compression level");
158 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000159 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000160 deflateEnd(&zst);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000161 zlib_error(zst, err, "while compressing data");
162 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000163 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000164
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000165 Py_BEGIN_ALLOW_THREADS;
166 err = deflate(&zst, Z_FINISH);
167 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000168
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000169 if (err != Z_STREAM_END) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000170 zlib_error(zst, err, "while compressing data");
171 deflateEnd(&zst);
172 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000173 }
Tim Peters977e5402001-10-17 03:57:20 +0000174
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000175 err=deflateEnd(&zst);
176 if (err == Z_OK)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000177 ReturnVal = PyString_FromStringAndSize((char *)output,
178 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000179 else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000180 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000181
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000183 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000184
Jeremy Hylton9714f992001-10-16 21:19:45 +0000185 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000186}
187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000189"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
190"\n"
191"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000193
Guido van Rossumfb221561997-04-29 15:38:09 +0000194static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000195PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000196{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000197 PyObject *result_str;
198 Byte *input;
199 int length, err;
Christian Heimes901071b2007-11-21 00:46:21 +0000200 int wsize=DEF_WBITS;
201 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000202 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000203
Christian Heimes901071b2007-11-21 00:46:21 +0000204 if (!PyArg_ParseTuple(args, "s#|in:decompress",
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000205 &input, &length, &wsize, &r_strlen))
206 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000207
Jeremy Hylton9714f992001-10-16 21:19:45 +0000208 if (r_strlen <= 0)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000209 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000210
Jeremy Hylton9714f992001-10-16 21:19:45 +0000211 zst.avail_in = length;
212 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000213
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000214 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000215 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000216
Jeremy Hylton9714f992001-10-16 21:19:45 +0000217 zst.zalloc = (alloc_func)NULL;
218 zst.zfree = (free_func)Z_NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000219 zst.next_out = (Byte *)PyString_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000220 zst.next_in = (Byte *)input;
221 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000222
Jeremy Hylton9714f992001-10-16 21:19:45 +0000223 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000224 case(Z_OK):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000225 break;
Tim Peters977e5402001-10-17 03:57:20 +0000226 case(Z_MEM_ERROR):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000227 PyErr_SetString(PyExc_MemoryError,
228 "Out of memory while decompressing data");
229 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000230 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000231 inflateEnd(&zst);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000232 zlib_error(zst, err, "while preparing to decompress data");
233 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000234 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000235
Jeremy Hylton9714f992001-10-16 21:19:45 +0000236 do {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000237 Py_BEGIN_ALLOW_THREADS
238 err=inflate(&zst, Z_FINISH);
239 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000241 switch(err) {
242 case(Z_STREAM_END):
243 break;
244 case(Z_BUF_ERROR):
245 /*
246 * If there is at least 1 byte of room according to zst.avail_out
247 * and we get this error, assume that it means zlib cannot
248 * process the inflate call() due to an error in the data.
249 */
250 if (zst.avail_out > 0) {
251 PyErr_Format(ZlibError, "Error %i while decompressing data",
252 err);
253 inflateEnd(&zst);
254 goto error;
255 }
256 /* fall through */
257 case(Z_OK):
258 /* need more memory */
259 if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
260 inflateEnd(&zst);
261 goto error;
262 }
263 zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
264 + r_strlen;
265 zst.avail_out = r_strlen;
266 r_strlen = r_strlen << 1;
267 break;
268 default:
269 inflateEnd(&zst);
270 zlib_error(zst, err, "while decompressing data");
271 goto error;
272 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000273 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000274
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000275 err = inflateEnd(&zst);
276 if (err != Z_OK) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000277 zlib_error(zst, err, "while finishing data decompression");
278 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000279 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000280
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000281 _PyString_Resize(&result_str, zst.total_out);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000282 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000283
284 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000285 Py_XDECREF(result_str);
286 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000287}
288
289static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000290PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000291{
Jeremy Hylton499000002001-10-16 21:59:35 +0000292 compobject *self;
293 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
294 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000295
Jeremy Hylton499000002001-10-16 21:59:35 +0000296 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000297 &memLevel, &strategy))
298 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000299
Jeremy Hylton499000002001-10-16 21:59:35 +0000300 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000301 if (self==NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000302 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000303 self->zst.zalloc = (alloc_func)NULL;
304 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000305 self->zst.next_in = NULL;
306 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000307 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
308 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000309 case (Z_OK):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000310 self->is_initialised = 1;
311 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000312 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000313 Py_DECREF(self);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000314 PyErr_SetString(PyExc_MemoryError,
315 "Can't allocate memory for compression object");
316 return NULL;
317 case(Z_STREAM_ERROR):
318 Py_DECREF(self);
319 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
320 return NULL;
321 default:
322 zlib_error(self->zst, err, "while creating compression object");
323 Py_DECREF(self);
324 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000325 }
326}
327
328static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000329PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000330{
Jeremy Hylton499000002001-10-16 21:59:35 +0000331 int wbits=DEF_WBITS, err;
332 compobject *self;
333 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000334 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000335
336 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000337 if (self == NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000338 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000339 self->zst.zalloc = (alloc_func)NULL;
340 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000341 self->zst.next_in = NULL;
342 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000343 err = inflateInit2(&self->zst, wbits);
344 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000345 case (Z_OK):
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000346 self->is_initialised = 1;
347 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000348 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000349 Py_DECREF(self);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000350 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
351 return NULL;
352 case (Z_MEM_ERROR):
353 Py_DECREF(self);
354 PyErr_SetString(PyExc_MemoryError,
355 "Can't allocate memory for decompression object");
356 return NULL;
357 default:
358 zlib_error(self->zst, err, "while creating decompression object");
359 Py_DECREF(self);
360 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000361 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000362}
363
364static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000365Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000366{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000367 if (self->is_initialised)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000368 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000369 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000370 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000371 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000372}
373
374static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000375Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000376{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000377 if (self->is_initialised)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000378 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000379 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000380 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000381 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000382}
383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000385"compress(data) -- Return a string containing data compressed.\n"
386"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000387"After calling this function, some of the input data may still\n"
388"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000390
391
Guido van Rossumfb221561997-04-29 15:38:09 +0000392static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000393PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000394{
Antoine Pitrouc7035cd2010-05-07 16:59:00 +0000395 int err, inplen;
396 Py_ssize_t length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000397 PyObject *RetVal;
398 Byte *input;
399 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000400
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000401 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000402 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000403
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000404 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000405 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000406
407 ENTER_ZLIB
408
Jeremy Hylton9714f992001-10-16 21:19:45 +0000409 start_total_out = self->zst.total_out;
410 self->zst.avail_in = inplen;
411 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000412 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000413 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000414
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000415 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000416 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000417 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000418
Jeremy Hylton9714f992001-10-16 21:19:45 +0000419 /* while Z_OK and the output buffer is full, there might be more output,
420 so extend the output buffer and try again */
421 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000422 if (_PyString_Resize(&RetVal, length << 1) < 0)
423 goto error;
424 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
425 + length;
426 self->zst.avail_out = length;
427 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000428
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000429 Py_BEGIN_ALLOW_THREADS
430 err = deflate(&(self->zst), Z_NO_FLUSH);
431 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000432 }
Tim Peters977e5402001-10-17 03:57:20 +0000433 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000434 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000435 condition.
436 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000437
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000438 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000439 zlib_error(self->zst, err, "while compressing");
440 Py_DECREF(RetVal);
441 RetVal = NULL;
442 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000443 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000444 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000445
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000446 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000447 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000448 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000449}
450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000452"decompress(data, max_length) -- Return a string containing the decompressed\n"
453"version of the data.\n"
454"\n"
455"After calling this function, some of the input data may still be stored in\n"
456"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000457"Call the flush() method to clear these buffers.\n"
458"If the max_length parameter is specified then the return value will be\n"
459"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000461
Guido van Rossumfb221561997-04-29 15:38:09 +0000462static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000463PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000464{
Antoine Pitrouc7035cd2010-05-07 16:59:00 +0000465 int err, inplen, max_length = 0;
466 Py_ssize_t old_length, length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000467 PyObject *RetVal;
468 Byte *input;
469 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000470
Tim Peters977e5402001-10-17 03:57:20 +0000471 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000472 &inplen, &max_length))
473 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000474 if (max_length < 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000475 PyErr_SetString(PyExc_ValueError,
476 "max_length must be greater than zero");
477 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000478 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000479
Jeremy Hylton9714f992001-10-16 21:19:45 +0000480 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000481 if (max_length && length > max_length)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000482 length = max_length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000483 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000484 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000485
486 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000487
Jeremy Hylton9714f992001-10-16 21:19:45 +0000488 start_total_out = self->zst.total_out;
489 self->zst.avail_in = inplen;
490 self->zst.next_in = input;
491 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000492 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000493
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000494 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000495 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000496 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000497
Jeremy Hylton9714f992001-10-16 21:19:45 +0000498 /* While Z_OK and the output buffer is full, there might be more output.
499 So extend the output buffer and try again.
500 */
Tim Peters977e5402001-10-17 03:57:20 +0000501 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000502 /* If max_length set, don't continue decompressing if we've already
503 reached the limit.
504 */
505 if (max_length && length >= max_length)
506 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000507
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000508 /* otherwise, ... */
509 old_length = length;
510 length = length << 1;
511 if (max_length && length > max_length)
512 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000513
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000514 if (_PyString_Resize(&RetVal, length) < 0)
515 goto error;
516 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
517 + old_length;
518 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000519
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000520 Py_BEGIN_ALLOW_THREADS
521 err = inflate(&(self->zst), Z_SYNC_FLUSH);
522 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000523 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000524
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000525 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000526 of specified size. Return the unconsumed tail in an attribute.*/
527 if(max_length) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000528 Py_DECREF(self->unconsumed_tail);
529 self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
530 self->zst.avail_in);
531 if(!self->unconsumed_tail) {
532 Py_DECREF(RetVal);
533 RetVal = NULL;
534 goto error;
535 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000536 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000537
Tim Peters977e5402001-10-17 03:57:20 +0000538 /* The end of the compressed data has been reached, so set the
539 unused_data attribute to a string containing the remainder of the
540 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000541 inflateEnd, but the old behaviour of only calling it on flush() is
542 preserved.
543 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000544 if (err == Z_STREAM_END) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000545 Py_XDECREF(self->unused_data); /* Free original empty string */
546 self->unused_data = PyString_FromStringAndSize(
547 (char *)self->zst.next_in, self->zst.avail_in);
548 if (self->unused_data == NULL) {
549 Py_DECREF(RetVal);
550 goto error;
551 }
552 /* We will only get Z_BUF_ERROR if the output buffer was full
553 but there wasn't more output when we tried again, so it is
554 not an error condition.
555 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000556 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000557 zlib_error(self->zst, err, "while decompressing");
558 Py_DECREF(RetVal);
559 RetVal = NULL;
560 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000561 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000562
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000563 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000564
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000565 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000566 LEAVE_ZLIB
567
568 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000569}
570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000571PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000572"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000573"\n"
574"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000575"default value used when mode is not specified is Z_FINISH.\n"
576"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000577"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000578
Guido van Rossumfb221561997-04-29 15:38:09 +0000579static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000580PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000581{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000582 int err, length = DEFAULTALLOC;
583 PyObject *RetVal;
584 int flushmode = Z_FINISH;
585 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000586
Jeremy Hylton9714f992001-10-16 21:19:45 +0000587 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000588 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000589
Jeremy Hylton9714f992001-10-16 21:19:45 +0000590 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
591 doing any work at all; just return an empty string. */
592 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000593 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000594 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000596 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000597 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000598
599 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000600
Jeremy Hylton9714f992001-10-16 21:19:45 +0000601 start_total_out = self->zst.total_out;
602 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000603 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000604 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605
606 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000607 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000608 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000609
Jeremy Hylton9714f992001-10-16 21:19:45 +0000610 /* while Z_OK and the output buffer is full, there might be more output,
611 so extend the output buffer and try again */
612 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000613 if (_PyString_Resize(&RetVal, length << 1) < 0)
614 goto error;
615 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
616 + length;
617 self->zst.avail_out = length;
618 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000619
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000620 Py_BEGIN_ALLOW_THREADS
621 err = deflate(&(self->zst), flushmode);
622 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000623 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000624
Jeremy Hylton9714f992001-10-16 21:19:45 +0000625 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000626 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000627 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000628 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000629 err = deflateEnd(&(self->zst));
630 if (err != Z_OK) {
631 zlib_error(self->zst, err, "from deflateEnd()");
632 Py_DECREF(RetVal);
633 RetVal = NULL;
634 goto error;
635 }
636 else
637 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000638
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000639 /* We will only get Z_BUF_ERROR if the output buffer was full
640 but there wasn't more output when we tried again, so it is
641 not an error condition.
642 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000643 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000644 zlib_error(self->zst, err, "while flushing");
645 Py_DECREF(RetVal);
646 RetVal = NULL;
647 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000648 }
Tim Peters977e5402001-10-17 03:57:20 +0000649
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000650 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000651
Tim Peters977e5402001-10-17 03:57:20 +0000652 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000653 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000654
655 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000656}
657
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000658#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000659PyDoc_STRVAR(comp_copy__doc__,
660"copy() -- Return a copy of the compression object.");
661
662static PyObject *
663PyZlib_copy(compobject *self)
664{
665 compobject *retval = NULL;
666 int err;
667
668 retval = newcompobject(&Comptype);
669 if (!retval) return NULL;
670
671 /* Copy the zstream state
672 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
673 */
674 ENTER_ZLIB
675 err = deflateCopy(&retval->zst, &self->zst);
676 switch(err) {
677 case(Z_OK):
678 break;
679 case(Z_STREAM_ERROR):
680 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
681 goto error;
682 case(Z_MEM_ERROR):
683 PyErr_SetString(PyExc_MemoryError,
684 "Can't allocate memory for compression object");
685 goto error;
686 default:
687 zlib_error(self->zst, err, "while copying compression object");
688 goto error;
689 }
690
Tim Peters402cc242006-05-17 01:30:11 +0000691 Py_INCREF(self->unused_data);
692 Py_INCREF(self->unconsumed_tail);
693 Py_XDECREF(retval->unused_data);
694 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000695 retval->unused_data = self->unused_data;
696 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000697
698 /* Mark it as being initialized */
699 retval->is_initialised = 1;
700
701 LEAVE_ZLIB
702 return (PyObject *)retval;
703
704error:
705 LEAVE_ZLIB
Tim Peters402cc242006-05-17 01:30:11 +0000706 Py_XDECREF(retval);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000707 return NULL;
708}
709
710PyDoc_STRVAR(decomp_copy__doc__,
711"copy() -- Return a copy of the decompression object.");
712
713static PyObject *
714PyZlib_uncopy(compobject *self)
715{
716 compobject *retval = NULL;
717 int err;
718
719 retval = newcompobject(&Decomptype);
720 if (!retval) return NULL;
721
722 /* Copy the zstream state
723 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
724 */
725 ENTER_ZLIB
726 err = inflateCopy(&retval->zst, &self->zst);
727 switch(err) {
728 case(Z_OK):
729 break;
730 case(Z_STREAM_ERROR):
731 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
732 goto error;
733 case(Z_MEM_ERROR):
734 PyErr_SetString(PyExc_MemoryError,
735 "Can't allocate memory for decompression object");
736 goto error;
737 default:
738 zlib_error(self->zst, err, "while copying decompression object");
739 goto error;
740 }
741
Tim Peters402cc242006-05-17 01:30:11 +0000742 Py_INCREF(self->unused_data);
743 Py_INCREF(self->unconsumed_tail);
744 Py_XDECREF(retval->unused_data);
745 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000746 retval->unused_data = self->unused_data;
747 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000748
749 /* Mark it as being initialized */
750 retval->is_initialised = 1;
751
752 LEAVE_ZLIB
753 return (PyObject *)retval;
754
755error:
756 LEAVE_ZLIB
757 Py_XDECREF(retval);
758 return NULL;
759}
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000760#endif
Georg Brandl8d3342b2006-05-16 07:38:27 +0000761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000762PyDoc_STRVAR(decomp_flush__doc__,
Georg Brandl22a9dc82006-04-01 07:39:41 +0000763"flush( [length] ) -- Return a string containing any remaining\n"
764"decompressed data. length, if given, is the initial size of the\n"
765"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000766"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000767"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000768
Guido van Rossumfb221561997-04-29 15:38:09 +0000769static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000770PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000771{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000772 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000773 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000774 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000775
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000776 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000777 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000778 if (length <= 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000779 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
780 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000781 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000782 if (!(retval = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000783 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000784
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000785
Jeremy Hylton9714f992001-10-16 21:19:45 +0000786 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000787
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000788 start_total_out = self->zst.total_out;
789 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000790 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000791
792 Py_BEGIN_ALLOW_THREADS
793 err = inflate(&(self->zst), Z_FINISH);
794 Py_END_ALLOW_THREADS
795
796 /* while Z_OK and the output buffer is full, there might be more output,
797 so extend the output buffer and try again */
798 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000799 if (_PyString_Resize(&retval, length << 1) < 0)
800 goto error;
801 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
802 self->zst.avail_out = length;
803 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000804
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000805 Py_BEGIN_ALLOW_THREADS
806 err = inflate(&(self->zst), Z_FINISH);
807 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000808 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000809
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000810 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
811 various data structures. Note we should only get Z_STREAM_END when
812 flushmode is Z_FINISH */
813 if (err == Z_STREAM_END) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000814 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000815 self->is_initialised = 0;
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000816 if (err != Z_OK) {
817 zlib_error(self->zst, err, "from inflateEnd()");
818 Py_DECREF(retval);
819 retval = NULL;
820 goto error;
821 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000822 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000823 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000824
825error:
826
Jeremy Hylton9714f992001-10-16 21:19:45 +0000827 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000828
Jeremy Hylton9714f992001-10-16 21:19:45 +0000829 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000830}
831
832static PyMethodDef comp_methods[] =
833{
Tim Peters977e5402001-10-17 03:57:20 +0000834 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000835 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000836 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000837 comp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000838#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000839 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
840 comp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000841#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000842 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000843};
844
845static PyMethodDef Decomp_methods[] =
846{
Tim Peters977e5402001-10-17 03:57:20 +0000847 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000848 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000849 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000850 decomp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000851#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000852 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
853 decomp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000854#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000856};
857
858static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000859Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000860{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000861 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
862 internal data. */
863
864 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000865}
866
867static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000868Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000869{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000870 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000871
Jeremy Hylton9714f992001-10-16 21:19:45 +0000872 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000873
Tim Peters977e5402001-10-17 03:57:20 +0000874 if (strcmp(name, "unused_data") == 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000875 Py_INCREF(self->unused_data);
876 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000877 } else if (strcmp(name, "unconsumed_tail") == 0) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000878 Py_INCREF(self->unconsumed_tail);
879 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000880 } else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000881 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000882
Jeremy Hylton9714f992001-10-16 21:19:45 +0000883 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000884
Jeremy Hylton9714f992001-10-16 21:19:45 +0000885 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000886}
887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000889"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
890"\n"
891"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000892"a signed integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000893
Guido van Rossumfb221561997-04-29 15:38:09 +0000894static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000895PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000896{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000897 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000898 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000899 int len, signed_val;
Tim Peters977e5402001-10-17 03:57:20 +0000900
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000901 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000902 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000903 /* In Python 2.x we return a signed integer regardless of native platform
904 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
905 * extended into a 64-bit long inside the integer object). 3.0 does the
906 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
907 signed_val = adler32(adler32val, buf, len);
908 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000909}
Tim Peters977e5402001-10-17 03:57:20 +0000910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000911PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000912"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
913"\n"
914"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000915"a signed integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000916
917static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000918PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000919{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000920 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000921 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000922 int len, signed_val;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000923
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000924 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000925 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000926 /* In Python 2.x we return a signed integer regardless of native platform
927 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
928 * extended into a 64-bit long inside the integer object). 3.0 does the
929 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
930 signed_val = crc32(crc32val, buf, len);
931 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000932}
Tim Peters977e5402001-10-17 03:57:20 +0000933
Guido van Rossumfb221561997-04-29 15:38:09 +0000934
935static PyMethodDef zlib_methods[] =
936{
Tim Peters977e5402001-10-17 03:57:20 +0000937 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000938 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000939 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000940 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000941 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000942 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000943 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
944 crc32__doc__},
945 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000946 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000947 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000948 decompressobj__doc__},
949 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000950};
951
Tim Peters0c322792002-07-17 16:49:03 +0000952static PyTypeObject Comptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000953 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000954 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000955 sizeof(compobject),
956 0,
957 (destructor)Comp_dealloc, /*tp_dealloc*/
958 0, /*tp_print*/
959 (getattrfunc)Comp_getattr, /*tp_getattr*/
960 0, /*tp_setattr*/
961 0, /*tp_compare*/
962 0, /*tp_repr*/
963 0, /*tp_as_number*/
964 0, /*tp_as_sequence*/
965 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000966};
967
Tim Peters0c322792002-07-17 16:49:03 +0000968static PyTypeObject Decomptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000969 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000970 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000971 sizeof(compobject),
972 0,
973 (destructor)Decomp_dealloc, /*tp_dealloc*/
974 0, /*tp_print*/
975 (getattrfunc)Decomp_getattr, /*tp_getattr*/
976 0, /*tp_setattr*/
977 0, /*tp_compare*/
978 0, /*tp_repr*/
979 0, /*tp_as_number*/
980 0, /*tp_as_sequence*/
981 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000982};
983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000984PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +0000985"The functions in this module allow compression and decompression using the\n"
986"zlib library, which is based on GNU zip.\n"
987"\n"
988"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
989"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000990"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000991"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000992"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000993"decompressobj([wbits]) -- Return a decompressor object.\n"
994"\n"
995"'wbits' is window buffer size.\n"
996"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000997"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +0000998
Mark Hammond62b1ab12002-07-23 06:31:15 +0000999PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001000PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001001{
Fred Drake4baedc12002-04-01 14:53:37 +00001002 PyObject *m, *ver;
Christian Heimese93237d2007-12-19 02:37:44 +00001003 Py_TYPE(&Comptype) = &PyType_Type;
1004 Py_TYPE(&Decomptype) = &PyType_Type;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001005 m = Py_InitModule4("zlib", zlib_methods,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001006 zlib_module_documentation,
1007 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001008 if (m == NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001009 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001010
Fred Drake4baedc12002-04-01 14:53:37 +00001011 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1012 if (ZlibError != NULL) {
1013 Py_INCREF(ZlibError);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001014 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001015 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001016 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1017 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1018 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1019 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1020 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1021 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1022 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1023 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1024 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001025
Jeremy Hylton9714f992001-10-16 21:19:45 +00001026 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1027 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1028 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1029 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001030
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001031 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001032 if (ver != NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001033 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001034
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001035 PyModule_AddStringConstant(m, "__version__", "1.0");
1036
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001037#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001038 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001039#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001040}