annotate Modules/zlibmodule.c @ 39940:dfd809dcf680 2.5

Bug #1446043: correctly raise a LookupError if an encoding name given to encodings.search_function() contains a dot. (backport from rev. 52075)
author Georg Brandl <georg@python.org>
date Sat, 30 Sep 2006 11:22:35 +0000
parents 618d088d9668
children 38b1deb4e4eb 6620a510e9a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* zlibmodule.c -- gzip-compatible data compression */
20327
54f05b9b1fc4 Update URL. Fixes bug #468118.
Martin v. Löwis <martin@v.loewis.de>
parents: 19810
diff changeset
2 /* See http://www.gzip.org/zlib/ */
16368
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
3
16369
c04ca6cba1e3 Docs for new Windows zlib build procedure.
Tim Peters <tim.peters@gmail.com>
parents: 16368
diff changeset
4 /* Windows users: read Python's PCbuild\readme.txt */
16368
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
5
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
7 #include "Python.h"
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
8 #include "zlib.h"
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
10 #ifdef WITH_THREAD
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
11 #include "pythread.h"
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
12
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
13 /* #defs ripped off from _tkinter.c, even though the situation here is much
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
14 simpler, because we don't have to worry about waiting for Tcl
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
15 events! And, since zlib itself is threadsafe, we don't need to worry
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
16 about re-entering zlib functions.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
17
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
18 N.B.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
19
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
20 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
21 that modify the components of preexisting de/compress objects, it
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
22 could prove to be a performance gain on multiprocessor machines if
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
23 there was an de/compress object-specific lock. However, for the
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
24 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
25 de/compress objects.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
26 */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
27
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
28 static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
29
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
30 #define ENTER_ZLIB \
20482
8f473179b073 Removed obsolete comments about confused string refcount tricks (Jeremy
Tim Peters <tim.peters@gmail.com>
parents: 20478
diff changeset
31 Py_BEGIN_ALLOW_THREADS \
8f473179b073 Removed obsolete comments about confused string refcount tricks (Jeremy
Tim Peters <tim.peters@gmail.com>
parents: 20478
diff changeset
32 PyThread_acquire_lock(zlib_lock, 1); \
8f473179b073 Removed obsolete comments about confused string refcount tricks (Jeremy
Tim Peters <tim.peters@gmail.com>
parents: 20478
diff changeset
33 Py_END_ALLOW_THREADS
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
34
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
35 #define LEAVE_ZLIB \
20482
8f473179b073 Removed obsolete comments about confused string refcount tricks (Jeremy
Tim Peters <tim.peters@gmail.com>
parents: 20478
diff changeset
36 PyThread_release_lock(zlib_lock);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
37
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
38 #else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
39
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
40 #define ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
41 #define LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
42
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
43 #endif
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
44
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 /* The following parameters are copied from zutil.h, version 0.95 */
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 #define DEFLATED 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 #if MAX_MEM_LEVEL >= 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # define DEF_MEM_LEVEL 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 #else
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 #endif
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 #define DEF_WBITS MAX_WBITS
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
10538
ccee3c28b7ef Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents: 10467
diff changeset
54 /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
ccee3c28b7ef Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents: 10467
diff changeset
55 #define DEFAULTALLOC (16*1024)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 #define PyInit_zlib initzlib
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
58 static PyTypeObject Comptype;
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
59 static PyTypeObject Decomptype;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 static PyObject *ZlibError;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
63 typedef struct
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
65 PyObject_HEAD
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
66 z_stream zst;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
67 PyObject *unused_data;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
68 PyObject *unconsumed_tail;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
69 int is_initialised;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 } compobject;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
72 static void
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
73 zlib_error(z_stream zst, int err, char *msg)
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
74 {
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
75 if (zst.msg == Z_NULL)
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
76 PyErr_Format(ZlibError, "Error %d %s", err, msg);
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
77 else
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
78 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
79 }
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
80
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
81 PyDoc_STRVAR(compressobj__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
82 "compressobj([level]) -- Return a compressor object.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
83 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
84 "Optional arg level is the compression level, in 1-9.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
85
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
86 PyDoc_STRVAR(decompressobj__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
87 "decompressobj([wbits]) -- Return a decompressor object.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
88 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
89 "Optional arg wbits is the window buffer size.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
90
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 static compobject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
92 newcompobject(PyTypeObject *type)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
94 compobject *self;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
95 self = PyObject_New(compobject, type);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
96 if (self == NULL)
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
97 return NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
98 self->is_initialised = 0;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
99 self->unused_data = PyString_FromString("");
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
100 if (self->unused_data == NULL) {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
101 Py_DECREF(self);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
102 return NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
103 }
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
104 self->unconsumed_tail = PyString_FromString("");
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
105 if (self->unconsumed_tail == NULL) {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
106 Py_DECREF(self);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
107 return NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
108 }
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
109 return self;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
112 PyDoc_STRVAR(compress__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
113 "compress(string[, level]) -- Returned compressed string.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
114 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
115 "Optional arg level is the compression level, in 1-9.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
116
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
118 PyZlib_compress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
120 PyObject *ReturnVal = NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
121 Byte *input, *output;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
122 int length, level=Z_DEFAULT_COMPRESSION, err;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
123 z_stream zst;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
124
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
125 /* require Python string object, optional 'level' arg */
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
126 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
127 return NULL;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
128
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
129 zst.avail_out = length + length/1000 + 12 + 1;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
130
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
131 output = (Byte*)malloc(zst.avail_out);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
132 if (output == NULL) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
133 PyErr_SetString(PyExc_MemoryError,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
134 "Can't allocate memory to compress data");
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
135 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 }
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
137
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
138 /* Past the point of no return. From here on out, we need to make sure
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
139 we clean up mallocs & INCREFs. */
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
140
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
141 zst.zalloc = (alloc_func)NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
142 zst.zfree = (free_func)Z_NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
143 zst.next_out = (Byte *)output;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
144 zst.next_in = (Byte *)input;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
145 zst.avail_in = length;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
146 err = deflateInit(&zst, level);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
147
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
148 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 case(Z_OK):
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
150 break;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 case(Z_MEM_ERROR):
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
152 PyErr_SetString(PyExc_MemoryError,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
153 "Out of memory while compressing data");
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
154 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 case(Z_STREAM_ERROR):
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
156 PyErr_SetString(ZlibError,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
157 "Bad compression level");
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
158 goto error;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
159 default:
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 deflateEnd(&zst);
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
161 zlib_error(zst, err, "while compressing data");
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
162 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
164
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
165 Py_BEGIN_ALLOW_THREADS;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
166 err = deflate(&zst, Z_FINISH);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
167 Py_END_ALLOW_THREADS;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
168
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
169 if (err != Z_STREAM_END) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
170 zlib_error(zst, err, "while compressing data");
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
171 deflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
172 goto error;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
173 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
174
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
175 err=deflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
176 if (err == Z_OK)
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
177 ReturnVal = PyString_FromStringAndSize((char *)output,
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
178 zst.total_out);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
179 else
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
180 zlib_error(zst, err, "while finishing compression");
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
181
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
182 error:
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
183 free(output);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
184
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
185 return ReturnVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
188 PyDoc_STRVAR(decompress__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
189 "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
190 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
191 "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
192 "the initial output buffer size.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
193
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
195 PyZlib_decompress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
197 PyObject *result_str;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
198 Byte *input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
199 int length, err;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
200 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
201 z_stream zst;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
202
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
203 if (!PyArg_ParseTuple(args, "s#|ii:decompress",
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
204 &input, &length, &wsize, &r_strlen))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
205 return NULL;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
206
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
207 if (r_strlen <= 0)
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
208 r_strlen = 1;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
209
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
210 zst.avail_in = length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
211 zst.avail_out = r_strlen;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
212
20470
8964ef84c92f Remove many calls to set MemoryError exceptions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20469
diff changeset
213 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
214 return NULL;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
215
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
216 zst.zalloc = (alloc_func)NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
217 zst.zfree = (free_func)Z_NULL;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
218 zst.next_out = (Byte *)PyString_AS_STRING(result_str);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
219 zst.next_in = (Byte *)input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
220 err = inflateInit2(&zst, wsize);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
221
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
222 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 case(Z_OK):
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
224 break;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
225 case(Z_MEM_ERROR):
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
226 PyErr_SetString(PyExc_MemoryError,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
227 "Out of memory while decompressing data");
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
228 goto error;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
229 default:
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 inflateEnd(&zst);
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
231 zlib_error(zst, err, "while preparing to decompress data");
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
232 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
234
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
235 do {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
236 Py_BEGIN_ALLOW_THREADS
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
237 err=inflate(&zst, Z_FINISH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
238 Py_END_ALLOW_THREADS
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
239
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
240 switch(err) {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
241 case(Z_STREAM_END):
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
242 break;
8128
fdba6397b729 Tiny patch for zlib 1.1.2
Guido van Rossum <guido@python.org>
parents: 6911
diff changeset
243 case(Z_BUF_ERROR):
15098
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
244 /*
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
245 * If there is at least 1 byte of room according to zst.avail_out
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
246 * and we get this error, assume that it means zlib cannot
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
247 * process the inflate call() due to an error in the data.
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
248 */
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
249 if (zst.avail_out > 0) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
250 PyErr_Format(ZlibError, "Error %i while decompressing data",
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
251 err);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
252 inflateEnd(&zst);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
253 goto error;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
254 }
15098
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
255 /* fall through */
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
256 case(Z_OK):
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
257 /* need more memory */
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
258 if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
259 inflateEnd(&zst);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
260 goto error;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
261 }
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
262 zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
263 + r_strlen;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
264 zst.avail_out = r_strlen;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
265 r_strlen = r_strlen << 1;
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
266 break;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
267 default:
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
268 inflateEnd(&zst);
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
269 zlib_error(zst, err, "while decompressing data");
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
270 goto error;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
271 }
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
272 } while (err != Z_STREAM_END);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
273
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
274 err = inflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
275 if (err != Z_OK) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
276 zlib_error(zst, err, "while finishing data decompression");
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
277 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
279
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
280 _PyString_Resize(&result_str, zst.total_out);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
281 return result_str;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
282
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
283 error:
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
284 Py_XDECREF(result_str);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
285 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
289 PyZlib_compressobj(PyObject *selfptr, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 {
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
291 compobject *self;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
292 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
293 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
294
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
295 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
296 &memLevel, &strategy))
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
297 return NULL;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
298
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
299 self = newcompobject(&Comptype);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
300 if (self==NULL)
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
301 return(NULL);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
302 self->zst.zalloc = (alloc_func)NULL;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
303 self->zst.zfree = (free_func)Z_NULL;
34264
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
304 self->zst.next_in = NULL;
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
305 self->zst.avail_in = 0;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
306 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
307 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 case (Z_OK):
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
309 self->is_initialised = 1;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
310 return (PyObject*)self;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 case (Z_MEM_ERROR):
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
312 Py_DECREF(self);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
313 PyErr_SetString(PyExc_MemoryError,
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
314 "Can't allocate memory for compression object");
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
315 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 case(Z_STREAM_ERROR):
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
317 Py_DECREF(self);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
318 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
319 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 default:
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
321 zlib_error(self->zst, err, "while creating compression object");
10028
167adf31786d Added missing DECREF's in the error branches when creating a compressor or
Andrew M. Kuchling <amk@amk.ca>
parents: 9995
diff changeset
322 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
328 PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 {
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
330 int wbits=DEF_WBITS, err;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
331 compobject *self;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
332 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
333 return NULL;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
334
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
335 self = newcompobject(&Decomptype);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
336 if (self == NULL)
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
337 return(NULL);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
338 self->zst.zalloc = (alloc_func)NULL;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
339 self->zst.zfree = (free_func)Z_NULL;
34264
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
340 self->zst.next_in = NULL;
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
341 self->zst.avail_in = 0;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
342 err = inflateInit2(&self->zst, wbits);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
343 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 case (Z_OK):
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
345 self->is_initialised = 1;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
346 return (PyObject*)self;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
347 case(Z_STREAM_ERROR):
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
348 Py_DECREF(self);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
349 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
350 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 case (Z_MEM_ERROR):
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
352 Py_DECREF(self);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
353 PyErr_SetString(PyExc_MemoryError,
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
354 "Can't allocate memory for decompression object");
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
355 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 default:
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
357 zlib_error(self->zst, err, "while creating decompression object");
10028
167adf31786d Added missing DECREF's in the error branches when creating a compressor or
Andrew M. Kuchling <amk@amk.ca>
parents: 9995
diff changeset
358 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 return NULL;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
360 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
364 Comp_dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 {
10028
167adf31786d Added missing DECREF's in the error branches when creating a compressor or
Andrew M. Kuchling <amk@amk.ca>
parents: 9995
diff changeset
366 if (self->is_initialised)
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
367 deflateEnd(&self->zst);
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
368 Py_XDECREF(self->unused_data);
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
369 Py_XDECREF(self->unconsumed_tail);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
370 PyObject_Del(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
374 Decomp_dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
376 if (self->is_initialised)
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
377 inflateEnd(&self->zst);
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
378 Py_XDECREF(self->unused_data);
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
379 Py_XDECREF(self->unconsumed_tail);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
380 PyObject_Del(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
383 PyDoc_STRVAR(comp_compress__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
384 "compress(data) -- Return a string containing data compressed.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
385 "\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
386 "After calling this function, some of the input data may still\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
387 "be stored in internal buffers for later processing.\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
388 "Call the flush() method to clear these buffers.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
389
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
390
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
392 PyZlib_objcompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
394 int err, inplen, length = DEFAULTALLOC;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
395 PyObject *RetVal;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
396 Byte *input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
397 unsigned long start_total_out;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
398
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
399 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
400 return NULL;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
401
20470
8964ef84c92f Remove many calls to set MemoryError exceptions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20469
diff changeset
402 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
403 return NULL;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
404
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
405 ENTER_ZLIB
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
406
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
407 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
408 self->zst.avail_in = inplen;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
409 self->zst.next_in = input;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
410 self->zst.avail_out = length;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
411 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
412
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
413 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
414 err = deflate(&(self->zst), Z_NO_FLUSH);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
415 Py_END_ALLOW_THREADS
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
416
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
417 /* while Z_OK and the output buffer is full, there might be more output,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
418 so extend the output buffer and try again */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
419 while (err == Z_OK && self->zst.avail_out == 0) {
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
420 if (_PyString_Resize(&RetVal, length << 1) < 0)
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
421 goto error;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
422 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
423 + length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
424 self->zst.avail_out = length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
425 length = length << 1;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
426
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
427 Py_BEGIN_ALLOW_THREADS
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
428 err = deflate(&(self->zst), Z_NO_FLUSH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
429 Py_END_ALLOW_THREADS
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
430 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
431 /* We will only get Z_BUF_ERROR if the output buffer was full but
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
432 there wasn't more output when we tried again, so it is not an error
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
433 condition.
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
434 */
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
435
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
436 if (err != Z_OK && err != Z_BUF_ERROR) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
437 zlib_error(self->zst, err, "while compressing");
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
438 Py_DECREF(RetVal);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
439 RetVal = NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
440 goto error;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
441 }
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
442 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
443
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
444 error:
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
445 LEAVE_ZLIB
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
446 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
449 PyDoc_STRVAR(decomp_decompress__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
450 "decompress(data, max_length) -- Return a string containing the decompressed\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
451 "version of the data.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
452 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
453 "After calling this function, some of the input data may still be stored in\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
454 "internal buffers for later processing.\n"
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
455 "Call the flush() method to clear these buffers.\n"
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
456 "If the max_length parameter is specified then the return value will be\n"
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
457 "no longer than max_length. Unconsumed input data will be stored in\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
458 "the unconsumed_tail attribute.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
459
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
461 PyZlib_objdecompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
463 int err, inplen, old_length, length = DEFAULTALLOC;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
464 int max_length = 0;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
465 PyObject *RetVal;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
466 Byte *input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
467 unsigned long start_total_out;
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
468
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
469 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
470 &inplen, &max_length))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
471 return NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
472 if (max_length < 0) {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
473 PyErr_SetString(PyExc_ValueError,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
474 "max_length must be greater than zero");
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
475 return NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
476 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
477
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
478 /* limit amount of data allocated to max_length */
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
479 if (max_length && length > max_length)
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
480 length = max_length;
20470
8964ef84c92f Remove many calls to set MemoryError exceptions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20469
diff changeset
481 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
482 return NULL;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
483
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
484 ENTER_ZLIB
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
485
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
486 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
487 self->zst.avail_in = inplen;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
488 self->zst.next_in = input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
489 self->zst.avail_out = length;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
490 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
491
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
492 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
493 err = inflate(&(self->zst), Z_SYNC_FLUSH);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
494 Py_END_ALLOW_THREADS
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
495
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
496 /* While Z_OK and the output buffer is full, there might be more output.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
497 So extend the output buffer and try again.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
498 */
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
499 while (err == Z_OK && self->zst.avail_out == 0) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
500 /* If max_length set, don't continue decompressing if we've already
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
501 reached the limit.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
502 */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
503 if (max_length && length >= max_length)
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
504 break;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
505
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
506 /* otherwise, ... */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
507 old_length = length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
508 length = length << 1;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
509 if (max_length && length > max_length)
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
510 length = max_length;
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
511
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
512 if (_PyString_Resize(&RetVal, length) < 0)
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
513 goto error;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
514 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
515 + old_length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
516 self->zst.avail_out = length - old_length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
517
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
518 Py_BEGIN_ALLOW_THREADS
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
519 err = inflate(&(self->zst), Z_SYNC_FLUSH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
520 Py_END_ALLOW_THREADS
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
521 }
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
522
34264
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
523 /* Not all of the compressed data could be accommodated in the output buffer
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
524 of specified size. Return the unconsumed tail in an attribute.*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
525 if(max_length) {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
526 Py_DECREF(self->unconsumed_tail);
20644
7c41c4fbf9c7 Added missing cast.
Jack Jansen <jack.jansen@cwi.nl>
parents: 20493
diff changeset
527 self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
528 self->zst.avail_in);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
529 if(!self->unconsumed_tail) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
530 Py_DECREF(RetVal);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
531 RetVal = NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
532 goto error;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
533 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
535
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
536 /* The end of the compressed data has been reached, so set the
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
537 unused_data attribute to a string containing the remainder of the
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
538 data in the string. Note that this is also a logical place to call
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
539 inflateEnd, but the old behaviour of only calling it on flush() is
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
540 preserved.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
541 */
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
542 if (err == Z_STREAM_END) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
543 Py_XDECREF(self->unused_data); /* Free original empty string */
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
544 self->unused_data = PyString_FromStringAndSize(
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
545 (char *)self->zst.next_in, self->zst.avail_in);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
546 if (self->unused_data == NULL) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
547 Py_DECREF(RetVal);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
548 goto error;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
549 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
550 /* We will only get Z_BUF_ERROR if the output buffer was full
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
551 but there wasn't more output when we tried again, so it is
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
552 not an error condition.
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
553 */
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
554 } else if (err != Z_OK && err != Z_BUF_ERROR) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
555 zlib_error(self->zst, err, "while decompressing");
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
556 Py_DECREF(RetVal);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
557 RetVal = NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
558 goto error;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
559 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
560
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
561 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
562
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
563 error:
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
564 LEAVE_ZLIB
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
565
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
566 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
569 PyDoc_STRVAR(comp_flush__doc__,
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
570 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
571 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
572 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
573 "default value used when mode is not specified is Z_FINISH.\n"
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
574 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
575 "calling the flush() method. Otherwise, more data can still be compressed.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
576
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
578 PyZlib_flush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
580 int err, length = DEFAULTALLOC;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
581 PyObject *RetVal;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
582 int flushmode = Z_FINISH;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
583 unsigned long start_total_out;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
584
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
585 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
586 return NULL;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
587
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
588 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
589 doing any work at all; just return an empty string. */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
590 if (flushmode == Z_NO_FLUSH) {
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
591 return PyString_FromStringAndSize(NULL, 0);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
592 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
593
20470
8964ef84c92f Remove many calls to set MemoryError exceptions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20469
diff changeset
594 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
595 return NULL;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
596
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
597 ENTER_ZLIB
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
598
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
599 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
600 self->zst.avail_in = 0;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
601 self->zst.avail_out = length;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
602 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
603
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
604 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
605 err = deflate(&(self->zst), flushmode);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
606 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
607
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
608 /* while Z_OK and the output buffer is full, there might be more output,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
609 so extend the output buffer and try again */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
610 while (err == Z_OK && self->zst.avail_out == 0) {
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
611 if (_PyString_Resize(&RetVal, length << 1) < 0)
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
612 goto error;
20478
071cfee8d8eb Undo needless INCREF chicanery introduced by SF patch #450702.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20477
diff changeset
613 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
614 + length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
615 self->zst.avail_out = length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
616 length = length << 1;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
617
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
618 Py_BEGIN_ALLOW_THREADS
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
619 err = deflate(&(self->zst), flushmode);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
620 Py_END_ALLOW_THREADS
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
621 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
622
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
623 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
624 various data structures. Note we should only get Z_STREAM_END when
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
625 flushmode is Z_FINISH, but checking both for safety*/
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
626 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
627 err = deflateEnd(&(self->zst));
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
628 if (err != Z_OK) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
629 zlib_error(self->zst, err, "from deflateEnd()");
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
630 Py_DECREF(RetVal);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
631 RetVal = NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
632 goto error;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
633 }
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
634 else
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
635 self->is_initialised = 0;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
636
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
637 /* We will only get Z_BUF_ERROR if the output buffer was full
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
638 but there wasn't more output when we tried again, so it is
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
639 not an error condition.
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
640 */
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
641 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
642 zlib_error(self->zst, err, "while flushing");
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
643 Py_DECREF(RetVal);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
644 RetVal = NULL;
23169
713ff889d094 Fix SF #544995 (zlib crash on second flush call)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22729
diff changeset
645 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
646 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
647
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23169
diff changeset
648 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
649
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
650 error:
20483
5d678356c894 Removed more comments that didn't make much sense.
Tim Peters <tim.peters@gmail.com>
parents: 20482
diff changeset
651 LEAVE_ZLIB
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
652
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
653 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
655
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38198
diff changeset
656 #ifdef HAVE_ZLIB_COPY
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
657 PyDoc_STRVAR(comp_copy__doc__,
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
658 "copy() -- Return a copy of the compression object.");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
659
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
660 static PyObject *
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
661 PyZlib_copy(compobject *self)
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
662 {
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
663 compobject *retval = NULL;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
664 int err;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
665
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
666 retval = newcompobject(&Comptype);
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
667 if (!retval) return NULL;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
668
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
669 /* Copy the zstream state
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
670 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
671 */
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
672 ENTER_ZLIB
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
673 err = deflateCopy(&retval->zst, &self->zst);
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
674 switch(err) {
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
675 case(Z_OK):
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
676 break;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
677 case(Z_STREAM_ERROR):
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
678 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
679 goto error;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
680 case(Z_MEM_ERROR):
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
681 PyErr_SetString(PyExc_MemoryError,
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
682 "Can't allocate memory for compression object");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
683 goto error;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
684 default:
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
685 zlib_error(self->zst, err, "while copying compression object");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
686 goto error;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
687 }
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
688
38198
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
689 Py_INCREF(self->unused_data);
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
690 Py_INCREF(self->unconsumed_tail);
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
691 Py_XDECREF(retval->unused_data);
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
692 Py_XDECREF(retval->unconsumed_tail);
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
693 retval->unused_data = self->unused_data;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
694 retval->unconsumed_tail = self->unconsumed_tail;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
695
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
696 /* Mark it as being initialized */
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
697 retval->is_initialised = 1;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
698
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
699 LEAVE_ZLIB
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
700 return (PyObject *)retval;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
701
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
702 error:
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
703 LEAVE_ZLIB
38198
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
704 Py_XDECREF(retval);
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
705 return NULL;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
706 }
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
707
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
708 PyDoc_STRVAR(decomp_copy__doc__,
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
709 "copy() -- Return a copy of the decompression object.");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
710
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
711 static PyObject *
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
712 PyZlib_uncopy(compobject *self)
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
713 {
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
714 compobject *retval = NULL;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
715 int err;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
716
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
717 retval = newcompobject(&Decomptype);
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
718 if (!retval) return NULL;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
719
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
720 /* Copy the zstream state
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
721 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
722 */
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
723 ENTER_ZLIB
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
724 err = inflateCopy(&retval->zst, &self->zst);
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
725 switch(err) {
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
726 case(Z_OK):
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
727 break;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
728 case(Z_STREAM_ERROR):
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
729 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
730 goto error;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
731 case(Z_MEM_ERROR):
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
732 PyErr_SetString(PyExc_MemoryError,
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
733 "Can't allocate memory for decompression object");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
734 goto error;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
735 default:
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
736 zlib_error(self->zst, err, "while copying decompression object");
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
737 goto error;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
738 }
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
739
38198
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
740 Py_INCREF(self->unused_data);
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
741 Py_INCREF(self->unconsumed_tail);
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
742 Py_XDECREF(retval->unused_data);
30188c523bc5 PyZlib_copy(), PyZlib_uncopy(): Repair leaks on the normal-case path.
Tim Peters <tim.peters@gmail.com>
parents: 38190
diff changeset
743 Py_XDECREF(retval->unconsumed_tail);
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
744 retval->unused_data = self->unused_data;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
745 retval->unconsumed_tail = self->unconsumed_tail;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
746
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
747 /* Mark it as being initialized */
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
748 retval->is_initialised = 1;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
749
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
750 LEAVE_ZLIB
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
751 return (PyObject *)retval;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
752
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
753 error:
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
754 LEAVE_ZLIB
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
755 Py_XDECREF(retval);
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
756 return NULL;
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
757 }
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38198
diff changeset
758 #endif
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
759
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
760 PyDoc_STRVAR(decomp_flush__doc__,
37373
f64812b2b5c1 Patch #1459631: documnent zlib.Decompress.flush() length parameter.
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
761 "flush( [length] ) -- Return a string containing any remaining\n"
f64812b2b5c1 Patch #1459631: documnent zlib.Decompress.flush() length parameter.
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
762 "decompressed data. length, if given, is the initial size of the\n"
f64812b2b5c1 Patch #1459631: documnent zlib.Decompress.flush() length parameter.
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
763 "output buffer.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
764 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
765 "The decompressor object can no longer be used after this call.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
766
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
768 PyZlib_unflush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 {
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
770 int err, length = DEFAULTALLOC;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
771 PyObject * retval = NULL;
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
772 unsigned long start_total_out;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
773
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
774 if (!PyArg_ParseTuple(args, "|i:flush", &length))
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
775 return NULL;
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
776 if (!(retval = PyString_FromStringAndSize(NULL, length)))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
777 return NULL;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
778
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
779
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
780 ENTER_ZLIB
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
781
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
782 start_total_out = self->zst.total_out;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
783 self->zst.avail_out = length;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
784 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
785
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
786 Py_BEGIN_ALLOW_THREADS
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
787 err = inflate(&(self->zst), Z_FINISH);
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
788 Py_END_ALLOW_THREADS
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
789
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
790 /* while Z_OK and the output buffer is full, there might be more output,
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
791 so extend the output buffer and try again */
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
792 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
793 if (_PyString_Resize(&retval, length << 1) < 0)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
794 goto error;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
795 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
796 self->zst.avail_out = length;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
797 length = length << 1;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
798
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
799 Py_BEGIN_ALLOW_THREADS
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
800 err = inflate(&(self->zst), Z_FINISH);
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
801 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
802 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
803
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
804 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
805 various data structures. Note we should only get Z_STREAM_END when
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
806 flushmode is Z_FINISH */
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
807 if (err == Z_STREAM_END) {
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
808 err = inflateEnd(&(self->zst));
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
809 self->is_initialised = 0;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
810 if (err != Z_OK) {
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
811 zlib_error(self->zst, err, "from inflateEnd()");
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
812 Py_DECREF(retval);
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
813 retval = NULL;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
814 goto error;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
815 }
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
816 }
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
817 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
818
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
819 error:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
820
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
821 LEAVE_ZLIB
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
822
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
823 return retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
824 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
825
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 static PyMethodDef comp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
828 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
829 comp_compress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
830 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
831 comp_flush__doc__},
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38198
diff changeset
832 #ifdef HAVE_ZLIB_COPY
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
833 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
834 comp_copy__doc__},
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38198
diff changeset
835 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
836 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
838
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 static PyMethodDef Decomp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
841 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
842 decomp_decompress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
843 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
844 decomp_flush__doc__},
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38198
diff changeset
845 #ifdef HAVE_ZLIB_COPY
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
846 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 37373
diff changeset
847 decomp_copy__doc__},
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38198
diff changeset
848 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
849 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
851
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
853 Comp_getattr(compobject *self, char *name)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 {
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
855 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
856 internal data. */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
857
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
858 return Py_FindMethod(comp_methods, (PyObject *)self, name);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
860
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
862 Decomp_getattr(compobject *self, char *name)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
864 PyObject * retval;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
865
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
866 ENTER_ZLIB
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
867
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
868 if (strcmp(name, "unused_data") == 0) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
869 Py_INCREF(self->unused_data);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
870 retval = self->unused_data;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
871 } else if (strcmp(name, "unconsumed_tail") == 0) {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
872 Py_INCREF(self->unconsumed_tail);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
873 retval = self->unconsumed_tail;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
874 } else
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
875 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
876
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
877 LEAVE_ZLIB
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
878
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
879 return retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
881
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
882 PyDoc_STRVAR(adler32__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
883 "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
884 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
885 "An optional starting value can be specified. The returned checksum is\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
886 "an integer.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
887
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
889 PyZlib_adler32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
890 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
891 uLong adler32val = adler32(0L, Z_NULL, 0);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
892 Byte *buf;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
893 int len;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
894
35914
3fd97de0a790 [Patch #1350573] zlib.crc32 doesn't handle 0xffffffff seed. Add tests and bugfix. Bug reported by John Schmidt; bugfix by Danny Yoo.
Andrew M. Kuchling <amk@amk.ca>
parents: 34264
diff changeset
895 if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val))
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
896 return NULL;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
897 adler32val = adler32(adler32val, buf, len);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
898 return PyInt_FromLong(adler32val);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
900
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
901 PyDoc_STRVAR(crc32__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
902 "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
903 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
904 "An optional starting value can be specified. The returned checksum is\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
905 "an integer.");
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
906
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
907 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
908 PyZlib_crc32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
909 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
910 uLong crc32val = crc32(0L, Z_NULL, 0);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
911 Byte *buf;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
912 int len;
35914
3fd97de0a790 [Patch #1350573] zlib.crc32 doesn't handle 0xffffffff seed. Add tests and bugfix. Bug reported by John Schmidt; bugfix by Danny Yoo.
Andrew M. Kuchling <amk@amk.ca>
parents: 34264
diff changeset
913 if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val))
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
914 return NULL;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
915 crc32val = crc32(crc32val, buf, len);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
916 return PyInt_FromLong(crc32val);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
918
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
919
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
920 static PyMethodDef zlib_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
922 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
923 adler32__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
924 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
925 compress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
926 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
927 compressobj__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
928 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
929 crc32__doc__},
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
930 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
931 decompress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
932 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
933 decompressobj__doc__},
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
934 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
935 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
936
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
937 static PyTypeObject Comptype = {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
938 PyObject_HEAD_INIT(0)
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
939 0,
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 20644
diff changeset
940 "zlib.Compress",
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
941 sizeof(compobject),
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
942 0,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
943 (destructor)Comp_dealloc, /*tp_dealloc*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
944 0, /*tp_print*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
945 (getattrfunc)Comp_getattr, /*tp_getattr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
946 0, /*tp_setattr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
947 0, /*tp_compare*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
948 0, /*tp_repr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
949 0, /*tp_as_number*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
950 0, /*tp_as_sequence*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
951 0, /*tp_as_mapping*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
953
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
954 static PyTypeObject Decomptype = {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
955 PyObject_HEAD_INIT(0)
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
956 0,
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 20644
diff changeset
957 "zlib.Decompress",
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
958 sizeof(compobject),
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
959 0,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
960 (destructor)Decomp_dealloc, /*tp_dealloc*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
961 0, /*tp_print*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
962 (getattrfunc)Decomp_getattr, /*tp_getattr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
963 0, /*tp_setattr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
964 0, /*tp_compare*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
965 0, /*tp_repr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
966 0, /*tp_as_number*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
967 0, /*tp_as_sequence*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
968 0, /*tp_as_mapping*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
970
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
971 PyDoc_STRVAR(zlib_module_documentation,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
972 "The functions in this module allow compression and decompression using the\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
973 "zlib library, which is based on GNU zip.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
974 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
975 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
976 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
977 "compressobj([level]) -- Return a compressor object.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
978 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
11607
69216c11a1bd Fix typo in docstring: wbites -> wbits
Andrew M. Kuchling <amk@amk.ca>
parents: 10571
diff changeset
979 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
980 "decompressobj([wbits]) -- Return a decompressor object.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
981 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
982 "'wbits' is window buffer size.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
983 "Compressor objects support compress() and flush() methods; decompressor\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
984 "objects support decompress() and flush().");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
985
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24283
diff changeset
986 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13496
diff changeset
987 PyInit_zlib(void)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 {
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
989 PyObject *m, *ver;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
990 Comptype.ob_type = &PyType_Type;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
991 Decomptype.ob_type = &PyType_Type;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
992 m = Py_InitModule4("zlib", zlib_methods,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
993 zlib_module_documentation,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
994 (PyObject*)NULL,PYTHON_API_VERSION);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35914
diff changeset
995 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35914
diff changeset
996 return;
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
997
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
998 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
999 if (ZlibError != NULL) {
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1000 Py_INCREF(ZlibError);
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1001 PyModule_AddObject(m, "error", ZlibError);
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1002 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1003 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1004 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1005 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1006 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1007 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1008 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1009 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1010 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1011 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1012
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1013 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1014 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1015 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1016 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1017
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1018 ver = PyString_FromString(ZLIB_VERSION);
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1019 if (ver != NULL)
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1020 PyModule_AddObject(m, "ZLIB_VERSION", ver);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1021
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1022 PyModule_AddStringConstant(m, "__version__", "1.0");
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1023
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1024 #ifdef WITH_THREAD
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1025 zlib_lock = PyThread_allocate_lock();
22217
da518fc78066 Changed C++ comment into standard comment.
Sjoerd Mullender <sjoerd@acm.org>
parents: 21313
diff changeset
1026 #endif /* WITH_THREAD */
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 }