annotate Modules/zlibmodule.c @ 19814:dc87d91d0dfd legacy-trunk

Don't call MacOS.SchedParams() in MachO, it doesn't exist.
author Jack Jansen <jack.jansen@cwi.nl>
date Sun, 09 Sep 2001 00:36:01 +0000
parents ce57fff93962
children 54f05b9b1fc4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* zlibmodule.c -- gzip-compatible data compression */
16368
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
2 /* See http://www.info-zip.org/pub/infozip/zlib/ */
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
3
16369
c04ca6cba1e3 Docs for new Windows zlib build procedure.
Tim Peters <tim.peters@gmail.com>
parents: 16368
diff changeset
4 /* Windows users: read Python's PCbuild\readme.txt */
16368
c696a52c2621 Fix [ Bug #129293 ] zlib library used for binary win32 distribution can crash
Mark Hammond <mhammond@skippinet.com.au>
parents: 15098
diff changeset
5
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
7 #include "Python.h"
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
8 #include "zlib.h"
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
10 #ifdef WITH_THREAD
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
11 #include "pythread.h"
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
12
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
13 /* #defs ripped off from _tkinter.c, even though the situation here is much
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
14 simpler, because we don't have to worry about waiting for Tcl
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
15 events! And, since zlib itself is threadsafe, we don't need to worry
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
16 about re-entering zlib functions.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
17
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
18 What we _do_ have to worry about is releasing the global lock _in
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
19 general_ in the zlibmodule functions, because of all the calls to
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
20 Python functions, which assume that the global lock is held. So
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 only two types of calls are wrapped in Py_BEGIN/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
22 those that grab the zlib lock, and those that involve other
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
23 time-consuming functions where we need to worry about holding up
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
24 other Python 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
25
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
26 We don't need to worry about the string inputs being modified out
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
27 from underneath us, because string objects are immutable. However,
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
28 we do need to make sure we take on ownership, so that the strings
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
29 are not deleted out from under us during a thread swap.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
30
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
31 N.B.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
32
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
33 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
34 that modify the components of preexisting de/compress objects, it
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
35 could prove to be a performance gain on multiprocessor machines if
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
36 there was an de/compress object-specific lock. However, for the
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
37 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
38 de/compress objects.
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
39
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
40 */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
41
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
42 static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
43
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
44 #define ENTER_ZLIB \
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
45 { Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(zlib_lock, 1); \
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
46 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
47
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
48 #define LEAVE_ZLIB \
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
49 PyThread_release_lock(zlib_lock); }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
50
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
51 #else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
52
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
53 #define ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
54 #define LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
55
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
56 #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
57
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 /* 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
59 #define DEFLATED 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #if MAX_MEM_LEVEL >= 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # define DEF_MEM_LEVEL 8
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 #else
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 #endif
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #define DEF_WBITS MAX_WBITS
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
10538
ccee3c28b7ef Patch by Andrew Kuchling to unflush() (flush() for deflating).
Guido van Rossum <guido@python.org>
parents: 10467
diff changeset
67 /* 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
68 #define DEFAULTALLOC (16*1024)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 #define PyInit_zlib initzlib
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 staticforward PyTypeObject Comptype;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 staticforward PyTypeObject Decomptype;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 static PyObject *ZlibError;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 typedef struct
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 PyObject_HEAD
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 z_stream zst;
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
80 PyObject *unused_data;
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
81 int is_initialised;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 } compobject;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
84 static char compressobj__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
85 "compressobj() -- Return a compressor object.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
86 "compressobj(level) -- Return a compressor object, using the given compression level.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
87 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
88
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
89 static char decompressobj__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
90 "decompressobj() -- Return a decompressor object.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
91 "decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
92 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
93
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 static compobject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
95 newcompobject(PyTypeObject *type)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
97 compobject *self;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
98 self = PyObject_New(compobject, type);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if (self == NULL)
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return NULL;
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
101 self->is_initialised = 0;
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
102 self->unused_data = PyString_FromString("");
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 return self;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
106 static char compress__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
107 "compress(string) -- Compress string using the default compression level, "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
108 "returning a string containing compressed data.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
109 "compress(string, level) -- Compress string, using the chosen compression "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
110 "level (from 1 to 9). Return a string containing the compressed data.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
111 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
112
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
114 PyZlib_compress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 {
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
116 PyObject *ReturnVal = NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 Byte *input, *output;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 int length, level=Z_DEFAULT_COMPRESSION, err;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 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
120 int return_error;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
121 PyObject * inputString;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
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
123 /* require Python string object, optional 'level' arg */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
124 if (!PyArg_ParseTuple(args, "S|i:compress", &inputString, &level))
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 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
126
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
127 /* now get a pointer to the internal string */
19810
ce57fff93962 Silence warnings about passing unsigned char** as char**.
Martin v. Löwis <martin@v.loewis.de>
parents: 19798
diff changeset
128 if (PyString_AsStringAndSize(inputString, (char**)&input, &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
129 return NULL;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
130
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
131 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
132
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 output=(Byte*)malloc(zst.avail_out);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if (output==NULL)
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 "Can't allocate memory to compress data");
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
138 free(output);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
139
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 }
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
142
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
143 /* Past the point of no return. From here on out, we need to make sure
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
144 we clean up mallocs & INCREFs. */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
145
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
146 Py_INCREF(inputString); /* increment so that we hold ref */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
147
9662
9ba7f50b825a replace missing zalloc initialization (test_zlib now runs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9661
diff changeset
148 zst.zalloc=(alloc_func)NULL;
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
149 zst.zfree=(free_func)Z_NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 zst.next_out=(Byte *)output;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 zst.next_in =(Byte *)input;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 zst.avail_in=length;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 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
154
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
155 return_error = 0;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 switch(err)
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 case(Z_OK):
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 break;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 case(Z_MEM_ERROR):
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 "Out of memory while compressing data");
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
163 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
164 break;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 case(Z_STREAM_ERROR):
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 PyErr_SetString(ZlibError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 "Bad compression 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
168 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
169 break;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 default:
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
172 if (zst.msg == Z_NULL)
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
173 PyErr_Format(ZlibError, "Error %i while compressing data",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
174 err);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
175 else
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
176 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
177 err, zst.msg);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 deflateEnd(&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
179 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 }
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
182
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
183 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
184 Py_BEGIN_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
185 err=deflate(&zst, Z_FINISH);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
186 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
187
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
188 switch(err)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
189 {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
190 case(Z_STREAM_END):
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
191 break;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
192 /* Are there other errors to be trapped here? */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
193 default:
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
194 {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
195 if (zst.msg == Z_NULL)
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
196 PyErr_Format(ZlibError, "Error %i while compressing data",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
197 err);
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
198 else
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
199 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
200 err, zst.msg);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
201
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
202 deflateEnd(&zst);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
203
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
204 return_error = 1;
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 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
206 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
207
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
208 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
209 err=deflateEnd(&zst);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
210 if (err == Z_OK)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
211 ReturnVal = PyString_FromStringAndSize((char *)output, zst.total_out);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
212 else {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
213 {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
214 if (zst.msg == Z_NULL)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
215 PyErr_Format(ZlibError, "Error %i while finishing compression",
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
216 err);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
217 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
218 PyErr_Format(ZlibError,
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
219 "Error %i while finishing compression: %.200s",
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
220 err, zst.msg);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
221 }
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 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
223
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
224 }
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
225 }
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
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 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
228 Py_DECREF(inputString);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
229
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 return ReturnVal;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
233 static char decompress__doc__[] =
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
234 "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
235 "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
236 "decompress(string, wbits, bufsize) -- Decompress the data in string with a window buffer size of wbits and an initial output buffer size of bufsize.\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
237 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
238
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
240 PyZlib_decompress(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 {
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
242 PyObject *result_str;
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
243 Byte *input;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 int length, err;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
245 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 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
247 int return_error;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
248 PyObject * inputString;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
249
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
250 if (!PyArg_ParseTuple(args, "S|ii:decompress", &inputString, &wsize, &r_strlen))
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
251 return NULL;
19810
ce57fff93962 Silence warnings about passing unsigned char** as char**.
Martin v. Löwis <martin@v.loewis.de>
parents: 19798
diff changeset
252 if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 return NULL;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
254
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
255 if (r_strlen <= 0)
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
256 r_strlen = 1;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
257
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 zst.avail_in=length;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
259 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
260
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
261 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
262 {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 "Can't allocate memory to decompress data");
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 return NULL;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
266 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
267
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
268 /* Past the point of no return. From here on out, we need to make sure
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
269 we clean up mallocs & INCREFs. */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
270
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
271 Py_INCREF(inputString); /* increment so that we hold ref */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
272
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
273 zst.zalloc=(alloc_func)NULL;
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
274 zst.zfree=(free_func)Z_NULL;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
275 zst.next_out=(Byte *)PyString_AsString(result_str);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 zst.next_in =(Byte *)input;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
277 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
278
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
279 return_error = 0;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 switch(err)
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 case(Z_OK):
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 break;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 case(Z_MEM_ERROR):
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 "Out of memory while decompressing data");
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
287 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 default:
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
290 if (zst.msg == Z_NULL)
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
291 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
292 err);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
293 else
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
294 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
295 "Error %i while preparing to decompress data: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
296 err, zst.msg);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 inflateEnd(&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
298
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
299 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
302
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 do
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 if (return_error)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
306 break;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
307
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
308 Py_BEGIN_ALLOW_THREADS
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 err=inflate(&zst, Z_FINISH);
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 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
311
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 switch(err)
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 {
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
314 case(Z_STREAM_END):
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
315 break;
8128
fdba6397b729 Tiny patch for zlib 1.1.2
Guido van Rossum <guido@python.org>
parents: 6911
diff changeset
316 case(Z_BUF_ERROR):
15098
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
317 /*
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
318 * If there is at least 1 byte of room according to zst.avail_out
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
319 * and we get this error, assume that it means zlib cannot
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
320 * process the inflate call() due to an error in the data.
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
321 */
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
322 if (zst.avail_out > 0)
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
323 {
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
324 PyErr_Format(ZlibError, "Error %i while decompressing data",
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
325 err);
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
326 inflateEnd(&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
327 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
328 break;
15098
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
329 }
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
330 /* fall through */
7de0808f1fe3 Patch #101810: check whether zst.avail_out is non-zero when getting
Andrew M. Kuchling <amk@amk.ca>
parents: 13816
diff changeset
331 case(Z_OK):
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
332 /* need more memory */
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
333 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 "Out of memory while decompressing data");
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 inflateEnd(&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
338 result_str = NULL;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
339 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 }
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6042
diff changeset
341 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
342 zst.avail_out=r_strlen;
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
343 r_strlen = r_strlen << 1;
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
344 break;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 default:
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
347 if (zst.msg == Z_NULL)
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
348 PyErr_Format(ZlibError, "Error %i while decompressing data",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
349 err);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
350 else
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
351 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
352 "Error %i while decompressing data: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
353 err, zst.msg);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 inflateEnd(&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
355 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 } 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
359
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
360 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
361 err=inflateEnd(&zst);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
362 if (err!=Z_OK)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
363 {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
364 if (zst.msg == Z_NULL)
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
365 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
366 "Error %i while finishing data decompression",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
367 err);
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
368 else
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
369 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
370 "Error %i while finishing data decompression: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
371 err, zst.msg);
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
372
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
373 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 }
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
376 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
377
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
378 if (!return_error)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
379 _PyString_Resize(&result_str, zst.total_out);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
380 else {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
381 Py_XDECREF(result_str); /* sets result_str == NULL, if not already */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
382 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
383 Py_DECREF(inputString);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
384
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
385 return result_str;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
389 PyZlib_compressobj(PyObject *selfptr, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 compobject *self;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
394
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11619
diff changeset
395 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
396 &memLevel, &strategy))
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
397 return NULL;
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
398
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
399 self = newcompobject(&Comptype);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 if (self==NULL) return(NULL);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
401 self->zst.zalloc = (alloc_func)NULL;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
402 self->zst.zfree = (free_func)Z_NULL;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
403 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 switch(err)
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 case (Z_OK):
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
407 self->is_initialised = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 return (PyObject*)self;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 case (Z_MEM_ERROR):
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
410 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 "Can't allocate memory for compression object");
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 case(Z_STREAM_ERROR):
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
415 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 PyErr_SetString(PyExc_ValueError,
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
417 "Invalid initialization option");
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 default:
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
421 if (self->zst.msg == Z_NULL)
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
422 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
423 "Error %i while creating compression object",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
424 err);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
425 else
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
426 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
427 "Error %i while creating compression object: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
428 err, self->zst.msg);
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
429 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
436 PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 int wbits=DEF_WBITS, err;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 compobject *self;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11619
diff changeset
440 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 self=newcompobject(&Decomptype);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 if (self==NULL) return(NULL);
5723
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
446 self->zst.zalloc=(alloc_func)NULL;
beee351f1f72 Small changes (casts etc.) by Jack, for Mac compilation.
Guido van Rossum <guido@python.org>
parents: 5722
diff changeset
447 self->zst.zfree=(free_func)Z_NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 err=inflateInit2(&self->zst, wbits);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 switch(err)
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
450 {
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 case (Z_OK):
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
452 self->is_initialised = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 return (PyObject*)self;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
454 case(Z_STREAM_ERROR):
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
455 Py_DECREF(self);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
456 PyErr_SetString(PyExc_ValueError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
457 "Invalid initialization option");
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
458 return NULL;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 case (Z_MEM_ERROR):
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
460 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 PyErr_SetString(PyExc_MemoryError,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 "Can't allocate memory for decompression object");
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 default:
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
465 {
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
466 if (self->zst.msg == Z_NULL)
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
467 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
468 "Error %i while creating decompression object",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
469 err);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
470 else
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
471 PyErr_Format(ZlibError,
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
472 "Error %i while creating decompression object: %.200s",
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
473 err, self->zst.msg);
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
474 Py_DECREF(self);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 return NULL;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 }
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
477 }
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
481 Comp_dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 {
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
483 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
484
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
485 if (self->is_initialised)
167adf31786d Added missing DECREF's in the error branches when creating a compressor or
Andrew M. Kuchling <amk@amk.ca>
parents: 9995
diff changeset
486 deflateEnd(&self->zst);
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
487 Py_XDECREF(self->unused_data);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
488 PyObject_Del(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
489
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
490 LEAVE_ZLIB
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
494 Decomp_dealloc(compobject *self)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 {
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
496 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
497
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
498 if (self->is_initialised)
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
499 inflateEnd(&self->zst);
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
500 Py_XDECREF(self->unused_data);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12255
diff changeset
501 PyObject_Del(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
502
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
503 LEAVE_ZLIB
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
506 static char comp_compress__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
507 "compress(data) -- Return a string containing a compressed version of the data.\n\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
508 "After calling this function, some of the input data may still\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
509 "be stored in internal buffers for later processing.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
510 "Call the flush() method to clear these buffers."
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
511 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
512
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
513
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
515 PyZlib_objcompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
517 int err, inplen, length = DEFAULTALLOC;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 PyObject *RetVal;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 Byte *input;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
520 unsigned long start_total_out;
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
521 int return_error;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
522 PyObject * inputString;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
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
524 if (!PyArg_ParseTuple(args, "S:compress", &inputString))
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
525 return NULL;
19810
ce57fff93962 Silence warnings about passing unsigned char** as char**.
Martin v. Löwis <martin@v.loewis.de>
parents: 19798
diff changeset
526 if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -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
527 return NULL;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
528
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
529 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
530 PyErr_SetString(PyExc_MemoryError,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
531 "Can't allocate memory to compress data");
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
532 return NULL;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
533 }
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
534
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 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
536
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
537 Py_INCREF(inputString);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
538
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
539 start_total_out = self->zst.total_out;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
540 self->zst.avail_in = inplen;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
541 self->zst.next_in = input;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
542 self->zst.avail_out = length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
543 self->zst.next_out = (unsigned char *)PyString_AsString(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
544
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
545 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
546 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
547 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
548
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
549 return_error = 0;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
550
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
551 /* while Z_OK and the output buffer is full, there might be more output,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
552 so extend the output buffer and try again */
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
553 while (err == Z_OK && self->zst.avail_out == 0) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
554 if (_PyString_Resize(&RetVal, length << 1) == -1) {
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
555 PyErr_SetString(PyExc_MemoryError,
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
556 "Can't allocate memory to compress data");
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
557 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
558 break;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
559 }
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
560 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
561 self->zst.avail_out = length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
562 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
563
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
564 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
565 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
566 Py_END_ALLOW_THREADS
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
567 }
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
568 /* We will only get Z_BUF_ERROR if the output buffer was full but there
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
569 wasn't more output when we tried again, so it is not an error condition */
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
570
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
571 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
572 if (err != Z_OK && err != Z_BUF_ERROR) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
573 if (self->zst.msg == Z_NULL)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
574 PyErr_Format(ZlibError, "Error %i while compressing",
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
575 err);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
576 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
577 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
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 err, self->zst.msg);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
579
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
580 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
581 Py_DECREF(RetVal);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
582 }
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
583 }
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
584
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
585 if (return_error)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
586 RetVal = NULL; /* should have been handled by DECREF */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
587 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
588 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
589
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
590 Py_DECREF(inputString);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
591
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
592 LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
593
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 return RetVal;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
597 static char decomp_decompress__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
598 "decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
599 "After calling this function, some of the input data may still\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
600 "be stored in internal buffers for later processing.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
601 "Call the flush() method to clear these buffers."
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
602 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
603
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
605 PyZlib_objdecompress(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
607 int err, inplen, length = DEFAULTALLOC;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 PyObject *RetVal;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 Byte *input;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
610 unsigned long start_total_out;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
611 int return_error;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
612 PyObject * inputString;
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
613
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
614 if (!PyArg_ParseTuple(args, "S:decompress", &inputString))
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 return NULL;
19810
ce57fff93962 Silence warnings about passing unsigned char** as char**.
Martin v. Löwis <martin@v.loewis.de>
parents: 19798
diff changeset
616 if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -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
617 return NULL;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
618
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
619 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
620 PyErr_SetString(PyExc_MemoryError,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
621 "Can't allocate memory to compress data");
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
622 return NULL;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
623 }
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
624
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
625 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
626 return_error = 0;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
627
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
628 Py_INCREF(inputString);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
629
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
630 start_total_out = self->zst.total_out;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
631 self->zst.avail_in = inplen;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
632 self->zst.next_in = input;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
633 self->zst.avail_out = length;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6042
diff changeset
634 self->zst.next_out = (unsigned char *)PyString_AsString(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
635
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
636 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
637 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
638 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
639
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
640 /* while Z_OK and the output buffer is full, there might be more output,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
641 so extend the output buffer and try again */
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
642 while (err == Z_OK && self->zst.avail_out == 0) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
643 if (_PyString_Resize(&RetVal, length << 1) == -1) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
644 PyErr_SetString(PyExc_MemoryError,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
645 "Can't allocate memory to compress data");
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
646 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
647 break;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
648 }
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
649 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
650 self->zst.avail_out = length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
651 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
652 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
653 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
654 Py_END_ALLOW_THREADS
6007
9ba72fc4f503 Many changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6003
diff changeset
655 }
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
656
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
657 /* The end of the compressed data has been reached, so set the unused_data
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
658 attribute to a string containing the remainder of the data in the string.
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
659 Note that this is also a logical place to call inflateEnd, but the old
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
660 behaviour of only calling it on flush() is preserved.*/
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
661 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
662 if (err == Z_STREAM_END) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
663 Py_XDECREF(self->unused_data); /* Free the original, empty string */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
664 self->unused_data = PyString_FromStringAndSize((char *)self->zst.next_in,
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
665 self->zst.avail_in);
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 if (self->unused_data == NULL) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
667 PyErr_SetString(PyExc_MemoryError,
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
668 "Can't allocate memory to unused_data");
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
669 Py_DECREF(RetVal);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
670 return_error = 1;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 }
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 /* We will only get Z_BUF_ERROR if the output buffer was full but there
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
673 wasn't more output when we tried again, so it is not an error
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
674 condition */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
675 } else if (err != Z_OK && err != Z_BUF_ERROR) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
676 if (self->zst.msg == Z_NULL)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
677 PyErr_Format(ZlibError, "Error %i while decompressing",
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
678 err);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
679 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
680 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
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 err, self->zst.msg);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
682 Py_DECREF(RetVal);
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 return_error = 1;
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 }
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
685 }
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
686
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
687 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
688 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
689 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
690 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
691 RetVal = NULL; /* should be handled by DECREF */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
692
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
693 Py_DECREF(inputString);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
694
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
695 LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
696
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 return RetVal;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
700 static char comp_flush__doc__[] =
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
701 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
702 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
703 "default value used when mode is not specified is Z_FINISH.\n"
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
704 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
705 "calling the flush() method. Otherwise, more data can still be compressed.\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
706 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
707
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
709 PyZlib_flush(compobject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
711 int err, length=DEFAULTALLOC;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 PyObject *RetVal;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
713 int flushmode = Z_FINISH;
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
714 unsigned long start_total_out;
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
715 int return_error;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
716
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11619
diff changeset
717 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
718 return NULL;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
719
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
720 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
721 doing any work at all; just return an empty string. */
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
722 if (flushmode == Z_NO_FLUSH) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
723 return PyString_FromStringAndSize(NULL, 0);
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
724 }
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
725
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
726 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
727 PyErr_SetString(PyExc_MemoryError,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
728 "Can't allocate memory to compress data");
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
729 return NULL;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
730 }
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
731
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
732 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
733
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
734 start_total_out = self->zst.total_out;
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
735 self->zst.avail_in = 0;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
736 self->zst.avail_out = length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
737 self->zst.next_out = (unsigned char *)PyString_AsString(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
738
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
739 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
740 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
741 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
742
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
743 return_error = 0;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
744
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
745 /* while Z_OK and the output buffer is full, there might be more output,
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
746 so extend the output buffer and try again */
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
747 while (err == Z_OK && self->zst.avail_out == 0) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
748 if (_PyString_Resize(&RetVal, length << 1) == -1) {
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
749 PyErr_SetString(PyExc_MemoryError,
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
750 "Can't allocate memory to compress data");
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
751 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
752 break;
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
753 }
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
754 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
755 self->zst.avail_out = length;
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
756 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
757
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
758 Py_BEGIN_ALLOW_THREADS
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
759 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
760 Py_END_ALLOW_THREADS
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
761 }
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
762
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
763 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
764 various data structures. Note we should only get Z_STREAM_END when
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
765 flushmode is Z_FINISH, but checking both for safety*/
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
766 if (!return_error) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
767 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
768 err=deflateEnd(&(self->zst));
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
769 if (err!=Z_OK) {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
770 if (self->zst.msg == Z_NULL)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
771 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
772 err);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
773 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
774 PyErr_Format(ZlibError,"Error %i from deflateEnd(): %.200s",
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
775 err, self->zst.msg);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
776
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
777 Py_DECREF(RetVal);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
778 return_error = 1;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
779 }
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
780 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
781 self->is_initialised = 0;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
782
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
783 /* We will only get Z_BUF_ERROR if the output buffer was full but there
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
784 wasn't more output when we tried again, so it is not an error
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
785 condition */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
786 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
787 if (self->zst.msg == Z_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
788 PyErr_Format(ZlibError, "Error %i while flushing",
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
789 err);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
790 else
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 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
792 err, self->zst.msg);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
793 Py_DECREF(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
794 return_error = 1;
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
795 }
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
796 }
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
797
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
798 if (!return_error)
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
799 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
800 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
801 RetVal = NULL; /* should have been handled by DECREF */
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
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
803 LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
804
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 return RetVal;
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
807
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
808 static char decomp_flush__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
809 "flush() -- Return a string containing any remaining decompressed data. "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
810 "The decompressor object can no longer be used after this call."
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
811 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
812
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
814 PyZlib_unflush(compobject *self, PyObject *args)
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
815 /*decompressor flush is a no-op because all pending data would have been
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
816 flushed by the decompress method. However, this routine previously called
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
817 inflateEnd, causing any further decompress or flush calls to raise
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
818 exceptions. This behaviour has been preserved.*/
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 {
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
820 int err;
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
821 PyObject * retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
822
13816
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
823 if (!PyArg_ParseTuple(args, ""))
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
824 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
825
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
826 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
827
16718
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
828 err=inflateEnd(&(self->zst));
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
829 if (err!=Z_OK) {
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
830 if (self->zst.msg == Z_NULL)
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
831 PyErr_Format(ZlibError, "Error %i from inflateEnd()",
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
832 err);
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
833 else
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
834 PyErr_Format(ZlibError, "Error %i from inflateEnd(): %.200s",
01c2470eeb2e Patch #103373 from Donovan Baarda: This patch:
Andrew M. Kuchling <amk@amk.ca>
parents: 16369
diff changeset
835 err, self->zst.msg);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
836
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
837 retval = NULL;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
838
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
839 } else {
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
840 self->is_initialised = 0;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
841 retval = PyString_FromStringAndSize(NULL, 0);
6042
6db3d52d4a78 Several bug fixes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6007
diff changeset
842 }
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
843
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
844 LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
845
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
846 return retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
848
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 static PyMethodDef comp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 {
13816
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
851 {"compress", (binaryfunc)PyZlib_objcompress,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
852 METH_VARARGS, comp_compress__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
853 {"flush", (binaryfunc)PyZlib_flush,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
854 METH_VARARGS, comp_flush__doc__},
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 {NULL, NULL}
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
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 static PyMethodDef Decomp_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 {
13816
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
860 {"decompress", (binaryfunc)PyZlib_objdecompress,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
861 METH_VARARGS, decomp_decompress__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
862 {"flush", (binaryfunc)PyZlib_unflush,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
863 METH_VARARGS, decomp_flush__doc__},
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 {NULL, NULL}
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
865 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
866
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
867 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
868 Comp_getattr(compobject *self, char *name)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 {
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
870 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
871 internal data. */
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
872
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
873 return Py_FindMethod(comp_methods, (PyObject *)self, name);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
875
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
877 Decomp_getattr(compobject *self, char *name)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 {
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
879 PyObject * retval;
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
880
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
881 ENTER_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
882
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
883 if (strcmp(name, "unused_data") == 0)
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
884 {
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
885 Py_INCREF(self->unused_data);
19798
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
886 retval = self->unused_data;
10467
255abab3c574 Add an .unused_data attribute to decompressor objects. If .unused_data
Andrew M. Kuchling <amk@amk.ca>
parents: 10440
diff changeset
887 }
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
888 else
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
889 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
890
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
891 LEAVE_ZLIB
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
892
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
893 return retval;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
895
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
896 static char adler32__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
897 "adler32(string) -- Compute an Adler-32 checksum of string, using "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
898 "a default starting value, and returning an integer value.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
899 "adler32(string, value) -- Compute an Adler-32 checksum of string, using "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
900 "the starting value provided, and returning an integer value\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
901 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
902
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
904 PyZlib_adler32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
906 uLong adler32val=adler32(0L, Z_NULL, 0);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
907 Byte *buf;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
908 int len;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
909
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11619
diff changeset
910 if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
911 {
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
912 return NULL;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
913 }
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
914 adler32val = adler32(adler32val, buf, len);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
915 return PyInt_FromLong(adler32val);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
916 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
917
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
918 static char crc32__doc__[] =
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
919 "crc32(string) -- Compute a CRC-32 checksum of string, using "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
920 "a default starting value, and returning an integer value.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
921 "crc32(string, value) -- Compute a CRC-32 checksum of string, using "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
922 "the starting value provided, and returning an integer value.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
923 ;
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
924
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
926 PyZlib_crc32(PyObject *self, PyObject *args)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
928 uLong crc32val=crc32(0L, Z_NULL, 0);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
929 Byte *buf;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
930 int len;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11619
diff changeset
931 if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
932 {
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
933 return NULL;
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
934 }
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
935 crc32val = crc32(crc32val, buf, len);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
936 return PyInt_FromLong(crc32val);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
938
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
939
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 static PyMethodDef zlib_methods[] =
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 {
13816
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
942 {"adler32", (PyCFunction)PyZlib_adler32,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
943 METH_VARARGS, adler32__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
944 {"compress", (PyCFunction)PyZlib_compress,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
945 METH_VARARGS, compress__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
946 {"compressobj", (PyCFunction)PyZlib_compressobj,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
947 METH_VARARGS, compressobj__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
948 {"crc32", (PyCFunction)PyZlib_crc32,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
949 METH_VARARGS, crc32__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
950 {"decompress", (PyCFunction)PyZlib_decompress,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
951 METH_VARARGS, decompress__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
952 {"decompressobj", (PyCFunction)PyZlib_decompressobj,
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
953 METH_VARARGS, decompressobj__doc__},
afbbe674d58a Use METH_VARARGS instead of numeric constant 1
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
954 {NULL, NULL}
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
956
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 statichere PyTypeObject Comptype = {
6911
2c49c02cf747 Win32 treatment.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
958 PyObject_HEAD_INIT(0)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 0,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 "Compress",
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
961 sizeof(compobject),
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 0,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
963 (destructor)Comp_dealloc, /*tp_dealloc*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
964 0, /*tp_print*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 (getattrfunc)Comp_getattr, /*tp_getattr*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 0, /*tp_setattr*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 0, /*tp_compare*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 0, /*tp_repr*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 0, /*tp_as_number*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 0, /*tp_as_sequence*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 0, /*tp_as_mapping*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
973
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 statichere PyTypeObject Decomptype = {
6911
2c49c02cf747 Win32 treatment.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
975 PyObject_HEAD_INIT(0)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 0,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 "Decompress",
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
978 sizeof(compobject),
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 0,
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 (destructor)Decomp_dealloc, /*tp_dealloc*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 0, /*tp_print*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 (getattrfunc)Decomp_getattr, /*tp_getattr*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 0, /*tp_setattr*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 0, /*tp_compare*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 0, /*tp_repr*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 0, /*tp_as_number*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 0, /*tp_as_sequence*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 0, /*tp_as_mapping*/
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 };
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
990
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 /* The following insint() routine was blatantly ripped off from
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 socketmodule.c */
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
993
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 /* Convenience routine to export an integer value.
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 For simplicity, errors (which are unlikely anyway) are ignored. */
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 static void
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12576
diff changeset
997 insint(PyObject *d, char *name, int value)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 PyObject *v = PyInt_FromLong((long) value);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 if (v == NULL) {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 /* Don't bother reporting this error */
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 PyErr_Clear();
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 else {
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 PyDict_SetItemString(d, name, v);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 Py_DECREF(v);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 }
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1010 static char zlib_module_documentation[]=
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1011 "The functions in this module allow compression and decompression "
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1012 "using the zlib library, which is based on GNU zip. \n\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1013 "adler32(string) -- Compute an Adler-32 checksum.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1014 "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1015 "compress(string) -- Compress a string.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1016 "compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1017 "compressobj([level]) -- Return a compressor object.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1018 "crc32(string) -- Compute a CRC-32 checksum.\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1019 "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
11607
69216c11a1bd Fix typo in docstring: wbites -> wbits
Andrew M. Kuchling <amk@amk.ca>
parents: 10571
diff changeset
1020 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1021 "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1022 "Compressor objects support compress() and flush() methods; decompressor \n"
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1023 "objects support decompress() and flush()."
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1024 ;
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1025
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 8636
diff changeset
1026 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13496
diff changeset
1027 PyInit_zlib(void)
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028 {
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1029 PyObject *m, *d, *ver;
6911
2c49c02cf747 Win32 treatment.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
1030 Comptype.ob_type = &PyType_Type;
2c49c02cf747 Win32 treatment.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
1031 Decomptype.ob_type = &PyType_Type;
5722
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1032 m = Py_InitModule4("zlib", zlib_methods,
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1033 zlib_module_documentation,
6c9736865608 Doc strings (AMK).
Guido van Rossum <guido@python.org>
parents: 5352
diff changeset
1034 (PyObject*)NULL,PYTHON_API_VERSION);
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6236
diff changeset
1036 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
11619
91245dc940eb For ZlibError and ZLIB_VERSION, only attempt to add entry to the
Fred Drake <fdrake@acm.org>
parents: 11607
diff changeset
1037 if (ZlibError != NULL)
91245dc940eb For ZlibError and ZLIB_VERSION, only attempt to add entry to the
Fred Drake <fdrake@acm.org>
parents: 11607
diff changeset
1038 PyDict_SetItemString(d, "error", ZlibError);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1039
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 insint(d, "MAX_WBITS", MAX_WBITS);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 insint(d, "DEFLATED", DEFLATED);
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1043 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1044 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1045 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1046 insint(d, "Z_FILTERED", Z_FILTERED);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1047 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1048 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
9657
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
1049
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
1050 insint(d, "Z_FINISH", Z_FINISH);
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
1051 insint(d, "Z_NO_FLUSH", Z_NO_FLUSH);
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
1052 insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
1053 insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH);
7dde4501f53a patches from Andrew
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 9652
diff changeset
1054
6236
6dfec320f910 Several changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6226
diff changeset
1055 ver = PyString_FromString(ZLIB_VERSION);
11619
91245dc940eb For ZlibError and ZLIB_VERSION, only attempt to add entry to the
Fred Drake <fdrake@acm.org>
parents: 11607
diff changeset
1056 if (ver != NULL) {
91245dc940eb For ZlibError and ZLIB_VERSION, only attempt to add entry to the
Fred Drake <fdrake@acm.org>
parents: 11607
diff changeset
1057 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
91245dc940eb For ZlibError and ZLIB_VERSION, only attempt to add entry to the
Fred Drake <fdrake@acm.org>
parents: 11607
diff changeset
1058 Py_DECREF(ver);
91245dc940eb For ZlibError and ZLIB_VERSION, only attempt to add entry to the
Fred Drake <fdrake@acm.org>
parents: 11607
diff changeset
1059 }
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
1060
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1061 #ifdef WITH_THREAD
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1062 zlib_lock = PyThread_allocate_lock();
091c01d07d98 Patch #450702: allow threads when calling into zlib, protect usage of
Martin v. Löwis <martin@v.loewis.de>
parents: 16735
diff changeset
1063 #endif // WITH_THREAD
5352
d5757aad1a4f Added Andrew Kuchling's zlib module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 }