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