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