Mercurial > cpython-lru_cache-weakref
annotate Modules/zlibmodule.c @ 60008:0dfd62076ded
Blocked revisions 79402,79406 via svnmerge
........
r79402 | benjamin.peterson | 2010-03-24 20:03:51 -0500 (Wed, 24 Mar 2010) | 1 line
set bsddb eol style
........
r79406 | benjamin.peterson | 2010-03-24 20:05:57 -0500 (Wed, 24 Mar 2010) | 1 line
fix eol properties on capsule files
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Thu, 25 Mar 2010 01:11:10 +0000 |
| parents | 919e2142726e |
| children | bcb0cf924693 |
| 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" |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
8 #include "structmember.h" |
|
5723
beee351f1f72
Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents:
5722
diff
changeset
|
9 #include "zlib.h" |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
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
|
11 #ifdef WITH_THREAD |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
12 #include "pythread.h" |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
13 #define ENTER_ZLIB(obj) \ |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
14 Py_BEGIN_ALLOW_THREADS; \ |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
15 PyThread_acquire_lock((obj)->lock, 1); \ |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
16 Py_END_ALLOW_THREADS; |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
17 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->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
|
18 #else |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
19 #define ENTER_ZLIB(obj) |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
20 #define LEAVE_ZLIB(obj) |
|
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
|
21 #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
|
22 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 /* 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
|
24 #define DEFLATED 8 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 #if MAX_MEM_LEVEL >= 8 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 # define DEF_MEM_LEVEL 8 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 #else |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 # define DEF_MEM_LEVEL MAX_MEM_LEVEL |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 #endif |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 #define DEF_WBITS MAX_WBITS |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
10538
ccee3c28b7ef
Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents:
10467
diff
changeset
|
32 /* 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
|
33 #define DEFAULTALLOC (16*1024) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
35 static PyTypeObject Comptype; |
|
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
36 static PyTypeObject Decomptype; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 static PyObject *ZlibError; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
40 typedef struct |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 { |
| 20469 | 42 PyObject_HEAD |
| 43 z_stream zst; | |
| 44 PyObject *unused_data; | |
| 45 PyObject *unconsumed_tail; | |
| 46 int is_initialised; | |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
47 #ifdef WITH_THREAD |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
48 PyThread_type_lock lock; |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
49 #endif |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 } compobject; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
52 static void |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
53 zlib_error(z_stream zst, int err, char *msg) |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
54 { |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
55 if (zst.msg == Z_NULL) |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
56 PyErr_Format(ZlibError, "Error %d %s", err, msg); |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
57 else |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
58 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
|
59 } |
|
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
60 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
61 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
|
62 "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
|
63 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
64 "Optional arg level is the compression level, in 1-9."); |
| 5722 | 65 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
66 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
|
67 "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
|
68 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
69 "Optional arg wbits is the window buffer size."); |
| 5722 | 70 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 static compobject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
72 newcompobject(PyTypeObject *type) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 { |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
74 compobject *self; |
| 20469 | 75 self = PyObject_New(compobject, type); |
| 76 if (self == NULL) | |
| 77 return NULL; | |
| 78 self->is_initialised = 0; | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
79 self->unused_data = PyBytes_FromStringAndSize("", 0); |
| 20469 | 80 if (self->unused_data == NULL) { |
| 81 Py_DECREF(self); | |
| 82 return NULL; | |
| 83 } | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
84 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0); |
| 20469 | 85 if (self->unconsumed_tail == NULL) { |
| 86 Py_DECREF(self); | |
| 87 return NULL; | |
| 88 } | |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
89 #ifdef WITH_THREAD |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
90 self->lock = PyThread_allocate_lock(); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
91 #endif |
| 20469 | 92 return self; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
95 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
|
96 "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
|
97 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
98 "Optional arg level is the compression level, in 1-9."); |
| 5722 | 99 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
101 PyZlib_compress(PyObject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 { |
| 20469 | 103 PyObject *ReturnVal = NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
104 Py_buffer pinput; |
| 20469 | 105 Byte *input, *output; |
| 106 int length, level=Z_DEFAULT_COMPRESSION, err; | |
| 107 z_stream zst; | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
108 |
| 20469 | 109 /* require Python string object, optional 'level' arg */ |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
110 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level)) |
| 20469 | 111 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
112 input = pinput.buf; |
|
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
113 length = pinput.len; |
|
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
|
114 |
| 20469 | 115 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
|
116 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
117 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
|
118 if (output == NULL) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
119 PyBuffer_Release(&pinput); |
| 20469 | 120 PyErr_SetString(PyExc_MemoryError, |
| 121 "Can't allocate memory to compress data"); | |
| 122 return NULL; | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 } |
| 9657 | 124 |
| 20469 | 125 /* Past the point of no return. From here on out, we need to make sure |
| 126 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
|
127 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
128 zst.zalloc = (alloc_func)NULL; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
129 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
|
130 zst.next_out = (Byte *)output; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
131 zst.next_in = (Byte *)input; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
132 zst.avail_in = length; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
133 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
|
134 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
135 switch(err) { |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 case(Z_OK): |
| 20469 | 137 break; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 case(Z_MEM_ERROR): |
| 20469 | 139 PyErr_SetString(PyExc_MemoryError, |
| 140 "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
|
141 goto error; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 case(Z_STREAM_ERROR): |
| 20469 | 143 PyErr_SetString(ZlibError, |
| 144 "Bad compression level"); | |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
145 goto error; |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
146 default: |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 deflateEnd(&zst); |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
148 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
|
149 goto error; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 } |
|
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
|
151 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
152 Py_BEGIN_ALLOW_THREADS; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
153 err = deflate(&zst, Z_FINISH); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
154 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
|
155 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
156 if (err != Z_STREAM_END) { |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
157 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
|
158 deflateEnd(&zst); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
159 goto error; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
160 } |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
161 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
162 err=deflateEnd(&zst); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
163 if (err == Z_OK) |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
164 ReturnVal = PyBytes_FromStringAndSize((char *)output, |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
165 zst.total_out); |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
166 else |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
167 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
|
168 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
169 error: |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
170 PyBuffer_Release(&pinput); |
| 20469 | 171 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
|
172 |
| 20469 | 173 return ReturnVal; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
176 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
|
177 "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
|
178 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
179 "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
|
180 "the initial output buffer size."); |
| 5722 | 181 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
183 PyZlib_decompress(PyObject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 { |
| 20469 | 185 PyObject *result_str; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
186 Py_buffer pinput; |
| 20469 | 187 Byte *input; |
| 188 int length, err; | |
|
43957
23befe540d0d
Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42750
diff
changeset
|
189 int wsize=DEF_WBITS; |
|
23befe540d0d
Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42750
diff
changeset
|
190 Py_ssize_t r_strlen=DEFAULTALLOC; |
| 20469 | 191 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
|
192 |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
193 if (!PyArg_ParseTuple(args, "y*|in:decompress", |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
194 &pinput, &wsize, &r_strlen)) |
| 20469 | 195 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
196 input = pinput.buf; |
|
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
197 length = pinput.len; |
| 6236 | 198 |
| 20469 | 199 if (r_strlen <= 0) |
| 200 r_strlen = 1; | |
| 9657 | 201 |
| 20469 | 202 zst.avail_in = length; |
| 203 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
|
204 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
205 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
206 PyBuffer_Release(&pinput); |
| 20469 | 207 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
208 } |
|
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
|
209 |
| 20469 | 210 zst.zalloc = (alloc_func)NULL; |
| 211 zst.zfree = (free_func)Z_NULL; | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
212 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str); |
| 20469 | 213 zst.next_in = (Byte *)input; |
| 214 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
|
215 |
| 20469 | 216 switch(err) { |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 case(Z_OK): |
| 20469 | 218 break; |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
219 case(Z_MEM_ERROR): |
| 20469 | 220 PyErr_SetString(PyExc_MemoryError, |
| 221 "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
|
222 goto error; |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
223 default: |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 inflateEnd(&zst); |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
225 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
|
226 goto error; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 } |
|
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
|
228 |
| 20469 | 229 do { |
| 230 Py_BEGIN_ALLOW_THREADS | |
| 231 err=inflate(&zst, Z_FINISH); | |
| 232 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
|
233 |
| 20469 | 234 switch(err) { |
| 235 case(Z_STREAM_END): | |
| 6007 | 236 break; |
|
8128
fdba6397b729
Tiny patch for zlib 1.1.2
Guido van Rossum <guido@python.org>
parents:
6911
diff
changeset
|
237 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
|
238 /* |
|
7de0808f1fe3
Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents:
13816
diff
changeset
|
239 * 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
|
240 * 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
|
241 * 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
|
242 */ |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
243 if (zst.avail_out > 0) { |
| 20469 | 244 PyErr_Format(ZlibError, "Error %i while decompressing data", |
| 245 err); | |
| 246 inflateEnd(&zst); | |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
247 goto error; |
| 20469 | 248 } |
|
15098
7de0808f1fe3
Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents:
13816
diff
changeset
|
249 /* 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
|
250 case(Z_OK): |
| 6007 | 251 /* need more memory */ |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
252 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) { |
| 20469 | 253 inflateEnd(&zst); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
254 goto error; |
| 20469 | 255 } |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
256 zst.next_out = |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
257 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen; |
| 20469 | 258 zst.avail_out = r_strlen; |
| 6007 | 259 r_strlen = r_strlen << 1; |
| 260 break; | |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
261 default: |
| 20469 | 262 inflateEnd(&zst); |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
263 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
|
264 goto error; |
| 20469 | 265 } |
| 266 } 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
|
267 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
268 err = inflateEnd(&zst); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
269 if (err != Z_OK) { |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
270 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
|
271 goto error; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 } |
|
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 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
274 if (_PyBytes_Resize(&result_str, zst.total_out) < 0) |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
275 goto error; |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
276 |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
277 PyBuffer_Release(&pinput); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
278 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
|
279 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
280 error: |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
281 PyBuffer_Release(&pinput); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
282 Py_XDECREF(result_str); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
283 return NULL; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
287 PyZlib_compressobj(PyObject *selfptr, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 { |
| 20475 | 289 compobject *self; |
| 290 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED; | |
| 291 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err; | |
| 6007 | 292 |
| 20475 | 293 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits, |
| 294 &memLevel, &strategy)) | |
| 295 return NULL; | |
| 6007 | 296 |
| 20475 | 297 self = newcompobject(&Comptype); |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
298 if (self==NULL) |
| 20475 | 299 return(NULL); |
| 300 self->zst.zalloc = (alloc_func)NULL; | |
| 301 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
|
302 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
|
303 self->zst.avail_in = 0; |
| 20475 | 304 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); |
| 305 switch(err) { | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 case (Z_OK): |
| 20475 | 307 self->is_initialised = 1; |
| 308 return (PyObject*)self; | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 case (Z_MEM_ERROR): |
| 20475 | 310 Py_DECREF(self); |
| 311 PyErr_SetString(PyExc_MemoryError, | |
| 312 "Can't allocate memory for compression object"); | |
| 313 return NULL; | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 case(Z_STREAM_ERROR): |
| 20475 | 315 Py_DECREF(self); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
316 PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
| 20475 | 317 return NULL; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 default: |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
319 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
|
320 Py_DECREF(self); |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 return NULL; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 } |
|
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 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
326 PyZlib_decompressobj(PyObject *selfptr, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 { |
| 20475 | 328 int wbits=DEF_WBITS, err; |
| 329 compobject *self; | |
| 330 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits)) | |
| 331 return NULL; | |
| 332 | |
| 333 self = newcompobject(&Decomptype); | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
334 if (self == NULL) |
| 20475 | 335 return(NULL); |
| 336 self->zst.zalloc = (alloc_func)NULL; | |
| 337 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
|
338 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
|
339 self->zst.avail_in = 0; |
| 20475 | 340 err = inflateInit2(&self->zst, wbits); |
| 341 switch(err) { | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 case (Z_OK): |
| 20475 | 343 self->is_initialised = 1; |
| 344 return (PyObject*)self; | |
| 6236 | 345 case(Z_STREAM_ERROR): |
| 20475 | 346 Py_DECREF(self); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
347 PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
| 20475 | 348 return NULL; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 case (Z_MEM_ERROR): |
| 20475 | 350 Py_DECREF(self); |
| 351 PyErr_SetString(PyExc_MemoryError, | |
| 352 "Can't allocate memory for decompression object"); | |
| 353 return NULL; | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 default: |
|
20474
a5a30ba2899f
Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20470
diff
changeset
|
355 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
|
356 Py_DECREF(self); |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 return NULL; |
| 20475 | 358 } |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 static void |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
362 Dealloc(compobject *self) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 { |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
364 #ifdef WITH_THREAD |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
365 PyThread_free_lock(self->lock); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
366 #endif |
|
10467
255abab3c574
Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents:
10440
diff
changeset
|
367 Py_XDECREF(self->unused_data); |
|
20466
60e12f83d2d2
[ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20327
diff
changeset
|
368 Py_XDECREF(self->unconsumed_tail); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12255
diff
changeset
|
369 PyObject_Del(self); |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 } |
|
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 static void |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
373 Comp_dealloc(compobject *self) |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
374 { |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
375 if (self->is_initialised) |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
376 deflateEnd(&self->zst); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
377 Dealloc(self); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
378 } |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
379 |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
380 static void |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
381 Decomp_dealloc(compobject *self) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 { |
|
16718
01c2470eeb2e
Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents:
16369
diff
changeset
|
383 if (self->is_initialised) |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
384 inflateEnd(&self->zst); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
385 Dealloc(self); |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
388 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
|
389 "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
|
390 "\n" |
| 5722 | 391 "After calling this function, some of the input data may still\n" |
| 392 "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
|
393 "Call the flush() method to clear these buffers."); |
| 5722 | 394 |
| 395 | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
397 PyZlib_objcompress(compobject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 { |
| 20469 | 399 int err, inplen, length = DEFAULTALLOC; |
| 400 PyObject *RetVal; | |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
401 Py_buffer pinput; |
| 20469 | 402 Byte *input; |
| 403 unsigned long start_total_out; | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
404 |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
405 if (!PyArg_ParseTuple(args, "y*:compress", &pinput)) |
| 20469 | 406 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
407 input = pinput.buf; |
|
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
408 inplen = pinput.len; |
|
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
|
409 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
410 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
411 PyBuffer_Release(&pinput); |
| 20469 | 412 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
413 } |
|
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
|
414 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
415 ENTER_ZLIB(self); |
| 20469 | 416 |
| 417 start_total_out = self->zst.total_out; | |
| 418 self->zst.avail_in = inplen; | |
| 419 self->zst.next_in = input; | |
|
16718
01c2470eeb2e
Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents:
16369
diff
changeset
|
420 self->zst.avail_out = length; |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
421 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal); |
| 20469 | 422 |
|
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
|
423 Py_BEGIN_ALLOW_THREADS |
|
16718
01c2470eeb2e
Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents:
16369
diff
changeset
|
424 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
|
425 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
|
426 |
| 20469 | 427 /* while Z_OK and the output buffer is full, there might be more output, |
| 428 so extend the output buffer and try again */ | |
| 429 while (err == Z_OK && self->zst.avail_out == 0) { | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
430 if (_PyBytes_Resize(&RetVal, length << 1) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
431 Py_DECREF(RetVal); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
432 RetVal = NULL; |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
433 goto error; |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
434 } |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
435 self->zst.next_out = |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
436 (unsigned char *)PyBytes_AS_STRING(RetVal) + length; |
| 20469 | 437 self->zst.avail_out = length; |
| 438 length = length << 1; | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
439 |
| 20469 | 440 Py_BEGIN_ALLOW_THREADS |
| 441 err = deflate(&(self->zst), Z_NO_FLUSH); | |
| 442 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
|
443 } |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
444 /* We will only get Z_BUF_ERROR if the output buffer was full but |
| 20469 | 445 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
|
446 condition. |
|
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
447 */ |
|
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
|
448 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
449 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
|
450 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
|
451 Py_DECREF(RetVal); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
452 RetVal = NULL; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
453 goto error; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
454 } |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
455 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
456 Py_DECREF(RetVal); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
457 RetVal = NULL; |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
458 } |
|
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
|
459 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
460 error: |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
461 LEAVE_ZLIB(self); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
462 PyBuffer_Release(&pinput); |
| 20469 | 463 return RetVal; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
465 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
466 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
|
467 "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
|
468 "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
|
469 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
470 "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
|
471 "internal buffers for later processing.\n" |
|
20466
60e12f83d2d2
[ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20327
diff
changeset
|
472 "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
|
473 "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
|
474 "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
|
475 "the unconsumed_tail attribute."); |
| 5722 | 476 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
478 PyZlib_objdecompress(compobject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 { |
| 20469 | 480 int err, inplen, old_length, length = DEFAULTALLOC; |
| 481 int max_length = 0; | |
| 482 PyObject *RetVal; | |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
483 Py_buffer pinput; |
| 20469 | 484 Byte *input; |
| 485 unsigned long start_total_out; | |
|
20466
60e12f83d2d2
[ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20327
diff
changeset
|
486 |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
487 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput, |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
488 &max_length)) |
| 20469 | 489 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
490 input = pinput.buf; |
|
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
491 inplen = pinput.len; |
| 20469 | 492 if (max_length < 0) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
493 PyBuffer_Release(&pinput); |
| 20469 | 494 PyErr_SetString(PyExc_ValueError, |
| 495 "max_length must be greater than zero"); | |
| 496 return NULL; | |
| 497 } | |
|
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
|
498 |
| 20469 | 499 /* limit amount of data allocated to max_length */ |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
500 if (max_length && length > max_length) |
| 20469 | 501 length = max_length; |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
502 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
503 PyBuffer_Release(&pinput); |
| 20469 | 504 return NULL; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
505 } |
|
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
|
506 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
507 ENTER_ZLIB(self); |
| 20469 | 508 |
| 509 start_total_out = self->zst.total_out; | |
| 510 self->zst.avail_in = inplen; | |
| 511 self->zst.next_in = input; | |
| 512 self->zst.avail_out = length; | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
513 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal); |
|
20466
60e12f83d2d2
[ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20327
diff
changeset
|
514 |
|
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
|
515 Py_BEGIN_ALLOW_THREADS |
|
16718
01c2470eeb2e
Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents:
16369
diff
changeset
|
516 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
|
517 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
|
518 |
| 20469 | 519 /* While Z_OK and the output buffer is full, there might be more output. |
| 520 So extend the output buffer and try again. | |
| 521 */ | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
522 while (err == Z_OK && self->zst.avail_out == 0) { |
| 20469 | 523 /* If max_length set, don't continue decompressing if we've already |
| 524 reached the limit. | |
| 525 */ | |
| 526 if (max_length && length >= max_length) | |
| 527 break; | |
| 528 | |
| 529 /* otherwise, ... */ | |
| 530 old_length = length; | |
| 531 length = length << 1; | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
532 if (max_length && length > max_length) |
| 20469 | 533 length = max_length; |
|
20466
60e12f83d2d2
[ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20327
diff
changeset
|
534 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
535 if (_PyBytes_Resize(&RetVal, length) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
536 Py_DECREF(RetVal); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
537 RetVal = NULL; |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
538 goto error; |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
539 } |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
540 self->zst.next_out = |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
541 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length; |
| 20469 | 542 self->zst.avail_out = length - old_length; |
| 543 | |
| 544 Py_BEGIN_ALLOW_THREADS | |
| 545 err = inflate(&(self->zst), Z_SYNC_FLUSH); | |
| 546 Py_END_ALLOW_THREADS | |
| 547 } | |
| 548 | |
|
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
|
549 /* Not all of the compressed data could be accommodated in the output buffer |
| 20469 | 550 of specified size. Return the unconsumed tail in an attribute.*/ |
| 551 if(max_length) { | |
| 552 Py_DECREF(self->unconsumed_tail); | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
553 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in, |
| 20469 | 554 self->zst.avail_in); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
555 if(!self->unconsumed_tail) { |
|
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; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
559 } |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
560 } |
| 20469 | 561 |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
562 /* 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
|
563 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
|
564 data in the string. Note that this is also a logical place to call |
| 20469 | 565 inflateEnd, but the old behaviour of only calling it on flush() is |
| 566 preserved. | |
| 567 */ | |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
568 if (err == Z_STREAM_END) { |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
569 Py_XDECREF(self->unused_data); /* Free original empty string */ |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
570 self->unused_data = PyBytes_FromStringAndSize( |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
571 (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
|
572 if (self->unused_data == NULL) { |
| 20469 | 573 Py_DECREF(RetVal); |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
574 goto error; |
| 20469 | 575 } |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
576 /* 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
|
577 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
|
578 not an error condition. |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
579 */ |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
580 } 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
|
581 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
|
582 Py_DECREF(RetVal); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
583 RetVal = NULL; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
584 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
|
585 } |
|
091c01d07d98
Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents:
16735
diff
changeset
|
586 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
587 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
588 Py_DECREF(RetVal); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
589 RetVal = NULL; |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
590 } |
|
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
|
591 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
592 error: |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
593 LEAVE_ZLIB(self); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
594 PyBuffer_Release(&pinput); |
| 20469 | 595 return RetVal; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
596 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
597 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
598 PyDoc_STRVAR(comp_flush__doc__, |
| 9657 | 599 "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
|
600 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
601 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n" |
| 9657 | 602 "default value used when mode is not specified is Z_FINISH.\n" |
| 603 "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
|
604 "calling the flush() method. Otherwise, more data can still be compressed."); |
| 5722 | 605 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
606 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
607 PyZlib_flush(compobject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
608 { |
| 20469 | 609 int err, length = DEFAULTALLOC; |
| 610 PyObject *RetVal; | |
| 611 int flushmode = Z_FINISH; | |
| 612 unsigned long start_total_out; | |
| 9657 | 613 |
| 20469 | 614 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) |
| 615 return NULL; | |
| 9657 | 616 |
| 20469 | 617 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in |
| 618 doing any work at all; just return an empty string. */ | |
| 619 if (flushmode == Z_NO_FLUSH) { | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
620 return PyBytes_FromStringAndSize(NULL, 0); |
| 20469 | 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 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
623 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) |
| 20469 | 624 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
|
625 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
626 ENTER_ZLIB(self); |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
627 |
| 20469 | 628 start_total_out = self->zst.total_out; |
| 629 self->zst.avail_in = 0; | |
|
16718
01c2470eeb2e
Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents:
16369
diff
changeset
|
630 self->zst.avail_out = length; |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
631 self->zst.next_out = (unsigned char *)PyBytes_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
|
632 |
|
091c01d07d98
Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents:
16735
diff
changeset
|
633 Py_BEGIN_ALLOW_THREADS |
|
16718
01c2470eeb2e
Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents:
16369
diff
changeset
|
634 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
|
635 Py_END_ALLOW_THREADS |
| 20469 | 636 |
| 637 /* while Z_OK and the output buffer is full, there might be more output, | |
| 638 so extend the output buffer and try again */ | |
| 639 while (err == Z_OK && self->zst.avail_out == 0) { | |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
640 if (_PyBytes_Resize(&RetVal, length << 1) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
641 Py_DECREF(RetVal); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
642 RetVal = NULL; |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
643 goto error; |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
644 } |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
645 self->zst.next_out = |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
646 (unsigned char *)PyBytes_AS_STRING(RetVal) + length; |
| 20469 | 647 self->zst.avail_out = length; |
| 648 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
|
649 |
| 20469 | 650 Py_BEGIN_ALLOW_THREADS |
| 651 err = deflate(&(self->zst), flushmode); | |
| 652 Py_END_ALLOW_THREADS | |
| 653 } | |
|
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
|
654 |
| 20469 | 655 /* 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
|
656 various data structures. Note we should only get Z_STREAM_END when |
| 20469 | 657 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
|
658 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
|
659 err = deflateEnd(&(self->zst)); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
660 if (err != Z_OK) { |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
661 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
|
662 Py_DECREF(RetVal); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
663 RetVal = NULL; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
664 goto error; |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
665 } |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
666 else |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
667 self->is_initialised = 0; |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
668 |
|
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
669 /* 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
|
670 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
|
671 not an error condition. |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
672 */ |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
673 } 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
|
674 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
|
675 Py_DECREF(RetVal); |
|
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
676 RetVal = NULL; |
|
23169
713ff889d094
Fix SF #544995 (zlib crash on second flush call)
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22729
diff
changeset
|
677 goto error; |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
678 } |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
679 |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
680 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
681 Py_DECREF(RetVal); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
682 RetVal = NULL; |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
683 } |
| 20469 | 684 |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
685 error: |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
686 LEAVE_ZLIB(self); |
|
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
|
687 |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
688 return RetVal; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
689 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
690 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
691 #ifdef HAVE_ZLIB_COPY |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
692 PyDoc_STRVAR(comp_copy__doc__, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
693 "copy() -- Return a copy of the compression object."); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
694 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
695 static PyObject * |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
696 PyZlib_copy(compobject *self) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
697 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
698 compobject *retval = NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
699 int err; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
700 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
701 retval = newcompobject(&Comptype); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
702 if (!retval) return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
703 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
704 /* Copy the zstream state |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
705 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
706 */ |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
707 ENTER_ZLIB(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
708 err = deflateCopy(&retval->zst, &self->zst); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
709 switch(err) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
710 case(Z_OK): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
711 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
712 case(Z_STREAM_ERROR): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
713 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state"); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
714 goto error; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
715 case(Z_MEM_ERROR): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
716 PyErr_SetString(PyExc_MemoryError, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
717 "Can't allocate memory for compression object"); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
718 goto error; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
719 default: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
720 zlib_error(self->zst, err, "while copying compression object"); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
721 goto error; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
722 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
723 Py_INCREF(self->unused_data); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
724 Py_INCREF(self->unconsumed_tail); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
725 Py_XDECREF(retval->unused_data); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
726 Py_XDECREF(retval->unconsumed_tail); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
727 retval->unused_data = self->unused_data; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
728 retval->unconsumed_tail = self->unconsumed_tail; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
729 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
730 /* Mark it as being initialized */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
731 retval->is_initialised = 1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
732 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
733 LEAVE_ZLIB(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
734 return (PyObject *)retval; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
735 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
736 error: |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
737 LEAVE_ZLIB(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
738 Py_XDECREF(retval); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
739 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
740 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
741 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
742 PyDoc_STRVAR(decomp_copy__doc__, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
743 "copy() -- Return a copy of the decompression object."); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
744 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
745 static PyObject * |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
746 PyZlib_uncopy(compobject *self) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
747 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
748 compobject *retval = NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
749 int err; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
750 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
751 retval = newcompobject(&Decomptype); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
752 if (!retval) return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
753 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
754 /* Copy the zstream state |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
755 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
756 */ |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
757 ENTER_ZLIB(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
758 err = inflateCopy(&retval->zst, &self->zst); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
759 switch(err) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
760 case(Z_OK): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
761 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
762 case(Z_STREAM_ERROR): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
763 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state"); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
764 goto error; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
765 case(Z_MEM_ERROR): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
766 PyErr_SetString(PyExc_MemoryError, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
767 "Can't allocate memory for decompression object"); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
768 goto error; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
769 default: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
770 zlib_error(self->zst, err, "while copying decompression object"); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
771 goto error; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
772 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
773 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
774 Py_INCREF(self->unused_data); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
775 Py_INCREF(self->unconsumed_tail); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
776 Py_XDECREF(retval->unused_data); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
777 Py_XDECREF(retval->unconsumed_tail); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
778 retval->unused_data = self->unused_data; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
779 retval->unconsumed_tail = self->unconsumed_tail; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
780 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
781 /* Mark it as being initialized */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
782 retval->is_initialised = 1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
783 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
784 LEAVE_ZLIB(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
785 return (PyObject *)retval; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
786 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
787 error: |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
788 LEAVE_ZLIB(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
789 Py_XDECREF(retval); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
790 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
791 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
792 #endif |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
793 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
794 PyDoc_STRVAR(decomp_flush__doc__, |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36324
diff
changeset
|
795 "flush( [length] ) -- Return a string containing any remaining\n" |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36324
diff
changeset
|
796 "decompressed data. length, if given, is the initial size of the\n" |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36324
diff
changeset
|
797 "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
|
798 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
799 "The decompressor object can no longer be used after this call."); |
| 5722 | 800 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
801 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
802 PyZlib_unflush(compobject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
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 int err, length = DEFAULTALLOC; |
|
20477
c23989e22ffa
Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20475
diff
changeset
|
805 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
|
806 unsigned long start_total_out; |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
807 |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
808 if (!PyArg_ParseTuple(args, "|i:flush", &length)) |
| 20469 | 809 return NULL; |
|
46370
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
810 if (length <= 0) { |
|
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
811 PyErr_SetString(PyExc_ValueError, "length must be greater than zero"); |
|
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
812 return NULL; |
|
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
813 } |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
814 if (!(retval = PyBytes_FromStringAndSize(NULL, length))) |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
815 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
|
816 |
|
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
|
817 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
818 ENTER_ZLIB(self); |
|
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
|
819 |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
820 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
|
821 self->zst.avail_out = length; |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
822 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval); |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
823 |
|
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
824 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
|
825 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
|
826 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
|
827 |
|
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
828 /* 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
|
829 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
|
830 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) { |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
831 if (_PyBytes_Resize(&retval, length << 1) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
832 Py_DECREF(retval); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
833 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
|
834 goto error; |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
835 } |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
836 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length; |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
837 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
|
838 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
|
839 |
|
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
840 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
|
841 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
|
842 Py_END_ALLOW_THREADS |
| 20469 | 843 } |
|
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
|
844 |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
845 /* 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
|
846 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
|
847 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
|
848 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
|
849 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
|
850 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
|
851 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
|
852 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
|
853 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
|
854 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
|
855 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
|
856 } |
|
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
857 } |
|
49051
c1a8f6b648ea
Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents:
48683
diff
changeset
|
858 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) { |
|
41829
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
859 Py_DECREF(retval); |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
860 retval = NULL; |
|
f7f2a49c1f2e
zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
861 } |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
862 |
|
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
863 error: |
|
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
864 |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
865 LEAVE_ZLIB(self); |
|
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
|
866 |
| 20469 | 867 return retval; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
868 } |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
869 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
870 static PyMethodDef comp_methods[] = |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
871 { |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
872 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS, |
| 20469 | 873 comp_compress__doc__}, |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
874 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS, |
| 20469 | 875 comp_flush__doc__}, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
876 #ifdef HAVE_ZLIB_COPY |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
877 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
878 comp_copy__doc__}, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
879 #endif |
| 20469 | 880 {NULL, NULL} |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
881 }; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
882 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
883 static PyMethodDef Decomp_methods[] = |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
884 { |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
885 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS, |
| 20469 | 886 decomp_decompress__doc__}, |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
887 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS, |
| 20469 | 888 decomp_flush__doc__}, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
889 #ifdef HAVE_ZLIB_COPY |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
890 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
891 decomp_copy__doc__}, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
892 #endif |
| 20469 | 893 {NULL, NULL} |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
894 }; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
895 |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
896 #define COMP_OFF(x) offsetof(compobject, x) |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
897 static PyMemberDef Decomp_members[] = { |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
898 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY}, |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
899 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY}, |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
900 {NULL}, |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
901 }; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
902 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
903 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
|
904 "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
|
905 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
906 "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
|
907 "an integer."); |
| 5722 | 908 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
909 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
910 PyZlib_adler32(PyObject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
911 { |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46042
diff
changeset
|
912 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */ |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
913 Py_buffer pbuf; |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
914 |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
915 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val)) |
|
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
916 return NULL; |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
917 /* Releasing the GIL for very small buffers is inefficient |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
918 and may lower performance */ |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
919 if (pbuf.len > 1024*5) { |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
920 Py_BEGIN_ALLOW_THREADS |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
921 adler32val = adler32(adler32val, pbuf.buf, pbuf.len); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
922 Py_END_ALLOW_THREADS |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
923 } else { |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
924 adler32val = adler32(adler32val, pbuf.buf, pbuf.len); |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
925 } |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
926 PyBuffer_Release(&pbuf); |
|
45951
132cf3a79126
crc32 always returns unsigned. cleanup the code a bit and revert r61648 with
Gregory P. Smith <greg@mad-scientist.com>
parents:
45935
diff
changeset
|
927 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU); |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
928 } |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
929 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
930 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
|
931 "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
|
932 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
933 "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
|
934 "an integer."); |
|
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 static PyObject * |
|
13496
03bc51e43f7a
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12576
diff
changeset
|
937 PyZlib_crc32(PyObject *self, PyObject *args) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
938 { |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46042
diff
changeset
|
939 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */ |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
940 Py_buffer pbuf; |
|
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
941 int signed_val; |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46042
diff
changeset
|
942 |
|
57736
919e2142726e
Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51270
diff
changeset
|
943 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val)) |
| 6236 | 944 return NULL; |
|
50453
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
945 /* Releasing the GIL for very small buffers is inefficient |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
946 and may lower performance */ |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
947 if (pbuf.len > 1024*5) { |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
948 Py_BEGIN_ALLOW_THREADS |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
949 signed_val = crc32(crc32val, pbuf.buf, pbuf.len); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
950 Py_END_ALLOW_THREADS |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
951 } else { |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
952 signed_val = crc32(crc32val, pbuf.buf, pbuf.len); |
|
400cfcaa8d46
Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
49051
diff
changeset
|
953 } |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48413
diff
changeset
|
954 PyBuffer_Release(&pbuf); |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46042
diff
changeset
|
955 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU); |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
956 } |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
957 |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
958 |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
959 static PyMethodDef zlib_methods[] = |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
960 { |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
961 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS, |
| 20469 | 962 adler32__doc__}, |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
963 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS, |
| 20469 | 964 compress__doc__}, |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
965 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS, |
| 20469 | 966 compressobj__doc__}, |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
967 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS, |
|
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
968 crc32__doc__}, |
|
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
969 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS, |
| 20469 | 970 decompress__doc__}, |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
971 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS, |
| 20469 | 972 decompressobj__doc__}, |
| 973 {NULL, NULL} | |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
974 }; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
975 |
|
24283
c0a67a1cab09
Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
976 static PyTypeObject Comptype = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41829
diff
changeset
|
977 PyVarObject_HEAD_INIT(0, 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
|
978 "zlib.Compress", |
| 20469 | 979 sizeof(compobject), |
| 980 0, | |
| 981 (destructor)Comp_dealloc, /*tp_dealloc*/ | |
| 982 0, /*tp_print*/ | |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
983 0, /*tp_getattr*/ |
| 20469 | 984 0, /*tp_setattr*/ |
|
51270
debd14c489b9
Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents:
50453
diff
changeset
|
985 0, /*tp_reserved*/ |
| 20469 | 986 0, /*tp_repr*/ |
| 987 0, /*tp_as_number*/ | |
| 988 0, /*tp_as_sequence*/ | |
| 989 0, /*tp_as_mapping*/ | |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
990 0, /*tp_hash*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
991 0, /*tp_call*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
992 0, /*tp_str*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
993 0, /*tp_getattro*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
994 0, /*tp_setattro*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
995 0, /*tp_as_buffer*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
996 Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
997 0, /*tp_doc*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
998 0, /*tp_traverse*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
999 0, /*tp_clear*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1000 0, /*tp_richcompare*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1001 0, /*tp_weaklistoffset*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1002 0, /*tp_iter*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1003 0, /*tp_iternext*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1004 comp_methods, /*tp_methods*/ |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1005 }; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1006 |
|
24283
c0a67a1cab09
Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1007 static PyTypeObject Decomptype = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41829
diff
changeset
|
1008 PyVarObject_HEAD_INIT(0, 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
|
1009 "zlib.Decompress", |
| 20469 | 1010 sizeof(compobject), |
| 1011 0, | |
| 1012 (destructor)Decomp_dealloc, /*tp_dealloc*/ | |
| 1013 0, /*tp_print*/ | |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1014 0, /*tp_getattr*/ |
| 20469 | 1015 0, /*tp_setattr*/ |
|
51270
debd14c489b9
Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents:
50453
diff
changeset
|
1016 0, /*tp_reserved*/ |
| 20469 | 1017 0, /*tp_repr*/ |
| 1018 0, /*tp_as_number*/ | |
| 1019 0, /*tp_as_sequence*/ | |
| 1020 0, /*tp_as_mapping*/ | |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1021 0, /*tp_hash*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1022 0, /*tp_call*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1023 0, /*tp_str*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1024 0, /*tp_getattro*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1025 0, /*tp_setattro*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1026 0, /*tp_as_buffer*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1027 Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1028 0, /*tp_doc*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1029 0, /*tp_traverse*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1030 0, /*tp_clear*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1031 0, /*tp_richcompare*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1032 0, /*tp_weaklistoffset*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1033 0, /*tp_iter*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1034 0, /*tp_iternext*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1035 Decomp_methods, /*tp_methods*/ |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1036 Decomp_members, /*tp_members*/ |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1037 }; |
|
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1038 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
1039 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
|
1040 "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
|
1041 "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
|
1042 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
1043 "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
|
1044 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n" |
| 5722 | 1045 "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
|
1046 "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
|
1047 "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
|
1048 "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
|
1049 "\n" |
|
4af8d24fa691
Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents:
20484
diff
changeset
|
1050 "'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
|
1051 "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
|
1052 "objects support decompress() and flush()."); |
| 5722 | 1053 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1054 static struct PyModuleDef zlibmodule = { |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1055 PyModuleDef_HEAD_INIT, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1056 "zlib", |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1057 zlib_module_documentation, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1058 -1, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1059 zlib_methods, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1060 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1061 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1062 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1063 NULL |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1064 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1065 |
|
24363
979ff5d0ae07
Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents:
24283
diff
changeset
|
1066 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13496
diff
changeset
|
1067 PyInit_zlib(void) |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1068 { |
|
22729
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22217
diff
changeset
|
1069 PyObject *m, *ver; |
|
48413
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1070 if (PyType_Ready(&Comptype) < 0) |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1071 return NULL; |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1072 if (PyType_Ready(&Decomptype) < 0) |
|
7e56016a6565
#3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47580
diff
changeset
|
1073 return NULL; |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1074 m = PyModule_Create(&zlibmodule); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35914
diff
changeset
|
1075 if (m == NULL) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1076 return NULL; |
|
22729
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22217
diff
changeset
|
1077 |
| 20469 | 1078 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
|
1079 if (ZlibError != NULL) { |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22217
diff
changeset
|
1080 Py_INCREF(ZlibError); |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22217
diff
changeset
|
1081 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
|
1082 } |
| 20469 | 1083 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS); |
| 1084 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED); | |
| 1085 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); | |
| 1086 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED); | |
| 1087 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION); | |
| 1088 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION); | |
| 1089 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED); | |
| 1090 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY); | |
| 1091 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
1092 |
| 20469 | 1093 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH); |
| 1094 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH); | |
| 1095 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH); | |
| 1096 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH); | |
|
20484
1a9494f9edb2
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
20483
diff
changeset
|
1097 |
|
42750
ce52cd71a8d9
Convert a bunch of constant strings in C to unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
1098 ver = PyUnicode_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
|
1099 if (ver != NULL) |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22217
diff
changeset
|
1100 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
|
1101 |
|
27653
106a44a4b975
- Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents:
24363
diff
changeset
|
1102 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
|
1103 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1104 return m; |
|
5352
d5757aad1a4f
Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1105 } |
