annotate Modules/zlibmodule.c @ 80272:ec00f8570c55

Merge typo fix.
author Ezio Melotti <ezio.melotti@gmail.com>
date Mon, 05 Nov 2012 22:23:16 +0200
parents bcdb3836be72
children 4440e45c10f9
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
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
11 #ifdef WITH_THREAD
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
12 #include "pythread.h"
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
13 #define ENTER_ZLIB(obj) \
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
14 Py_BEGIN_ALLOW_THREADS; \
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
15 PyThread_acquire_lock((obj)->lock, 1); \
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
16 Py_END_ALLOW_THREADS;
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
17 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
18 #else
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
19 #define ENTER_ZLIB(obj)
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
20 #define LEAVE_ZLIB(obj)
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
21 #endif
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
22
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 /* The following parameters are copied from zutil.h, version 0.95 */
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #define DEFLATED 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 #if MAX_MEM_LEVEL >= 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # define DEF_MEM_LEVEL 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #else
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #endif
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #define DEF_WBITS MAX_WBITS
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
10538
ccee3c28b7ef Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents: 10467
diff changeset
32 /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
ccee3c28b7ef Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents: 10467
diff changeset
33 #define DEFAULTALLOC (16*1024)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
35 static PyTypeObject Comptype;
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
36 static PyTypeObject Decomptype;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 static PyObject *ZlibError;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
40 typedef struct
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
42 PyObject_HEAD
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
43 z_stream zst;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
44 PyObject *unused_data;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
45 PyObject *unconsumed_tail;
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
46 char eof;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
47 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
48 PyObject *zdict;
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
49 #ifdef WITH_THREAD
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
50 PyThread_type_lock lock;
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
51 #endif
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 } compobject;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
54 static void
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
55 zlib_error(z_stream zst, int err, char *msg)
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
56 {
72111
cc9e794bf94f Issue #12839: Fix crash in zlib module due to version mismatch.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70119
diff changeset
57 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
58 /* 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
59 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
60 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
61 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
62 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
63 zmsg = zst.msg;
61523
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
64 if (zmsg == Z_NULL) {
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
65 switch (err) {
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
66 case Z_BUF_ERROR:
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
67 zmsg = "incomplete or truncated stream";
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
68 break;
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
69 case Z_STREAM_ERROR:
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
70 zmsg = "inconsistent stream state";
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
71 break;
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
72 case Z_DATA_ERROR:
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
73 zmsg = "invalid input data";
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
74 break;
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
75 }
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 if (zmsg == Z_NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
78 PyErr_Format(ZlibError, "Error %d %s", err, msg);
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
79 else
61523
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
80 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
81 }
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
82
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
83 PyDoc_STRVAR(compressobj__doc__,
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
84 "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
85 " 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
86 " -- 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
87 "\n"
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
88 "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
89 "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
90 "\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
91 "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
92 "\n"
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
93 "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
94 "\n"
77559
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
95 "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
96 "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
97 "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
98 "\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
99 "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
100 "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
101 "\n"
1cfa44cb5af0 Document the rest of zlib.compressobj()'s arguments.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77558
diff changeset
102 "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
103 "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
104
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
105 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
106 "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
107 "\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
108 "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
109 "\n"
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
110 "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
111 "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
112
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 static compobject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
114 newcompobject(PyTypeObject *type)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
116 compobject *self;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
117 self = PyObject_New(compobject, type);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
118 if (self == NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
119 return NULL;
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
120 self->eof = 0;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
121 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
122 self->zdict = NULL;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
123 self->unused_data = PyBytes_FromStringAndSize("", 0);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
124 if (self->unused_data == NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
125 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
126 return NULL;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
127 }
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
128 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
129 if (self->unconsumed_tail == NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
130 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
131 return NULL;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
132 }
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
133 #ifdef WITH_THREAD
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
134 self->lock = PyThread_allocate_lock();
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
135 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
136 return self;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
139 PyDoc_STRVAR(compress__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
140 "compress(string[, level]) -- Returned compressed string.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
141 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
142 "Optional arg level is the compression level, in 1-9.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
143
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
145 PyZlib_compress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
147 PyObject *ReturnVal = NULL;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
148 Py_buffer pinput;
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
149 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
150 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
151 int level=Z_DEFAULT_COMPRESSION, err;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
152 z_stream zst;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
153
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
154 /* require Python string object, optional 'level' arg */
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
155 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
156 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
157
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
158 if (pinput.len > UINT_MAX) {
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
159 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
160 "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
161 goto error;
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
162 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
163 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
164 length = (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
165
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
166 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
167
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
168 output = (Byte*)malloc(zst.avail_out);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
169 if (output == NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
170 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
171 "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
172 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 }
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
174
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
175 /* 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
176 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
177
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
178 zst.zalloc = (alloc_func)NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
179 zst.zfree = (free_func)Z_NULL;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
180 zst.next_out = (Byte *)output;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
181 zst.next_in = (Byte *)input;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
182 zst.avail_in = length;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
183 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
184
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
185 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 case(Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
187 break;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 case(Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
189 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
190 "Out of memory while compressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
191 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 case(Z_STREAM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
193 PyErr_SetString(ZlibError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
194 "Bad compression level");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
195 goto error;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
196 default:
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 deflateEnd(&zst);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
198 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
199 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 }
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
201
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
202 Py_BEGIN_ALLOW_THREADS;
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
203 err = deflate(&zst, Z_FINISH);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
204 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
205
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
206 if (err != Z_STREAM_END) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
207 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
208 deflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
209 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
210 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
211
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
212 err=deflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
213 if (err == Z_OK)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
214 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
215 zst.total_out);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
216 else
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
217 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
218
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
219 error:
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
220 PyBuffer_Release(&pinput);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
221 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
222
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
223 return ReturnVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
226 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
227 "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
228 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
229 "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
230 "the initial output buffer size.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
231
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
233 PyZlib_decompress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 {
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
235 PyObject *result_str = NULL;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
236 Py_buffer pinput;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
237 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
238 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
239 int err;
43957
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
240 int wsize=DEF_WBITS;
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
241 Py_ssize_t r_strlen=DEFAULTALLOC;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
242 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
243
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
244 if (!PyArg_ParseTuple(args, "y*|in:decompress",
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
245 &pinput, &wsize, &r_strlen))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
246 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
247
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
248 if (pinput.len > UINT_MAX) {
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
249 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
250 "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
251 goto error;
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
252 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
253 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
254 length = (unsigned int)pinput.len;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
255
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
256 if (r_strlen <= 0)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
257 r_strlen = 1;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
258
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
259 zst.avail_in = length;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
260 zst.avail_out = r_strlen;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
261
70103
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
262 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
850624632e9a Issue #8650: Backport 64-bit safety fixes for compress() and decompress().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70099
diff changeset
263 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
264
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
265 zst.zalloc = (alloc_func)NULL;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
266 zst.zfree = (free_func)Z_NULL;
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
267 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
268 zst.next_in = (Byte *)input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
269 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
270
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
271 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 case(Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
273 break;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
274 case(Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
275 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
276 "Out of memory while decompressing data");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
277 goto error;
20474
a5a30ba2899f Add zlib_error() helper.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20470
diff changeset
278 default:
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 inflateEnd(&zst);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
280 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
281 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
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
283
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
284 do {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
285 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
286 err=inflate(&zst, Z_FINISH);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
287 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
288
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
289 switch(err) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
290 case(Z_STREAM_END):
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
291 break;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
292 case(Z_BUF_ERROR):
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
293 /*
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
294 * 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
295 * 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
296 * 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
297 */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
298 if (zst.avail_out > 0) {
61523
48d9664c47a3 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61466
diff changeset
299 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
300 inflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
301 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
302 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
303 /* fall through */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
304 case(Z_OK):
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
305 /* need more memory */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
306 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
307 inflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
308 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
309 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
310 zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
311 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
312 zst.avail_out = r_strlen;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
313 r_strlen = r_strlen << 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
314 break;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
315 default:
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
316 inflateEnd(&zst);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
317 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
318 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
319 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
320 } 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
321
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
322 err = inflateEnd(&zst);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
323 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
324 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
325 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
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
327
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
328 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
329 goto error;
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
330
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
331 PyBuffer_Release(&pinput);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
332 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
333
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
334 error:
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
335 PyBuffer_Release(&pinput);
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
336 Py_XDECREF(result_str);
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
337 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 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
341 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
342 {
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
343 compobject *self;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
344 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
345 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
346 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
347 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
348 "memLevel", "strategy", "zdict", NULL};
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
349
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
350 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
351 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
352 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
353 &memLevel, &strategy, &zdict))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
354 return NULL;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
355
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
356 self = newcompobject(&Comptype);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
357 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
358 goto error;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
359 self->zst.zalloc = (alloc_func)NULL;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
360 self->zst.zfree = (free_func)Z_NULL;
34264
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
361 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
362 self->zst.avail_in = 0;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
363 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
364 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 case (Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
366 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
367 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
368 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
369 } else {
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
370 err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
371 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
372 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
373 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
374 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
375 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
376 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
377 default:
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
378 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
379 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
380 }
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
381 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 case (Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
383 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
384 "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
385 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 case(Z_STREAM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
387 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
388 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 default:
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
390 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
391 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 }
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
393
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
394 error:
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
395 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
396 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
397 success:
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
398 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
399 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
400 return (PyObject*)self;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 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
404 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
405 {
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
406 static char *kwlist[] = {"wbits", "zdict", NULL};
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
407 int wbits=DEF_WBITS, err;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
408 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
409 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
410
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
411 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
412 kwlist, &wbits, &zdict))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
413 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
414 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
415 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
416 "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
417 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
418 }
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
419
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
420 self = newcompobject(&Decomptype);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
421 if (self == NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
422 return(NULL);
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
423 self->zst.zalloc = (alloc_func)NULL;
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
424 self->zst.zfree = (free_func)Z_NULL;
34264
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 27653
diff changeset
425 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
426 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
427 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
428 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
429 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
430 }
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
431 err = inflateInit2(&self->zst, wbits);
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
432 switch(err) {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 case (Z_OK):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
434 self->is_initialised = 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
435 return (PyObject*)self;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
436 case(Z_STREAM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
437 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
438 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
439 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 case (Z_MEM_ERROR):
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
441 Py_DECREF(self);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
442 PyErr_SetString(PyExc_MemoryError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
443 "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
444 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 default:
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
446 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
447 Py_DECREF(self);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
448 return NULL;
20475
2f095c3e007d More reformatting.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20474
diff changeset
449 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 static void
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
453 Dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 {
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
455 #ifdef WITH_THREAD
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
456 PyThread_free_lock(self->lock);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
457 #endif
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
458 Py_XDECREF(self->unused_data);
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
459 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
460 Py_XDECREF(self->zdict);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
461 PyObject_Del(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 static void
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
465 Comp_dealloc(compobject *self)
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
466 {
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
467 if (self->is_initialised)
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
468 deflateEnd(&self->zst);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
469 Dealloc(self);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
470 }
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
471
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
472 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
473 Decomp_dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
475 if (self->is_initialised)
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
476 inflateEnd(&self->zst);
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
477 Dealloc(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
480 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
481 "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
482 "\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
483 "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
484 "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
485 "Call the flush() method to clear these buffers.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
486
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
487
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
489 PyZlib_objcompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 {
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
491 int err;
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
492 unsigned int inplen;
61370
bcb0cf924693 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57736
diff changeset
493 Py_ssize_t 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
494 PyObject *RetVal = NULL;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
495 Py_buffer pinput;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
496 Byte *input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
497 unsigned long start_total_out;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
498
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
499 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
500 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
501 if (pinput.len > UINT_MAX) {
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
502 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
503 "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
504 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
505 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
506 input = pinput.buf;
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
507 inplen = pinput.len;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
508
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
509 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
510 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
511
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
512 ENTER_ZLIB(self);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
513
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
514 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
515 self->zst.avail_in = inplen;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
516 self->zst.next_in = input;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
517 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
518 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
519
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
520 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
521 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
522 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
523
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
524 /* 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
525 so extend the output buffer and try again */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
526 while (err == Z_OK && self->zst.avail_out == 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
527 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
528 Py_DECREF(RetVal);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
529 RetVal = NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
530 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
531 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
532 self->zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
533 (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
534 self->zst.avail_out = length;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
535 length = length << 1;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
536
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
537 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
538 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
539 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
540 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
541 /* 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
542 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
543 condition.
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
544 */
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
545
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
546 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
547 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
548 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
549 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
550 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
551 }
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
552 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
553 Py_DECREF(RetVal);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
554 RetVal = NULL;
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
555 }
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
556
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
557 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
558 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
559 error_outer:
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
560 PyBuffer_Release(&pinput);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
561 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
564 PyDoc_STRVAR(decomp_decompress__doc__,
20485
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
565 "decompress(data, max_length) -- Return a string containing the decompressed\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
566 "version of the data.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
567 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
568 "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
569 "internal buffers for later processing.\n"
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
570 "Call the flush() method to clear these buffers.\n"
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
571 "If the max_length parameter is specified then the return value will be\n"
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
572 "no longer than max_length. Unconsumed input data will be stored in\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
573 "the unconsumed_tail attribute.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
574
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
576 PyZlib_objdecompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 {
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
578 int err, max_length = 0;
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
579 unsigned int inplen;
61370
bcb0cf924693 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57736
diff changeset
580 Py_ssize_t 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
581 PyObject *RetVal = NULL;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
582 Py_buffer pinput;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
583 Byte *input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
584 unsigned long start_total_out;
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
585
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
586 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
587 &max_length))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
588 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
589 if (pinput.len > UINT_MAX) {
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
590 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
591 "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
592 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
593 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
594 input = pinput.buf;
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
595 inplen = pinput.len;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
596 if (max_length < 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
597 PyErr_SetString(PyExc_ValueError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
598 "max_length must be greater than zero");
70118
0b5211d19a14 Issue #8650: Make zlib.[de]compressobj().[de]compress() 64-bit clean.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70103
diff changeset
599 goto error_outer;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
600 }
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
601
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
602 /* limit amount of data allocated to max_length */
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
603 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
604 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
605 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
606 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
607
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
608 ENTER_ZLIB(self);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
609
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
610 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
611 self->zst.avail_in = inplen;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
612 self->zst.next_in = input;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
613 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
614 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
615
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
616 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
617 err = inflate(&(self->zst), Z_SYNC_FLUSH);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
618 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
619
77528
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
620 if (err == Z_NEED_DICT && self->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
621 Py_buffer 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
622 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
623 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
624 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
625 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
626 }
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
627 err = inflateSetDictionary(&(self->zst), zdict_buf.buf, zdict_buf.len);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
628 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
629 if (err != 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
630 zlib_error(self->zst, err, "while decompressing data");
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
631 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
632 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
633 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
634 }
77558
4931cbc58cde Tidy up comments from dd4f7d5c51c7 (zlib compression dictionary support).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 77528
diff changeset
635 /* 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
636 Py_BEGIN_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
637 err = inflate(&(self->zst), Z_SYNC_FLUSH);
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
638 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
639 }
dd4f7d5c51c7 Issue #14684: Add support for predefined compression dictionaries to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72341
diff changeset
640
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
641 /* 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
642 So extend the output buffer and try again.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
643 */
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
644 while (err == Z_OK && self->zst.avail_out == 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
645 /* 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
646 reached the limit.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
647 */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
648 if (max_length && length >= max_length)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
649 break;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
650
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
651 /* otherwise, ... */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
652 old_length = length;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
653 length = length << 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
654 if (max_length && length > max_length)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
655 length = max_length;
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20327
diff changeset
656
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
657 if (_PyBytes_Resize(&RetVal, length) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
658 Py_DECREF(RetVal);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
659 RetVal = NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
660 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
661 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
662 self->zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
663 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
664 self->zst.avail_out = length - old_length;
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
665
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
666 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
667 err = inflate(&(self->zst), Z_SYNC_FLUSH);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
668 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
669 }
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
670
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
671 if(max_length) {
70099
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
672 /* Not all of the compressed data could be accommodated in a buffer of
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
673 the specified size. Return the unconsumed tail in an attribute. */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
674 Py_DECREF(self->unconsumed_tail);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
675 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
676 self->zst.avail_in);
70099
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
677 }
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
678 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
679 /* All of the compressed data was consumed. Clear unconsumed_tail. */
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
680 Py_DECREF(self->unconsumed_tail);
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
681 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
682 }
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
683 if (self->unconsumed_tail == NULL) {
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
684 Py_DECREF(RetVal);
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
685 RetVal = NULL;
6a45567c7245 Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68054
diff changeset
686 goto error;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
688
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
689 /* The end of the compressed data has been reached, so set the
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
690 unused_data attribute to a string containing the remainder of the
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
691 data in the string. Note that this is also a logical place to call
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
692 inflateEnd, but the old behaviour of only calling it on flush() is
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
693 preserved.
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
694 */
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
695 if (err == Z_STREAM_END) {
80255
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
696 if (self->zst.avail_in > 0) {
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
697 /* Append the leftover data to the existing value of unused_data. */
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
698 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
699 Py_ssize_t new_size = old_size + self->zst.avail_in;
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
700 PyObject *new_data;
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
701 if (new_size <= old_size) { /* Check for overflow. */
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
702 PyErr_NoMemory();
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
703 Py_DECREF(RetVal);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
704 RetVal = NULL;
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
705 goto error;
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
706 }
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
707 new_data = PyBytes_FromStringAndSize(NULL, new_size);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
708 if (new_data == NULL) {
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
709 Py_DECREF(RetVal);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
710 RetVal = NULL;
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
711 goto error;
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
712 }
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
713 Py_MEMCPY(PyBytes_AS_STRING(new_data),
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
714 PyBytes_AS_STRING(self->unused_data), old_size);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
715 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
716 self->zst.next_in, self->zst.avail_in);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
717 Py_DECREF(self->unused_data);
be882735e0b6 Issue #16350: Fix zlib decompressor handling of unused_data with multiple calls to decompress() after EOF.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72111
diff changeset
718 self->unused_data = new_data;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
719 }
71857
bb6c2d5c811d Issue #12646: Add an 'eof' attribute to zlib.Decompress.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 70120
diff changeset
720 self->eof = 1;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
721 /* 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
722 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
723 not an error condition.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
724 */
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
725 } else 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
726 zlib_error(self->zst, err, "while decompressing data");
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
727 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
728 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
729 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
730 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
731
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
732 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
733 Py_DECREF(RetVal);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
734 RetVal = NULL;
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
735 }
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
736
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
737 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
738 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
739 error_outer:
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
740 PyBuffer_Release(&pinput);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
741 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
743
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
744 PyDoc_STRVAR(comp_flush__doc__,
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
745 "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
746 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
747 "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
748 "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
749 "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
750 "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
751
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
753 PyZlib_flush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 {
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
755 int err, length = DEFAULTALLOC;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
756 PyObject *RetVal;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
757 int flushmode = Z_FINISH;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
758 unsigned long start_total_out;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
759
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
760 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
761 return NULL;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
762
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
763 /* 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
764 doing any work at all; just return an empty string. */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
765 if (flushmode == Z_NO_FLUSH) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
766 return PyBytes_FromStringAndSize(NULL, 0);
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
767 }
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
768
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
769 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
770 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
771
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
772 ENTER_ZLIB(self);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
773
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
774 start_total_out = self->zst.total_out;
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
775 self->zst.avail_in = 0;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
776 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
777 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
778
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
779 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
780 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
781 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
782
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
783 /* 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
784 so extend the output buffer and try again */
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
785 while (err == Z_OK && self->zst.avail_out == 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
786 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
787 Py_DECREF(RetVal);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
788 RetVal = NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
789 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
790 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
791 self->zst.next_out =
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
792 (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
793 self->zst.avail_out = length;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
794 length = length << 1;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
795
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
796 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
797 err = deflate(&(self->zst), flushmode);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
798 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
799 }
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
800
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
801 /* 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
802 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
803 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
804 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
805 err = deflateEnd(&(self->zst));
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
806 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
807 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
808 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
809 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
810 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
811 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
812 else
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
813 self->is_initialised = 0;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
814
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
815 /* 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
816 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
817 not an error condition.
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
818 */
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
819 } 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
820 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
821 Py_DECREF(RetVal);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
822 RetVal = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
823 goto error;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
824 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
825
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
826 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
827 Py_DECREF(RetVal);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
828 RetVal = NULL;
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
829 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
830
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
831 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
832 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
833
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
834 return RetVal;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
836
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
837 #ifdef HAVE_ZLIB_COPY
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
838 PyDoc_STRVAR(comp_copy__doc__,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
839 "copy() -- Return a copy of the compression object.");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
840
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
841 static PyObject *
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
842 PyZlib_copy(compobject *self)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
843 {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
844 compobject *retval = NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
845 int err;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
846
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
847 retval = newcompobject(&Comptype);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
848 if (!retval) return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
849
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
850 /* Copy the zstream state
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
851 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
852 */
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
853 ENTER_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
854 err = deflateCopy(&retval->zst, &self->zst);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
855 switch(err) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
856 case(Z_OK):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
857 break;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
858 case(Z_STREAM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
859 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
860 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
861 case(Z_MEM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
862 PyErr_SetString(PyExc_MemoryError,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
863 "Can't allocate memory for compression object");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
864 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
865 default:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
866 zlib_error(self->zst, err, "while copying compression object");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
867 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
868 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
869 Py_INCREF(self->unused_data);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
870 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
871 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: 37879
diff changeset
872 Py_XDECREF(retval->unused_data);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
873 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
874 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: 37879
diff changeset
875 retval->unused_data = self->unused_data;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
876 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
877 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
878 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: 37879
diff changeset
879
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
880 /* Mark it as being initialized */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
881 retval->is_initialised = 1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
882
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
883 LEAVE_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
884 return (PyObject *)retval;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
885
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
886 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
887 LEAVE_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
888 Py_XDECREF(retval);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
889 return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
890 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
891
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
892 PyDoc_STRVAR(decomp_copy__doc__,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
893 "copy() -- Return a copy of the decompression object.");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
894
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
895 static PyObject *
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
896 PyZlib_uncopy(compobject *self)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
897 {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
898 compobject *retval = NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
899 int err;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
900
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
901 retval = newcompobject(&Decomptype);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
902 if (!retval) return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
903
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
904 /* Copy the zstream state
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
905 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
906 */
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
907 ENTER_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
908 err = inflateCopy(&retval->zst, &self->zst);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
909 switch(err) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
910 case(Z_OK):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
911 break;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
912 case(Z_STREAM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
913 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
914 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
915 case(Z_MEM_ERROR):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
916 PyErr_SetString(PyExc_MemoryError,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
917 "Can't allocate memory for decompression object");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
918 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
919 default:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
920 zlib_error(self->zst, err, "while copying decompression object");
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
921 goto error;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
922 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
923
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
924 Py_INCREF(self->unused_data);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
925 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
926 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: 37879
diff changeset
927 Py_XDECREF(retval->unused_data);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
928 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
929 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: 37879
diff changeset
930 retval->unused_data = self->unused_data;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
931 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
932 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
933 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: 37879
diff changeset
934
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
935 /* Mark it as being initialized */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
936 retval->is_initialised = 1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
937
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
938 LEAVE_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
939 return (PyObject *)retval;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
940
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
941 error:
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
942 LEAVE_ZLIB(self);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
943 Py_XDECREF(retval);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
944 return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
945 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
946 #endif
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
947
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
948 PyDoc_STRVAR(decomp_flush__doc__,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36324
diff changeset
949 "flush( [length] ) -- Return a string containing any remaining\n"
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36324
diff changeset
950 "decompressed data. length, if given, is the initial size of the\n"
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36324
diff changeset
951 "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
952 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
953 "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
954
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
956 PyZlib_unflush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 {
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
958 int err, length = DEFAULTALLOC;
20477
c23989e22ffa Simplify and fix error handling for most cases.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20475
diff changeset
959 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
960 unsigned long start_total_out;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
961
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
962 if (!PyArg_ParseTuple(args, "|i:flush", &length))
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
963 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
964 if (length <= 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
965 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
966 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
967 }
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
968 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
969 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
970
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
971
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
972 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
973
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
974 start_total_out = self->zst.total_out;
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
975 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
976 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
977
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
978 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
979 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
980 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
981
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
982 /* 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
983 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
984 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
985 if (_PyBytes_Resize(&retval, length << 1) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
986 Py_DECREF(retval);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
987 retval = NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
988 goto error;
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
989 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
990 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
991 self->zst.avail_out = length;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
992 length = length << 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
993
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
994 Py_BEGIN_ALLOW_THREADS
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
995 err = inflate(&(self->zst), Z_FINISH);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
996 Py_END_ALLOW_THREADS
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
997 }
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
998
71858
65d61ed991d9 Fix incorrect comment in zlib.Decompress.flush().
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71857
diff changeset
999 /* 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
1000 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
1001 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
1002 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
1003 err = inflateEnd(&(self->zst));
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1004 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
1005 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
1006 Py_DECREF(retval);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1007 retval = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1008 goto error;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1009 }
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1010 }
49051
c1a8f6b648ea Fixes release blocker issue #3492 and #3790.
Gregory P. Smith <greg@mad-scientist.com>
parents: 48683
diff changeset
1011 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
41829
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1012 Py_DECREF(retval);
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1013 retval = NULL;
f7f2a49c1f2e zlib now uses bytes everywhere, and the test passes.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1014 }
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1015
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1016 error:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1017
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1018 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
1019
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1020 return retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 static PyMethodDef comp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1025 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1026 comp_compress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1027 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1028 comp_flush__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1029 #ifdef HAVE_ZLIB_COPY
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1030 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1031 comp_copy__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1032 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1033 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 static PyMethodDef Decomp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1038 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1039 decomp_decompress__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1040 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1041 decomp_flush__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1042 #ifdef HAVE_ZLIB_COPY
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1043 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1044 decomp_copy__doc__},
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1045 #endif
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1046 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1049 #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
1050 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
1051 {"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
1052 {"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
1053 {"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
1054 {NULL},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1055 };
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1057 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
1058 "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
1059 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1060 "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
1061 "an integer.");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1062
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
1064 PyZlib_adler32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 {
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
1066 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
1067 Py_buffer pbuf;
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1068
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
1069 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
1070 return NULL;
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1071 /* 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
1072 and may lower performance */
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1073 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
1074 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
1075 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
1076
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1077 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
1078 /* 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
1079 length as an unsigned int, which may be narrower than Py_ssize_t. */
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1080 while (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
1081 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
1082 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
1083 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
1084 }
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1085 adler32val = adler32(adler32val, buf, len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1086 Py_END_ALLOW_THREADS
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1087 } else {
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
1088 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1089 }
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1090 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
1091 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1093
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1094 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
1095 "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
1096 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1097 "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
1098 "an integer.");
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
1101 PyZlib_crc32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 {
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
1103 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
1104 Py_buffer pbuf;
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
1105 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
1106
57736
919e2142726e Merged revisions 76836 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
1107 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
1108 return NULL;
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1109 /* 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
1110 and may lower performance */
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1111 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
1112 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
1113 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
1114
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1115 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
1116 /* 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
1117 length as an unsigned int, which may be narrower than Py_ssize_t. */
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1118 while (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
1119 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
1120 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
1121 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
1122 }
f8a77fd675f5 Merged revisions 88460,88464,88466,88486,88511,88652 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61523
diff changeset
1123 signed_val = crc32(crc32val, buf, len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1124 Py_END_ALLOW_THREADS
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1125 } else {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1126 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
50453
400cfcaa8d46 Issue #4738: finer-grained locking in the zlib module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49051
diff changeset
1127 }
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48413
diff changeset
1128 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
1129 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 }
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1131
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 static PyMethodDef zlib_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134 {
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1135 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1136 adler32__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1137 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1138 compress__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
1139 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1140 compressobj__doc__},
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1141 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1142 crc32__doc__},
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1143 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1144 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
1145 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1146 decompressobj__doc__},
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1147 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1150 static PyTypeObject Comptype = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41829
diff changeset
1151 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
1152 "zlib.Compress",
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1153 sizeof(compobject),
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1154 0,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1155 (destructor)Comp_dealloc, /*tp_dealloc*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1156 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
1157 0, /*tp_getattr*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1158 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
1159 0, /*tp_reserved*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1160 0, /*tp_repr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1161 0, /*tp_as_number*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1162 0, /*tp_as_sequence*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1163 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
1164 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
1165 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
1166 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
1167 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
1168 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
1169 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
1170 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
1171 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
1172 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
1173 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
1174 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
1175 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
1176 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
1177 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
1178 comp_methods, /*tp_methods*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1181 static PyTypeObject Decomptype = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41829
diff changeset
1182 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
1183 "zlib.Decompress",
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1184 sizeof(compobject),
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1185 0,
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1186 (destructor)Decomp_dealloc, /*tp_dealloc*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1187 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
1188 0, /*tp_getattr*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1189 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
1190 0, /*tp_reserved*/
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1191 0, /*tp_repr*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1192 0, /*tp_as_number*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1193 0, /*tp_as_sequence*/
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1194 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
1195 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
1196 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
1197 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
1198 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
1199 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
1200 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
1201 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
1202 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
1203 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
1204 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
1205 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
1206 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
1207 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
1208 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
1209 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
1210 Decomp_members, /*tp_members*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1213 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
1214 "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
1215 "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
1216 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1217 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1218 "compress(string[, level]) -- Compress string, with compression level in 1-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
1219 "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
1220 "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
1221 "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
1222 "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
1223 "\n"
4af8d24fa691 Simplify and regularize docstrings. Also reformat so that each docstring
Tim Peters <tim.peters@gmail.com>
parents: 20484
diff changeset
1224 "'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
1225 "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
1226 "objects support decompress() and flush().");
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1227
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1228 static struct PyModuleDef zlibmodule = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1229 PyModuleDef_HEAD_INIT,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1230 "zlib",
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1231 zlib_module_documentation,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1232 -1,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1233 zlib_methods,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1234 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1235 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1236 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1237 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1238 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1239
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24283
diff changeset
1240 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13496
diff changeset
1241 PyInit_zlib(void)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242 {
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1243 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
1244 if (PyType_Ready(&Comptype) < 0)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1245 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
1246 if (PyType_Ready(&Decomptype) < 0)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1247 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
1248 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
1249 if (m == NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1250 return NULL;
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1251
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1252 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
1253 if (ZlibError != NULL) {
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22217
diff changeset
1254 Py_INCREF(ZlibError);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1255 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
1256 }
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1257 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1258 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1259 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1260 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1261 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1262 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1263 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1264 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1265 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1266
20469
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1267 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1268 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1269 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
c46e94fd6986 Reformat!
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20466
diff changeset
1270 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
20484
1a9494f9edb2 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 20483
diff changeset
1271
42750
ce52cd71a8d9 Convert a bunch of constant strings in C to unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
1272 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
1273 if (ver != NULL)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61370
diff changeset
1274 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
1275
72341
b21d1de6d78e Issue #12306: Add ZLIB_RUNTIME_VERSION to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72112
diff changeset
1276 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
1277 if (ver != NULL)
b21d1de6d78e Issue #12306: Add ZLIB_RUNTIME_VERSION to the zlib module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72112
diff changeset
1278 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
1279
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24363
diff changeset
1280 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
1281
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47300
diff changeset
1282 return m;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 }