annotate Modules/_bz2module.c @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents b9df25608ad0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
1 /* _bz2 - Low-level Python interface to libbzip2. */
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
2
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3 #define PY_SSIZE_T_CLEAN
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
4
26365
d1d8959b138a Include Python.h first.
Martin v. Löwis <martin@v.loewis.de>
parents: 26123
diff changeset
5 #include "Python.h"
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
6 #include "structmember.h"
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
7
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
8 #ifdef WITH_THREAD
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
9 #include "pythread.h"
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
10 #endif
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
11
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
12 #include <bzlib.h>
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
13 #include <stdio.h>
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
14
28861
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
15
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
16 #ifndef BZ_CONFIG_ERROR
28861
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
17 #define BZ2_bzCompress bzCompress
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
18 #define BZ2_bzCompressInit bzCompressInit
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
19 #define BZ2_bzCompressEnd bzCompressEnd
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
20 #define BZ2_bzDecompress bzDecompress
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
21 #define BZ2_bzDecompressInit bzDecompressInit
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
22 #define BZ2_bzDecompressEnd bzDecompressEnd
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
23 #endif /* ! BZ_CONFIG_ERROR */
28861
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
24
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
25
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
26 #ifdef WITH_THREAD
56853
86531de75894 Merged revisions 75820 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51270
diff changeset
27 #define ACQUIRE_LOCK(obj) do { \
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
28 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
29 Py_BEGIN_ALLOW_THREADS \
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
30 PyThread_acquire_lock((obj)->lock, 1); \
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
31 Py_END_ALLOW_THREADS \
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
32 } } while (0)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
33 #define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
34 #else
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
35 #define ACQUIRE_LOCK(obj)
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
36 #define RELEASE_LOCK(obj)
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
37 #endif
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
38
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
39
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
40 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
41 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
42 bz_stream bzs;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
43 int flushed;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
44 #ifdef WITH_THREAD
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
45 PyThread_type_lock lock;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
46 #endif
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
47 } BZ2Compressor;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
48
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
49 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
50 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
51 bz_stream bzs;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
52 char eof; /* T_BOOL expects a char */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
53 PyObject *unused_data;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
54 #ifdef WITH_THREAD
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
55 PyThread_type_lock lock;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
56 #endif
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
57 } BZ2Decompressor;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
58
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
59
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
60 /* Helper functions. */
63457
3c0ffe39557d Merged revisions 83440 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61467
diff changeset
61
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
62 static int
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
63 catch_bz2_error(int bzerror)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
64 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
65 switch(bzerror) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
66 case BZ_OK:
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
67 case BZ_RUN_OK:
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
68 case BZ_FLUSH_OK:
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
69 case BZ_FINISH_OK:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
70 case BZ_STREAM_END:
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
71 return 0;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
72
28861
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
73 #ifdef BZ_CONFIG_ERROR
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
74 case BZ_CONFIG_ERROR:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
75 PyErr_SetString(PyExc_SystemError,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
76 "libbzip2 was not compiled correctly");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
77 return 1;
28861
64230ee8eea5 Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 28437
diff changeset
78 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
79 case BZ_PARAM_ERROR:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
80 PyErr_SetString(PyExc_ValueError,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
81 "Internal error - "
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
82 "invalid parameters passed to libbzip2");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
83 return 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
84 case BZ_MEM_ERROR:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
85 PyErr_NoMemory();
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
86 return 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
87 case BZ_DATA_ERROR:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
88 case BZ_DATA_ERROR_MAGIC:
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
89 PyErr_SetString(PyExc_IOError, "Invalid data stream");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
90 return 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
91 case BZ_IO_ERROR:
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
92 PyErr_SetString(PyExc_IOError, "Unknown I/O error");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
93 return 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
94 case BZ_UNEXPECTED_EOF:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
95 PyErr_SetString(PyExc_EOFError,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
96 "Compressed file ended before the logical "
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
97 "end-of-stream was detected");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
98 return 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
99 case BZ_SEQUENCE_ERROR:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
100 PyErr_SetString(PyExc_RuntimeError,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
101 "Internal error - "
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
102 "Invalid sequence of commands sent to libbzip2");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
103 return 1;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
104 default:
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
105 PyErr_Format(PyExc_IOError,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
106 "Unrecognized error from libbzip2: %d", bzerror);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
107 return 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
108 }
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
109 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
110
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
111 #if BUFSIZ < 8192
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
112 #define SMALLCHUNK 8192
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
113 #else
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
114 #define SMALLCHUNK BUFSIZ
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
115 #endif
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
116
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
117 static int
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
118 grow_buffer(PyObject **buf)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
119 {
72907
d18c80a8c119 Issue #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68257
diff changeset
120 /* Expand the buffer by an amount proportional to the current size,
d18c80a8c119 Issue #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68257
diff changeset
121 giving us amortized linear-time behavior. Use a less-than-double
d18c80a8c119 Issue #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 68257
diff changeset
122 growth factor to avoid excessive allocation. */
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
123 size_t size = PyBytes_GET_SIZE(*buf);
79879
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
124 size_t new_size = size + (size >> 3) + 6;
79878
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
125 if (new_size > size) {
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
126 return _PyBytes_Resize(buf, new_size);
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
127 } else { /* overflow */
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
128 PyErr_SetString(PyExc_OverflowError,
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
129 "Unable to allocate buffer - output too large");
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
130 return -1;
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
131 }
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
132 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
133
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
134
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
135 /* BZ2Compressor class. */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
136
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
137 static PyObject *
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
138 compress(BZ2Compressor *c, char *data, size_t len, int action)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
139 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
140 size_t data_size = 0;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
141 PyObject *result;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
142
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
143 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
144 if (result == NULL)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
145 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
146
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
147 c->bzs.next_in = data;
81254
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
148 c->bzs.avail_in = 0;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
149 c->bzs.next_out = PyBytes_AS_STRING(result);
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
150 c->bzs.avail_out = SMALLCHUNK;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
151 for (;;) {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
152 char *this_out;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
153 int bzerror;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
154
81254
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
155 /* On a 64-bit system, len might not fit in avail_in (an unsigned int).
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
156 Do compression in chunks of no more than UINT_MAX bytes each. */
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
157 if (c->bzs.avail_in == 0 && len > 0) {
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
158 c->bzs.avail_in = (unsigned int)Py_MIN(len, UINT_MAX);
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
159 len -= c->bzs.avail_in;
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
160 }
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
161
81254
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
162 /* In regular compression mode, stop when input data is exhausted. */
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
163 if (action == BZ_RUN && c->bzs.avail_in == 0)
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
164 break;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
165
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
166 if (c->bzs.avail_out == 0) {
79879
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
167 size_t buffer_left = PyBytes_GET_SIZE(result) - data_size;
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
168 if (buffer_left == 0) {
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
169 if (grow_buffer(&result) < 0)
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
170 goto error;
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
171 c->bzs.next_out = PyBytes_AS_STRING(result) + data_size;
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
172 buffer_left = PyBytes_GET_SIZE(result) - data_size;
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
173 }
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
174 c->bzs.avail_out = (unsigned int)Py_MIN(buffer_left, UINT_MAX);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
175 }
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
176
81254
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
177 Py_BEGIN_ALLOW_THREADS
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
178 this_out = c->bzs.next_out;
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
179 bzerror = BZ2_bzCompress(&c->bzs, action);
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
180 data_size += c->bzs.next_out - this_out;
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
181 Py_END_ALLOW_THREADS
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
182 if (catch_bz2_error(bzerror))
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
183 goto error;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
184
81254
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
185 /* In flushing mode, stop when all buffered data has been flushed. */
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
186 if (action == BZ_FINISH && bzerror == BZ_STREAM_END)
011981143087 Issue #16828: Fix error incorrectly raised by bz2.compress('').
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81253 79879
diff changeset
187 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
188 }
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
189 if (data_size != PyBytes_GET_SIZE(result))
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
190 if (_PyBytes_Resize(&result, data_size) < 0)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
191 goto error;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
192 return result;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
193
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
194 error:
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
195 Py_XDECREF(result);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
196 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
197 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
198
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
199 PyDoc_STRVAR(BZ2Compressor_compress__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
200 "compress(data) -> bytes\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
201 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
202 "Provide data to the compressor object. Returns a chunk of\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
203 "compressed data if possible, or b'' otherwise.\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
204 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
205 "When you have finished providing data to the compressor, call the\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
206 "flush() method to finish the compression process.\n");
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
207
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
208 static PyObject *
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
209 BZ2Compressor_compress(BZ2Compressor *self, PyObject *args)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
210 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
211 Py_buffer buffer;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
212 PyObject *result = NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
213
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
214 if (!PyArg_ParseTuple(args, "y*:compress", &buffer))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
215 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
216
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
217 ACQUIRE_LOCK(self);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
218 if (self->flushed)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
219 PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
220 else
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
221 result = compress(self, buffer.buf, buffer.len, BZ_RUN);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
222 RELEASE_LOCK(self);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
223 PyBuffer_Release(&buffer);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
224 return result;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
225 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
226
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
227 PyDoc_STRVAR(BZ2Compressor_flush__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
228 "flush() -> bytes\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
229 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
230 "Finish the compression process. Returns the compressed data left\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
231 "in internal buffers.\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
232 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
233 "The compressor object may not be used after this method is called.\n");
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
234
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
235 static PyObject *
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
236 BZ2Compressor_flush(BZ2Compressor *self, PyObject *noargs)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
237 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
238 PyObject *result = NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
239
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
240 ACQUIRE_LOCK(self);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
241 if (self->flushed)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
242 PyErr_SetString(PyExc_ValueError, "Repeated call to flush()");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
243 else {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
244 self->flushed = 1;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
245 result = compress(self, NULL, 0, BZ_FINISH);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
246 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
247 RELEASE_LOCK(self);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
248 return result;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
249 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
250
86704
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
251 static PyObject *
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
252 BZ2Compressor_getstate(BZ2Compressor *self, PyObject *noargs)
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
253 {
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
254 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
255 Py_TYPE(self)->tp_name);
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
256 return NULL;
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
257 }
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
258
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
259 static void*
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
260 BZ2_Malloc(void* ctx, int items, int size)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
261 {
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
262 if (items < 0 || size < 0)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
263 return NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
264 if ((size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
265 return NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
266 /* PyMem_Malloc() cannot be used: compress() and decompress()
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
267 release the GIL */
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
268 return PyMem_RawMalloc(items * size);
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
269 }
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
270
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
271 static void
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
272 BZ2_Free(void* ctx, void *ptr)
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
273 {
84496
12f26c356611 Issue #18227: "Free" function of bz2, lzma and zlib modules has no return value (void)
Victor Stinner <victor.stinner@gmail.com>
parents: 84494
diff changeset
274 PyMem_RawFree(ptr);
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
275 }
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
276
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
277 static int
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
278 BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
279 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
280 int compresslevel = 9;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
281 int bzerror;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
282
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
283 if (!PyArg_ParseTuple(args, "|i:BZ2Compressor", &compresslevel))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
284 return -1;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
285 if (!(1 <= compresslevel && compresslevel <= 9)) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
286 PyErr_SetString(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
287 "compresslevel must be between 1 and 9");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
288 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
289 }
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
290
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
291 #ifdef WITH_THREAD
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
292 self->lock = PyThread_allocate_lock();
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
293 if (self->lock == NULL) {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
294 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
295 return -1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
296 }
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
297 #endif
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
298
84494
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
299 self->bzs.opaque = NULL;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
300 self->bzs.bzalloc = BZ2_Malloc;
a876d9d2e4fc Issue #18227: Use PyMem_RawAlloc() in bz2, lzma and zlib modules
Victor Stinner <victor.stinner@gmail.com>
parents: 84025
diff changeset
301 self->bzs.bzfree = BZ2_Free;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
302 bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
303 if (catch_bz2_error(bzerror))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
304 goto error;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
305
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
306 return 0;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
307
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
308 error:
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
309 #ifdef WITH_THREAD
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
310 PyThread_free_lock(self->lock);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
311 self->lock = NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
312 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
313 return -1;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
314 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
315
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
316 static void
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
317 BZ2Compressor_dealloc(BZ2Compressor *self)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
318 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
319 BZ2_bzCompressEnd(&self->bzs);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
320 #ifdef WITH_THREAD
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
321 if (self->lock != NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
322 PyThread_free_lock(self->lock);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
323 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
324 Py_TYPE(self)->tp_free((PyObject *)self);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
325 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
326
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
327 static PyMethodDef BZ2Compressor_methods[] = {
86704
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
328 {"compress", (PyCFunction)BZ2Compressor_compress, METH_VARARGS,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
329 BZ2Compressor_compress__doc__},
86704
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
330 {"flush", (PyCFunction)BZ2Compressor_flush, METH_NOARGS,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
331 BZ2Compressor_flush__doc__},
86704
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
332 {"__getstate__", (PyCFunction)BZ2Compressor_getstate, METH_NOARGS},
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
333 {NULL}
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
334 };
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
335
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
336 PyDoc_STRVAR(BZ2Compressor__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
337 "BZ2Compressor(compresslevel=9)\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
338 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
339 "Create a compressor object for compressing data incrementally.\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
340 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
341 "compresslevel, if given, must be a number between 1 and 9.\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
342 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
343 "For one-shot compression, use the compress() function instead.\n");
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
344
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
345 static PyTypeObject BZ2Compressor_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
346 PyVarObject_HEAD_INIT(NULL, 0)
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
347 "_bz2.BZ2Compressor", /* tp_name */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
348 sizeof(BZ2Compressor), /* tp_basicsize */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
349 0, /* tp_itemsize */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
350 (destructor)BZ2Compressor_dealloc, /* tp_dealloc */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
351 0, /* tp_print */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
352 0, /* tp_getattr */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
353 0, /* tp_setattr */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
354 0, /* tp_reserved */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
355 0, /* tp_repr */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
356 0, /* tp_as_number */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
357 0, /* tp_as_sequence */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
358 0, /* tp_as_mapping */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
359 0, /* tp_hash */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
360 0, /* tp_call */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
361 0, /* tp_str */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
362 0, /* tp_getattro */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
363 0, /* tp_setattro */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
364 0, /* tp_as_buffer */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
365 Py_TPFLAGS_DEFAULT, /* tp_flags */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
366 BZ2Compressor__doc__, /* tp_doc */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
367 0, /* tp_traverse */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
368 0, /* tp_clear */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
369 0, /* tp_richcompare */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
370 0, /* tp_weaklistoffset */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
371 0, /* tp_iter */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
372 0, /* tp_iternext */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
373 BZ2Compressor_methods, /* tp_methods */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
374 0, /* tp_members */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
375 0, /* tp_getset */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
376 0, /* tp_base */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
377 0, /* tp_dict */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
378 0, /* tp_descr_get */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
379 0, /* tp_descr_set */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
380 0, /* tp_dictoffset */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
381 (initproc)BZ2Compressor_init, /* tp_init */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
382 0, /* tp_alloc */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
383 PyType_GenericNew, /* tp_new */
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
384 };
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
385
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
386
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
387 /* BZ2Decompressor class. */
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
388
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
389 static PyObject *
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
390 decompress(BZ2Decompressor *d, char *data, size_t len)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
391 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
392 size_t data_size = 0;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
393 PyObject *result;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
394
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
395 result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
396 if (result == NULL)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
397 return result;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
398 d->bzs.next_in = data;
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
399 /* On a 64-bit system, len might not fit in avail_in (an unsigned int).
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
400 Do decompression in chunks of no more than UINT_MAX bytes each. */
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
401 d->bzs.avail_in = (unsigned int)Py_MIN(len, UINT_MAX);
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
402 len -= d->bzs.avail_in;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
403 d->bzs.next_out = PyBytes_AS_STRING(result);
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
404 d->bzs.avail_out = SMALLCHUNK;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
405 for (;;) {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
406 char *this_out;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
407 int bzerror;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
408
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
409 Py_BEGIN_ALLOW_THREADS
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
410 this_out = d->bzs.next_out;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
411 bzerror = BZ2_bzDecompress(&d->bzs);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
412 data_size += d->bzs.next_out - this_out;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
413 Py_END_ALLOW_THREADS
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
414 if (catch_bz2_error(bzerror))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
415 goto error;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
416 if (bzerror == BZ_STREAM_END) {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
417 d->eof = 1;
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
418 len += d->bzs.avail_in;
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
419 if (len > 0) { /* Save leftover input to unused_data */
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
420 Py_CLEAR(d->unused_data);
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
421 d->unused_data = PyBytes_FromStringAndSize(d->bzs.next_in, len);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
422 if (d->unused_data == NULL)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
423 goto error;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
424 }
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
425 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
426 }
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
427 if (d->bzs.avail_in == 0) {
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
428 if (len == 0)
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
429 break;
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
430 d->bzs.avail_in = (unsigned int)Py_MIN(len, UINT_MAX);
69275
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
431 len -= d->bzs.avail_in;
0010cc5f22d4 Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69112
diff changeset
432 }
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
433 if (d->bzs.avail_out == 0) {
79879
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
434 size_t buffer_left = PyBytes_GET_SIZE(result) - data_size;
79878
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
435 if (buffer_left == 0) {
79879
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
436 if (grow_buffer(&result) < 0)
79878
ebb8c7d79f52 Issue #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 72907
diff changeset
437 goto error;
79879
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
438 d->bzs.next_out = PyBytes_AS_STRING(result) + data_size;
25fdf297c077 Merge #14398: Fix size truncation and overflow bugs in bz2 module.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 79878 72908
diff changeset
439 buffer_left = PyBytes_GET_SIZE(result) - data_size;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
440 }
84025
46401ce03547 Issue #9566: Fix compiler warning on Windows 64-bit in _bz2module.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84022
diff changeset
441 d->bzs.avail_out = (unsigned int)Py_MIN(buffer_left, UINT_MAX);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
442 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
443 }
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
444 if (data_size != PyBytes_GET_SIZE(result))
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
445 if (_PyBytes_Resize(&result, data_size) < 0)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
446 goto error;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
447 return result;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
448
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
449 error:
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
450 Py_XDECREF(result);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
451 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
452 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
453
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
454 PyDoc_STRVAR(BZ2Decompressor_decompress__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
455 "decompress(data) -> bytes\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
456 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
457 "Provide data to the decompressor object. Returns a chunk of\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
458 "decompressed data if possible, or b'' otherwise.\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
459 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
460 "Attempting to decompress data after the end of stream is reached\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
461 "raises an EOFError. Any data found after the end of the stream\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
462 "is ignored and saved in the unused_data attribute.\n");
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
463
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
464 static PyObject *
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
465 BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
466 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
467 Py_buffer buffer;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
468 PyObject *result = NULL;
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
469
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
470 if (!PyArg_ParseTuple(args, "y*:decompress", &buffer))
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
471 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
472
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
473 ACQUIRE_LOCK(self);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
474 if (self->eof)
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
475 PyErr_SetString(PyExc_EOFError, "End of stream already reached");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
476 else
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
477 result = decompress(self, buffer.buf, buffer.len);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
478 RELEASE_LOCK(self);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
479 PyBuffer_Release(&buffer);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
480 return result;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
481 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
482
86704
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
483 static PyObject *
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
484 BZ2Decompressor_getstate(BZ2Decompressor *self, PyObject *noargs)
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
485 {
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
486 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
487 Py_TYPE(self)->tp_name);
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
488 return NULL;
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
489 }
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
490
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
491 static int
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
492 BZ2Decompressor_init(BZ2Decompressor *self, PyObject *args, PyObject *kwargs)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
493 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
494 int bzerror;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
495
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
496 if (!PyArg_ParseTuple(args, ":BZ2Decompressor"))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
497 return -1;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
498
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
499 #ifdef WITH_THREAD
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
500 self->lock = PyThread_allocate_lock();
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
501 if (self->lock == NULL) {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
502 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
503 return -1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
504 }
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
505 #endif
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
506
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
507 self->unused_data = PyBytes_FromStringAndSize("", 0);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
508 if (self->unused_data == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
509 goto error;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
510
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
511 bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
512 if (catch_bz2_error(bzerror))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
513 goto error;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
514
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
515 return 0;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
516
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
517 error:
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
518 Py_CLEAR(self->unused_data);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
519 #ifdef WITH_THREAD
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
520 PyThread_free_lock(self->lock);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
521 self->lock = NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
522 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
523 return -1;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
524 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
525
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
526 static void
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
527 BZ2Decompressor_dealloc(BZ2Decompressor *self)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
528 {
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
529 BZ2_bzDecompressEnd(&self->bzs);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
530 Py_CLEAR(self->unused_data);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
531 #ifdef WITH_THREAD
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
532 if (self->lock != NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
533 PyThread_free_lock(self->lock);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
534 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
535 Py_TYPE(self)->tp_free((PyObject *)self);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
536 }
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
537
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
538 static PyMethodDef BZ2Decompressor_methods[] = {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
539 {"decompress", (PyCFunction)BZ2Decompressor_decompress, METH_VARARGS,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
540 BZ2Decompressor_decompress__doc__},
86704
be363c1e94fe #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 81254
diff changeset
541 {"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS},
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
542 {NULL}
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
543 };
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
544
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
545 PyDoc_STRVAR(BZ2Decompressor_eof__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
546 "True if the end-of-stream marker has been reached.");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
547
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
548 PyDoc_STRVAR(BZ2Decompressor_unused_data__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
549 "Data found after the end of the compressed stream.");
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
550
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
551 static PyMemberDef BZ2Decompressor_members[] = {
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
552 {"eof", T_BOOL, offsetof(BZ2Decompressor, eof),
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
553 READONLY, BZ2Decompressor_eof__doc__},
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
554 {"unused_data", T_OBJECT_EX, offsetof(BZ2Decompressor, unused_data),
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
555 READONLY, BZ2Decompressor_unused_data__doc__},
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
556 {NULL}
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
557 };
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
558
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
559 PyDoc_STRVAR(BZ2Decompressor__doc__,
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
560 "BZ2Decompressor()\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
561 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
562 "Create a decompressor object for decompressing data incrementally.\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
563 "\n"
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
564 "For one-shot decompression, use the decompress() function instead.\n");
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
565
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
566 static PyTypeObject BZ2Decompressor_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
567 PyVarObject_HEAD_INIT(NULL, 0)
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
568 "_bz2.BZ2Decompressor", /* tp_name */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
569 sizeof(BZ2Decompressor), /* tp_basicsize */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
570 0, /* tp_itemsize */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
571 (destructor)BZ2Decompressor_dealloc,/* tp_dealloc */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
572 0, /* tp_print */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
573 0, /* tp_getattr */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
574 0, /* tp_setattr */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
575 0, /* tp_reserved */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
576 0, /* tp_repr */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
577 0, /* tp_as_number */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
578 0, /* tp_as_sequence */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
579 0, /* tp_as_mapping */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
580 0, /* tp_hash */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
581 0, /* tp_call */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
582 0, /* tp_str */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
583 0, /* tp_getattro */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
584 0, /* tp_setattro */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
585 0, /* tp_as_buffer */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
586 Py_TPFLAGS_DEFAULT, /* tp_flags */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
587 BZ2Decompressor__doc__, /* tp_doc */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
588 0, /* tp_traverse */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
589 0, /* tp_clear */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
590 0, /* tp_richcompare */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
591 0, /* tp_weaklistoffset */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
592 0, /* tp_iter */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
593 0, /* tp_iternext */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
594 BZ2Decompressor_methods, /* tp_methods */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
595 BZ2Decompressor_members, /* tp_members */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
596 0, /* tp_getset */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
597 0, /* tp_base */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
598 0, /* tp_dict */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
599 0, /* tp_descr_get */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
600 0, /* tp_descr_set */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
601 0, /* tp_dictoffset */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
602 (initproc)BZ2Decompressor_init, /* tp_init */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
603 0, /* tp_alloc */
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
604 PyType_GenericNew, /* tp_new */
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
605 };
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
606
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
607
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
608 /* Module initialization. */
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
609
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
610 static struct PyModuleDef _bz2module = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
611 PyModuleDef_HEAD_INIT,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
612 "_bz2",
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
613 NULL,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
614 -1,
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
615 NULL,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
616 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
617 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
618 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
619 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
620 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
621
29726
85e52ec85629 Use appropriate macros not the deprecated DL_IMPORT/DL_EXPORT macros
Neal Norwitz <nnorwitz@gmail.com>
parents: 28949
diff changeset
622 PyMODINIT_FUNC
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
623 PyInit__bz2(void)
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
624 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
625 PyObject *m;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
626
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
627 if (PyType_Ready(&BZ2Compressor_Type) < 0)
64911
51d8fffcd260 Merged revisions 84980 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63457
diff changeset
628 return NULL;
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
629 if (PyType_Ready(&BZ2Decompressor_Type) < 0)
64911
51d8fffcd260 Merged revisions 84980 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63457
diff changeset
630 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
631
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
632 m = PyModule_Create(&_bz2module);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
633 if (m == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
634 return NULL;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
635
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
636 Py_INCREF(&BZ2Compressor_Type);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
637 PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Compressor_Type);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
638
69112
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
639 Py_INCREF(&BZ2Decompressor_Type);
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
640 PyModule_AddObject(m, "BZ2Decompressor",
2cb07a46f4b5 Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
641 (PyObject *)&BZ2Decompressor_Type);
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
642
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60888
diff changeset
643 return m;
26037
521e89c4ff44 Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff changeset
644 }