annotate Modules/zlibmodule.c @ 87488:124e51c19e4f

Issue #19734: Also run pip version check in isolated mode
author Nick Coghlan <ncoghlan@gmail.com>
date Sun, 24 Nov 2013 16:49:20 +1000
parents 78ec18f5cb45
children b67b19859fb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* zlibmodule.c -- gzip-compatible data compression */
20327
54f05b9b1fc4 Update URL. Fixes bug #468118.
Martin v. Löwis <martin@v.loewis.de>
parents: 19810
diff changeset
2 /* See http://www.gzip.org/zlib/ */
16368
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
3
16369
c04ca6cba1e3 Docs for new Windows zlib build procedure.
Tim Peters <tim.peters@gmail.com>
parents: 16368
diff changeset
4 /* Windows users: read Python's PCbuild\readme.txt */
16368
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
5
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
7 #include "Python.h"
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
8 #include "structmember.h"
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
9 #include "zlib.h"
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
11
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
12 #ifdef WITH_THREAD
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
13 #include "pythread.h"
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
14 #define ENTER_ZLIB(obj) \
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
15 Py_BEGIN_ALLOW_THREADS; \
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
16 PyThread_acquire_lock((obj)->lock, 1); \
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
17 Py_END_ALLOW_THREADS;
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
18 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
19 #else
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
20 #define ENTER_ZLIB(obj)
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
21 #define LEAVE_ZLIB(obj)
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
22 #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
23
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 /* 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
25 #define DEFLATED 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 #if MAX_MEM_LEVEL >= 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # define DEF_MEM_LEVEL 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 #else
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #endif
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #define DEF_WBITS MAX_WBITS
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
10538
ccee3c28b7ef Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents: 10467
diff changeset
33 /* 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
34 #define DEFAULTALLOC (16*1024)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
36 static PyTypeObject Comptype;
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
37 static PyTypeObject Decomptype;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 static PyObject *ZlibError;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
41 typedef struct
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
43 PyObject_HEAD
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
44 z_stream zst;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
45 PyObject *unused_data;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
46 PyObject *unconsumed_tail;
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
47 char eof;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
48 int is_initialised;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
49 PyObject *zdict;
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
50 #ifdef WITH_THREAD
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
51 PyThread_type_lock lock;
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
52 #endif
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 } compobject;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
55 static void
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
56 zlib_error(z_stream zst, int err, char *msg)
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
57 {
72111
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
58 const char *zmsg = Z_NULL;
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
59 /* In case of a version mismatch, zst.msg won't be initialized.
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
60 Check for this case first, before looking at zst.msg. */
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
61 if (err == Z_VERSION_ERROR)
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
62 zmsg = "library version mismatch";
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
63 if (zmsg == Z_NULL)
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
64 zmsg = zst.msg;
61523
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
65 if (zmsg == Z_NULL) {
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
66 switch (err) {
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
67 case Z_BUF_ERROR:
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
68 zmsg = "incomplete or truncated stream";
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
69 break;
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
70 case Z_STREAM_ERROR:
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
71 zmsg = "inconsistent stream state";
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
72 break;
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
73 case Z_DATA_ERROR:
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
74 zmsg = "invalid input data";
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
75 break;
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
76 }
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
77 }
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
78 if (zmsg == Z_NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
79 PyErr_Format(ZlibError, "Error %d %s", err, msg);
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
80 else
61523
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
81 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
82 }
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
83
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
84 /*[clinic]
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
85 module zlib
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
86 class zlib.Compress
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
87 class zlib.Decompress
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
88 [clinic]*/
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
89 /*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
90
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
91 PyDoc_STRVAR(compressobj__doc__,
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
92 "compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
93 " strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
94 " -- 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
95 "\n"
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
96 "level is the compression level (an integer in the range 0-9; default is 6).\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
97 "Higher compression levels are slower, but produce smaller results.\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
98 "\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
99 "method is the compression algorithm. If given, this must be DEFLATED.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
100 "\n"
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
101 "wbits is the base two logarithm of the window size (range: 8..15).\n"
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
102 "\n"
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
103 "memlevel controls the amount of memory used for internal compression state.\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
104 "Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
105 "faster compression, and smaller output.\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
106 "\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
107 "strategy is used to tune the compression algorithm. Possible values are\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
108 "Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
109 "\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
110 "zdict is the predefined compression dictionary - a sequence of bytes\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
111 "containing subsequences that are likely to occur in the input data.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
112
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
113 PyDoc_STRVAR(decompressobj__doc__,
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
114 "decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
115 "\n"
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
116 "Optional arg wbits is the window buffer size.\n"
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
117 "\n"
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
118 "Optional arg zdict is the predefined compression dictionary. This must be\n"
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
119 "the same dictionary as used by the compressor that produced the input data.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
120
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 static compobject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
122 newcompobject(PyTypeObject *type)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
124 compobject *self;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
125 self = PyObject_New(compobject, type);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
126 if (self == NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
127 return NULL;
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
128 self->eof = 0;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
129 self->is_initialised = 0;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
130 self->zdict = NULL;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
131 self->unused_data = PyBytes_FromStringAndSize("", 0);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
132 if (self->unused_data == NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
133 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
134 return NULL;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
135 }
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
136 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
137 if (self->unconsumed_tail == NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
138 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
139 return NULL;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
140 }
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
141 #ifdef WITH_THREAD
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
142 self->lock = PyThread_allocate_lock();
84518
2ef2edfd1a4c Issue #18408: Fix zlib.compressobj() to handle PyThread_allocate_lock() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 84496
diff changeset
143 if (self->lock == NULL) {
2ef2edfd1a4c Issue #18408: Fix zlib.compressobj() to handle PyThread_allocate_lock() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 84496
diff changeset
144 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
2ef2edfd1a4c Issue #18408: Fix zlib.compressobj() to handle PyThread_allocate_lock() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 84496
diff changeset
145 return NULL;
2ef2edfd1a4c Issue #18408: Fix zlib.compressobj() to handle PyThread_allocate_lock() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 84496
diff changeset
146 }
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
147 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
148 return self;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
151 static void*
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
152 PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
153 {
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
154 if (items > (size_t)PY_SSIZE_T_MAX / size)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
155 return NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
156 /* PyMem_Malloc() cannot be used: the GIL is not held when
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
157 inflate() and deflate() are called */
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
158 return PyMem_RawMalloc(items * size);
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
159 }
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
160
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
161 static void
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
162 PyZlib_Free(voidpf ctx, void *ptr)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
163 {
84496
12f26c356611 Issue #18227: "Free" function of bz2, lzma and zlib modules has no return value (void)
Victor Stinner <victor.stinner@gmail.com>
parents: 84494
diff changeset
164 PyMem_RawFree(ptr);
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
165 }
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
166
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
167 /*[clinic]
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
168
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
169 zlib.compress
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
170 bytes: Py_buffer
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
171 Binary data to be compressed.
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
172 [
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
173 level: int
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
174 Compression level, in 0-9.
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
175 ]
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
176 /
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
177
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
178 Returns compressed string.
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
179
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
180 [clinic]*/
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
181
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
182 PyDoc_STRVAR(zlib_compress__doc__,
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
183 "compress(bytes, [level])\n"
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
184 "Returns 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
185 "\n"
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
186 " bytes\n"
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
187 " Binary data to be compressed.\n"
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
188 " level\n"
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
189 " Compression level, in 0-9.");
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
190
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
191 #define ZLIB_COMPRESS_METHODDEF \
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
192 {"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
193
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
194 static PyObject *
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
195 zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level);
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
196
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 static PyObject *
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
198 zlib_compress(PyModuleDef *module, PyObject *args)
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
199 {
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
200 PyObject *return_value = NULL;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
201 Py_buffer bytes;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
202 int group_right_1 = 0;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
203 int level = 0;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
204
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
205 switch (PyTuple_Size(args)) {
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
206 case 1:
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
207 if (!PyArg_ParseTuple(args, "y*:compress", &bytes))
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
208 return NULL;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
209 break;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
210 case 2:
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
211 if (!PyArg_ParseTuple(args, "y*i:compress", &bytes, &level))
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
212 return NULL;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
213 group_right_1 = 1;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
214 break;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
215 default:
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
216 PyErr_SetString(PyExc_TypeError, "zlib.compress requires 1 to 2 arguments");
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
217 return NULL;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
218 }
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
219 return_value = zlib_compress_impl(module, &bytes, group_right_1, level);
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
220
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
221 /* Cleanup for bytes */
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
222 if (bytes.buf)
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
223 PyBuffer_Release(&bytes);
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
224
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
225 return return_value;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
226 }
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
227
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
228 static PyObject *
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
229 zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level)
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
230 /*[clinic checksum: f490708eff84be652b5ebe7fe622ab73ac12c888]*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
232 PyObject *ReturnVal = NULL;
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
233 Byte *input, *output = NULL;
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
234 unsigned int length;
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
235 int err;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
236 z_stream zst;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
237
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
238 if (!group_right_1)
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
239 level = Z_DEFAULT_COMPRESSION;
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
240
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
241 if ((size_t)bytes->len > UINT_MAX) {
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
242 PyErr_SetString(PyExc_OverflowError,
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
243 "Size does not fit in an unsigned int");
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
244 goto error;
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
245 }
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
246 input = bytes->buf;
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
247 length = (unsigned int)bytes->len;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
248
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
249 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
250
84491
41ef797e6639 Issue #18203: Replace malloc() with PyMem_Malloc() in Python modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84320
diff changeset
251 output = (Byte*)PyMem_Malloc(zst.avail_out);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
252 if (output == NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
253 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
254 "Can't allocate memory to compress data");
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
255 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 }
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
257
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
258 /* Past the point of no return. From here on out, we need to make sure
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
259 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
260
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
261 zst.opaque = NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
262 zst.zalloc = PyZlib_Malloc;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
263 zst.zfree = PyZlib_Free;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
264 zst.next_out = (Byte *)output;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
265 zst.next_in = (Byte *)input;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
266 zst.avail_in = length;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
267 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
268
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
269 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 case(Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
271 break;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 case(Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
273 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
274 "Out of memory while compressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
275 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 case(Z_STREAM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
277 PyErr_SetString(ZlibError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
278 "Bad compression level");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
279 goto error;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
280 default:
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 deflateEnd(&zst);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
282 zlib_error(zst, err, "while compressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
283 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 }
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 Py_BEGIN_ALLOW_THREADS;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
287 err = deflate(&zst, Z_FINISH);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
288 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
289
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
290 if (err != Z_STREAM_END) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
291 zlib_error(zst, err, "while compressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
292 deflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
293 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
294 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
295
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
296 err=deflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
297 if (err == Z_OK)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
298 ReturnVal = PyBytes_FromStringAndSize((char *)output,
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
299 zst.total_out);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
300 else
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
301 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
302
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
303 error:
84491
41ef797e6639 Issue #18203: Replace malloc() with PyMem_Malloc() in Python modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84320
diff changeset
304 PyMem_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
305
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
306 return ReturnVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
309 /*[python]
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
310
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
311 class uint_converter(CConverter):
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
312 type = 'unsigned int'
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
313 converter = 'uint_converter'
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
314
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
315 [python]*/
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
316 /*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
317
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
318 static int
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
319 uint_converter(PyObject *obj, void *ptr)
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
320 {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
321 long val;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
322 unsigned long uval;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
323
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
324 val = PyLong_AsLong(obj);
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
325 if (val == -1 && PyErr_Occurred()) {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
326 uval = PyLong_AsUnsignedLong(obj);
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
327 if (uval == (unsigned long)-1 && PyErr_Occurred())
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
328 return 0;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
329 if (uval > UINT_MAX) {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
330 PyErr_SetString(PyExc_OverflowError,
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
331 "Python int too large for C unsigned int");
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
332 return 0;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
333 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
334 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
335 else {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
336 if (val < 0) {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
337 PyErr_SetString(PyExc_ValueError,
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
338 "value must be positive");
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
339 return 0;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
340 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
341 uval = (unsigned long)val;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
342 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
343
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
344 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
345 return 1;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
346 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
347
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
348 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
349 "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
350 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
351 "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
352 "the initial output buffer size.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
353
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
355 PyZlib_decompress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 {
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
357 PyObject *result_str = NULL;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
358 Py_buffer pinput;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
359 Byte *input;
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
360 unsigned int length;
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
361 int err;
43957
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
362 int wsize=DEF_WBITS;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
363 unsigned int bufsize = DEFAULTALLOC, new_bufsize;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
364 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
365
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
366 if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
367 &pinput, &wsize, uint_converter, &bufsize))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
368 return NULL;
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
369
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
370 if ((size_t)pinput.len > UINT_MAX) {
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
371 PyErr_SetString(PyExc_OverflowError,
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
372 "Size does not fit in an unsigned int");
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
373 goto error;
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
374 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
375 input = pinput.buf;
80066
96fc87997ce9 Issue #9566: Explicit downcast to fix compiler warnings on Win64
Victor Stinner <victor.stinner@gmail.com>
parents: 77559
diff changeset
376 length = (unsigned int)pinput.len;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
377
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
378 if (bufsize == 0)
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
379 bufsize = 1;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
380
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
381 zst.avail_in = length;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
382 zst.avail_out = bufsize;
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
383
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
384 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
385 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
386
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
387 zst.opaque = NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
388 zst.zalloc = PyZlib_Malloc;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
389 zst.zfree = PyZlib_Free;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
390 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
391 zst.next_in = (Byte *)input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
392 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
393
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
394 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 case(Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
396 break;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
397 case(Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
398 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
399 "Out of memory while decompressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
400 goto error;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
401 default:
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 inflateEnd(&zst);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
403 zlib_error(zst, err, "while preparing to decompress data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
404 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 }
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
406
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
407 do {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
408 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
409 err=inflate(&zst, Z_FINISH);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
410 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
411
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
412 switch(err) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
413 case(Z_STREAM_END):
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
414 break;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
415 case(Z_BUF_ERROR):
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
416 /*
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
417 * If there is at least 1 byte of room according to zst.avail_out
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
418 * and we get this error, assume that it means zlib cannot
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
419 * process the inflate call() due to an error in the data.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
420 */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
421 if (zst.avail_out > 0) {
61523
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
422 zlib_error(zst, err, "while decompressing data");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
423 inflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
424 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
425 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
426 /* fall through */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
427 case(Z_OK):
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
428 /* need more memory */
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
429 if (bufsize <= (UINT_MAX >> 1))
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
430 new_bufsize = bufsize << 1;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
431 else
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
432 new_bufsize = UINT_MAX;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
433 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
434 inflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
435 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
436 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
437 zst.next_out =
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
438 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
439 zst.avail_out = bufsize;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
440 bufsize = new_bufsize;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
441 break;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
442 default:
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
443 inflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
444 zlib_error(zst, err, "while decompressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
445 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
446 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
447 } 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
448
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
449 err = inflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
450 if (err != Z_OK) {
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
451 zlib_error(zst, err, "while finishing decompression");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
452 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 }
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
454
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
455 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
456 goto error;
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
457
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
458 PyBuffer_Release(&pinput);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
459 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
460
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
461 error:
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
462 PyBuffer_Release(&pinput);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
463 Py_XDECREF(result_str);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
464 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 static PyObject *
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
468 PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
470 compobject *self = NULL;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
471 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
472 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
473 Py_buffer zdict;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
474 static char *kwlist[] = {"level", "method", "wbits",
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
475 "memLevel", "strategy", "zdict", NULL};
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
476
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
477 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
478 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
479 kwlist, &level, &method, &wbits,
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
480 &memLevel, &strategy, &zdict))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
481 return NULL;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
482
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
483 if (zdict.buf != NULL && (size_t)zdict.len > UINT_MAX) {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
484 PyErr_SetString(PyExc_OverflowError,
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
485 "zdict length does not fit in an unsigned int");
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
486 goto error;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
487 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
488
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
489 self = newcompobject(&Comptype);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
490 if (self==NULL)
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
491 goto error;
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
492 self->zst.opaque = NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
493 self->zst.zalloc = PyZlib_Malloc;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
494 self->zst.zfree = PyZlib_Free;
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
495 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
496 self->zst.avail_in = 0;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
497 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
498 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 case (Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
500 self->is_initialised = 1;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
501 if (zdict.buf == NULL) {
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
502 goto success;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
503 } else {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
504 err = deflateSetDictionary(&self->zst,
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
505 zdict.buf, (unsigned int)zdict.len);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
506 switch (err) {
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
507 case (Z_OK):
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
508 goto success;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
509 case (Z_STREAM_ERROR):
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
510 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
511 goto error;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
512 default:
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
513 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
514 goto error;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
515 }
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
516 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 case (Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
518 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
519 "Can't allocate memory for compression object");
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
520 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 case(Z_STREAM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
522 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
523 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 default:
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
525 zlib_error(self->zst, err, "while creating compression object");
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
526 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 }
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
529 error:
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
530 Py_XDECREF(self);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
531 self = NULL;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
532 success:
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
533 if (zdict.buf != NULL)
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
534 PyBuffer_Release(&zdict);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
535 return (PyObject*)self;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 static PyObject *
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
539 PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 {
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
541 static char *kwlist[] = {"wbits", "zdict", NULL};
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
542 int wbits=DEF_WBITS, err;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
543 compobject *self;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
544 PyObject *zdict=NULL;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
545
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
546 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
547 kwlist, &wbits, &zdict))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
548 return NULL;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
549 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
550 PyErr_SetString(PyExc_TypeError,
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
551 "zdict argument must support the buffer protocol");
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
552 return NULL;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
553 }
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
554
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
555 self = newcompobject(&Decomptype);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
556 if (self == NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
557 return(NULL);
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
558 self->zst.opaque = NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
559 self->zst.zalloc = PyZlib_Malloc;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84491
diff changeset
560 self->zst.zfree = PyZlib_Free;
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
561 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
562 self->zst.avail_in = 0;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
563 if (zdict != NULL) {
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
564 Py_INCREF(zdict);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
565 self->zdict = zdict;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
566 }
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
567 err = inflateInit2(&self->zst, wbits);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
568 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 case (Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
570 self->is_initialised = 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
571 return (PyObject*)self;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
572 case(Z_STREAM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
573 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
574 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
575 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 case (Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
577 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
578 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
579 "Can't allocate memory for decompression object");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
580 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 default:
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
582 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
583 Py_DECREF(self);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
584 return NULL;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
585 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 static void
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
589 Dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 {
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
591 #ifdef WITH_THREAD
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
592 PyThread_free_lock(self->lock);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
593 #endif
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
594 Py_XDECREF(self->unused_data);
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
595 Py_XDECREF(self->unconsumed_tail);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
596 Py_XDECREF(self->zdict);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
597 PyObject_Del(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 static void
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
601 Comp_dealloc(compobject *self)
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
602 {
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
603 if (self->is_initialised)
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
604 deflateEnd(&self->zst);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
605 Dealloc(self);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
606 }
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
607
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
608 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
609 Decomp_dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
611 if (self->is_initialised)
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
612 inflateEnd(&self->zst);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
613 Dealloc(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
616 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
617 "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
618 "\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
619 "After calling this function, some of the input data may still\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
620 "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
621 "Call the flush() method to clear these buffers.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
622
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
623
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
625 PyZlib_objcompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 {
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
627 int err;
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
628 unsigned int inplen;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
629 unsigned int length = DEFAULTALLOC, new_length;
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
630 PyObject *RetVal = NULL;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
631 Py_buffer pinput;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
632 Byte *input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
633 unsigned long start_total_out;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
634
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
635 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
636 return NULL;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
637 if ((size_t)pinput.len > UINT_MAX) {
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
638 PyErr_SetString(PyExc_OverflowError,
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
639 "Size does not fit in an unsigned int");
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
640 goto error_outer;
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
641 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
642 input = pinput.buf;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
643 inplen = (unsigned int)pinput.len;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
644
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
645 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
646 goto error_outer;
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
647
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
648 ENTER_ZLIB(self);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
649
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
650 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
651 self->zst.avail_in = inplen;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
652 self->zst.next_in = input;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
653 self->zst.avail_out = length;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
654 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
655
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
656 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
657 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
658 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
659
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
660 /* while Z_OK and the output buffer is full, there might be more output,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
661 so extend the output buffer and try again */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
662 while (err == Z_OK && self->zst.avail_out == 0) {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
663 if (length <= (UINT_MAX >> 1))
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
664 new_length = length << 1;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
665 else
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
666 new_length = UINT_MAX;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
667 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
668 Py_CLEAR(RetVal);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
669 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
670 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
671 self->zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
672 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
673 self->zst.avail_out = length;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
674 length = new_length;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
675
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
676 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
677 err = deflate(&(self->zst), Z_NO_FLUSH);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
678 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
679 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
680 /* We will only get Z_BUF_ERROR if the output buffer was full but
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
681 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
682 condition.
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
683 */
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
684
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
685 if (err != Z_OK && err != Z_BUF_ERROR) {
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
686 zlib_error(self->zst, err, "while compressing data");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
687 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
688 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
689 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
690 }
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
691 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
692 Py_CLEAR(RetVal);
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
693 }
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
694
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
695 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
696 LEAVE_ZLIB(self);
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
697 error_outer:
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
698 PyBuffer_Release(&pinput);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
699 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701
80350
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
702 /* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
703 self->unused_data or self->unconsumed_tail, as appropriate. */
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
704 static int
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
705 save_unconsumed_input(compobject *self, int err)
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
706 {
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
707 if (err == Z_STREAM_END) {
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
708 /* The end of the compressed data has been reached. Store the leftover
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
709 input data in self->unused_data. */
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
710 if (self->zst.avail_in > 0) {
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
711 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
712 Py_ssize_t new_size;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
713 PyObject *new_data;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
714 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
80350
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
715 PyErr_NoMemory();
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
716 return -1;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
717 }
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
718 new_size = old_size + self->zst.avail_in;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
719 new_data = PyBytes_FromStringAndSize(NULL, new_size);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
720 if (new_data == NULL)
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
721 return -1;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
722 Py_MEMCPY(PyBytes_AS_STRING(new_data),
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
723 PyBytes_AS_STRING(self->unused_data), old_size);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
724 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
725 self->zst.next_in, self->zst.avail_in);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
726 Py_DECREF(self->unused_data);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
727 self->unused_data = new_data;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
728 self->zst.avail_in = 0;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
729 }
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
730 }
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
731 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
732 /* This code handles two distinct cases:
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
733 1. Output limit was reached. Save leftover input in unconsumed_tail.
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
734 2. All input data was consumed. Clear unconsumed_tail. */
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
735 PyObject *new_data = PyBytes_FromStringAndSize(
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
736 (char *)self->zst.next_in, self->zst.avail_in);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
737 if (new_data == NULL)
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
738 return -1;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
739 Py_DECREF(self->unconsumed_tail);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
740 self->unconsumed_tail = new_data;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
741 }
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
742 return 0;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
743 }
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
744
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
745 /*[clinic]
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
746
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
747 zlib.Decompress.decompress
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
748
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
749 data: Py_buffer
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
750 The binary data to decompress.
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
751 max_length: uint = 0
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
752 The maximum allowable length of the decompressed data.
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
753 Unconsumed input data will be stored in
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
754 the unconsumed_tail attribute.
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
755 /
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
756
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
757 Return a string containing the decompressed version of the data.
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
758
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
759 After calling this function, some of the input data may still be stored in
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
760 internal buffers for later processing.
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
761 Call the flush() method to clear these buffers.
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
762 [clinic]*/
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
763
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
764 PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
765 "decompress(data, max_length=0)\n"
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
766 "Return a string containing the decompressed version of the data.\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
767 "\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
768 " data\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
769 " The binary data to decompress.\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
770 " max_length\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
771 " The maximum allowable length of the decompressed data.\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
772 " Unconsumed input data will be stored in\n"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
773 " the unconsumed_tail attribute.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
774 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
775 "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
776 "internal buffers for later processing.\n"
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
777 "Call the flush() method to clear these buffers.");
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
778
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
779 #define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
780 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
781
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
782 static PyObject *
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
783 zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, unsigned int max_length);
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
784
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 static PyObject *
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
786 zlib_Decompress_decompress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 {
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
788 PyObject *return_value = NULL;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
789 Py_buffer data;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
790 unsigned int max_length = 0;
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
791
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
792 if (!PyArg_ParseTuple(args,
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
793 "y*|O&:decompress",
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
794 &data, uint_converter, &max_length))
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
795 goto exit;
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
796 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
797
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
798 exit:
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
799 /* Cleanup for data */
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
800 if (data.buf)
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
801 PyBuffer_Release(&data);
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
802
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
803 return return_value;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
804 }
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
805
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
806 static PyObject *
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
807 zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, unsigned int max_length)
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
808 /*[clinic checksum: 4683928665a1fa6987f5c57cada4a22807a78fbb]*/
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
809 {
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
810 compobject *zself = (compobject *)self;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
811 int err;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
812 unsigned int old_length, length = DEFAULTALLOC;
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
813 PyObject *RetVal = NULL;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
814 unsigned long start_total_out;
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
815
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
816 if ((size_t)data->len > UINT_MAX) {
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
817 PyErr_SetString(PyExc_OverflowError,
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
818 "Size does not fit in an unsigned int");
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
819 return NULL;
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
820 }
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
821
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
822 /* limit amount of data allocated to max_length */
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
823 if (max_length && length > max_length)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
824 length = max_length;
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
825 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
826 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
827
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
828 ENTER_ZLIB(zself);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
829
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
830 start_total_out = zself->zst.total_out;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
831 zself->zst.avail_in = (unsigned int)data->len;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
832 zself->zst.next_in = data->buf;
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
833 zself->zst.avail_out = length;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
834 zself->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
835
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
836 Py_BEGIN_ALLOW_THREADS
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
837 err = inflate(&(zself->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
838 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
839
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
840 if (err == Z_NEED_DICT && zself->zdict != NULL) {
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
841 Py_buffer zdict_buf;
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
842 if (PyObject_GetBuffer(zself->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
843 Py_DECREF(RetVal);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
844 RetVal = NULL;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
845 goto error;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
846 }
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
847
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
848 if ((size_t)zdict_buf.len > UINT_MAX) {
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
849 PyErr_SetString(PyExc_OverflowError,
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
850 "zdict length does not fit in an unsigned int");
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
851 PyBuffer_Release(&zdict_buf);
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
852 Py_CLEAR(RetVal);
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
853 goto error;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
854 }
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
855
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
856 err = inflateSetDictionary(&(zself->zst),
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
857 zdict_buf.buf, (unsigned int)zdict_buf.len);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
858 PyBuffer_Release(&zdict_buf);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
859 if (err != Z_OK) {
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
860 zlib_error(zself->zst, err, "while decompressing data");
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
861 Py_CLEAR(RetVal);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
862 goto error;
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
863 }
77558
4931cbc58cde Tidy up comments from dd4f7d5c51c7 (zlib compression dictionary support).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77528
diff changeset
864 /* Repeat the call to inflate. */
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
865 Py_BEGIN_ALLOW_THREADS
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
866 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
867 Py_END_ALLOW_THREADS
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
868 }
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
869
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
870 /* While Z_OK and the output buffer is full, there might be more output.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
871 So extend the output buffer and try again.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
872 */
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
873 while (err == Z_OK && zself->zst.avail_out == 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
874 /* If max_length set, don't continue decompressing if we've already
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
875 reached the limit.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
876 */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
877 if (max_length && length >= max_length)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
878 break;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
879
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
880 /* otherwise, ... */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
881 old_length = length;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
882 length = length << 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
883 if (max_length && length > max_length)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
884 length = max_length;
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
885
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
886 if (_PyBytes_Resize(&RetVal, length) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
887 Py_CLEAR(RetVal);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
888 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
889 }
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
890 zself->zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
891 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
892 zself->zst.avail_out = length - old_length;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
893
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
894 Py_BEGIN_ALLOW_THREADS
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
895 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
896 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
897 }
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
898
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
899 if (save_unconsumed_input(zself, err) < 0) {
70099
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
900 Py_DECREF(RetVal);
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
901 RetVal = NULL;
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
902 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
904
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
905 if (err == Z_STREAM_END) {
80351
654eb5163ba1 Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80256 80350
diff changeset
906 /* This is the logical place to call inflateEnd, but the old behaviour
654eb5163ba1 Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80256 80350
diff changeset
907 of only calling it on flush() is preserved. */
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
908 zself->eof = 1;
80351
654eb5163ba1 Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80256 80350
diff changeset
909 } else if (err != Z_OK && err != Z_BUF_ERROR) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
910 /* We will only get Z_BUF_ERROR if the output buffer was full
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
911 but there wasn't more output when we tried again, so it is
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
912 not an error condition.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
913 */
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
914 zlib_error(zself->zst, err, "while decompressing data");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
915 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
916 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
917 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
918 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
919
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
920 if (_PyBytes_Resize(&RetVal, zself->zst.total_out - start_total_out) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
921 Py_CLEAR(RetVal);
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
922 }
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
923
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
924 error:
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
925 LEAVE_ZLIB(zself);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
926 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
928
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
929 PyDoc_STRVAR(comp_flush__doc__,
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
930 "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
931 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
932 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
933 "default value used when mode is not specified is Z_FINISH.\n"
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
934 "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
935 "calling the flush() method. Otherwise, more data can still be compressed.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
936
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
938 PyZlib_flush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
940 int err;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
941 unsigned int length = DEFAULTALLOC, new_length;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
942 PyObject *RetVal;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
943 int flushmode = Z_FINISH;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
944 unsigned long start_total_out;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
945
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
946 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
947 return NULL;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
948
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
949 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
950 doing any work at all; just return an empty string. */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
951 if (flushmode == Z_NO_FLUSH) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
952 return PyBytes_FromStringAndSize(NULL, 0);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
953 }
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
954
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
955 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
956 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
957
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
958 ENTER_ZLIB(self);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
959
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
960 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
961 self->zst.avail_in = 0;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
962 self->zst.avail_out = length;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
963 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
964
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
965 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
966 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
967 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
968
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
969 /* while Z_OK and the output buffer is full, there might be more output,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
970 so extend the output buffer and try again */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
971 while (err == Z_OK && self->zst.avail_out == 0) {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
972 if (length <= (UINT_MAX >> 1))
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
973 new_length = length << 1;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
974 else
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
975 new_length = UINT_MAX;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
976 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
977 Py_CLEAR(RetVal);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
978 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
979 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
980 self->zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
981 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
982 self->zst.avail_out = length;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
983 length = new_length;
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
984
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
985 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
986 err = deflate(&(self->zst), flushmode);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
987 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
988 }
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
989
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
990 /* 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
991 various data structures. Note we should only get Z_STREAM_END when
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
992 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
993 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
994 err = deflateEnd(&(self->zst));
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
995 if (err != Z_OK) {
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
996 zlib_error(self->zst, err, "while finishing compression");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
997 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
998 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
999 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1000 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1001 else
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1002 self->is_initialised = 0;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1003
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1004 /* We will only get Z_BUF_ERROR if the output buffer was full
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1005 but there wasn't more output when we tried again, so it is
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1006 not an error condition.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1007 */
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
1008 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1009 zlib_error(self->zst, err, "while flushing");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1010 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1011 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1012 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
1013 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1014
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
1015 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
1016 Py_CLEAR(RetVal);
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1017 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1018
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1019 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1020 LEAVE_ZLIB(self);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1021
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
1022 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1025 #ifdef HAVE_ZLIB_COPY
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1026
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1027 /*[clinic]
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1028 zlib.Compress.copy
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1029
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1030 Return a copy of the compression object.
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1031 [clinic]*/
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1032
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1033 PyDoc_STRVAR(zlib_Compress_copy__doc__,
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
1034 "copy()\n"
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
1035 "Return a copy of the compression object.");
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1036
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1037 #define ZLIB_COMPRESS_COPY_METHODDEF \
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1038 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1039
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1040 static PyObject *
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1041 zlib_Compress_copy(PyObject *self)
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87466
diff changeset
1042 /*[clinic checksum: 8d30351f05defbc2b335c2a78d18f07aa367bb1d]*/
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1043 {
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1044 compobject *zself = (compobject *)self;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1045 compobject *retval = NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1046 int err;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1047
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1048 retval = newcompobject(&Comptype);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1049 if (!retval) return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1050
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1051 /* Copy the zstream state
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1052 * 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: 37373
diff changeset
1053 */
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1054 ENTER_ZLIB(zself);
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1055 err = deflateCopy(&retval->zst, &zself->zst);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1056 switch(err) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1057 case(Z_OK):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1058 break;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1059 case(Z_STREAM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1060 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: 37373
diff changeset
1061 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1062 case(Z_MEM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1063 PyErr_SetString(PyExc_MemoryError,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1064 "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: 37373
diff changeset
1065 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1066 default:
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1067 zlib_error(zself->zst, err, "while copying compression object");
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1068 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1069 }
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1070 Py_INCREF(zself->unused_data);
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1071 Py_INCREF(zself->unconsumed_tail);
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1072 Py_XINCREF(zself->zdict);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1073 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: 37373
diff changeset
1074 Py_XDECREF(retval->unconsumed_tail);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1075 Py_XDECREF(retval->zdict);
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1076 retval->unused_data = zself->unused_data;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1077 retval->unconsumed_tail = zself->unconsumed_tail;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1078 retval->zdict = zself->zdict;
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1079 retval->eof = zself->eof;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1080
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1081 /* 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: 37373
diff changeset
1082 retval->is_initialised = 1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1083
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1084 LEAVE_ZLIB(zself);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1085 return (PyObject *)retval;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1086
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1087 error:
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 84520
diff changeset
1088 LEAVE_ZLIB(zself);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1089 Py_XDECREF(retval);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1090 return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1091 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1092
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1093 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: 37373
diff changeset
1094 "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: 37373
diff changeset
1095
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1096 static PyObject *
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1097 PyZlib_uncopy(compobject *self)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1098 {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1099 compobject *retval = NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1100 int err;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1101
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1102 retval = newcompobject(&Decomptype);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1103 if (!retval) return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1104
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1105 /* Copy the zstream state
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1106 * 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: 37373
diff changeset
1107 */
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1108 ENTER_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1109 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: 37373
diff changeset
1110 switch(err) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1111 case(Z_OK):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1112 break;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1113 case(Z_STREAM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1114 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: 37373
diff changeset
1115 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1116 case(Z_MEM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1117 PyErr_SetString(PyExc_MemoryError,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1118 "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: 37373
diff changeset
1119 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1120 default:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1121 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: 37373
diff changeset
1122 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1123 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1124
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1125 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: 37373
diff changeset
1126 Py_INCREF(self->unconsumed_tail);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1127 Py_XINCREF(self->zdict);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1128 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: 37373
diff changeset
1129 Py_XDECREF(retval->unconsumed_tail);
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1130 Py_XDECREF(retval->zdict);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1131 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: 37373
diff changeset
1132 retval->unconsumed_tail = self->unconsumed_tail;
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1133 retval->zdict = self->zdict;
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
1134 retval->eof = self->eof;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1135
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1136 /* 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: 37373
diff changeset
1137 retval->is_initialised = 1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1138
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1139 LEAVE_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1140 return (PyObject *)retval;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1141
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1142 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1143 LEAVE_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1144 Py_XDECREF(retval);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1145 return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1146 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1147 #endif
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37373
diff changeset
1148
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1149 PyDoc_STRVAR(decomp_flush__doc__,
37373
f64812b2b5c1 Patch #1459631: documnent zlib.Decompress.flush() length parameter.
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
1150 "flush( [length] ) -- Return a string containing any remaining\n"
f64812b2b5c1 Patch #1459631: documnent zlib.Decompress.flush() length parameter.
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
1151 "decompressed data. length, if given, is the initial size of the\n"
f64812b2b5c1 Patch #1459631: documnent zlib.Decompress.flush() length parameter.
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
1152 "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
1153 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1154 "The decompressor object can no longer be used after this call.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1155
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
1157 PyZlib_unflush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1158 {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1159 int err;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1160 unsigned int length = DEFAULTALLOC, new_length;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
1161 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
1162 unsigned long start_total_out;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1163 Py_ssize_t size;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1164
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1165 if (!PyArg_ParseTuple(args, "|O&:flush", uint_converter, &length))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1166 return NULL;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1167 if (length == 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1168 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1169 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
1170 }
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1171
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
1172 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1173 return NULL;
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1174
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
1175
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1176 ENTER_ZLIB(self);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1177
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1178 size = PyBytes_GET_SIZE(self->unconsumed_tail);
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1179
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1180 start_total_out = self->zst.total_out;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1181 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1182 or equal than UINT_MAX */
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1183 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
80354
a1db815d0829 Issue #16411: Fix a bug where zlib.decompressobj().flush() might try to access previously-freed memory.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80350
diff changeset
1184 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1185 self->zst.avail_out = length;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
1186 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
1187
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1188 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
1189 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
1190 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
1191
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1192 /* 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
1193 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
1194 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1195 if (length <= (UINT_MAX >> 1))
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1196 new_length = length << 1;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1197 else
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1198 new_length = UINT_MAX;
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1199 if (_PyBytes_Resize(&retval, new_length) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
1200 Py_CLEAR(retval);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1201 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1202 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1203 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1204 self->zst.avail_out = length;
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1205 length = new_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
1206
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1207 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1208 err = inflate(&(self->zst), Z_FINISH);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1209 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1210 }
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
1211
80350
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1212 if (save_unconsumed_input(self, err) < 0) {
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1213 Py_DECREF(retval);
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1214 retval = NULL;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1215 goto error;
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1216 }
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1217
71858
65d61ed991d9 Fix incorrect comment in zlib.Decompress.flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71857
diff changeset
1218 /* If at end of stream, clean up any memory allocated by zlib. */
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1219 if (err == Z_STREAM_END) {
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
1220 self->eof = 1;
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1221 self->is_initialised = 0;
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
1222 err = inflateEnd(&(self->zst));
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1223 if (err != Z_OK) {
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
1224 zlib_error(self->zst, err, "while finishing decompression");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1225 Py_DECREF(retval);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1226 retval = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1227 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1228 }
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1229 }
80350
85886268c40b Issue #16350, part 2: Set unused_data (and unconsumed_tail) correctly in decompressobj().flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80255
diff changeset
1230
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
1231 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
84520
ed0c9d77e179 Issue #18408: Fix usage of _PyBytes_Resize()
Victor Stinner <victor.stinner@gmail.com>
parents: 84518
diff changeset
1232 Py_CLEAR(retval);
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1233 }
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1234
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1235 error:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1236
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1237 LEAVE_ZLIB(self);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1238
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1239 return retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242 static PyMethodDef comp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1244 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1245 comp_compress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1246 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1247 comp_flush__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1248 #ifdef HAVE_ZLIB_COPY
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1249 ZLIB_COMPRESS_COPY_METHODDEF
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1250 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1251 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1252 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1253
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 static PyMethodDef Decomp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255 {
87245
64c853a88a10 Argument Clinic: rename "self" to "module" for module-level functions.
Larry Hastings <larry@hastings.org>
parents: 86465
diff changeset
1256 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1257 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1258 decomp_flush__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1259 #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: 37373
diff changeset
1260 {"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: 37373
diff changeset
1261 decomp_copy__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1262 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1263 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1266 #define COMP_OFF(x) offsetof(compobject, x)
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1267 static PyMemberDef Decomp_members[] = {
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1268 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1269 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
1270 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1271 {NULL},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1272 };
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1273
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1274 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
1275 "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
1276 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1277 "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
1278 "an integer.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1279
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
1281 PyZlib_adler32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 {
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
1283 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1284 Py_buffer pbuf;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1285
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
1286 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
1287 return NULL;
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1288 /* Releasing the GIL for very small buffers is inefficient
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1289 and may lower performance */
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1290 if (pbuf.len > 1024*5) {
68054
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1291 unsigned char *buf = pbuf.buf;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1292 Py_ssize_t len = pbuf.len;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1293
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1294 Py_BEGIN_ALLOW_THREADS
68054
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1295 /* Avoid truncation of length for very large buffers. adler32() takes
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1296 length as an unsigned int, which may be narrower than Py_ssize_t. */
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1297 while ((size_t)len > UINT_MAX) {
68054
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1298 adler32val = adler32(adler32val, buf, UINT_MAX);
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1299 buf += (size_t) UINT_MAX;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1300 len -= (size_t) UINT_MAX;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1301 }
84320
931e1bc090f6 Issue #9566: zlib: Explicit cast to unsigned int to fix a compiler warning on Windows x64
Victor Stinner <victor.stinner@gmail.com>
parents: 83867
diff changeset
1302 adler32val = adler32(adler32val, buf, (unsigned int)len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1303 Py_END_ALLOW_THREADS
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1304 } else {
84320
931e1bc090f6 Issue #9566: zlib: Explicit cast to unsigned int to fix a compiler warning on Windows x64
Victor Stinner <victor.stinner@gmail.com>
parents: 83867
diff changeset
1305 adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1306 }
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1307 PyBuffer_Release(&pbuf);
45951
132cf3a79126 crc32 always returns unsigned. cleanup the code a bit and revert r61648 with
Gregory P. Smith <greg@mad-scientist.com>
parents: 45935
diff changeset
1308 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1310
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1311 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
1312 "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
1313 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1314 "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
1315 "an integer.");
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1316
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
1318 PyZlib_crc32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 {
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
1320 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
1321 Py_buffer pbuf;
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
1322 int signed_val;
46084
6bea2f1351b1 Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46042
diff changeset
1323
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
1324 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1325 return NULL;
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1326 /* Releasing the GIL for very small buffers is inefficient
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1327 and may lower performance */
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1328 if (pbuf.len > 1024*5) {
68054
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1329 unsigned char *buf = pbuf.buf;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1330 Py_ssize_t len = pbuf.len;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1331
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1332 Py_BEGIN_ALLOW_THREADS
68054
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1333 /* Avoid truncation of length for very large buffers. crc32() takes
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1334 length as an unsigned int, which may be narrower than Py_ssize_t. */
87319
f947fe289db8 Close #18294: Fix the zlib module to make it 64-bit safe
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1335 while ((size_t)len > UINT_MAX) {
68054
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1336 crc32val = crc32(crc32val, buf, UINT_MAX);
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1337 buf += (size_t) UINT_MAX;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1338 len -= (size_t) UINT_MAX;
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1339 }
84320
931e1bc090f6 Issue #9566: zlib: Explicit cast to unsigned int to fix a compiler warning on Windows x64
Victor Stinner <victor.stinner@gmail.com>
parents: 83867
diff changeset
1340 signed_val = crc32(crc32val, buf, (unsigned int)len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1341 Py_END_ALLOW_THREADS
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1342 } else {
84320
931e1bc090f6 Issue #9566: zlib: Explicit cast to unsigned int to fix a compiler warning on Windows x64
Victor Stinner <victor.stinner@gmail.com>
parents: 83867
diff changeset
1343 signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1344 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
1345 PyBuffer_Release(&pbuf);
46084
6bea2f1351b1 Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46042
diff changeset
1346 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1348
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350 static PyMethodDef zlib_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1352 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1353 adler32__doc__},
87466
760ccd78e874 Issue #19730: Argument Clinic now supports all the existing PyArg
Larry Hastings <larry@hastings.org>
parents: 87319
diff changeset
1354 ZLIB_COMPRESS_METHODDEF
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1355 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1356 compressobj__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1357 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1358 crc32__doc__},
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1359 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1360 decompress__doc__},
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1361 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1362 decompressobj__doc__},
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1363 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1366 static PyTypeObject Comptype = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41829
diff changeset
1367 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
1368 "zlib.Compress",
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1369 sizeof(compobject),
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1370 0,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1371 (destructor)Comp_dealloc, /*tp_dealloc*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1372 0, /*tp_print*/
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1373 0, /*tp_getattr*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1374 0, /*tp_setattr*/
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 50453
diff changeset
1375 0, /*tp_reserved*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1376 0, /*tp_repr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1377 0, /*tp_as_number*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1378 0, /*tp_as_sequence*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1379 0, /*tp_as_mapping*/
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1380 0, /*tp_hash*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1381 0, /*tp_call*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1382 0, /*tp_str*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1383 0, /*tp_getattro*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1384 0, /*tp_setattro*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1385 0, /*tp_as_buffer*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1386 Py_TPFLAGS_DEFAULT, /*tp_flags*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1387 0, /*tp_doc*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1388 0, /*tp_traverse*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1389 0, /*tp_clear*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1390 0, /*tp_richcompare*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1391 0, /*tp_weaklistoffset*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1392 0, /*tp_iter*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1393 0, /*tp_iternext*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1394 comp_methods, /*tp_methods*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1397 static PyTypeObject Decomptype = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41829
diff changeset
1398 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
1399 "zlib.Decompress",
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1400 sizeof(compobject),
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1401 0,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1402 (destructor)Decomp_dealloc, /*tp_dealloc*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1403 0, /*tp_print*/
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1404 0, /*tp_getattr*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1405 0, /*tp_setattr*/
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 50453
diff changeset
1406 0, /*tp_reserved*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1407 0, /*tp_repr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1408 0, /*tp_as_number*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1409 0, /*tp_as_sequence*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1410 0, /*tp_as_mapping*/
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1411 0, /*tp_hash*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1412 0, /*tp_call*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1413 0, /*tp_str*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1414 0, /*tp_getattro*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1415 0, /*tp_setattro*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1416 0, /*tp_as_buffer*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1417 Py_TPFLAGS_DEFAULT, /*tp_flags*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1418 0, /*tp_doc*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1419 0, /*tp_traverse*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1420 0, /*tp_clear*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1421 0, /*tp_richcompare*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1422 0, /*tp_weaklistoffset*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1423 0, /*tp_iter*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1424 0, /*tp_iternext*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1425 Decomp_methods, /*tp_methods*/
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1426 Decomp_members, /*tp_members*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1427 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1428
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1429 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
1430 "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
1431 "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
1432 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1433 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
80386
9dde30690f44 Issue #15677: Document that zlib and gzip accept a compression level of 0 to mean 'no compression'.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 80354
diff changeset
1434 "compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1435 "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
1436 "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
1437 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
1438 "decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1439 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1440 "'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
1441 "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
1442 "objects support decompress() and flush().");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1443
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1444 static struct PyModuleDef zlibmodule = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1445 PyModuleDef_HEAD_INIT,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1446 "zlib",
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1447 zlib_module_documentation,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1448 -1,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1449 zlib_methods,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1450 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1451 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1452 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1453 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1454 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1455
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24283
diff changeset
1456 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13496
diff changeset
1457 PyInit_zlib(void)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1458 {
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1459 PyObject *m, *ver;
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1460 if (PyType_Ready(&Comptype) < 0)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1461 return NULL;
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1462 if (PyType_Ready(&Decomptype) < 0)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1463 return NULL;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1464 m = PyModule_Create(&zlibmodule);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35914
diff changeset
1465 if (m == NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1466 return NULL;
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1467
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1468 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
1469 if (ZlibError != NULL) {
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1470 Py_INCREF(ZlibError);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1471 PyModule_AddObject(m, "error", ZlibError);
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1472 }
83867
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1473 PyModule_AddIntMacro(m, MAX_WBITS);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1474 PyModule_AddIntMacro(m, DEFLATED);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1475 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1476 PyModule_AddIntMacro(m, Z_BEST_SPEED);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1477 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1478 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1479 PyModule_AddIntMacro(m, Z_FILTERED);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1480 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1481 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1482
83867
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1483 PyModule_AddIntMacro(m, Z_FINISH);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1484 PyModule_AddIntMacro(m, Z_NO_FLUSH);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1485 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
12cbb5183d98 Issue #17917: Use PyModule_AddIntMacro() instead of PyModule_AddIntConstant()
Charles-Francois Natali <cf.natali@gmail.com>
parents: 83676
diff changeset
1486 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1487
42750
ce52cd71a8d9 Convert a bunch of constant strings in C to unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
1488 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
1489 if (ver != NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1490 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
1491
72341
b21d1de6d78e Issue #12306: Add ZLIB_RUNTIME_VERSION to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72112
diff changeset
1492 ver = PyUnicode_FromString(zlibVersion());
b21d1de6d78e Issue #12306: Add ZLIB_RUNTIME_VERSION to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72112
diff changeset
1493 if (ver != NULL)
b21d1de6d78e Issue #12306: Add ZLIB_RUNTIME_VERSION to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72112
diff changeset
1494 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
b21d1de6d78e Issue #12306: Add ZLIB_RUNTIME_VERSION to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72112
diff changeset
1495
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1496 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
1497
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1498 return m;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1499 }