Mercurial > cpython-lru_cache-weakref
annotate Modules/_bz2module.c @ 70817:dd775b1561ba
Fix markup.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 14 Jun 2011 21:09:55 +0200 |
| parents | 0010cc5f22d4 |
| children | 4a6709a071d0 |
| 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 |
|
69275
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
39 #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
40 |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
41 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
42 typedef struct { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
43 PyObject_HEAD |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
44 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
|
45 int flushed; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
46 #ifdef WITH_THREAD |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
47 PyThread_type_lock lock; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
48 #endif |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
49 } BZ2Compressor; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
50 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
51 typedef struct { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
52 PyObject_HEAD |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
53 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
|
54 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
|
55 PyObject *unused_data; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
56 #ifdef WITH_THREAD |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
57 PyThread_type_lock lock; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
58 #endif |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
59 } BZ2Decompressor; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
60 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
61 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
62 /* Helper functions. */ |
|
63457
3c0ffe39557d
Merged revisions 83440 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61467
diff
changeset
|
63 |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
64 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
|
65 catch_bz2_error(int bzerror) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
66 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
67 switch(bzerror) { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 case BZ_FINISH_OK: |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
72 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
|
73 return 0; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
74 |
|
28861
64230ee8eea5
Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
28437
diff
changeset
|
75 #ifdef BZ_CONFIG_ERROR |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
76 case BZ_CONFIG_ERROR: |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
77 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
|
78 "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
|
79 return 1; |
|
28861
64230ee8eea5
Applying patch by Neal Norwitz:
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
28437
diff
changeset
|
80 #endif |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
81 case BZ_PARAM_ERROR: |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
82 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
|
83 "Internal error - " |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
84 "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
|
85 return 1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
86 case BZ_MEM_ERROR: |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
87 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
|
88 return 1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
89 case BZ_DATA_ERROR: |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
90 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
|
91 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
|
92 return 1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
93 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
|
94 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
|
95 return 1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
96 case BZ_UNEXPECTED_EOF: |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
97 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
|
98 "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
|
99 "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
|
100 return 1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
101 case BZ_SEQUENCE_ERROR: |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
102 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
|
103 "Internal error - " |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
104 "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
|
105 return 1; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
106 default: |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
107 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
|
108 "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
|
109 return 1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
110 } |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
111 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
112 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
113 #if BUFSIZ < 8192 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
114 #define SMALLCHUNK 8192 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
115 #else |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
116 #define SMALLCHUNK BUFSIZ |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
117 #endif |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
118 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
119 #if SIZEOF_INT < 4 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
120 #define BIGCHUNK (512 * 32) |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
121 #else |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
122 #define BIGCHUNK (512 * 1024) |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
123 #endif |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
124 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
125 static int |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
126 grow_buffer(PyObject **buf) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
127 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
128 size_t size = PyBytes_GET_SIZE(*buf); |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
129 if (size <= SMALLCHUNK) |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
130 return _PyBytes_Resize(buf, size + SMALLCHUNK); |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
131 else if (size <= BIGCHUNK) |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
132 return _PyBytes_Resize(buf, size * 2); |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
133 else |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
134 return _PyBytes_Resize(buf, size + BIGCHUNK); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
135 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
136 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
137 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
138 /* BZ2Compressor class. */ |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
139 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
140 static PyObject * |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
141 compress(BZ2Compressor *c, char *data, size_t len, int action) |
|
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 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
|
144 PyObject *result; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
145 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
146 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
|
147 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
|
148 return NULL; |
|
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_in = data; |
|
69275
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
150 /* 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
|
151 Do compression in chunks of no more than UINT_MAX bytes each. */ |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
152 c->bzs.avail_in = MIN(len, UINT_MAX); |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
153 len -= c->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
|
154 c->bzs.next_out = PyBytes_AS_STRING(result); |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
155 c->bzs.avail_out = 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
|
156 for (;;) { |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
157 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
|
158 int bzerror; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
159 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
160 Py_BEGIN_ALLOW_THREADS |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
161 this_out = c->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
|
162 bzerror = BZ2_bzCompress(&c->bzs, action); |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
163 data_size += c->bzs.next_out - this_out; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
164 Py_END_ALLOW_THREADS |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
165 if (catch_bz2_error(bzerror)) |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
166 goto error; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
167 |
|
69275
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
168 if (c->bzs.avail_in == 0 && len > 0) { |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
169 c->bzs.avail_in = MIN(len, UINT_MAX); |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
170 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
|
171 } |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
172 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
173 /* In regular compression mode, stop when input data is exhausted. |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
174 In flushing mode, stop when all buffered data 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
|
175 if ((action == BZ_RUN && c->bzs.avail_in == 0) || |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
176 (action == BZ_FINISH && 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
|
177 break; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
178 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
179 if (c->bzs.avail_out == 0) { |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
180 if (grow_buffer(&result) < 0) |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
181 goto error; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
182 c->bzs.next_out = PyBytes_AS_STRING(result) + data_size; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
183 c->bzs.avail_out = 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
|
184 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
185 } |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
186 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
|
187 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
|
188 goto error; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
189 return result; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
190 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
191 error: |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
192 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
|
193 return NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
194 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
195 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
196 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
|
197 "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
|
198 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
199 "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
|
200 "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
|
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 "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
|
203 "flush() method to finish the compression process.\n"); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
204 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
205 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
|
206 BZ2Compressor_compress(BZ2Compressor *self, PyObject *args) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
207 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
208 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
|
209 PyObject *result = NULL; |
|
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 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
|
212 return NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
213 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 else |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
218 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
|
219 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
|
220 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
|
221 return result; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
222 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
223 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
224 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
|
225 "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
|
226 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
227 "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
|
228 "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
|
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 "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
|
231 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
232 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
|
233 BZ2Compressor_flush(BZ2Compressor *self, PyObject *noargs) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
234 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
235 PyObject *result = NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
236 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 else { |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
241 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
|
242 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
|
243 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
244 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
|
245 return result; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
246 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
247 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
248 static int |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
249 BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
250 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
251 int compresslevel = 9; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
252 int bzerror; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
253 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
254 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
|
255 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
|
256 if (!(1 <= compresslevel && compresslevel <= 9)) { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
257 PyErr_SetString(PyExc_ValueError, |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
258 "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
|
259 return -1; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
260 } |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
261 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
262 #ifdef WITH_THREAD |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
263 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
|
264 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
|
265 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
|
266 return -1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
267 } |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
268 #endif |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
269 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
270 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
|
271 if (catch_bz2_error(bzerror)) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
272 goto error; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
273 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
274 return 0; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
275 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
276 error: |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
277 #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
|
278 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
|
279 self->lock = NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
280 #endif |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
281 return -1; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
282 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
283 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
284 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
|
285 BZ2Compressor_dealloc(BZ2Compressor *self) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
286 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
287 BZ2_bzCompressEnd(&self->bzs); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
288 #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
|
289 if (self->lock != NULL) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
290 PyThread_free_lock(self->lock); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
291 #endif |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
292 Py_TYPE(self)->tp_free((PyObject *)self); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
293 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
294 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
295 static PyMethodDef BZ2Compressor_methods[] = { |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
296 {"compress", (PyCFunction)BZ2Compressor_compress, METH_VARARGS, |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
297 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
|
298 {"flush", (PyCFunction)BZ2Compressor_flush, METH_NOARGS, |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
299 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
|
300 {NULL} |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
301 }; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
302 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
303 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
|
304 "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
|
305 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
306 "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
|
307 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
308 "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
|
309 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
310 "For one-shot compression, use the compress() function instead.\n"); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
311 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
312 static PyTypeObject BZ2Compressor_Type = { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
313 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
|
314 "_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
|
315 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
|
316 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
|
317 (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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 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
|
323 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
|
324 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
|
325 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
|
326 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 (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
|
349 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
|
350 PyType_GenericNew, /* tp_new */ |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
351 }; |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
352 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
353 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
354 /* BZ2Decompressor class. */ |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
355 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
356 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
|
357 decompress(BZ2Decompressor *d, char *data, size_t len) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
358 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
359 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
|
360 PyObject *result; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
361 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
362 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
|
363 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
|
364 return result; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
365 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
|
366 /* 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
|
367 Do decompression in chunks of no more than UINT_MAX bytes each. */ |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
368 d->bzs.avail_in = MIN(len, UINT_MAX); |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
369 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
|
370 d->bzs.next_out = PyBytes_AS_STRING(result); |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
371 d->bzs.avail_out = 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
|
372 for (;;) { |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
373 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
|
374 int bzerror; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
375 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 if (catch_bz2_error(bzerror)) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
382 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
|
383 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
|
384 d->eof = 1; |
|
69275
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
385 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
|
386 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
|
387 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
|
388 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
|
389 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
|
390 goto error; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
391 } |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
392 break; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
393 } |
|
69275
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
394 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
|
395 if (len == 0) |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
396 break; |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
397 d->bzs.avail_in = MIN(len, UINT_MAX); |
|
0010cc5f22d4
Fix 64-bit safety issue in BZ2Compressor and BZ2Decompressor.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents:
69112
diff
changeset
|
398 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
|
399 } |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
400 if (d->bzs.avail_out == 0) { |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
401 if (grow_buffer(&result) < 0) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
402 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
|
403 d->bzs.next_out = PyBytes_AS_STRING(result) + data_size; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
404 d->bzs.avail_out = 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
|
405 } |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
406 } |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
407 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
|
408 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
|
409 goto error; |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
410 return result; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
411 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
412 error: |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
413 Py_XDECREF(result); |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
414 return NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
415 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
416 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
417 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
|
418 "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
|
419 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
420 "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
|
421 "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
|
422 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
423 "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
|
424 "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
|
425 "is ignored and saved in the unused_data attribute.\n"); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
426 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
427 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
|
428 BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
429 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
430 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
|
431 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
|
432 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
433 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
|
434 return NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
435 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
436 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
|
437 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
|
438 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
|
439 else |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
440 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
|
441 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
|
442 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
|
443 return result; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
444 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
445 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
446 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
|
447 BZ2Decompressor_init(BZ2Decompressor *self, PyObject *args, PyObject *kwargs) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
448 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
449 int bzerror; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
450 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
451 if (!PyArg_ParseTuple(args, ":BZ2Decompressor")) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
452 return -1; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
453 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
454 #ifdef WITH_THREAD |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
455 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
|
456 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
|
457 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
|
458 return -1; |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
459 } |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
460 #endif |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
461 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
462 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
|
463 if (self->unused_data == NULL) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
464 goto error; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
465 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
466 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
|
467 if (catch_bz2_error(bzerror)) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
468 goto error; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
469 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
470 return 0; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
471 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
472 error: |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
473 Py_CLEAR(self->unused_data); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
474 #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
|
475 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
|
476 self->lock = NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
477 #endif |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
478 return -1; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
479 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
480 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
481 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
|
482 BZ2Decompressor_dealloc(BZ2Decompressor *self) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
483 { |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
484 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
|
485 Py_CLEAR(self->unused_data); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
486 #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
|
487 if (self->lock != NULL) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
488 PyThread_free_lock(self->lock); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
489 #endif |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
490 Py_TYPE(self)->tp_free((PyObject *)self); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
491 } |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
492 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
493 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
|
494 {"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
|
495 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
|
496 {NULL} |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
497 }; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
498 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
499 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
|
500 "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
|
501 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
502 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
|
503 "Data found after the end of the compressed stream."); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
504 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
505 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
|
506 {"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
|
507 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
|
508 {"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
|
509 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
|
510 {NULL} |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
511 }; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
512 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
513 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
|
514 "BZ2Decompressor()\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
515 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
516 "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
|
517 "\n" |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
518 "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
|
519 |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
520 static PyTypeObject BZ2Decompressor_Type = { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
521 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
|
522 "_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
|
523 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
|
524 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
|
525 (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
|
526 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 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
|
532 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
|
533 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 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
|
555 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
|
556 (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
|
557 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
|
558 PyType_GenericNew, /* tp_new */ |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
559 }; |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
560 |
|
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
561 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
562 /* Module initialization. */ |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
563 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
564 static struct PyModuleDef _bz2module = { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
565 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
|
566 "_bz2", |
|
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
567 NULL, |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
568 -1, |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
569 NULL, |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
570 NULL, |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
571 NULL, |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
572 NULL, |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
573 NULL |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
574 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
575 |
|
29726
85e52ec85629
Use appropriate macros not the deprecated DL_IMPORT/DL_EXPORT macros
Neal Norwitz <nnorwitz@gmail.com>
parents:
28949
diff
changeset
|
576 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
|
577 PyInit__bz2(void) |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
578 { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
579 PyObject *m; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
580 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
581 if (PyType_Ready(&BZ2Compressor_Type) < 0) |
|
64911
51d8fffcd260
Merged revisions 84980 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63457
diff
changeset
|
582 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
|
583 if (PyType_Ready(&BZ2Decompressor_Type) < 0) |
|
64911
51d8fffcd260
Merged revisions 84980 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63457
diff
changeset
|
584 return NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
585 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
586 m = PyModule_Create(&_bz2module); |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
587 if (m == NULL) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
588 return NULL; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
589 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
590 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
|
591 PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Compressor_Type); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
592 |
|
69112
2cb07a46f4b5
Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
593 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
|
594 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
|
595 (PyObject *)&BZ2Decompressor_Type); |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
596 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60888
diff
changeset
|
597 return m; |
|
26037
521e89c4ff44
Patch implementing bz2 module.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
diff
changeset
|
598 } |
