blob: 16201bd6486c5bfbe66df646ea99553fa0452514 [file] [log] [blame]
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001/*
2
3python-bz2 - python bz2 library interface
4
5Copyright (c) 2002 Gustavo Niemeyer <niemeyer@conectiva.com>
6Copyright (c) 2002 Python Software Foundation; All Rights Reserved
7
8*/
9
Martin v. Löwise17af7b2002-11-23 09:16:19 +000010#include "Python.h"
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000011#include <stdio.h>
12#include <bzlib.h>
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000013#include "structmember.h"
14
15#ifdef WITH_THREAD
16#include "pythread.h"
17#endif
18
19static char __author__[] =
20"The bz2 python module was written by:\n\
21\n\
22 Gustavo Niemeyer <niemeyer@conectiva.com>\n\
23";
24
Georg Brandl33a5f2a2005-08-21 14:16:04 +000025/* Our very own off_t-like type, 64-bit if possible */
26/* copied from Objects/fileobject.c */
27#if !defined(HAVE_LARGEFILE_SUPPORT)
28typedef off_t Py_off_t;
29#elif SIZEOF_OFF_T >= 8
30typedef off_t Py_off_t;
31#elif SIZEOF_FPOS_T >= 8
32typedef fpos_t Py_off_t;
33#else
34#error "Large file support, but neither off_t nor fpos_t is large enough."
35#endif
36
Gregory P. Smithdd96db62008-06-09 04:58:54 +000037#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000038
39#define MODE_CLOSED 0
40#define MODE_READ 1
41#define MODE_READ_EOF 2
42#define MODE_WRITE 3
43
Christian Heimese93237d2007-12-19 02:37:44 +000044#define BZ2FileObject_Check(v) (Py_TYPE(v) == &BZ2File_Type)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000045
Gustavo Niemeyer7628f1f2003-04-27 06:25:24 +000046
47#ifdef BZ_CONFIG_ERROR
48
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000049#if SIZEOF_LONG >= 8
50#define BZS_TOTAL_OUT(bzs) \
51 (((long)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
52#elif SIZEOF_LONG_LONG >= 8
53#define BZS_TOTAL_OUT(bzs) \
Martin v. Löwisb9a0f912003-03-29 10:06:18 +000054 (((PY_LONG_LONG)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000055#else
56#define BZS_TOTAL_OUT(bzs) \
Neal Norwitz20bad742006-01-17 05:27:39 +000057 bzs->total_out_lo32
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000058#endif
59
Gustavo Niemeyer7628f1f2003-04-27 06:25:24 +000060#else /* ! BZ_CONFIG_ERROR */
61
62#define BZ2_bzRead bzRead
63#define BZ2_bzReadOpen bzReadOpen
64#define BZ2_bzReadClose bzReadClose
65#define BZ2_bzWrite bzWrite
66#define BZ2_bzWriteOpen bzWriteOpen
67#define BZ2_bzWriteClose bzWriteClose
68#define BZ2_bzCompress bzCompress
69#define BZ2_bzCompressInit bzCompressInit
70#define BZ2_bzCompressEnd bzCompressEnd
71#define BZ2_bzDecompress bzDecompress
72#define BZ2_bzDecompressInit bzDecompressInit
73#define BZ2_bzDecompressEnd bzDecompressEnd
74
75#define BZS_TOTAL_OUT(bzs) bzs->total_out
76
77#endif /* ! BZ_CONFIG_ERROR */
78
79
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000080#ifdef WITH_THREAD
81#define ACQUIRE_LOCK(obj) PyThread_acquire_lock(obj->lock, 1)
82#define RELEASE_LOCK(obj) PyThread_release_lock(obj->lock)
83#else
84#define ACQUIRE_LOCK(obj)
85#define RELEASE_LOCK(obj)
86#endif
87
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000088/* Bits in f_newlinetypes */
89#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
90#define NEWLINE_CR 1 /* \r newline seen */
91#define NEWLINE_LF 2 /* \n newline seen */
92#define NEWLINE_CRLF 4 /* \r\n newline seen */
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +000093
94/* ===================================================================== */
95/* Structure definitions. */
96
97typedef struct {
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +000098 PyObject_HEAD
99 PyObject *file;
100
101 char* f_buf; /* Allocated readahead buffer */
102 char* f_bufend; /* Points after last occupied position */
103 char* f_bufptr; /* Current buffer position */
104
105 int f_softspace; /* Flag used by 'print' command */
106
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000107 int f_univ_newline; /* Handle any newline convention */
108 int f_newlinetypes; /* Types of newlines seen */
109 int f_skipnextlf; /* Skip next \n */
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000110
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000111 BZFILE *fp;
112 int mode;
Georg Brandla8bcecc2005-09-03 07:49:53 +0000113 Py_off_t pos;
114 Py_off_t size;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000115#ifdef WITH_THREAD
116 PyThread_type_lock lock;
117#endif
118} BZ2FileObject;
119
120typedef struct {
121 PyObject_HEAD
122 bz_stream bzs;
123 int running;
124#ifdef WITH_THREAD
125 PyThread_type_lock lock;
126#endif
127} BZ2CompObject;
128
129typedef struct {
130 PyObject_HEAD
131 bz_stream bzs;
132 int running;
133 PyObject *unused_data;
134#ifdef WITH_THREAD
135 PyThread_type_lock lock;
136#endif
137} BZ2DecompObject;
138
139/* ===================================================================== */
140/* Utility functions. */
141
142static int
143Util_CatchBZ2Error(int bzerror)
144{
145 int ret = 0;
146 switch(bzerror) {
147 case BZ_OK:
148 case BZ_STREAM_END:
149 break;
150
Gustavo Niemeyer7628f1f2003-04-27 06:25:24 +0000151#ifdef BZ_CONFIG_ERROR
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000152 case BZ_CONFIG_ERROR:
153 PyErr_SetString(PyExc_SystemError,
154 "the bz2 library was not compiled "
155 "correctly");
156 ret = 1;
157 break;
Gustavo Niemeyer7628f1f2003-04-27 06:25:24 +0000158#endif
Tim Peterse3228092002-11-09 04:21:44 +0000159
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000160 case BZ_PARAM_ERROR:
161 PyErr_SetString(PyExc_ValueError,
162 "the bz2 library has received wrong "
163 "parameters");
164 ret = 1;
165 break;
Tim Peterse3228092002-11-09 04:21:44 +0000166
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000167 case BZ_MEM_ERROR:
168 PyErr_NoMemory();
169 ret = 1;
170 break;
171
172 case BZ_DATA_ERROR:
173 case BZ_DATA_ERROR_MAGIC:
174 PyErr_SetString(PyExc_IOError, "invalid data stream");
175 ret = 1;
176 break;
177
178 case BZ_IO_ERROR:
179 PyErr_SetString(PyExc_IOError, "unknown IO error");
180 ret = 1;
181 break;
182
183 case BZ_UNEXPECTED_EOF:
184 PyErr_SetString(PyExc_EOFError,
185 "compressed file ended before the "
186 "logical end-of-stream was detected");
187 ret = 1;
188 break;
189
190 case BZ_SEQUENCE_ERROR:
191 PyErr_SetString(PyExc_RuntimeError,
192 "wrong sequence of bz2 library "
193 "commands used");
194 ret = 1;
195 break;
196 }
197 return ret;
198}
199
200#if BUFSIZ < 8192
201#define SMALLCHUNK 8192
202#else
203#define SMALLCHUNK BUFSIZ
204#endif
205
206#if SIZEOF_INT < 4
207#define BIGCHUNK (512 * 32)
208#else
209#define BIGCHUNK (512 * 1024)
210#endif
211
212/* This is a hacked version of Python's fileobject.c:new_buffersize(). */
213static size_t
214Util_NewBufferSize(size_t currentsize)
215{
216 if (currentsize > SMALLCHUNK) {
217 /* Keep doubling until we reach BIGCHUNK;
218 then keep adding BIGCHUNK. */
219 if (currentsize <= BIGCHUNK)
220 return currentsize + currentsize;
221 else
222 return currentsize + BIGCHUNK;
223 }
224 return currentsize + SMALLCHUNK;
225}
226
227/* This is a hacked version of Python's fileobject.c:get_line(). */
228static PyObject *
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000229Util_GetLine(BZ2FileObject *f, int n)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000230{
231 char c;
232 char *buf, *end;
233 size_t total_v_size; /* total # of slots in buffer */
234 size_t used_v_size; /* # used slots in buffer */
235 size_t increment; /* amount to increment the buffer */
236 PyObject *v;
237 int bzerror;
Sean Reifscheider8335acb2007-09-17 05:45:04 +0000238 int bytes_read;
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000239 int newlinetypes = f->f_newlinetypes;
240 int skipnextlf = f->f_skipnextlf;
241 int univ_newline = f->f_univ_newline;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000242
243 total_v_size = n > 0 ? n : 100;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000244 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000245 if (v == NULL)
246 return NULL;
247
248 buf = BUF(v);
249 end = buf + total_v_size;
250
251 for (;;) {
252 Py_BEGIN_ALLOW_THREADS
Sean Reifscheider8335acb2007-09-17 05:45:04 +0000253 while (buf != end) {
254 bytes_read = BZ2_bzRead(&bzerror, f->fp, &c, 1);
255 f->pos++;
256 if (bytes_read == 0) break;
257 if (univ_newline) {
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +0000258 if (skipnextlf) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000259 skipnextlf = 0;
260 if (c == '\n') {
Sean Reifscheider8335acb2007-09-17 05:45:04 +0000261 /* Seeing a \n here with skipnextlf true means we
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000262 * saw a \r before.
263 */
264 newlinetypes |= NEWLINE_CRLF;
Sean Reifscheider8335acb2007-09-17 05:45:04 +0000265 if (bzerror != BZ_OK) break;
266 bytes_read = BZ2_bzRead(&bzerror, f->fp, &c, 1);
267 f->pos++;
268 if (bytes_read == 0) break;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000269 } else {
270 newlinetypes |= NEWLINE_CR;
271 }
272 }
273 if (c == '\r') {
274 skipnextlf = 1;
275 c = '\n';
Sean Reifscheider8335acb2007-09-17 05:45:04 +0000276 } else if (c == '\n')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000277 newlinetypes |= NEWLINE_LF;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000278 }
Sean Reifscheider8335acb2007-09-17 05:45:04 +0000279 *buf++ = c;
280 if (bzerror != BZ_OK || c == '\n') break;
281 }
282 if (univ_newline && bzerror == BZ_STREAM_END && skipnextlf)
283 newlinetypes |= NEWLINE_CR;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000284 Py_END_ALLOW_THREADS
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000285 f->f_newlinetypes = newlinetypes;
286 f->f_skipnextlf = skipnextlf;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000287 if (bzerror == BZ_STREAM_END) {
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000288 f->size = f->pos;
289 f->mode = MODE_READ_EOF;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000290 break;
291 } else if (bzerror != BZ_OK) {
292 Util_CatchBZ2Error(bzerror);
293 Py_DECREF(v);
294 return NULL;
295 }
296 if (c == '\n')
297 break;
298 /* Must be because buf == end */
299 if (n > 0)
300 break;
301 used_v_size = total_v_size;
302 increment = total_v_size >> 2; /* mild exponential growth */
303 total_v_size += increment;
304 if (total_v_size > INT_MAX) {
305 PyErr_SetString(PyExc_OverflowError,
306 "line is longer than a Python string can hold");
307 Py_DECREF(v);
308 return NULL;
309 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000310 if (_PyString_Resize(&v, total_v_size) < 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000311 return NULL;
312 buf = BUF(v) + used_v_size;
313 end = BUF(v) + total_v_size;
314 }
315
316 used_v_size = buf - BUF(v);
317 if (used_v_size != total_v_size)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000318 _PyString_Resize(&v, used_v_size);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000319 return v;
320}
321
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000322/* This is a hacked version of Python's
323 * fileobject.c:Py_UniversalNewlineFread(). */
324size_t
325Util_UnivNewlineRead(int *bzerror, BZFILE *stream,
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000326 char* buf, size_t n, BZ2FileObject *f)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000327{
328 char *dst = buf;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000329 int newlinetypes, skipnextlf;
330
331 assert(buf != NULL);
332 assert(stream != NULL);
333
334 if (!f->f_univ_newline)
335 return BZ2_bzRead(bzerror, stream, buf, n);
336
337 newlinetypes = f->f_newlinetypes;
338 skipnextlf = f->f_skipnextlf;
339
340 /* Invariant: n is the number of bytes remaining to be filled
341 * in the buffer.
342 */
343 while (n) {
344 size_t nread;
345 int shortread;
346 char *src = dst;
347
348 nread = BZ2_bzRead(bzerror, stream, dst, n);
349 assert(nread <= n);
350 n -= nread; /* assuming 1 byte out for each in; will adjust */
351 shortread = n != 0; /* true iff EOF or error */
352 while (nread--) {
353 char c = *src++;
354 if (c == '\r') {
355 /* Save as LF and set flag to skip next LF. */
356 *dst++ = '\n';
357 skipnextlf = 1;
358 }
359 else if (skipnextlf && c == '\n') {
360 /* Skip LF, and remember we saw CR LF. */
361 skipnextlf = 0;
362 newlinetypes |= NEWLINE_CRLF;
363 ++n;
364 }
365 else {
366 /* Normal char to be stored in buffer. Also
367 * update the newlinetypes flag if either this
368 * is an LF or the previous char was a CR.
369 */
370 if (c == '\n')
371 newlinetypes |= NEWLINE_LF;
372 else if (skipnextlf)
373 newlinetypes |= NEWLINE_CR;
374 *dst++ = c;
375 skipnextlf = 0;
376 }
377 }
378 if (shortread) {
379 /* If this is EOF, update type flags. */
380 if (skipnextlf && *bzerror == BZ_STREAM_END)
381 newlinetypes |= NEWLINE_CR;
382 break;
383 }
384 }
385 f->f_newlinetypes = newlinetypes;
386 f->f_skipnextlf = skipnextlf;
387 return dst - buf;
388}
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000389
390/* This is a hacked version of Python's fileobject.c:drop_readahead(). */
391static void
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000392Util_DropReadAhead(BZ2FileObject *f)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000393{
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000394 if (f->f_buf != NULL) {
395 PyMem_Free(f->f_buf);
396 f->f_buf = NULL;
397 }
398}
399
400/* This is a hacked version of Python's fileobject.c:readahead(). */
401static int
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000402Util_ReadAhead(BZ2FileObject *f, int bufsize)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000403{
404 int chunksize;
405 int bzerror;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000406
407 if (f->f_buf != NULL) {
Tim Peterse3228092002-11-09 04:21:44 +0000408 if((f->f_bufend - f->f_bufptr) >= 1)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000409 return 0;
410 else
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000411 Util_DropReadAhead(f);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000412 }
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000413 if (f->mode == MODE_READ_EOF) {
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000414 f->f_bufptr = f->f_buf;
415 f->f_bufend = f->f_buf;
416 return 0;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000417 }
418 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
419 return -1;
420 }
421 Py_BEGIN_ALLOW_THREADS
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000422 chunksize = Util_UnivNewlineRead(&bzerror, f->fp, f->f_buf,
423 bufsize, f);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000424 Py_END_ALLOW_THREADS
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000425 f->pos += chunksize;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000426 if (bzerror == BZ_STREAM_END) {
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000427 f->size = f->pos;
428 f->mode = MODE_READ_EOF;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000429 } else if (bzerror != BZ_OK) {
430 Util_CatchBZ2Error(bzerror);
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000431 Util_DropReadAhead(f);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000432 return -1;
433 }
434 f->f_bufptr = f->f_buf;
435 f->f_bufend = f->f_buf + chunksize;
436 return 0;
437}
438
439/* This is a hacked version of Python's
440 * fileobject.c:readahead_get_line_skip(). */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000441static PyStringObject *
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000442Util_ReadAheadGetLineSkip(BZ2FileObject *f, int skip, int bufsize)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000443{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000444 PyStringObject* s;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000445 char *bufptr;
446 char *buf;
447 int len;
448
449 if (f->f_buf == NULL)
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000450 if (Util_ReadAhead(f, bufsize) < 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000451 return NULL;
452
453 len = f->f_bufend - f->f_bufptr;
Tim Peterse3228092002-11-09 04:21:44 +0000454 if (len == 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000455 return (PyStringObject *)
456 PyString_FromStringAndSize(NULL, skip);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000457 bufptr = memchr(f->f_bufptr, '\n', len);
458 if (bufptr != NULL) {
459 bufptr++; /* Count the '\n' */
460 len = bufptr - f->f_bufptr;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000461 s = (PyStringObject *)
462 PyString_FromStringAndSize(NULL, skip+len);
Tim Peterse3228092002-11-09 04:21:44 +0000463 if (s == NULL)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000464 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000465 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000466 f->f_bufptr = bufptr;
467 if (bufptr == f->f_bufend)
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000468 Util_DropReadAhead(f);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000469 } else {
470 bufptr = f->f_bufptr;
471 buf = f->f_buf;
472 f->f_buf = NULL; /* Force new readahead buffer */
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000473 s = Util_ReadAheadGetLineSkip(f, skip+len,
474 bufsize + (bufsize>>2));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000475 if (s == NULL) {
476 PyMem_Free(buf);
477 return NULL;
478 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000479 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000480 PyMem_Free(buf);
481 }
482 return s;
483}
484
485/* ===================================================================== */
486/* Methods of BZ2File. */
487
488PyDoc_STRVAR(BZ2File_read__doc__,
489"read([size]) -> string\n\
490\n\
491Read at most size uncompressed bytes, returned as a string. If the size\n\
492argument is negative or omitted, read until EOF is reached.\n\
493");
494
495/* This is a hacked version of Python's fileobject.c:file_read(). */
496static PyObject *
497BZ2File_read(BZ2FileObject *self, PyObject *args)
498{
499 long bytesrequested = -1;
500 size_t bytesread, buffersize, chunksize;
501 int bzerror;
502 PyObject *ret = NULL;
Tim Peterse3228092002-11-09 04:21:44 +0000503
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000504 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
505 return NULL;
Tim Peterse3228092002-11-09 04:21:44 +0000506
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000507 ACQUIRE_LOCK(self);
508 switch (self->mode) {
509 case MODE_READ:
510 break;
511 case MODE_READ_EOF:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000512 ret = PyString_FromString("");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000513 goto cleanup;
514 case MODE_CLOSED:
515 PyErr_SetString(PyExc_ValueError,
516 "I/O operation on closed file");
517 goto cleanup;
518 default:
519 PyErr_SetString(PyExc_IOError,
520 "file is not ready for reading");
521 goto cleanup;
522 }
523
524 if (bytesrequested < 0)
525 buffersize = Util_NewBufferSize((size_t)0);
526 else
527 buffersize = bytesrequested;
528 if (buffersize > INT_MAX) {
529 PyErr_SetString(PyExc_OverflowError,
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +0000530 "requested number of bytes is "
531 "more than a Python string can hold");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000532 goto cleanup;
533 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000534 ret = PyString_FromStringAndSize((char *)NULL, buffersize);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000535 if (ret == NULL)
536 goto cleanup;
537 bytesread = 0;
538
539 for (;;) {
540 Py_BEGIN_ALLOW_THREADS
541 chunksize = Util_UnivNewlineRead(&bzerror, self->fp,
542 BUF(ret)+bytesread,
543 buffersize-bytesread,
544 self);
545 self->pos += chunksize;
546 Py_END_ALLOW_THREADS
547 bytesread += chunksize;
548 if (bzerror == BZ_STREAM_END) {
549 self->size = self->pos;
550 self->mode = MODE_READ_EOF;
551 break;
552 } else if (bzerror != BZ_OK) {
553 Util_CatchBZ2Error(bzerror);
554 Py_DECREF(ret);
555 ret = NULL;
556 goto cleanup;
557 }
558 if (bytesrequested < 0) {
559 buffersize = Util_NewBufferSize(buffersize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000560 if (_PyString_Resize(&ret, buffersize) < 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000561 goto cleanup;
562 } else {
563 break;
564 }
565 }
566 if (bytesread != buffersize)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000567 _PyString_Resize(&ret, bytesread);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000568
569cleanup:
570 RELEASE_LOCK(self);
571 return ret;
572}
573
574PyDoc_STRVAR(BZ2File_readline__doc__,
575"readline([size]) -> string\n\
576\n\
577Return the next line from the file, as a string, retaining newline.\n\
578A non-negative size argument will limit the maximum number of bytes to\n\
579return (an incomplete line may be returned then). Return an empty\n\
580string at EOF.\n\
581");
582
583static PyObject *
584BZ2File_readline(BZ2FileObject *self, PyObject *args)
585{
586 PyObject *ret = NULL;
587 int sizehint = -1;
588
589 if (!PyArg_ParseTuple(args, "|i:readline", &sizehint))
590 return NULL;
591
592 ACQUIRE_LOCK(self);
593 switch (self->mode) {
594 case MODE_READ:
595 break;
596 case MODE_READ_EOF:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000597 ret = PyString_FromString("");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000598 goto cleanup;
599 case MODE_CLOSED:
600 PyErr_SetString(PyExc_ValueError,
601 "I/O operation on closed file");
602 goto cleanup;
603 default:
604 PyErr_SetString(PyExc_IOError,
605 "file is not ready for reading");
606 goto cleanup;
607 }
608
609 if (sizehint == 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000610 ret = PyString_FromString("");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000611 else
612 ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint);
613
614cleanup:
615 RELEASE_LOCK(self);
616 return ret;
617}
618
619PyDoc_STRVAR(BZ2File_readlines__doc__,
620"readlines([size]) -> list\n\
621\n\
622Call readline() repeatedly and return a list of lines read.\n\
623The optional size argument, if given, is an approximate bound on the\n\
624total number of bytes in the lines returned.\n\
625");
626
627/* This is a hacked version of Python's fileobject.c:file_readlines(). */
628static PyObject *
629BZ2File_readlines(BZ2FileObject *self, PyObject *args)
630{
631 long sizehint = 0;
632 PyObject *list = NULL;
633 PyObject *line;
634 char small_buffer[SMALLCHUNK];
635 char *buffer = small_buffer;
636 size_t buffersize = SMALLCHUNK;
637 PyObject *big_buffer = NULL;
638 size_t nfilled = 0;
639 size_t nread;
640 size_t totalread = 0;
641 char *p, *q, *end;
642 int err;
643 int shortread = 0;
644 int bzerror;
645
646 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
647 return NULL;
648
649 ACQUIRE_LOCK(self);
650 switch (self->mode) {
651 case MODE_READ:
652 break;
653 case MODE_READ_EOF:
654 list = PyList_New(0);
655 goto cleanup;
656 case MODE_CLOSED:
657 PyErr_SetString(PyExc_ValueError,
658 "I/O operation on closed file");
659 goto cleanup;
660 default:
661 PyErr_SetString(PyExc_IOError,
662 "file is not ready for reading");
663 goto cleanup;
664 }
665
666 if ((list = PyList_New(0)) == NULL)
667 goto cleanup;
668
669 for (;;) {
670 Py_BEGIN_ALLOW_THREADS
671 nread = Util_UnivNewlineRead(&bzerror, self->fp,
672 buffer+nfilled,
673 buffersize-nfilled, self);
674 self->pos += nread;
675 Py_END_ALLOW_THREADS
676 if (bzerror == BZ_STREAM_END) {
677 self->size = self->pos;
678 self->mode = MODE_READ_EOF;
679 if (nread == 0) {
680 sizehint = 0;
681 break;
682 }
683 shortread = 1;
684 } else if (bzerror != BZ_OK) {
685 Util_CatchBZ2Error(bzerror);
686 error:
687 Py_DECREF(list);
688 list = NULL;
689 goto cleanup;
690 }
691 totalread += nread;
692 p = memchr(buffer+nfilled, '\n', nread);
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000693 if (!shortread && p == NULL) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000694 /* Need a larger buffer to fit this line */
695 nfilled += nread;
696 buffersize *= 2;
697 if (buffersize > INT_MAX) {
698 PyErr_SetString(PyExc_OverflowError,
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000699 "line is longer than a Python string can hold");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000700 goto error;
701 }
702 if (big_buffer == NULL) {
703 /* Create the big buffer */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000704 big_buffer = PyString_FromStringAndSize(
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000705 NULL, buffersize);
706 if (big_buffer == NULL)
707 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000708 buffer = PyString_AS_STRING(big_buffer);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000709 memcpy(buffer, small_buffer, nfilled);
710 }
711 else {
712 /* Grow the big buffer */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000713 _PyString_Resize(&big_buffer, buffersize);
714 buffer = PyString_AS_STRING(big_buffer);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000715 }
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000716 continue;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000717 }
718 end = buffer+nfilled+nread;
719 q = buffer;
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000720 while (p != NULL) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000721 /* Process complete lines */
722 p++;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000723 line = PyString_FromStringAndSize(q, p-q);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000724 if (line == NULL)
725 goto error;
726 err = PyList_Append(list, line);
727 Py_DECREF(line);
728 if (err != 0)
729 goto error;
730 q = p;
731 p = memchr(q, '\n', end-q);
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000732 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000733 /* Move the remaining incomplete line to the start */
734 nfilled = end-q;
735 memmove(buffer, q, nfilled);
736 if (sizehint > 0)
737 if (totalread >= (size_t)sizehint)
738 break;
739 if (shortread) {
740 sizehint = 0;
741 break;
742 }
743 }
744 if (nfilled != 0) {
745 /* Partial last line */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000746 line = PyString_FromStringAndSize(buffer, nfilled);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000747 if (line == NULL)
748 goto error;
749 if (sizehint > 0) {
750 /* Need to complete the last line */
751 PyObject *rest = Util_GetLine(self, 0);
752 if (rest == NULL) {
753 Py_DECREF(line);
754 goto error;
755 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000756 PyString_Concat(&line, rest);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000757 Py_DECREF(rest);
758 if (line == NULL)
759 goto error;
760 }
761 err = PyList_Append(list, line);
762 Py_DECREF(line);
763 if (err != 0)
764 goto error;
765 }
766
767 cleanup:
768 RELEASE_LOCK(self);
769 if (big_buffer) {
770 Py_DECREF(big_buffer);
771 }
772 return list;
773}
774
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000775PyDoc_STRVAR(BZ2File_xreadlines__doc__,
776"xreadlines() -> self\n\
777\n\
778For backward compatibility. BZ2File objects now include the performance\n\
779optimizations previously implemented in the xreadlines module.\n\
780");
781
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000782PyDoc_STRVAR(BZ2File_write__doc__,
783"write(data) -> None\n\
784\n\
785Write the 'data' string to file. Note that due to buffering, close() may\n\
786be needed before the file on disk reflects the data written.\n\
787");
788
789/* This is a hacked version of Python's fileobject.c:file_write(). */
790static PyObject *
791BZ2File_write(BZ2FileObject *self, PyObject *args)
792{
793 PyObject *ret = NULL;
794 char *buf;
795 int len;
796 int bzerror;
797
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000798 if (!PyArg_ParseTuple(args, "s#:write", &buf, &len))
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000799 return NULL;
Tim Peterse3228092002-11-09 04:21:44 +0000800
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000801 ACQUIRE_LOCK(self);
802 switch (self->mode) {
803 case MODE_WRITE:
804 break;
Tim Peterse3228092002-11-09 04:21:44 +0000805
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000806 case MODE_CLOSED:
807 PyErr_SetString(PyExc_ValueError,
808 "I/O operation on closed file");
Georg Brandl3335a7a2006-08-14 21:42:55 +0000809 goto cleanup;
Tim Peterse3228092002-11-09 04:21:44 +0000810
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000811 default:
812 PyErr_SetString(PyExc_IOError,
813 "file is not ready for writing");
Georg Brandl3335a7a2006-08-14 21:42:55 +0000814 goto cleanup;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000815 }
816
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000817 self->f_softspace = 0;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000818
819 Py_BEGIN_ALLOW_THREADS
820 BZ2_bzWrite (&bzerror, self->fp, buf, len);
821 self->pos += len;
822 Py_END_ALLOW_THREADS
Tim Peterse3228092002-11-09 04:21:44 +0000823
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000824 if (bzerror != BZ_OK) {
825 Util_CatchBZ2Error(bzerror);
826 goto cleanup;
827 }
Tim Peterse3228092002-11-09 04:21:44 +0000828
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000829 Py_INCREF(Py_None);
830 ret = Py_None;
831
832cleanup:
833 RELEASE_LOCK(self);
834 return ret;
835}
836
837PyDoc_STRVAR(BZ2File_writelines__doc__,
838"writelines(sequence_of_strings) -> None\n\
839\n\
840Write the sequence of strings to the file. Note that newlines are not\n\
841added. The sequence can be any iterable object producing strings. This is\n\
842equivalent to calling write() for each string.\n\
843");
844
845/* This is a hacked version of Python's fileobject.c:file_writelines(). */
846static PyObject *
847BZ2File_writelines(BZ2FileObject *self, PyObject *seq)
848{
849#define CHUNKSIZE 1000
850 PyObject *list = NULL;
851 PyObject *iter = NULL;
852 PyObject *ret = NULL;
853 PyObject *line;
854 int i, j, index, len, islist;
855 int bzerror;
856
857 ACQUIRE_LOCK(self);
Georg Brandl3335a7a2006-08-14 21:42:55 +0000858 switch (self->mode) {
859 case MODE_WRITE:
860 break;
861
862 case MODE_CLOSED:
863 PyErr_SetString(PyExc_ValueError,
864 "I/O operation on closed file");
865 goto error;
866
867 default:
868 PyErr_SetString(PyExc_IOError,
869 "file is not ready for writing");
870 goto error;
871 }
872
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000873 islist = PyList_Check(seq);
874 if (!islist) {
875 iter = PyObject_GetIter(seq);
876 if (iter == NULL) {
877 PyErr_SetString(PyExc_TypeError,
878 "writelines() requires an iterable argument");
879 goto error;
880 }
881 list = PyList_New(CHUNKSIZE);
882 if (list == NULL)
883 goto error;
884 }
885
886 /* Strategy: slurp CHUNKSIZE lines into a private list,
887 checking that they are all strings, then write that list
888 without holding the interpreter lock, then come back for more. */
889 for (index = 0; ; index += CHUNKSIZE) {
890 if (islist) {
891 Py_XDECREF(list);
892 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
893 if (list == NULL)
894 goto error;
895 j = PyList_GET_SIZE(list);
896 }
897 else {
898 for (j = 0; j < CHUNKSIZE; j++) {
899 line = PyIter_Next(iter);
900 if (line == NULL) {
901 if (PyErr_Occurred())
902 goto error;
903 break;
904 }
905 PyList_SetItem(list, j, line);
906 }
907 }
908 if (j == 0)
909 break;
910
911 /* Check that all entries are indeed strings. If not,
912 apply the same rules as for file.write() and
913 convert the rets to strings. This is slow, but
914 seems to be the only way since all conversion APIs
915 could potentially execute Python code. */
916 for (i = 0; i < j; i++) {
917 PyObject *v = PyList_GET_ITEM(list, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000918 if (!PyString_Check(v)) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000919 const char *buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000920 Py_ssize_t len;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000921 if (PyObject_AsCharBuffer(v, &buffer, &len)) {
922 PyErr_SetString(PyExc_TypeError,
923 "writelines() "
924 "argument must be "
925 "a sequence of "
926 "strings");
927 goto error;
928 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000929 line = PyString_FromStringAndSize(buffer,
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000930 len);
931 if (line == NULL)
932 goto error;
933 Py_DECREF(v);
934 PyList_SET_ITEM(list, i, line);
935 }
936 }
937
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +0000938 self->f_softspace = 0;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000939
940 /* Since we are releasing the global lock, the
941 following code may *not* execute Python code. */
942 Py_BEGIN_ALLOW_THREADS
943 for (i = 0; i < j; i++) {
944 line = PyList_GET_ITEM(list, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000945 len = PyString_GET_SIZE(line);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000946 BZ2_bzWrite (&bzerror, self->fp,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000947 PyString_AS_STRING(line), len);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000948 if (bzerror != BZ_OK) {
949 Py_BLOCK_THREADS
950 Util_CatchBZ2Error(bzerror);
951 goto error;
952 }
953 }
954 Py_END_ALLOW_THREADS
955
956 if (j < CHUNKSIZE)
957 break;
958 }
959
960 Py_INCREF(Py_None);
961 ret = Py_None;
962
963 error:
964 RELEASE_LOCK(self);
965 Py_XDECREF(list);
966 Py_XDECREF(iter);
967 return ret;
968#undef CHUNKSIZE
969}
970
971PyDoc_STRVAR(BZ2File_seek__doc__,
972"seek(offset [, whence]) -> None\n\
973\n\
974Move to new file position. Argument offset is a byte count. Optional\n\
975argument whence defaults to 0 (offset from start of file, offset\n\
976should be >= 0); other values are 1 (move relative to current position,\n\
977positive or negative), and 2 (move relative to end of file, usually\n\
978negative, although many platforms allow seeking beyond the end of a file).\n\
979\n\
980Note that seeking of bz2 files is emulated, and depending on the parameters\n\
981the operation may be extremely slow.\n\
982");
983
984static PyObject *
985BZ2File_seek(BZ2FileObject *self, PyObject *args)
986{
987 int where = 0;
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000988 PyObject *offobj;
989 Py_off_t offset;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000990 char small_buffer[SMALLCHUNK];
991 char *buffer = small_buffer;
992 size_t buffersize = SMALLCHUNK;
Andrew M. Kuchling44b054b2006-12-18 19:22:24 +0000993 Py_off_t bytesread = 0;
Georg Brandla8bcecc2005-09-03 07:49:53 +0000994 size_t readsize;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000995 int chunksize;
996 int bzerror;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +0000997 PyObject *ret = NULL;
Tim Peterse3228092002-11-09 04:21:44 +0000998
Georg Brandl33a5f2a2005-08-21 14:16:04 +0000999 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &where))
1000 return NULL;
1001#if !defined(HAVE_LARGEFILE_SUPPORT)
1002 offset = PyInt_AsLong(offobj);
1003#else
1004 offset = PyLong_Check(offobj) ?
1005 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
1006#endif
1007 if (PyErr_Occurred())
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001008 return NULL;
1009
1010 ACQUIRE_LOCK(self);
1011 Util_DropReadAhead(self);
1012 switch (self->mode) {
1013 case MODE_READ:
1014 case MODE_READ_EOF:
1015 break;
Tim Peterse3228092002-11-09 04:21:44 +00001016
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001017 case MODE_CLOSED:
1018 PyErr_SetString(PyExc_ValueError,
1019 "I/O operation on closed file");
Neal Norwitzd3f91902006-09-23 04:11:38 +00001020 goto cleanup;
Tim Peterse3228092002-11-09 04:21:44 +00001021
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001022 default:
1023 PyErr_SetString(PyExc_IOError,
1024 "seek works only while reading");
Neal Norwitzd3f91902006-09-23 04:11:38 +00001025 goto cleanup;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001026 }
1027
Georg Brandl47fab922006-02-18 21:57:25 +00001028 if (where == 2) {
1029 if (self->size == -1) {
1030 assert(self->mode != MODE_READ_EOF);
1031 for (;;) {
1032 Py_BEGIN_ALLOW_THREADS
1033 chunksize = Util_UnivNewlineRead(
1034 &bzerror, self->fp,
1035 buffer, buffersize,
1036 self);
1037 self->pos += chunksize;
1038 Py_END_ALLOW_THREADS
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001039
Georg Brandl47fab922006-02-18 21:57:25 +00001040 bytesread += chunksize;
1041 if (bzerror == BZ_STREAM_END) {
1042 break;
1043 } else if (bzerror != BZ_OK) {
1044 Util_CatchBZ2Error(bzerror);
1045 goto cleanup;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001046 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001047 }
Georg Brandl47fab922006-02-18 21:57:25 +00001048 self->mode = MODE_READ_EOF;
1049 self->size = self->pos;
1050 bytesread = 0;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001051 }
Georg Brandl47fab922006-02-18 21:57:25 +00001052 offset = self->size + offset;
1053 } else if (where == 1) {
1054 offset = self->pos + offset;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001055 }
1056
Georg Brandl47fab922006-02-18 21:57:25 +00001057 /* Before getting here, offset must be the absolute position the file
1058 * pointer should be set to. */
1059
1060 if (offset >= self->pos) {
1061 /* we can move forward */
1062 offset -= self->pos;
1063 } else {
1064 /* we cannot move back, so rewind the stream */
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001065 BZ2_bzReadClose(&bzerror, self->fp);
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001066 if (self->fp) {
Gregory P. Smith73bee442008-04-12 20:37:48 +00001067 PyFile_DecUseCount((PyFileObject *)self->file);
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001068 self->fp = NULL;
1069 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001070 if (bzerror != BZ_OK) {
1071 Util_CatchBZ2Error(bzerror);
1072 goto cleanup;
1073 }
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001074 ret = PyObject_CallMethod(self->file, "seek", "(i)", 0);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001075 if (!ret)
1076 goto cleanup;
1077 Py_DECREF(ret);
1078 ret = NULL;
1079 self->pos = 0;
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001080 self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file),
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001081 0, 0, NULL, 0);
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001082 if (self->fp)
Gregory P. Smith73bee442008-04-12 20:37:48 +00001083 PyFile_IncUseCount((PyFileObject *)self->file);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001084 if (bzerror != BZ_OK) {
1085 Util_CatchBZ2Error(bzerror);
1086 goto cleanup;
1087 }
1088 self->mode = MODE_READ;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001089 }
1090
Georg Brandl47fab922006-02-18 21:57:25 +00001091 if (offset <= 0 || self->mode == MODE_READ_EOF)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001092 goto exit;
1093
1094 /* Before getting here, offset must be set to the number of bytes
1095 * to walk forward. */
1096 for (;;) {
Georg Brandla8bcecc2005-09-03 07:49:53 +00001097 if (offset-bytesread > buffersize)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001098 readsize = buffersize;
1099 else
Georg Brandla8bcecc2005-09-03 07:49:53 +00001100 /* offset might be wider that readsize, but the result
1101 * of the subtraction is bound by buffersize (see the
1102 * condition above). buffersize is 8192. */
1103 readsize = (size_t)(offset-bytesread);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001104 Py_BEGIN_ALLOW_THREADS
1105 chunksize = Util_UnivNewlineRead(&bzerror, self->fp,
1106 buffer, readsize, self);
1107 self->pos += chunksize;
1108 Py_END_ALLOW_THREADS
1109 bytesread += chunksize;
1110 if (bzerror == BZ_STREAM_END) {
1111 self->size = self->pos;
1112 self->mode = MODE_READ_EOF;
1113 break;
1114 } else if (bzerror != BZ_OK) {
1115 Util_CatchBZ2Error(bzerror);
1116 goto cleanup;
1117 }
1118 if (bytesread == offset)
1119 break;
1120 }
1121
1122exit:
1123 Py_INCREF(Py_None);
1124 ret = Py_None;
1125
1126cleanup:
1127 RELEASE_LOCK(self);
1128 return ret;
1129}
1130
1131PyDoc_STRVAR(BZ2File_tell__doc__,
1132"tell() -> int\n\
1133\n\
1134Return the current file position, an integer (may be a long integer).\n\
1135");
1136
1137static PyObject *
1138BZ2File_tell(BZ2FileObject *self, PyObject *args)
1139{
1140 PyObject *ret = NULL;
1141
1142 if (self->mode == MODE_CLOSED) {
1143 PyErr_SetString(PyExc_ValueError,
1144 "I/O operation on closed file");
1145 goto cleanup;
1146 }
1147
Georg Brandla8bcecc2005-09-03 07:49:53 +00001148#if !defined(HAVE_LARGEFILE_SUPPORT)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001149 ret = PyInt_FromLong(self->pos);
Georg Brandla8bcecc2005-09-03 07:49:53 +00001150#else
1151 ret = PyLong_FromLongLong(self->pos);
1152#endif
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001153
1154cleanup:
1155 return ret;
1156}
1157
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001158PyDoc_STRVAR(BZ2File_close__doc__,
1159"close() -> None or (perhaps) an integer\n\
1160\n\
1161Close the file. Sets data attribute .closed to true. A closed file\n\
1162cannot be used for further I/O operations. close() may be called more\n\
1163than once without error.\n\
1164");
1165
1166static PyObject *
1167BZ2File_close(BZ2FileObject *self)
1168{
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001169 PyObject *ret = NULL;
1170 int bzerror = BZ_OK;
1171
1172 ACQUIRE_LOCK(self);
1173 switch (self->mode) {
1174 case MODE_READ:
1175 case MODE_READ_EOF:
1176 BZ2_bzReadClose(&bzerror, self->fp);
1177 break;
1178 case MODE_WRITE:
1179 BZ2_bzWriteClose(&bzerror, self->fp,
1180 0, NULL, NULL);
1181 break;
1182 }
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001183 if (self->fp) {
Gregory P. Smith73bee442008-04-12 20:37:48 +00001184 PyFile_DecUseCount((PyFileObject *)self->file);
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001185 self->fp = NULL;
1186 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001187 self->mode = MODE_CLOSED;
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001188 ret = PyObject_CallMethod(self->file, "close", NULL);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001189 if (bzerror != BZ_OK) {
1190 Util_CatchBZ2Error(bzerror);
1191 Py_XDECREF(ret);
1192 ret = NULL;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001193 }
1194
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001195 RELEASE_LOCK(self);
1196 return ret;
1197}
1198
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001199static PyObject *BZ2File_getiter(BZ2FileObject *self);
1200
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001201static PyMethodDef BZ2File_methods[] = {
1202 {"read", (PyCFunction)BZ2File_read, METH_VARARGS, BZ2File_read__doc__},
1203 {"readline", (PyCFunction)BZ2File_readline, METH_VARARGS, BZ2File_readline__doc__},
1204 {"readlines", (PyCFunction)BZ2File_readlines, METH_VARARGS, BZ2File_readlines__doc__},
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001205 {"xreadlines", (PyCFunction)BZ2File_getiter, METH_VARARGS, BZ2File_xreadlines__doc__},
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001206 {"write", (PyCFunction)BZ2File_write, METH_VARARGS, BZ2File_write__doc__},
1207 {"writelines", (PyCFunction)BZ2File_writelines, METH_O, BZ2File_writelines__doc__},
1208 {"seek", (PyCFunction)BZ2File_seek, METH_VARARGS, BZ2File_seek__doc__},
1209 {"tell", (PyCFunction)BZ2File_tell, METH_NOARGS, BZ2File_tell__doc__},
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001210 {"close", (PyCFunction)BZ2File_close, METH_NOARGS, BZ2File_close__doc__},
1211 {NULL, NULL} /* sentinel */
1212};
1213
1214
1215/* ===================================================================== */
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001216/* Getters and setters of BZ2File. */
1217
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001218/* This is a hacked version of Python's fileobject.c:get_newlines(). */
1219static PyObject *
1220BZ2File_get_newlines(BZ2FileObject *self, void *closure)
1221{
1222 switch (self->f_newlinetypes) {
1223 case NEWLINE_UNKNOWN:
1224 Py_INCREF(Py_None);
1225 return Py_None;
1226 case NEWLINE_CR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001227 return PyString_FromString("\r");
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001228 case NEWLINE_LF:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001229 return PyString_FromString("\n");
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001230 case NEWLINE_CR|NEWLINE_LF:
1231 return Py_BuildValue("(ss)", "\r", "\n");
1232 case NEWLINE_CRLF:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001233 return PyString_FromString("\r\n");
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001234 case NEWLINE_CR|NEWLINE_CRLF:
1235 return Py_BuildValue("(ss)", "\r", "\r\n");
1236 case NEWLINE_LF|NEWLINE_CRLF:
1237 return Py_BuildValue("(ss)", "\n", "\r\n");
1238 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1239 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1240 default:
1241 PyErr_Format(PyExc_SystemError,
1242 "Unknown newlines value 0x%x\n",
1243 self->f_newlinetypes);
1244 return NULL;
1245 }
1246}
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001247
1248static PyObject *
1249BZ2File_get_closed(BZ2FileObject *self, void *closure)
1250{
1251 return PyInt_FromLong(self->mode == MODE_CLOSED);
1252}
1253
1254static PyObject *
1255BZ2File_get_mode(BZ2FileObject *self, void *closure)
1256{
1257 return PyObject_GetAttrString(self->file, "mode");
1258}
1259
1260static PyObject *
1261BZ2File_get_name(BZ2FileObject *self, void *closure)
1262{
1263 return PyObject_GetAttrString(self->file, "name");
1264}
1265
1266static PyGetSetDef BZ2File_getset[] = {
1267 {"closed", (getter)BZ2File_get_closed, NULL,
1268 "True if the file is closed"},
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001269 {"newlines", (getter)BZ2File_get_newlines, NULL,
1270 "end-of-line convention used in this file"},
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001271 {"mode", (getter)BZ2File_get_mode, NULL,
1272 "file mode ('r', 'w', or 'U')"},
1273 {"name", (getter)BZ2File_get_name, NULL,
1274 "file name"},
1275 {NULL} /* Sentinel */
1276};
1277
1278
1279/* ===================================================================== */
1280/* Members of BZ2File_Type. */
1281
1282#undef OFF
1283#define OFF(x) offsetof(BZ2FileObject, x)
1284
1285static PyMemberDef BZ2File_members[] = {
1286 {"softspace", T_INT, OFF(f_softspace), 0,
1287 "flag indicating that a space needs to be printed; used by print"},
1288 {NULL} /* Sentinel */
1289};
1290
1291/* ===================================================================== */
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001292/* Slot definitions for BZ2File_Type. */
1293
1294static int
1295BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
1296{
Martin v. Löwis15e62742006-02-27 16:46:16 +00001297 static char *kwlist[] = {"filename", "mode", "buffering",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001298 "compresslevel", 0};
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001299 PyObject *name;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001300 char *mode = "r";
1301 int buffering = -1;
1302 int compresslevel = 9;
1303 int bzerror;
1304 int mode_char = 0;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001305
1306 self->size = -1;
Tim Peterse3228092002-11-09 04:21:44 +00001307
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001308 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sii:BZ2File",
1309 kwlist, &name, &mode, &buffering,
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001310 &compresslevel))
1311 return -1;
1312
1313 if (compresslevel < 1 || compresslevel > 9) {
1314 PyErr_SetString(PyExc_ValueError,
1315 "compresslevel must be between 1 and 9");
1316 return -1;
1317 }
1318
1319 for (;;) {
1320 int error = 0;
1321 switch (*mode) {
1322 case 'r':
1323 case 'w':
1324 if (mode_char)
1325 error = 1;
1326 mode_char = *mode;
1327 break;
1328
1329 case 'b':
1330 break;
1331
1332 case 'U':
Neal Norwitz2a30cd02006-07-10 01:18:57 +00001333#ifdef __VMS
1334 self->f_univ_newline = 0;
1335#else
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001336 self->f_univ_newline = 1;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00001337#endif
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001338 break;
1339
1340 default:
1341 error = 1;
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001342 break;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001343 }
1344 if (error) {
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001345 PyErr_Format(PyExc_ValueError,
1346 "invalid mode char %c", *mode);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001347 return -1;
1348 }
1349 mode++;
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001350 if (*mode == '\0')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001351 break;
1352 }
1353
Georg Brandl6b95f1d2005-06-03 19:47:00 +00001354 if (mode_char == 0) {
1355 mode_char = 'r';
1356 }
1357
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001358 mode = (mode_char == 'r') ? "rb" : "wb";
Tim Peterse3228092002-11-09 04:21:44 +00001359
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001360 self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)",
1361 name, mode, buffering);
1362 if (self->file == NULL)
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001363 return -1;
1364
1365 /* From now on, we have stuff to dealloc, so jump to error label
1366 * instead of returning */
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001367
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001368#ifdef WITH_THREAD
1369 self->lock = PyThread_allocate_lock();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001370 if (!self->lock) {
1371 PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001372 goto error;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001373 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001374#endif
1375
1376 if (mode_char == 'r')
1377 self->fp = BZ2_bzReadOpen(&bzerror,
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001378 PyFile_AsFile(self->file),
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001379 0, 0, NULL, 0);
1380 else
1381 self->fp = BZ2_bzWriteOpen(&bzerror,
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001382 PyFile_AsFile(self->file),
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001383 compresslevel, 0, 0);
1384
1385 if (bzerror != BZ_OK) {
1386 Util_CatchBZ2Error(bzerror);
1387 goto error;
1388 }
Gregory P. Smith73bee442008-04-12 20:37:48 +00001389 PyFile_IncUseCount((PyFileObject *)self->file);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001390
1391 self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE;
1392
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001393 return 0;
1394
1395error:
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001396 Py_CLEAR(self->file);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001397#ifdef WITH_THREAD
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001398 if (self->lock) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001399 PyThread_free_lock(self->lock);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001400 self->lock = NULL;
1401 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001402#endif
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001403 return -1;
1404}
1405
1406static void
1407BZ2File_dealloc(BZ2FileObject *self)
1408{
1409 int bzerror;
1410#ifdef WITH_THREAD
1411 if (self->lock)
1412 PyThread_free_lock(self->lock);
1413#endif
1414 switch (self->mode) {
1415 case MODE_READ:
1416 case MODE_READ_EOF:
1417 BZ2_bzReadClose(&bzerror, self->fp);
1418 break;
1419 case MODE_WRITE:
1420 BZ2_bzWriteClose(&bzerror, self->fp,
1421 0, NULL, NULL);
1422 break;
1423 }
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001424 if (self->fp) {
Gregory P. Smith73bee442008-04-12 20:37:48 +00001425 PyFile_DecUseCount((PyFileObject *)self->file);
Gregory P. Smithc20adf82008-04-07 06:33:21 +00001426 self->fp = NULL;
1427 }
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001428 Util_DropReadAhead(self);
Gustavo Niemeyer572f5232003-04-29 14:53:08 +00001429 Py_XDECREF(self->file);
Christian Heimese93237d2007-12-19 02:37:44 +00001430 Py_TYPE(self)->tp_free((PyObject *)self);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001431}
1432
1433/* This is a hacked version of Python's fileobject.c:file_getiter(). */
1434static PyObject *
1435BZ2File_getiter(BZ2FileObject *self)
1436{
1437 if (self->mode == MODE_CLOSED) {
1438 PyErr_SetString(PyExc_ValueError,
1439 "I/O operation on closed file");
1440 return NULL;
1441 }
1442 Py_INCREF((PyObject*)self);
1443 return (PyObject *)self;
1444}
1445
1446/* This is a hacked version of Python's fileobject.c:file_iternext(). */
1447#define READAHEAD_BUFSIZE 8192
1448static PyObject *
1449BZ2File_iternext(BZ2FileObject *self)
1450{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001451 PyStringObject* ret;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001452 ACQUIRE_LOCK(self);
1453 if (self->mode == MODE_CLOSED) {
Gregory P. Smith3b1e6b22008-07-07 04:31:58 +00001454 RELEASE_LOCK(self);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001455 PyErr_SetString(PyExc_ValueError,
1456 "I/O operation on closed file");
1457 return NULL;
1458 }
1459 ret = Util_ReadAheadGetLineSkip(self, 0, READAHEAD_BUFSIZE);
1460 RELEASE_LOCK(self);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001461 if (ret == NULL || PyString_GET_SIZE(ret) == 0) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001462 Py_XDECREF(ret);
1463 return NULL;
1464 }
1465 return (PyObject *)ret;
1466}
1467
1468/* ===================================================================== */
1469/* BZ2File_Type definition. */
1470
1471PyDoc_VAR(BZ2File__doc__) =
1472PyDoc_STR(
1473"BZ2File(name [, mode='r', buffering=0, compresslevel=9]) -> file object\n\
1474\n\
1475Open a bz2 file. The mode can be 'r' or 'w', for reading (default) or\n\
1476writing. When opened for writing, the file will be created if it doesn't\n\
1477exist, and truncated otherwise. If the buffering argument is given, 0 means\n\
1478unbuffered, and larger numbers specify the buffer size. If compresslevel\n\
1479is given, must be a number between 1 and 9.\n\
1480")
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001481PyDoc_STR(
1482"\n\
1483Add a 'U' to mode to open the file for input with universal newline\n\
1484support. Any line ending in the input file will be seen as a '\\n' in\n\
1485Python. Also, a file so opened gains the attribute 'newlines'; the value\n\
1486for this attribute is one of None (no newline read yet), '\\r', '\\n',\n\
1487'\\r\\n' or a tuple containing all the newline types seen. Universal\n\
1488newlines are available only when reading.\n\
1489")
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001490;
1491
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001492static PyTypeObject BZ2File_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001493 PyVarObject_HEAD_INIT(NULL, 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001494 "bz2.BZ2File", /*tp_name*/
1495 sizeof(BZ2FileObject), /*tp_basicsize*/
1496 0, /*tp_itemsize*/
1497 (destructor)BZ2File_dealloc, /*tp_dealloc*/
1498 0, /*tp_print*/
1499 0, /*tp_getattr*/
1500 0, /*tp_setattr*/
1501 0, /*tp_compare*/
1502 0, /*tp_repr*/
1503 0, /*tp_as_number*/
1504 0, /*tp_as_sequence*/
1505 0, /*tp_as_mapping*/
1506 0, /*tp_hash*/
1507 0, /*tp_call*/
1508 0, /*tp_str*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001509 PyObject_GenericGetAttr,/*tp_getattro*/
1510 PyObject_GenericSetAttr,/*tp_setattro*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001511 0, /*tp_as_buffer*/
1512 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
1513 BZ2File__doc__, /*tp_doc*/
1514 0, /*tp_traverse*/
1515 0, /*tp_clear*/
1516 0, /*tp_richcompare*/
1517 0, /*tp_weaklistoffset*/
1518 (getiterfunc)BZ2File_getiter, /*tp_iter*/
1519 (iternextfunc)BZ2File_iternext, /*tp_iternext*/
1520 BZ2File_methods, /*tp_methods*/
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001521 BZ2File_members, /*tp_members*/
1522 BZ2File_getset, /*tp_getset*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001523 0, /*tp_base*/
1524 0, /*tp_dict*/
1525 0, /*tp_descr_get*/
1526 0, /*tp_descr_set*/
1527 0, /*tp_dictoffset*/
1528 (initproc)BZ2File_init, /*tp_init*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001529 PyType_GenericAlloc, /*tp_alloc*/
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001530 PyType_GenericNew, /*tp_new*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001531 _PyObject_Del, /*tp_free*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001532 0, /*tp_is_gc*/
1533};
1534
1535
1536/* ===================================================================== */
1537/* Methods of BZ2Comp. */
1538
1539PyDoc_STRVAR(BZ2Comp_compress__doc__,
1540"compress(data) -> string\n\
1541\n\
1542Provide more data to the compressor object. It will return chunks of\n\
1543compressed data whenever possible. When you've finished providing data\n\
1544to compress, call the flush() method to finish the compression process,\n\
1545and return what is left in the internal buffers.\n\
1546");
1547
1548static PyObject *
1549BZ2Comp_compress(BZ2CompObject *self, PyObject *args)
1550{
1551 char *data;
1552 int datasize;
1553 int bufsize = SMALLCHUNK;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001554 PY_LONG_LONG totalout;
Gustavo Niemeyer7d7930b2002-11-05 18:41:53 +00001555 PyObject *ret = NULL;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001556 bz_stream *bzs = &self->bzs;
1557 int bzerror;
1558
Walter Dörwaldbb9c7392004-11-01 17:10:19 +00001559 if (!PyArg_ParseTuple(args, "s#:compress", &data, &datasize))
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001560 return NULL;
1561
Gustavo Niemeyera6e436e2004-02-14 00:02:45 +00001562 if (datasize == 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001563 return PyString_FromString("");
Gustavo Niemeyera6e436e2004-02-14 00:02:45 +00001564
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001565 ACQUIRE_LOCK(self);
1566 if (!self->running) {
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001567 PyErr_SetString(PyExc_ValueError,
1568 "this object was already flushed");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001569 goto error;
1570 }
1571
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001572 ret = PyString_FromStringAndSize(NULL, bufsize);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001573 if (!ret)
1574 goto error;
1575
1576 bzs->next_in = data;
1577 bzs->avail_in = datasize;
1578 bzs->next_out = BUF(ret);
1579 bzs->avail_out = bufsize;
1580
1581 totalout = BZS_TOTAL_OUT(bzs);
1582
1583 for (;;) {
1584 Py_BEGIN_ALLOW_THREADS
1585 bzerror = BZ2_bzCompress(bzs, BZ_RUN);
1586 Py_END_ALLOW_THREADS
1587 if (bzerror != BZ_RUN_OK) {
1588 Util_CatchBZ2Error(bzerror);
1589 goto error;
1590 }
Georg Brandla47337f2007-03-13 12:34:25 +00001591 if (bzs->avail_in == 0)
1592 break; /* no more input data */
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001593 if (bzs->avail_out == 0) {
1594 bufsize = Util_NewBufferSize(bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001595 if (_PyString_Resize(&ret, bufsize) < 0) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001596 BZ2_bzCompressEnd(bzs);
1597 goto error;
1598 }
1599 bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
1600 - totalout);
1601 bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001602 }
1603 }
1604
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001605 _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001606
1607 RELEASE_LOCK(self);
1608 return ret;
1609
1610error:
1611 RELEASE_LOCK(self);
1612 Py_XDECREF(ret);
1613 return NULL;
1614}
1615
1616PyDoc_STRVAR(BZ2Comp_flush__doc__,
1617"flush() -> string\n\
1618\n\
1619Finish the compression process and return what is left in internal buffers.\n\
1620You must not use the compressor object after calling this method.\n\
1621");
1622
1623static PyObject *
1624BZ2Comp_flush(BZ2CompObject *self)
1625{
1626 int bufsize = SMALLCHUNK;
Gustavo Niemeyer7d7930b2002-11-05 18:41:53 +00001627 PyObject *ret = NULL;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001628 bz_stream *bzs = &self->bzs;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001629 PY_LONG_LONG totalout;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001630 int bzerror;
1631
1632 ACQUIRE_LOCK(self);
1633 if (!self->running) {
1634 PyErr_SetString(PyExc_ValueError, "object was already "
1635 "flushed");
1636 goto error;
1637 }
1638 self->running = 0;
1639
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001640 ret = PyString_FromStringAndSize(NULL, bufsize);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001641 if (!ret)
1642 goto error;
1643
1644 bzs->next_out = BUF(ret);
1645 bzs->avail_out = bufsize;
1646
1647 totalout = BZS_TOTAL_OUT(bzs);
1648
1649 for (;;) {
1650 Py_BEGIN_ALLOW_THREADS
1651 bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
1652 Py_END_ALLOW_THREADS
1653 if (bzerror == BZ_STREAM_END) {
1654 break;
1655 } else if (bzerror != BZ_FINISH_OK) {
1656 Util_CatchBZ2Error(bzerror);
1657 goto error;
1658 }
1659 if (bzs->avail_out == 0) {
1660 bufsize = Util_NewBufferSize(bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001661 if (_PyString_Resize(&ret, bufsize) < 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001662 goto error;
1663 bzs->next_out = BUF(ret);
1664 bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
1665 - totalout);
1666 bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
1667 }
1668 }
1669
1670 if (bzs->avail_out != 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001671 _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001672
1673 RELEASE_LOCK(self);
1674 return ret;
1675
1676error:
1677 RELEASE_LOCK(self);
1678 Py_XDECREF(ret);
1679 return NULL;
1680}
1681
1682static PyMethodDef BZ2Comp_methods[] = {
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001683 {"compress", (PyCFunction)BZ2Comp_compress, METH_VARARGS,
1684 BZ2Comp_compress__doc__},
1685 {"flush", (PyCFunction)BZ2Comp_flush, METH_NOARGS,
1686 BZ2Comp_flush__doc__},
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001687 {NULL, NULL} /* sentinel */
1688};
1689
1690
1691/* ===================================================================== */
1692/* Slot definitions for BZ2Comp_Type. */
1693
1694static int
1695BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
1696{
1697 int compresslevel = 9;
1698 int bzerror;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001699 static char *kwlist[] = {"compresslevel", 0};
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001700
1701 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:BZ2Compressor",
1702 kwlist, &compresslevel))
1703 return -1;
1704
1705 if (compresslevel < 1 || compresslevel > 9) {
1706 PyErr_SetString(PyExc_ValueError,
1707 "compresslevel must be between 1 and 9");
1708 goto error;
1709 }
1710
1711#ifdef WITH_THREAD
1712 self->lock = PyThread_allocate_lock();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001713 if (!self->lock) {
1714 PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001715 goto error;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001716 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001717#endif
1718
1719 memset(&self->bzs, 0, sizeof(bz_stream));
1720 bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
1721 if (bzerror != BZ_OK) {
1722 Util_CatchBZ2Error(bzerror);
1723 goto error;
1724 }
1725
1726 self->running = 1;
1727
1728 return 0;
1729error:
1730#ifdef WITH_THREAD
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001731 if (self->lock) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001732 PyThread_free_lock(self->lock);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001733 self->lock = NULL;
1734 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001735#endif
1736 return -1;
1737}
1738
1739static void
1740BZ2Comp_dealloc(BZ2CompObject *self)
1741{
1742#ifdef WITH_THREAD
1743 if (self->lock)
1744 PyThread_free_lock(self->lock);
1745#endif
1746 BZ2_bzCompressEnd(&self->bzs);
Christian Heimese93237d2007-12-19 02:37:44 +00001747 Py_TYPE(self)->tp_free((PyObject *)self);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001748}
1749
1750
1751/* ===================================================================== */
1752/* BZ2Comp_Type definition. */
1753
1754PyDoc_STRVAR(BZ2Comp__doc__,
1755"BZ2Compressor([compresslevel=9]) -> compressor object\n\
1756\n\
1757Create a new compressor object. This object may be used to compress\n\
1758data sequentially. If you want to compress data in one shot, use the\n\
1759compress() function instead. The compresslevel parameter, if given,\n\
1760must be a number between 1 and 9.\n\
1761");
1762
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001763static PyTypeObject BZ2Comp_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001764 PyVarObject_HEAD_INIT(NULL, 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001765 "bz2.BZ2Compressor", /*tp_name*/
1766 sizeof(BZ2CompObject), /*tp_basicsize*/
1767 0, /*tp_itemsize*/
1768 (destructor)BZ2Comp_dealloc, /*tp_dealloc*/
1769 0, /*tp_print*/
1770 0, /*tp_getattr*/
1771 0, /*tp_setattr*/
1772 0, /*tp_compare*/
1773 0, /*tp_repr*/
1774 0, /*tp_as_number*/
1775 0, /*tp_as_sequence*/
1776 0, /*tp_as_mapping*/
1777 0, /*tp_hash*/
1778 0, /*tp_call*/
1779 0, /*tp_str*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001780 PyObject_GenericGetAttr,/*tp_getattro*/
1781 PyObject_GenericSetAttr,/*tp_setattro*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001782 0, /*tp_as_buffer*/
1783 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
1784 BZ2Comp__doc__, /*tp_doc*/
1785 0, /*tp_traverse*/
1786 0, /*tp_clear*/
1787 0, /*tp_richcompare*/
1788 0, /*tp_weaklistoffset*/
1789 0, /*tp_iter*/
1790 0, /*tp_iternext*/
1791 BZ2Comp_methods, /*tp_methods*/
1792 0, /*tp_members*/
1793 0, /*tp_getset*/
1794 0, /*tp_base*/
1795 0, /*tp_dict*/
1796 0, /*tp_descr_get*/
1797 0, /*tp_descr_set*/
1798 0, /*tp_dictoffset*/
1799 (initproc)BZ2Comp_init, /*tp_init*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001800 PyType_GenericAlloc, /*tp_alloc*/
1801 PyType_GenericNew, /*tp_new*/
1802 _PyObject_Del, /*tp_free*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001803 0, /*tp_is_gc*/
1804};
1805
1806
1807/* ===================================================================== */
1808/* Members of BZ2Decomp. */
1809
Gustavo Niemeyera33d0aa2003-02-11 18:46:20 +00001810#undef OFF
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001811#define OFF(x) offsetof(BZ2DecompObject, x)
1812
1813static PyMemberDef BZ2Decomp_members[] = {
1814 {"unused_data", T_OBJECT, OFF(unused_data), RO},
1815 {NULL} /* Sentinel */
1816};
1817
1818
1819/* ===================================================================== */
1820/* Methods of BZ2Decomp. */
1821
1822PyDoc_STRVAR(BZ2Decomp_decompress__doc__,
1823"decompress(data) -> string\n\
1824\n\
1825Provide more data to the decompressor object. It will return chunks\n\
1826of decompressed data whenever possible. If you try to decompress data\n\
1827after the end of stream is found, EOFError will be raised. If any data\n\
1828was found after the end of stream, it'll be ignored and saved in\n\
1829unused_data attribute.\n\
1830");
1831
1832static PyObject *
1833BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args)
1834{
1835 char *data;
1836 int datasize;
1837 int bufsize = SMALLCHUNK;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001838 PY_LONG_LONG totalout;
Neal Norwitz18142c02002-11-05 18:17:32 +00001839 PyObject *ret = NULL;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001840 bz_stream *bzs = &self->bzs;
1841 int bzerror;
1842
Walter Dörwaldbb9c7392004-11-01 17:10:19 +00001843 if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize))
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001844 return NULL;
1845
1846 ACQUIRE_LOCK(self);
1847 if (!self->running) {
1848 PyErr_SetString(PyExc_EOFError, "end of stream was "
1849 "already found");
1850 goto error;
1851 }
1852
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001853 ret = PyString_FromStringAndSize(NULL, bufsize);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001854 if (!ret)
1855 goto error;
1856
1857 bzs->next_in = data;
1858 bzs->avail_in = datasize;
1859 bzs->next_out = BUF(ret);
1860 bzs->avail_out = bufsize;
1861
1862 totalout = BZS_TOTAL_OUT(bzs);
1863
1864 for (;;) {
1865 Py_BEGIN_ALLOW_THREADS
1866 bzerror = BZ2_bzDecompress(bzs);
1867 Py_END_ALLOW_THREADS
1868 if (bzerror == BZ_STREAM_END) {
1869 if (bzs->avail_in != 0) {
1870 Py_DECREF(self->unused_data);
1871 self->unused_data =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001872 PyString_FromStringAndSize(bzs->next_in,
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001873 bzs->avail_in);
1874 }
1875 self->running = 0;
1876 break;
1877 }
1878 if (bzerror != BZ_OK) {
1879 Util_CatchBZ2Error(bzerror);
1880 goto error;
1881 }
Georg Brandla47337f2007-03-13 12:34:25 +00001882 if (bzs->avail_in == 0)
1883 break; /* no more input data */
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001884 if (bzs->avail_out == 0) {
1885 bufsize = Util_NewBufferSize(bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001886 if (_PyString_Resize(&ret, bufsize) < 0) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001887 BZ2_bzDecompressEnd(bzs);
1888 goto error;
1889 }
1890 bzs->next_out = BUF(ret);
1891 bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
1892 - totalout);
1893 bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001894 }
1895 }
1896
1897 if (bzs->avail_out != 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001898 _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001899
1900 RELEASE_LOCK(self);
1901 return ret;
1902
1903error:
1904 RELEASE_LOCK(self);
1905 Py_XDECREF(ret);
1906 return NULL;
1907}
1908
1909static PyMethodDef BZ2Decomp_methods[] = {
1910 {"decompress", (PyCFunction)BZ2Decomp_decompress, METH_VARARGS, BZ2Decomp_decompress__doc__},
1911 {NULL, NULL} /* sentinel */
1912};
1913
1914
1915/* ===================================================================== */
1916/* Slot definitions for BZ2Decomp_Type. */
1917
1918static int
1919BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
1920{
1921 int bzerror;
1922
1923 if (!PyArg_ParseTuple(args, ":BZ2Decompressor"))
1924 return -1;
1925
1926#ifdef WITH_THREAD
1927 self->lock = PyThread_allocate_lock();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001928 if (!self->lock) {
1929 PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001930 goto error;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001931 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001932#endif
1933
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001934 self->unused_data = PyString_FromString("");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001935 if (!self->unused_data)
1936 goto error;
1937
1938 memset(&self->bzs, 0, sizeof(bz_stream));
1939 bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
1940 if (bzerror != BZ_OK) {
1941 Util_CatchBZ2Error(bzerror);
1942 goto error;
1943 }
1944
1945 self->running = 1;
1946
1947 return 0;
1948
1949error:
1950#ifdef WITH_THREAD
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001951 if (self->lock) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001952 PyThread_free_lock(self->lock);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001953 self->lock = NULL;
1954 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001955#endif
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001956 Py_CLEAR(self->unused_data);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001957 return -1;
1958}
1959
1960static void
1961BZ2Decomp_dealloc(BZ2DecompObject *self)
1962{
1963#ifdef WITH_THREAD
1964 if (self->lock)
1965 PyThread_free_lock(self->lock);
1966#endif
1967 Py_XDECREF(self->unused_data);
1968 BZ2_bzDecompressEnd(&self->bzs);
Christian Heimese93237d2007-12-19 02:37:44 +00001969 Py_TYPE(self)->tp_free((PyObject *)self);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001970}
1971
1972
1973/* ===================================================================== */
1974/* BZ2Decomp_Type definition. */
1975
1976PyDoc_STRVAR(BZ2Decomp__doc__,
1977"BZ2Decompressor() -> decompressor object\n\
1978\n\
1979Create a new decompressor object. This object may be used to decompress\n\
1980data sequentially. If you want to decompress data in one shot, use the\n\
1981decompress() function instead.\n\
1982");
1983
Gustavo Niemeyer49ea7be2002-11-08 14:31:49 +00001984static PyTypeObject BZ2Decomp_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001985 PyVarObject_HEAD_INIT(NULL, 0)
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001986 "bz2.BZ2Decompressor", /*tp_name*/
1987 sizeof(BZ2DecompObject), /*tp_basicsize*/
1988 0, /*tp_itemsize*/
1989 (destructor)BZ2Decomp_dealloc, /*tp_dealloc*/
1990 0, /*tp_print*/
1991 0, /*tp_getattr*/
1992 0, /*tp_setattr*/
1993 0, /*tp_compare*/
1994 0, /*tp_repr*/
1995 0, /*tp_as_number*/
1996 0, /*tp_as_sequence*/
1997 0, /*tp_as_mapping*/
1998 0, /*tp_hash*/
1999 0, /*tp_call*/
2000 0, /*tp_str*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002001 PyObject_GenericGetAttr,/*tp_getattro*/
2002 PyObject_GenericSetAttr,/*tp_setattro*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002003 0, /*tp_as_buffer*/
2004 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
2005 BZ2Decomp__doc__, /*tp_doc*/
2006 0, /*tp_traverse*/
2007 0, /*tp_clear*/
2008 0, /*tp_richcompare*/
2009 0, /*tp_weaklistoffset*/
2010 0, /*tp_iter*/
2011 0, /*tp_iternext*/
2012 BZ2Decomp_methods, /*tp_methods*/
2013 BZ2Decomp_members, /*tp_members*/
2014 0, /*tp_getset*/
2015 0, /*tp_base*/
2016 0, /*tp_dict*/
2017 0, /*tp_descr_get*/
2018 0, /*tp_descr_set*/
2019 0, /*tp_dictoffset*/
2020 (initproc)BZ2Decomp_init, /*tp_init*/
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002021 PyType_GenericAlloc, /*tp_alloc*/
2022 PyType_GenericNew, /*tp_new*/
2023 _PyObject_Del, /*tp_free*/
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002024 0, /*tp_is_gc*/
2025};
2026
2027
2028/* ===================================================================== */
2029/* Module functions. */
2030
2031PyDoc_STRVAR(bz2_compress__doc__,
2032"compress(data [, compresslevel=9]) -> string\n\
2033\n\
2034Compress data in one shot. If you want to compress data sequentially,\n\
2035use an instance of BZ2Compressor instead. The compresslevel parameter, if\n\
2036given, must be a number between 1 and 9.\n\
2037");
2038
2039static PyObject *
2040bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs)
2041{
2042 int compresslevel=9;
2043 char *data;
2044 int datasize;
2045 int bufsize;
Gustavo Niemeyer7d7930b2002-11-05 18:41:53 +00002046 PyObject *ret = NULL;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002047 bz_stream _bzs;
2048 bz_stream *bzs = &_bzs;
2049 int bzerror;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002050 static char *kwlist[] = {"data", "compresslevel", 0};
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002051
2052 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i",
2053 kwlist, &data, &datasize,
2054 &compresslevel))
2055 return NULL;
2056
2057 if (compresslevel < 1 || compresslevel > 9) {
2058 PyErr_SetString(PyExc_ValueError,
2059 "compresslevel must be between 1 and 9");
2060 return NULL;
2061 }
2062
2063 /* Conforming to bz2 manual, this is large enough to fit compressed
2064 * data in one shot. We will check it later anyway. */
2065 bufsize = datasize + (datasize/100+1) + 600;
2066
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002067 ret = PyString_FromStringAndSize(NULL, bufsize);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002068 if (!ret)
2069 return NULL;
2070
2071 memset(bzs, 0, sizeof(bz_stream));
2072
2073 bzs->next_in = data;
2074 bzs->avail_in = datasize;
2075 bzs->next_out = BUF(ret);
2076 bzs->avail_out = bufsize;
2077
2078 bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0);
2079 if (bzerror != BZ_OK) {
2080 Util_CatchBZ2Error(bzerror);
2081 Py_DECREF(ret);
2082 return NULL;
2083 }
Tim Peterse3228092002-11-09 04:21:44 +00002084
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002085 for (;;) {
2086 Py_BEGIN_ALLOW_THREADS
2087 bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
2088 Py_END_ALLOW_THREADS
2089 if (bzerror == BZ_STREAM_END) {
2090 break;
2091 } else if (bzerror != BZ_FINISH_OK) {
2092 BZ2_bzCompressEnd(bzs);
2093 Util_CatchBZ2Error(bzerror);
2094 Py_DECREF(ret);
2095 return NULL;
2096 }
2097 if (bzs->avail_out == 0) {
2098 bufsize = Util_NewBufferSize(bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002099 if (_PyString_Resize(&ret, bufsize) < 0) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002100 BZ2_bzCompressEnd(bzs);
2101 Py_DECREF(ret);
2102 return NULL;
2103 }
2104 bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
2105 bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
2106 }
2107 }
2108
2109 if (bzs->avail_out != 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002110 _PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002111 BZ2_bzCompressEnd(bzs);
2112
2113 return ret;
2114}
2115
2116PyDoc_STRVAR(bz2_decompress__doc__,
2117"decompress(data) -> decompressed data\n\
2118\n\
2119Decompress data in one shot. If you want to decompress data sequentially,\n\
2120use an instance of BZ2Decompressor instead.\n\
2121");
2122
2123static PyObject *
2124bz2_decompress(PyObject *self, PyObject *args)
2125{
2126 char *data;
2127 int datasize;
2128 int bufsize = SMALLCHUNK;
2129 PyObject *ret;
2130 bz_stream _bzs;
2131 bz_stream *bzs = &_bzs;
2132 int bzerror;
2133
Walter Dörwaldbb9c7392004-11-01 17:10:19 +00002134 if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize))
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002135 return NULL;
2136
2137 if (datasize == 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002138 return PyString_FromString("");
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002139
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002140 ret = PyString_FromStringAndSize(NULL, bufsize);
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002141 if (!ret)
2142 return NULL;
2143
2144 memset(bzs, 0, sizeof(bz_stream));
2145
2146 bzs->next_in = data;
2147 bzs->avail_in = datasize;
2148 bzs->next_out = BUF(ret);
2149 bzs->avail_out = bufsize;
2150
2151 bzerror = BZ2_bzDecompressInit(bzs, 0, 0);
2152 if (bzerror != BZ_OK) {
2153 Util_CatchBZ2Error(bzerror);
2154 Py_DECREF(ret);
2155 return NULL;
2156 }
Tim Peterse3228092002-11-09 04:21:44 +00002157
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002158 for (;;) {
2159 Py_BEGIN_ALLOW_THREADS
2160 bzerror = BZ2_bzDecompress(bzs);
2161 Py_END_ALLOW_THREADS
2162 if (bzerror == BZ_STREAM_END) {
2163 break;
2164 } else if (bzerror != BZ_OK) {
2165 BZ2_bzDecompressEnd(bzs);
2166 Util_CatchBZ2Error(bzerror);
2167 Py_DECREF(ret);
2168 return NULL;
2169 }
Georg Brandla47337f2007-03-13 12:34:25 +00002170 if (bzs->avail_in == 0) {
2171 BZ2_bzDecompressEnd(bzs);
2172 PyErr_SetString(PyExc_ValueError,
2173 "couldn't find end of stream");
2174 Py_DECREF(ret);
2175 return NULL;
2176 }
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002177 if (bzs->avail_out == 0) {
2178 bufsize = Util_NewBufferSize(bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002179 if (_PyString_Resize(&ret, bufsize) < 0) {
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002180 BZ2_bzDecompressEnd(bzs);
2181 Py_DECREF(ret);
2182 return NULL;
2183 }
2184 bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
2185 bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002186 }
2187 }
2188
2189 if (bzs->avail_out != 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002190 _PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002191 BZ2_bzDecompressEnd(bzs);
2192
2193 return ret;
2194}
2195
2196static PyMethodDef bz2_methods[] = {
2197 {"compress", (PyCFunction) bz2_compress, METH_VARARGS|METH_KEYWORDS,
2198 bz2_compress__doc__},
2199 {"decompress", (PyCFunction) bz2_decompress, METH_VARARGS,
2200 bz2_decompress__doc__},
2201 {NULL, NULL} /* sentinel */
2202};
2203
2204/* ===================================================================== */
2205/* Initialization function. */
2206
2207PyDoc_STRVAR(bz2__doc__,
2208"The python bz2 module provides a comprehensive interface for\n\
2209the bz2 compression library. It implements a complete file\n\
2210interface, one shot (de)compression functions, and types for\n\
2211sequential (de)compression.\n\
2212");
2213
Neal Norwitz21d896c2003-07-01 20:15:21 +00002214PyMODINIT_FUNC
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002215initbz2(void)
2216{
2217 PyObject *m;
2218
Christian Heimese93237d2007-12-19 02:37:44 +00002219 Py_TYPE(&BZ2File_Type) = &PyType_Type;
2220 Py_TYPE(&BZ2Comp_Type) = &PyType_Type;
2221 Py_TYPE(&BZ2Decomp_Type) = &PyType_Type;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002222
2223 m = Py_InitModule3("bz2", bz2_methods, bz2__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002224 if (m == NULL)
2225 return;
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002226
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002227 PyModule_AddObject(m, "__author__", PyString_FromString(__author__));
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00002228
2229 Py_INCREF(&BZ2File_Type);
2230 PyModule_AddObject(m, "BZ2File", (PyObject *)&BZ2File_Type);
2231
2232 Py_INCREF(&BZ2Comp_Type);
2233 PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Comp_Type);
2234
2235 Py_INCREF(&BZ2Decomp_Type);
2236 PyModule_AddObject(m, "BZ2Decompressor", (PyObject *)&BZ2Decomp_Type);
2237}