[pypy-svn] r30999 - in pypy/dist/pypy/module/bz2: . test
rhymes at codespeak.net
rhymes at codespeak.net
Fri Aug 4 16:10:48 CEST 2006
Author: rhymes
Date: Fri Aug 4 16:10:40 2006
New Revision: 30999
Added:
pypy/dist/pypy/module/bz2/
pypy/dist/pypy/module/bz2/__init__.py
pypy/dist/pypy/module/bz2/app_bz2.py
pypy/dist/pypy/module/bz2/bz2module.c
pypy/dist/pypy/module/bz2/bzlib.py
pypy/dist/pypy/module/bz2/fileobject.py
pypy/dist/pypy/module/bz2/interp_bz2.py
pypy/dist/pypy/module/bz2/test/
pypy/dist/pypy/module/bz2/test/test_bz2.py
Log:
bz2 module initial import. Creation of BZ2File object works.
Added: pypy/dist/pypy/module/bz2/__init__.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/__init__.py Fri Aug 4 16:10:40 2006
@@ -0,0 +1,10 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+ interpleveldefs = {
+ 'BZ2File': 'interp_bz2.BZ2File',
+ }
+
+ appleveldefs = {
+ '__doc__': 'app_bz2.__doc__'
+ }
Added: pypy/dist/pypy/module/bz2/app_bz2.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/app_bz2.py Fri Aug 4 16:10:40 2006
@@ -0,0 +1,4 @@
+__doc__ = """The python bz2 module provides a comprehensive interface for
+the bz2 compression library. It implements a complete file
+interface, one shot (de)compression functions, and types for
+sequential (de)compression."""
Added: pypy/dist/pypy/module/bz2/bz2module.c
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/bz2module.c Fri Aug 4 16:10:40 2006
@@ -0,0 +1,2198 @@
+/*
+
+python-bz2 - python bz2 library interface
+
+Copyright (c) 2002 Gustavo Niemeyer <niemeyer at conectiva.com>
+Copyright (c) 2002 Python Software Foundation; All Rights Reserved
+
+*/
+
+#include "Python.h"
+#include <stdio.h>
+#include <bzlib.h>
+#include "structmember.h"
+
+#ifdef WITH_THREAD
+#include "pythread.h"
+#endif
+
+static char __author__[] =
+"The bz2 python module was written by:\n\
+\n\
+ Gustavo Niemeyer <niemeyer at conectiva.com>\n\
+";
+
+/* Our very own off_t-like type, 64-bit if possible */
+/* copied from Objects/fileobject.c */
+#if !defined(HAVE_LARGEFILE_SUPPORT)
+typedef off_t Py_off_t;
+#elif SIZEOF_OFF_T >= 8
+typedef off_t Py_off_t;
+#elif SIZEOF_FPOS_T >= 8
+typedef fpos_t Py_off_t;
+#else
+#error "Large file support, but neither off_t nor fpos_t is large enough."
+#endif
+
+#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
+
+#define MODE_CLOSED 0
+#define MODE_READ 1
+#define MODE_READ_EOF 2
+#define MODE_WRITE 3
+
+#define BZ2FileObject_Check(v) ((v)->ob_type == &BZ2File_Type)
+
+
+#ifdef BZ_CONFIG_ERROR
+
+#if SIZEOF_LONG >= 8
+#define BZS_TOTAL_OUT(bzs) \
+ (((long)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
+#elif SIZEOF_LONG_LONG >= 8
+#define BZS_TOTAL_OUT(bzs) \
+ (((PY_LONG_LONG)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
+#else
+#define BZS_TOTAL_OUT(bzs) \
+ bzs->total_out_lo32
+#endif
+
+#else /* ! BZ_CONFIG_ERROR */
+
+#define BZ2_bzRead bzRead
+#define BZ2_bzReadOpen bzReadOpen
+#define BZ2_bzReadClose bzReadClose
+#define BZ2_bzWrite bzWrite
+#define BZ2_bzWriteOpen bzWriteOpen
+#define BZ2_bzWriteClose bzWriteClose
+#define BZ2_bzCompress bzCompress
+#define BZ2_bzCompressInit bzCompressInit
+#define BZ2_bzCompressEnd bzCompressEnd
+#define BZ2_bzDecompress bzDecompress
+#define BZ2_bzDecompressInit bzDecompressInit
+#define BZ2_bzDecompressEnd bzDecompressEnd
+
+#define BZS_TOTAL_OUT(bzs) bzs->total_out
+
+#endif /* ! BZ_CONFIG_ERROR */
+
+
+#ifdef WITH_THREAD
+#define ACQUIRE_LOCK(obj) PyThread_acquire_lock(obj->lock, 1)
+#define RELEASE_LOCK(obj) PyThread_release_lock(obj->lock)
+#else
+#define ACQUIRE_LOCK(obj)
+#define RELEASE_LOCK(obj)
+#endif
+
+/* Bits in f_newlinetypes */
+#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
+#define NEWLINE_CR 1 /* \r newline seen */
+#define NEWLINE_LF 2 /* \n newline seen */
+#define NEWLINE_CRLF 4 /* \r\n newline seen */
+
+/* ===================================================================== */
+/* Structure definitions. */
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *file;
+
+ char* f_buf; /* Allocated readahead buffer */
+ char* f_bufend; /* Points after last occupied position */
+ char* f_bufptr; /* Current buffer position */
+
+ int f_softspace; /* Flag used by 'print' command */
+
+ int f_univ_newline; /* Handle any newline convention */
+ int f_newlinetypes; /* Types of newlines seen */
+ int f_skipnextlf; /* Skip next \n */
+
+ BZFILE *fp;
+ int mode;
+ Py_off_t pos;
+ Py_off_t size;
+#ifdef WITH_THREAD
+ PyThread_type_lock lock;
+#endif
+} BZ2FileObject;
+
+typedef struct {
+ PyObject_HEAD
+ bz_stream bzs;
+ int running;
+#ifdef WITH_THREAD
+ PyThread_type_lock lock;
+#endif
+} BZ2CompObject;
+
+typedef struct {
+ PyObject_HEAD
+ bz_stream bzs;
+ int running;
+ PyObject *unused_data;
+#ifdef WITH_THREAD
+ PyThread_type_lock lock;
+#endif
+} BZ2DecompObject;
+
+/* ===================================================================== */
+/* Utility functions. */
+
+static int
+Util_CatchBZ2Error(int bzerror)
+{
+ int ret = 0;
+ switch(bzerror) {
+ case BZ_OK:
+ case BZ_STREAM_END:
+ break;
+
+#ifdef BZ_CONFIG_ERROR
+ case BZ_CONFIG_ERROR:
+ PyErr_SetString(PyExc_SystemError,
+ "the bz2 library was not compiled "
+ "correctly");
+ ret = 1;
+ break;
+#endif
+
+ case BZ_PARAM_ERROR:
+ PyErr_SetString(PyExc_ValueError,
+ "the bz2 library has received wrong "
+ "parameters");
+ ret = 1;
+ break;
+
+ case BZ_MEM_ERROR:
+ PyErr_NoMemory();
+ ret = 1;
+ break;
+
+ case BZ_DATA_ERROR:
+ case BZ_DATA_ERROR_MAGIC:
+ PyErr_SetString(PyExc_IOError, "invalid data stream");
+ ret = 1;
+ break;
+
+ case BZ_IO_ERROR:
+ PyErr_SetString(PyExc_IOError, "unknown IO error");
+ ret = 1;
+ break;
+
+ case BZ_UNEXPECTED_EOF:
+ PyErr_SetString(PyExc_EOFError,
+ "compressed file ended before the "
+ "logical end-of-stream was detected");
+ ret = 1;
+ break;
+
+ case BZ_SEQUENCE_ERROR:
+ PyErr_SetString(PyExc_RuntimeError,
+ "wrong sequence of bz2 library "
+ "commands used");
+ ret = 1;
+ break;
+ }
+ return ret;
+}
+
+#if BUFSIZ < 8192
+#define SMALLCHUNK 8192
+#else
+#define SMALLCHUNK BUFSIZ
+#endif
+
+#if SIZEOF_INT < 4
+#define BIGCHUNK (512 * 32)
+#else
+#define BIGCHUNK (512 * 1024)
+#endif
+
+/* This is a hacked version of Python's fileobject.c:new_buffersize(). */
+static size_t
+Util_NewBufferSize(size_t currentsize)
+{
+ if (currentsize > SMALLCHUNK) {
+ /* Keep doubling until we reach BIGCHUNK;
+ then keep adding BIGCHUNK. */
+ if (currentsize <= BIGCHUNK)
+ return currentsize + currentsize;
+ else
+ return currentsize + BIGCHUNK;
+ }
+ return currentsize + SMALLCHUNK;
+}
+
+/* This is a hacked version of Python's fileobject.c:get_line(). */
+static PyObject *
+Util_GetLine(BZ2FileObject *f, int n)
+{
+ char c;
+ char *buf, *end;
+ size_t total_v_size; /* total # of slots in buffer */
+ size_t used_v_size; /* # used slots in buffer */
+ size_t increment; /* amount to increment the buffer */
+ PyObject *v;
+ int bzerror;
+ int newlinetypes = f->f_newlinetypes;
+ int skipnextlf = f->f_skipnextlf;
+ int univ_newline = f->f_univ_newline;
+
+ total_v_size = n > 0 ? n : 100;
+ v = PyString_FromStringAndSize((char *)NULL, total_v_size);
+ if (v == NULL)
+ return NULL;
+
+ buf = BUF(v);
+ end = buf + total_v_size;
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ if (univ_newline) {
+ while (1) {
+ BZ2_bzRead(&bzerror, f->fp, &c, 1);
+ f->pos++;
+ if (bzerror != BZ_OK || buf == end)
+ break;
+ if (skipnextlf) {
+ skipnextlf = 0;
+ if (c == '\n') {
+ /* Seeing a \n here with
+ * skipnextlf true means we
+ * saw a \r before.
+ */
+ newlinetypes |= NEWLINE_CRLF;
+ BZ2_bzRead(&bzerror, f->fp,
+ &c, 1);
+ if (bzerror != BZ_OK)
+ break;
+ } else {
+ newlinetypes |= NEWLINE_CR;
+ }
+ }
+ if (c == '\r') {
+ skipnextlf = 1;
+ c = '\n';
+ } else if ( c == '\n')
+ newlinetypes |= NEWLINE_LF;
+ *buf++ = c;
+ if (c == '\n') break;
+ }
+ if (bzerror == BZ_STREAM_END && skipnextlf)
+ newlinetypes |= NEWLINE_CR;
+ } else /* If not universal newlines use the normal loop */
+ do {
+ BZ2_bzRead(&bzerror, f->fp, &c, 1);
+ f->pos++;
+ *buf++ = c;
+ } while (bzerror == BZ_OK && c != '\n' && buf != end);
+ Py_END_ALLOW_THREADS
+ f->f_newlinetypes = newlinetypes;
+ f->f_skipnextlf = skipnextlf;
+ if (bzerror == BZ_STREAM_END) {
+ f->size = f->pos;
+ f->mode = MODE_READ_EOF;
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(v);
+ return NULL;
+ }
+ if (c == '\n')
+ break;
+ /* Must be because buf == end */
+ if (n > 0)
+ break;
+ used_v_size = total_v_size;
+ increment = total_v_size >> 2; /* mild exponential growth */
+ total_v_size += increment;
+ if (total_v_size > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "line is longer than a Python string can hold");
+ Py_DECREF(v);
+ return NULL;
+ }
+ if (_PyString_Resize(&v, total_v_size) < 0)
+ return NULL;
+ buf = BUF(v) + used_v_size;
+ end = BUF(v) + total_v_size;
+ }
+
+ used_v_size = buf - BUF(v);
+ if (used_v_size != total_v_size)
+ _PyString_Resize(&v, used_v_size);
+ return v;
+}
+
+/* This is a hacked version of Python's
+ * fileobject.c:Py_UniversalNewlineFread(). */
+size_t
+Util_UnivNewlineRead(int *bzerror, BZFILE *stream,
+ char* buf, size_t n, BZ2FileObject *f)
+{
+ char *dst = buf;
+ int newlinetypes, skipnextlf;
+
+ assert(buf != NULL);
+ assert(stream != NULL);
+
+ if (!f->f_univ_newline)
+ return BZ2_bzRead(bzerror, stream, buf, n);
+
+ newlinetypes = f->f_newlinetypes;
+ skipnextlf = f->f_skipnextlf;
+
+ /* Invariant: n is the number of bytes remaining to be filled
+ * in the buffer.
+ */
+ while (n) {
+ size_t nread;
+ int shortread;
+ char *src = dst;
+
+ nread = BZ2_bzRead(bzerror, stream, dst, n);
+ assert(nread <= n);
+ n -= nread; /* assuming 1 byte out for each in; will adjust */
+ shortread = n != 0; /* true iff EOF or error */
+ while (nread--) {
+ char c = *src++;
+ if (c == '\r') {
+ /* Save as LF and set flag to skip next LF. */
+ *dst++ = '\n';
+ skipnextlf = 1;
+ }
+ else if (skipnextlf && c == '\n') {
+ /* Skip LF, and remember we saw CR LF. */
+ skipnextlf = 0;
+ newlinetypes |= NEWLINE_CRLF;
+ ++n;
+ }
+ else {
+ /* Normal char to be stored in buffer. Also
+ * update the newlinetypes flag if either this
+ * is an LF or the previous char was a CR.
+ */
+ if (c == '\n')
+ newlinetypes |= NEWLINE_LF;
+ else if (skipnextlf)
+ newlinetypes |= NEWLINE_CR;
+ *dst++ = c;
+ skipnextlf = 0;
+ }
+ }
+ if (shortread) {
+ /* If this is EOF, update type flags. */
+ if (skipnextlf && *bzerror == BZ_STREAM_END)
+ newlinetypes |= NEWLINE_CR;
+ break;
+ }
+ }
+ f->f_newlinetypes = newlinetypes;
+ f->f_skipnextlf = skipnextlf;
+ return dst - buf;
+}
+
+/* This is a hacked version of Python's fileobject.c:drop_readahead(). */
+static void
+Util_DropReadAhead(BZ2FileObject *f)
+{
+ if (f->f_buf != NULL) {
+ PyMem_Free(f->f_buf);
+ f->f_buf = NULL;
+ }
+}
+
+/* This is a hacked version of Python's fileobject.c:readahead(). */
+static int
+Util_ReadAhead(BZ2FileObject *f, int bufsize)
+{
+ int chunksize;
+ int bzerror;
+
+ if (f->f_buf != NULL) {
+ if((f->f_bufend - f->f_bufptr) >= 1)
+ return 0;
+ else
+ Util_DropReadAhead(f);
+ }
+ if (f->mode == MODE_READ_EOF) {
+ f->f_bufptr = f->f_buf;
+ f->f_bufend = f->f_buf;
+ return 0;
+ }
+ if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
+ return -1;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = Util_UnivNewlineRead(&bzerror, f->fp, f->f_buf,
+ bufsize, f);
+ Py_END_ALLOW_THREADS
+ f->pos += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ f->size = f->pos;
+ f->mode = MODE_READ_EOF;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Util_DropReadAhead(f);
+ return -1;
+ }
+ f->f_bufptr = f->f_buf;
+ f->f_bufend = f->f_buf + chunksize;
+ return 0;
+}
+
+/* This is a hacked version of Python's
+ * fileobject.c:readahead_get_line_skip(). */
+static PyStringObject *
+Util_ReadAheadGetLineSkip(BZ2FileObject *f, int skip, int bufsize)
+{
+ PyStringObject* s;
+ char *bufptr;
+ char *buf;
+ int len;
+
+ if (f->f_buf == NULL)
+ if (Util_ReadAhead(f, bufsize) < 0)
+ return NULL;
+
+ len = f->f_bufend - f->f_bufptr;
+ if (len == 0)
+ return (PyStringObject *)
+ PyString_FromStringAndSize(NULL, skip);
+ bufptr = memchr(f->f_bufptr, '\n', len);
+ if (bufptr != NULL) {
+ bufptr++; /* Count the '\n' */
+ len = bufptr - f->f_bufptr;
+ s = (PyStringObject *)
+ PyString_FromStringAndSize(NULL, skip+len);
+ if (s == NULL)
+ return NULL;
+ memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
+ f->f_bufptr = bufptr;
+ if (bufptr == f->f_bufend)
+ Util_DropReadAhead(f);
+ } else {
+ bufptr = f->f_bufptr;
+ buf = f->f_buf;
+ f->f_buf = NULL; /* Force new readahead buffer */
+ s = Util_ReadAheadGetLineSkip(f, skip+len,
+ bufsize + (bufsize>>2));
+ if (s == NULL) {
+ PyMem_Free(buf);
+ return NULL;
+ }
+ memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
+ PyMem_Free(buf);
+ }
+ return s;
+}
+
+/* ===================================================================== */
+/* Methods of BZ2File. */
+
+PyDoc_STRVAR(BZ2File_read__doc__,
+"read([size]) -> string\n\
+\n\
+Read at most size uncompressed bytes, returned as a string. If the size\n\
+argument is negative or omitted, read until EOF is reached.\n\
+");
+
+/* This is a hacked version of Python's fileobject.c:file_read(). */
+static PyObject *
+BZ2File_read(BZ2FileObject *self, PyObject *args)
+{
+ long bytesrequested = -1;
+ size_t bytesread, buffersize, chunksize;
+ int bzerror;
+ PyObject *ret = NULL;
+
+ if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ break;
+ case MODE_READ_EOF:
+ ret = PyString_FromString("");
+ goto cleanup;
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for reading");
+ goto cleanup;
+ }
+
+ if (bytesrequested < 0)
+ buffersize = Util_NewBufferSize((size_t)0);
+ else
+ buffersize = bytesrequested;
+ if (buffersize > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "requested number of bytes is "
+ "more than a Python string can hold");
+ goto cleanup;
+ }
+ ret = PyString_FromStringAndSize((char *)NULL, buffersize);
+ if (ret == NULL)
+ goto cleanup;
+ bytesread = 0;
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = Util_UnivNewlineRead(&bzerror, self->fp,
+ BUF(ret)+bytesread,
+ buffersize-bytesread,
+ self);
+ self->pos += chunksize;
+ Py_END_ALLOW_THREADS
+ bytesread += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ self->size = self->pos;
+ self->mode = MODE_READ_EOF;
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ ret = NULL;
+ goto cleanup;
+ }
+ if (bytesrequested < 0) {
+ buffersize = Util_NewBufferSize(buffersize);
+ if (_PyString_Resize(&ret, buffersize) < 0)
+ goto cleanup;
+ } else {
+ break;
+ }
+ }
+ if (bytesread != buffersize)
+ _PyString_Resize(&ret, bytesread);
+
+cleanup:
+ RELEASE_LOCK(self);
+ return ret;
+}
+
+PyDoc_STRVAR(BZ2File_readline__doc__,
+"readline([size]) -> string\n\
+\n\
+Return the next line from the file, as a string, retaining newline.\n\
+A non-negative size argument will limit the maximum number of bytes to\n\
+return (an incomplete line may be returned then). Return an empty\n\
+string at EOF.\n\
+");
+
+static PyObject *
+BZ2File_readline(BZ2FileObject *self, PyObject *args)
+{
+ PyObject *ret = NULL;
+ int sizehint = -1;
+
+ if (!PyArg_ParseTuple(args, "|i:readline", &sizehint))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ break;
+ case MODE_READ_EOF:
+ ret = PyString_FromString("");
+ goto cleanup;
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for reading");
+ goto cleanup;
+ }
+
+ if (sizehint == 0)
+ ret = PyString_FromString("");
+ else
+ ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint);
+
+cleanup:
+ RELEASE_LOCK(self);
+ return ret;
+}
+
+PyDoc_STRVAR(BZ2File_readlines__doc__,
+"readlines([size]) -> list\n\
+\n\
+Call readline() repeatedly and return a list of lines read.\n\
+The optional size argument, if given, is an approximate bound on the\n\
+total number of bytes in the lines returned.\n\
+");
+
+/* This is a hacked version of Python's fileobject.c:file_readlines(). */
+static PyObject *
+BZ2File_readlines(BZ2FileObject *self, PyObject *args)
+{
+ long sizehint = 0;
+ PyObject *list = NULL;
+ PyObject *line;
+ char small_buffer[SMALLCHUNK];
+ char *buffer = small_buffer;
+ size_t buffersize = SMALLCHUNK;
+ PyObject *big_buffer = NULL;
+ size_t nfilled = 0;
+ size_t nread;
+ size_t totalread = 0;
+ char *p, *q, *end;
+ int err;
+ int shortread = 0;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ break;
+ case MODE_READ_EOF:
+ list = PyList_New(0);
+ goto cleanup;
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for reading");
+ goto cleanup;
+ }
+
+ if ((list = PyList_New(0)) == NULL)
+ goto cleanup;
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ nread = Util_UnivNewlineRead(&bzerror, self->fp,
+ buffer+nfilled,
+ buffersize-nfilled, self);
+ self->pos += nread;
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ self->size = self->pos;
+ self->mode = MODE_READ_EOF;
+ if (nread == 0) {
+ sizehint = 0;
+ break;
+ }
+ shortread = 1;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ error:
+ Py_DECREF(list);
+ list = NULL;
+ goto cleanup;
+ }
+ totalread += nread;
+ p = memchr(buffer+nfilled, '\n', nread);
+ if (!shortread && p == NULL) {
+ /* Need a larger buffer to fit this line */
+ nfilled += nread;
+ buffersize *= 2;
+ if (buffersize > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "line is longer than a Python string can hold");
+ goto error;
+ }
+ if (big_buffer == NULL) {
+ /* Create the big buffer */
+ big_buffer = PyString_FromStringAndSize(
+ NULL, buffersize);
+ if (big_buffer == NULL)
+ goto error;
+ buffer = PyString_AS_STRING(big_buffer);
+ memcpy(buffer, small_buffer, nfilled);
+ }
+ else {
+ /* Grow the big buffer */
+ _PyString_Resize(&big_buffer, buffersize);
+ buffer = PyString_AS_STRING(big_buffer);
+ }
+ continue;
+ }
+ end = buffer+nfilled+nread;
+ q = buffer;
+ while (p != NULL) {
+ /* Process complete lines */
+ p++;
+ line = PyString_FromStringAndSize(q, p-q);
+ if (line == NULL)
+ goto error;
+ err = PyList_Append(list, line);
+ Py_DECREF(line);
+ if (err != 0)
+ goto error;
+ q = p;
+ p = memchr(q, '\n', end-q);
+ }
+ /* Move the remaining incomplete line to the start */
+ nfilled = end-q;
+ memmove(buffer, q, nfilled);
+ if (sizehint > 0)
+ if (totalread >= (size_t)sizehint)
+ break;
+ if (shortread) {
+ sizehint = 0;
+ break;
+ }
+ }
+ if (nfilled != 0) {
+ /* Partial last line */
+ line = PyString_FromStringAndSize(buffer, nfilled);
+ if (line == NULL)
+ goto error;
+ if (sizehint > 0) {
+ /* Need to complete the last line */
+ PyObject *rest = Util_GetLine(self, 0);
+ if (rest == NULL) {
+ Py_DECREF(line);
+ goto error;
+ }
+ PyString_Concat(&line, rest);
+ Py_DECREF(rest);
+ if (line == NULL)
+ goto error;
+ }
+ err = PyList_Append(list, line);
+ Py_DECREF(line);
+ if (err != 0)
+ goto error;
+ }
+
+ cleanup:
+ RELEASE_LOCK(self);
+ if (big_buffer) {
+ Py_DECREF(big_buffer);
+ }
+ return list;
+}
+
+PyDoc_STRVAR(BZ2File_xreadlines__doc__,
+"xreadlines() -> self\n\
+\n\
+For backward compatibility. BZ2File objects now include the performance\n\
+optimizations previously implemented in the xreadlines module.\n\
+");
+
+PyDoc_STRVAR(BZ2File_write__doc__,
+"write(data) -> None\n\
+\n\
+Write the 'data' string to file. Note that due to buffering, close() may\n\
+be needed before the file on disk reflects the data written.\n\
+");
+
+/* This is a hacked version of Python's fileobject.c:file_write(). */
+static PyObject *
+BZ2File_write(BZ2FileObject *self, PyObject *args)
+{
+ PyObject *ret = NULL;
+ char *buf;
+ int len;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "s#:write", &buf, &len))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_WRITE:
+ break;
+
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;;
+
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for writing");
+ goto cleanup;;
+ }
+
+ self->f_softspace = 0;
+
+ Py_BEGIN_ALLOW_THREADS
+ BZ2_bzWrite (&bzerror, self->fp, buf, len);
+ self->pos += len;
+ Py_END_ALLOW_THREADS
+
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+
+ Py_INCREF(Py_None);
+ ret = Py_None;
+
+cleanup:
+ RELEASE_LOCK(self);
+ return ret;
+}
+
+PyDoc_STRVAR(BZ2File_writelines__doc__,
+"writelines(sequence_of_strings) -> None\n\
+\n\
+Write the sequence of strings to the file. Note that newlines are not\n\
+added. The sequence can be any iterable object producing strings. This is\n\
+equivalent to calling write() for each string.\n\
+");
+
+/* This is a hacked version of Python's fileobject.c:file_writelines(). */
+static PyObject *
+BZ2File_writelines(BZ2FileObject *self, PyObject *seq)
+{
+#define CHUNKSIZE 1000
+ PyObject *list = NULL;
+ PyObject *iter = NULL;
+ PyObject *ret = NULL;
+ PyObject *line;
+ int i, j, index, len, islist;
+ int bzerror;
+
+ ACQUIRE_LOCK(self);
+ islist = PyList_Check(seq);
+ if (!islist) {
+ iter = PyObject_GetIter(seq);
+ if (iter == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "writelines() requires an iterable argument");
+ goto error;
+ }
+ list = PyList_New(CHUNKSIZE);
+ if (list == NULL)
+ goto error;
+ }
+
+ /* Strategy: slurp CHUNKSIZE lines into a private list,
+ checking that they are all strings, then write that list
+ without holding the interpreter lock, then come back for more. */
+ for (index = 0; ; index += CHUNKSIZE) {
+ if (islist) {
+ Py_XDECREF(list);
+ list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
+ if (list == NULL)
+ goto error;
+ j = PyList_GET_SIZE(list);
+ }
+ else {
+ for (j = 0; j < CHUNKSIZE; j++) {
+ line = PyIter_Next(iter);
+ if (line == NULL) {
+ if (PyErr_Occurred())
+ goto error;
+ break;
+ }
+ PyList_SetItem(list, j, line);
+ }
+ }
+ if (j == 0)
+ break;
+
+ /* Check that all entries are indeed strings. If not,
+ apply the same rules as for file.write() and
+ convert the rets to strings. This is slow, but
+ seems to be the only way since all conversion APIs
+ could potentially execute Python code. */
+ for (i = 0; i < j; i++) {
+ PyObject *v = PyList_GET_ITEM(list, i);
+ if (!PyString_Check(v)) {
+ const char *buffer;
+ Py_ssize_t len;
+ if (PyObject_AsCharBuffer(v, &buffer, &len)) {
+ PyErr_SetString(PyExc_TypeError,
+ "writelines() "
+ "argument must be "
+ "a sequence of "
+ "strings");
+ goto error;
+ }
+ line = PyString_FromStringAndSize(buffer,
+ len);
+ if (line == NULL)
+ goto error;
+ Py_DECREF(v);
+ PyList_SET_ITEM(list, i, line);
+ }
+ }
+
+ self->f_softspace = 0;
+
+ /* Since we are releasing the global lock, the
+ following code may *not* execute Python code. */
+ Py_BEGIN_ALLOW_THREADS
+ for (i = 0; i < j; i++) {
+ line = PyList_GET_ITEM(list, i);
+ len = PyString_GET_SIZE(line);
+ BZ2_bzWrite (&bzerror, self->fp,
+ PyString_AS_STRING(line), len);
+ if (bzerror != BZ_OK) {
+ Py_BLOCK_THREADS
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ }
+ Py_END_ALLOW_THREADS
+
+ if (j < CHUNKSIZE)
+ break;
+ }
+
+ Py_INCREF(Py_None);
+ ret = Py_None;
+
+ error:
+ RELEASE_LOCK(self);
+ Py_XDECREF(list);
+ Py_XDECREF(iter);
+ return ret;
+#undef CHUNKSIZE
+}
+
+PyDoc_STRVAR(BZ2File_seek__doc__,
+"seek(offset [, whence]) -> None\n\
+\n\
+Move to new file position. Argument offset is a byte count. Optional\n\
+argument whence defaults to 0 (offset from start of file, offset\n\
+should be >= 0); other values are 1 (move relative to current position,\n\
+positive or negative), and 2 (move relative to end of file, usually\n\
+negative, although many platforms allow seeking beyond the end of a file).\n\
+\n\
+Note that seeking of bz2 files is emulated, and depending on the parameters\n\
+the operation may be extremely slow.\n\
+");
+
+static PyObject *
+BZ2File_seek(BZ2FileObject *self, PyObject *args)
+{
+ int where = 0;
+ PyObject *offobj;
+ Py_off_t offset;
+ char small_buffer[SMALLCHUNK];
+ char *buffer = small_buffer;
+ size_t buffersize = SMALLCHUNK;
+ int bytesread = 0;
+ size_t readsize;
+ int chunksize;
+ int bzerror;
+ PyObject *ret = NULL;
+
+ if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &where))
+ return NULL;
+#if !defined(HAVE_LARGEFILE_SUPPORT)
+ offset = PyInt_AsLong(offobj);
+#else
+ offset = PyLong_Check(offobj) ?
+ PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
+#endif
+ if (PyErr_Occurred())
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ Util_DropReadAhead(self);
+ switch (self->mode) {
+ case MODE_READ:
+ case MODE_READ_EOF:
+ break;
+
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;;
+
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "seek works only while reading");
+ goto cleanup;;
+ }
+
+ if (where == 2) {
+ if (self->size == -1) {
+ assert(self->mode != MODE_READ_EOF);
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = Util_UnivNewlineRead(
+ &bzerror, self->fp,
+ buffer, buffersize,
+ self);
+ self->pos += chunksize;
+ Py_END_ALLOW_THREADS
+
+ bytesread += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ }
+ self->mode = MODE_READ_EOF;
+ self->size = self->pos;
+ bytesread = 0;
+ }
+ offset = self->size + offset;
+ } else if (where == 1) {
+ offset = self->pos + offset;
+ }
+
+ /* Before getting here, offset must be the absolute position the file
+ * pointer should be set to. */
+
+ if (offset >= self->pos) {
+ /* we can move forward */
+ offset -= self->pos;
+ } else {
+ /* we cannot move back, so rewind the stream */
+ BZ2_bzReadClose(&bzerror, self->fp);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ ret = PyObject_CallMethod(self->file, "seek", "(i)", 0);
+ if (!ret)
+ goto cleanup;
+ Py_DECREF(ret);
+ ret = NULL;
+ self->pos = 0;
+ self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file),
+ 0, 0, NULL, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ self->mode = MODE_READ;
+ }
+
+ if (offset <= 0 || self->mode == MODE_READ_EOF)
+ goto exit;
+
+ /* Before getting here, offset must be set to the number of bytes
+ * to walk forward. */
+ for (;;) {
+ if (offset-bytesread > buffersize)
+ readsize = buffersize;
+ else
+ /* offset might be wider that readsize, but the result
+ * of the subtraction is bound by buffersize (see the
+ * condition above). buffersize is 8192. */
+ readsize = (size_t)(offset-bytesread);
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = Util_UnivNewlineRead(&bzerror, self->fp,
+ buffer, readsize, self);
+ self->pos += chunksize;
+ Py_END_ALLOW_THREADS
+ bytesread += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ self->size = self->pos;
+ self->mode = MODE_READ_EOF;
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ if (bytesread == offset)
+ break;
+ }
+
+exit:
+ Py_INCREF(Py_None);
+ ret = Py_None;
+
+cleanup:
+ RELEASE_LOCK(self);
+ return ret;
+}
+
+PyDoc_STRVAR(BZ2File_tell__doc__,
+"tell() -> int\n\
+\n\
+Return the current file position, an integer (may be a long integer).\n\
+");
+
+static PyObject *
+BZ2File_tell(BZ2FileObject *self, PyObject *args)
+{
+ PyObject *ret = NULL;
+
+ if (self->mode == MODE_CLOSED) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ }
+
+#if !defined(HAVE_LARGEFILE_SUPPORT)
+ ret = PyInt_FromLong(self->pos);
+#else
+ ret = PyLong_FromLongLong(self->pos);
+#endif
+
+cleanup:
+ return ret;
+}
+
+PyDoc_STRVAR(BZ2File_close__doc__,
+"close() -> None or (perhaps) an integer\n\
+\n\
+Close the file. Sets data attribute .closed to true. A closed file\n\
+cannot be used for further I/O operations. close() may be called more\n\
+than once without error.\n\
+");
+
+static PyObject *
+BZ2File_close(BZ2FileObject *self)
+{
+ PyObject *ret = NULL;
+ int bzerror = BZ_OK;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ case MODE_READ_EOF:
+ BZ2_bzReadClose(&bzerror, self->fp);
+ break;
+ case MODE_WRITE:
+ BZ2_bzWriteClose(&bzerror, self->fp,
+ 0, NULL, NULL);
+ break;
+ }
+ self->mode = MODE_CLOSED;
+ ret = PyObject_CallMethod(self->file, "close", NULL);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_XDECREF(ret);
+ ret = NULL;
+ }
+
+ RELEASE_LOCK(self);
+ return ret;
+}
+
+static PyObject *BZ2File_getiter(BZ2FileObject *self);
+
+static PyMethodDef BZ2File_methods[] = {
+ {"read", (PyCFunction)BZ2File_read, METH_VARARGS, BZ2File_read__doc__},
+ {"readline", (PyCFunction)BZ2File_readline, METH_VARARGS, BZ2File_readline__doc__},
+ {"readlines", (PyCFunction)BZ2File_readlines, METH_VARARGS, BZ2File_readlines__doc__},
+ {"xreadlines", (PyCFunction)BZ2File_getiter, METH_VARARGS, BZ2File_xreadlines__doc__},
+ {"write", (PyCFunction)BZ2File_write, METH_VARARGS, BZ2File_write__doc__},
+ {"writelines", (PyCFunction)BZ2File_writelines, METH_O, BZ2File_writelines__doc__},
+ {"seek", (PyCFunction)BZ2File_seek, METH_VARARGS, BZ2File_seek__doc__},
+ {"tell", (PyCFunction)BZ2File_tell, METH_NOARGS, BZ2File_tell__doc__},
+ {"close", (PyCFunction)BZ2File_close, METH_NOARGS, BZ2File_close__doc__},
+ {NULL, NULL} /* sentinel */
+};
+
+
+/* ===================================================================== */
+/* Getters and setters of BZ2File. */
+
+/* This is a hacked version of Python's fileobject.c:get_newlines(). */
+static PyObject *
+BZ2File_get_newlines(BZ2FileObject *self, void *closure)
+{
+ switch (self->f_newlinetypes) {
+ case NEWLINE_UNKNOWN:
+ Py_INCREF(Py_None);
+ return Py_None;
+ case NEWLINE_CR:
+ return PyString_FromString("\r");
+ case NEWLINE_LF:
+ return PyString_FromString("\n");
+ case NEWLINE_CR|NEWLINE_LF:
+ return Py_BuildValue("(ss)", "\r", "\n");
+ case NEWLINE_CRLF:
+ return PyString_FromString("\r\n");
+ case NEWLINE_CR|NEWLINE_CRLF:
+ return Py_BuildValue("(ss)", "\r", "\r\n");
+ case NEWLINE_LF|NEWLINE_CRLF:
+ return Py_BuildValue("(ss)", "\n", "\r\n");
+ case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
+ return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "Unknown newlines value 0x%x\n",
+ self->f_newlinetypes);
+ return NULL;
+ }
+}
+
+static PyObject *
+BZ2File_get_closed(BZ2FileObject *self, void *closure)
+{
+ return PyInt_FromLong(self->mode == MODE_CLOSED);
+}
+
+static PyObject *
+BZ2File_get_mode(BZ2FileObject *self, void *closure)
+{
+ return PyObject_GetAttrString(self->file, "mode");
+}
+
+static PyObject *
+BZ2File_get_name(BZ2FileObject *self, void *closure)
+{
+ return PyObject_GetAttrString(self->file, "name");
+}
+
+static PyGetSetDef BZ2File_getset[] = {
+ {"closed", (getter)BZ2File_get_closed, NULL,
+ "True if the file is closed"},
+ {"newlines", (getter)BZ2File_get_newlines, NULL,
+ "end-of-line convention used in this file"},
+ {"mode", (getter)BZ2File_get_mode, NULL,
+ "file mode ('r', 'w', or 'U')"},
+ {"name", (getter)BZ2File_get_name, NULL,
+ "file name"},
+ {NULL} /* Sentinel */
+};
+
+
+/* ===================================================================== */
+/* Members of BZ2File_Type. */
+
+#undef OFF
+#define OFF(x) offsetof(BZ2FileObject, x)
+
+static PyMemberDef BZ2File_members[] = {
+ {"softspace", T_INT, OFF(f_softspace), 0,
+ "flag indicating that a space needs to be printed; used by print"},
+ {NULL} /* Sentinel */
+};
+
+/* ===================================================================== */
+/* Slot definitions for BZ2File_Type. */
+
+static int
+BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
+{
+ static char *kwlist[] = {"filename", "mode", "buffering",
+ "compresslevel", 0};
+ PyObject *name;
+ char *mode = "r";
+ int buffering = -1;
+ int compresslevel = 9;
+ int bzerror;
+ int mode_char = 0;
+
+ self->size = -1;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sii:BZ2File",
+ kwlist, &name, &mode, &buffering,
+ &compresslevel))
+ return -1;
+
+ if (compresslevel < 1 || compresslevel > 9) {
+ PyErr_SetString(PyExc_ValueError,
+ "compresslevel must be between 1 and 9");
+ return -1;
+ }
+
+ for (;;) {
+ int error = 0;
+ switch (*mode) {
+ case 'r':
+ case 'w':
+ if (mode_char)
+ error = 1;
+ mode_char = *mode;
+ break;
+
+ case 'b':
+ break;
+
+ case 'U':
+ self->f_univ_newline = 1;
+ break;
+
+ default:
+ error = 1;
+ break;
+ }
+ if (error) {
+ PyErr_Format(PyExc_ValueError,
+ "invalid mode char %c", *mode);
+ return -1;
+ }
+ mode++;
+ if (*mode == '\0')
+ break;
+ }
+
+ if (mode_char == 0) {
+ mode_char = 'r';
+ }
+
+ mode = (mode_char == 'r') ? "rb" : "wb";
+
+ self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)",
+ name, mode, buffering);
+ if (self->file == NULL)
+ return -1;
+
+ /* From now on, we have stuff to dealloc, so jump to error label
+ * instead of returning */
+
+#ifdef WITH_THREAD
+ self->lock = PyThread_allocate_lock();
+ if (!self->lock)
+ goto error;
+#endif
+
+ if (mode_char == 'r')
+ self->fp = BZ2_bzReadOpen(&bzerror,
+ PyFile_AsFile(self->file),
+ 0, 0, NULL, 0);
+ else
+ self->fp = BZ2_bzWriteOpen(&bzerror,
+ PyFile_AsFile(self->file),
+ compresslevel, 0, 0);
+
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+
+ self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE;
+
+ return 0;
+
+error:
+ Py_DECREF(self->file);
+#ifdef WITH_THREAD
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+#endif
+ return -1;
+}
+
+static void
+BZ2File_dealloc(BZ2FileObject *self)
+{
+ int bzerror;
+#ifdef WITH_THREAD
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+#endif
+ switch (self->mode) {
+ case MODE_READ:
+ case MODE_READ_EOF:
+ BZ2_bzReadClose(&bzerror, self->fp);
+ break;
+ case MODE_WRITE:
+ BZ2_bzWriteClose(&bzerror, self->fp,
+ 0, NULL, NULL);
+ break;
+ }
+ Util_DropReadAhead(self);
+ Py_XDECREF(self->file);
+ self->ob_type->tp_free((PyObject *)self);
+}
+
+/* This is a hacked version of Python's fileobject.c:file_getiter(). */
+static PyObject *
+BZ2File_getiter(BZ2FileObject *self)
+{
+ if (self->mode == MODE_CLOSED) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ return NULL;
+ }
+ Py_INCREF((PyObject*)self);
+ return (PyObject *)self;
+}
+
+/* This is a hacked version of Python's fileobject.c:file_iternext(). */
+#define READAHEAD_BUFSIZE 8192
+static PyObject *
+BZ2File_iternext(BZ2FileObject *self)
+{
+ PyStringObject* ret;
+ ACQUIRE_LOCK(self);
+ if (self->mode == MODE_CLOSED) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ return NULL;
+ }
+ ret = Util_ReadAheadGetLineSkip(self, 0, READAHEAD_BUFSIZE);
+ RELEASE_LOCK(self);
+ if (ret == NULL || PyString_GET_SIZE(ret) == 0) {
+ Py_XDECREF(ret);
+ return NULL;
+ }
+ return (PyObject *)ret;
+}
+
+/* ===================================================================== */
+/* BZ2File_Type definition. */
+
+PyDoc_VAR(BZ2File__doc__) =
+PyDoc_STR(
+"BZ2File(name [, mode='r', buffering=0, compresslevel=9]) -> file object\n\
+\n\
+Open a bz2 file. The mode can be 'r' or 'w', for reading (default) or\n\
+writing. When opened for writing, the file will be created if it doesn't\n\
+exist, and truncated otherwise. If the buffering argument is given, 0 means\n\
+unbuffered, and larger numbers specify the buffer size. If compresslevel\n\
+is given, must be a number between 1 and 9.\n\
+")
+PyDoc_STR(
+"\n\
+Add a 'U' to mode to open the file for input with universal newline\n\
+support. Any line ending in the input file will be seen as a '\\n' in\n\
+Python. Also, a file so opened gains the attribute 'newlines'; the value\n\
+for this attribute is one of None (no newline read yet), '\\r', '\\n',\n\
+'\\r\\n' or a tuple containing all the newline types seen. Universal\n\
+newlines are available only when reading.\n\
+")
+;
+
+static PyTypeObject BZ2File_Type = {
+ PyObject_HEAD_INIT(NULL)
+ 0, /*ob_size*/
+ "bz2.BZ2File", /*tp_name*/
+ sizeof(BZ2FileObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)BZ2File_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr,/*tp_getattro*/
+ PyObject_GenericSetAttr,/*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ BZ2File__doc__, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ (getiterfunc)BZ2File_getiter, /*tp_iter*/
+ (iternextfunc)BZ2File_iternext, /*tp_iternext*/
+ BZ2File_methods, /*tp_methods*/
+ BZ2File_members, /*tp_members*/
+ BZ2File_getset, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ (initproc)BZ2File_init, /*tp_init*/
+ PyType_GenericAlloc, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ _PyObject_Del, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+
+/* ===================================================================== */
+/* Methods of BZ2Comp. */
+
+PyDoc_STRVAR(BZ2Comp_compress__doc__,
+"compress(data) -> string\n\
+\n\
+Provide more data to the compressor object. It will return chunks of\n\
+compressed data whenever possible. When you've finished providing data\n\
+to compress, call the flush() method to finish the compression process,\n\
+and return what is left in the internal buffers.\n\
+");
+
+static PyObject *
+BZ2Comp_compress(BZ2CompObject *self, PyObject *args)
+{
+ char *data;
+ int datasize;
+ int bufsize = SMALLCHUNK;
+ PY_LONG_LONG totalout;
+ PyObject *ret = NULL;
+ bz_stream *bzs = &self->bzs;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "s#:compress", &data, &datasize))
+ return NULL;
+
+ if (datasize == 0)
+ return PyString_FromString("");
+
+ ACQUIRE_LOCK(self);
+ if (!self->running) {
+ PyErr_SetString(PyExc_ValueError,
+ "this object was already flushed");
+ goto error;
+ }
+
+ ret = PyString_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ goto error;
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ totalout = BZS_TOTAL_OUT(bzs);
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzCompress(bzs, BZ_RUN);
+ Py_END_ALLOW_THREADS
+ if (bzerror != BZ_RUN_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyString_Resize(&ret, bufsize) < 0) {
+ BZ2_bzCompressEnd(bzs);
+ goto error;
+ }
+ bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
+ - totalout);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ } else if (bzs->avail_in == 0) {
+ break;
+ }
+ }
+
+ _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
+
+ RELEASE_LOCK(self);
+ return ret;
+
+error:
+ RELEASE_LOCK(self);
+ Py_XDECREF(ret);
+ return NULL;
+}
+
+PyDoc_STRVAR(BZ2Comp_flush__doc__,
+"flush() -> string\n\
+\n\
+Finish the compression process and return what is left in internal buffers.\n\
+You must not use the compressor object after calling this method.\n\
+");
+
+static PyObject *
+BZ2Comp_flush(BZ2CompObject *self)
+{
+ int bufsize = SMALLCHUNK;
+ PyObject *ret = NULL;
+ bz_stream *bzs = &self->bzs;
+ PY_LONG_LONG totalout;
+ int bzerror;
+
+ ACQUIRE_LOCK(self);
+ if (!self->running) {
+ PyErr_SetString(PyExc_ValueError, "object was already "
+ "flushed");
+ goto error;
+ }
+ self->running = 0;
+
+ ret = PyString_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ goto error;
+
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ totalout = BZS_TOTAL_OUT(bzs);
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_FINISH_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyString_Resize(&ret, bufsize) < 0)
+ goto error;
+ bzs->next_out = BUF(ret);
+ bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
+ - totalout);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (bzs->avail_out != 0)
+ _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
+
+ RELEASE_LOCK(self);
+ return ret;
+
+error:
+ RELEASE_LOCK(self);
+ Py_XDECREF(ret);
+ return NULL;
+}
+
+static PyMethodDef BZ2Comp_methods[] = {
+ {"compress", (PyCFunction)BZ2Comp_compress, METH_VARARGS,
+ BZ2Comp_compress__doc__},
+ {"flush", (PyCFunction)BZ2Comp_flush, METH_NOARGS,
+ BZ2Comp_flush__doc__},
+ {NULL, NULL} /* sentinel */
+};
+
+
+/* ===================================================================== */
+/* Slot definitions for BZ2Comp_Type. */
+
+static int
+BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
+{
+ int compresslevel = 9;
+ int bzerror;
+ static char *kwlist[] = {"compresslevel", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:BZ2Compressor",
+ kwlist, &compresslevel))
+ return -1;
+
+ if (compresslevel < 1 || compresslevel > 9) {
+ PyErr_SetString(PyExc_ValueError,
+ "compresslevel must be between 1 and 9");
+ goto error;
+ }
+
+#ifdef WITH_THREAD
+ self->lock = PyThread_allocate_lock();
+ if (!self->lock)
+ goto error;
+#endif
+
+ memset(&self->bzs, 0, sizeof(bz_stream));
+ bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+
+ self->running = 1;
+
+ return 0;
+error:
+#ifdef WITH_THREAD
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+#endif
+ return -1;
+}
+
+static void
+BZ2Comp_dealloc(BZ2CompObject *self)
+{
+#ifdef WITH_THREAD
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+#endif
+ BZ2_bzCompressEnd(&self->bzs);
+ self->ob_type->tp_free((PyObject *)self);
+}
+
+
+/* ===================================================================== */
+/* BZ2Comp_Type definition. */
+
+PyDoc_STRVAR(BZ2Comp__doc__,
+"BZ2Compressor([compresslevel=9]) -> compressor object\n\
+\n\
+Create a new compressor object. This object may be used to compress\n\
+data sequentially. If you want to compress data in one shot, use the\n\
+compress() function instead. The compresslevel parameter, if given,\n\
+must be a number between 1 and 9.\n\
+");
+
+static PyTypeObject BZ2Comp_Type = {
+ PyObject_HEAD_INIT(NULL)
+ 0, /*ob_size*/
+ "bz2.BZ2Compressor", /*tp_name*/
+ sizeof(BZ2CompObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)BZ2Comp_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr,/*tp_getattro*/
+ PyObject_GenericSetAttr,/*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ BZ2Comp__doc__, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ BZ2Comp_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ (initproc)BZ2Comp_init, /*tp_init*/
+ PyType_GenericAlloc, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ _PyObject_Del, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+
+/* ===================================================================== */
+/* Members of BZ2Decomp. */
+
+#undef OFF
+#define OFF(x) offsetof(BZ2DecompObject, x)
+
+static PyMemberDef BZ2Decomp_members[] = {
+ {"unused_data", T_OBJECT, OFF(unused_data), RO},
+ {NULL} /* Sentinel */
+};
+
+
+/* ===================================================================== */
+/* Methods of BZ2Decomp. */
+
+PyDoc_STRVAR(BZ2Decomp_decompress__doc__,
+"decompress(data) -> string\n\
+\n\
+Provide more data to the decompressor object. It will return chunks\n\
+of decompressed data whenever possible. If you try to decompress data\n\
+after the end of stream is found, EOFError will be raised. If any data\n\
+was found after the end of stream, it'll be ignored and saved in\n\
+unused_data attribute.\n\
+");
+
+static PyObject *
+BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args)
+{
+ char *data;
+ int datasize;
+ int bufsize = SMALLCHUNK;
+ PY_LONG_LONG totalout;
+ PyObject *ret = NULL;
+ bz_stream *bzs = &self->bzs;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ if (!self->running) {
+ PyErr_SetString(PyExc_EOFError, "end of stream was "
+ "already found");
+ goto error;
+ }
+
+ ret = PyString_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ goto error;
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ totalout = BZS_TOTAL_OUT(bzs);
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzDecompress(bzs);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ if (bzs->avail_in != 0) {
+ Py_DECREF(self->unused_data);
+ self->unused_data =
+ PyString_FromStringAndSize(bzs->next_in,
+ bzs->avail_in);
+ }
+ self->running = 0;
+ break;
+ }
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyString_Resize(&ret, bufsize) < 0) {
+ BZ2_bzDecompressEnd(bzs);
+ goto error;
+ }
+ bzs->next_out = BUF(ret);
+ bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
+ - totalout);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ } else if (bzs->avail_in == 0) {
+ break;
+ }
+ }
+
+ if (bzs->avail_out != 0)
+ _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
+
+ RELEASE_LOCK(self);
+ return ret;
+
+error:
+ RELEASE_LOCK(self);
+ Py_XDECREF(ret);
+ return NULL;
+}
+
+static PyMethodDef BZ2Decomp_methods[] = {
+ {"decompress", (PyCFunction)BZ2Decomp_decompress, METH_VARARGS, BZ2Decomp_decompress__doc__},
+ {NULL, NULL} /* sentinel */
+};
+
+
+/* ===================================================================== */
+/* Slot definitions for BZ2Decomp_Type. */
+
+static int
+BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
+{
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, ":BZ2Decompressor"))
+ return -1;
+
+#ifdef WITH_THREAD
+ self->lock = PyThread_allocate_lock();
+ if (!self->lock)
+ goto error;
+#endif
+
+ self->unused_data = PyString_FromString("");
+ if (!self->unused_data)
+ goto error;
+
+ memset(&self->bzs, 0, sizeof(bz_stream));
+ bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+
+ self->running = 1;
+
+ return 0;
+
+error:
+#ifdef WITH_THREAD
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+#endif
+ Py_XDECREF(self->unused_data);
+ return -1;
+}
+
+static void
+BZ2Decomp_dealloc(BZ2DecompObject *self)
+{
+#ifdef WITH_THREAD
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+#endif
+ Py_XDECREF(self->unused_data);
+ BZ2_bzDecompressEnd(&self->bzs);
+ self->ob_type->tp_free((PyObject *)self);
+}
+
+
+/* ===================================================================== */
+/* BZ2Decomp_Type definition. */
+
+PyDoc_STRVAR(BZ2Decomp__doc__,
+"BZ2Decompressor() -> decompressor object\n\
+\n\
+Create a new decompressor object. This object may be used to decompress\n\
+data sequentially. If you want to decompress data in one shot, use the\n\
+decompress() function instead.\n\
+");
+
+static PyTypeObject BZ2Decomp_Type = {
+ PyObject_HEAD_INIT(NULL)
+ 0, /*ob_size*/
+ "bz2.BZ2Decompressor", /*tp_name*/
+ sizeof(BZ2DecompObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)BZ2Decomp_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr,/*tp_getattro*/
+ PyObject_GenericSetAttr,/*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ BZ2Decomp__doc__, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ BZ2Decomp_methods, /*tp_methods*/
+ BZ2Decomp_members, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ (initproc)BZ2Decomp_init, /*tp_init*/
+ PyType_GenericAlloc, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ _PyObject_Del, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+
+/* ===================================================================== */
+/* Module functions. */
+
+PyDoc_STRVAR(bz2_compress__doc__,
+"compress(data [, compresslevel=9]) -> string\n\
+\n\
+Compress data in one shot. If you want to compress data sequentially,\n\
+use an instance of BZ2Compressor instead. The compresslevel parameter, if\n\
+given, must be a number between 1 and 9.\n\
+");
+
+static PyObject *
+bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int compresslevel=9;
+ char *data;
+ int datasize;
+ int bufsize;
+ PyObject *ret = NULL;
+ bz_stream _bzs;
+ bz_stream *bzs = &_bzs;
+ int bzerror;
+ static char *kwlist[] = {"data", "compresslevel", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i",
+ kwlist, &data, &datasize,
+ &compresslevel))
+ return NULL;
+
+ if (compresslevel < 1 || compresslevel > 9) {
+ PyErr_SetString(PyExc_ValueError,
+ "compresslevel must be between 1 and 9");
+ return NULL;
+ }
+
+ /* Conforming to bz2 manual, this is large enough to fit compressed
+ * data in one shot. We will check it later anyway. */
+ bufsize = datasize + (datasize/100+1) + 600;
+
+ ret = PyString_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ return NULL;
+
+ memset(bzs, 0, sizeof(bz_stream));
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ return NULL;
+ }
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_FINISH_OK) {
+ BZ2_bzCompressEnd(bzs);
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyString_Resize(&ret, bufsize) < 0) {
+ BZ2_bzCompressEnd(bzs);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (bzs->avail_out != 0)
+ _PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs));
+ BZ2_bzCompressEnd(bzs);
+
+ return ret;
+}
+
+PyDoc_STRVAR(bz2_decompress__doc__,
+"decompress(data) -> decompressed data\n\
+\n\
+Decompress data in one shot. If you want to decompress data sequentially,\n\
+use an instance of BZ2Decompressor instead.\n\
+");
+
+static PyObject *
+bz2_decompress(PyObject *self, PyObject *args)
+{
+ char *data;
+ int datasize;
+ int bufsize = SMALLCHUNK;
+ PyObject *ret;
+ bz_stream _bzs;
+ bz_stream *bzs = &_bzs;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize))
+ return NULL;
+
+ if (datasize == 0)
+ return PyString_FromString("");
+
+ ret = PyString_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ return NULL;
+
+ memset(bzs, 0, sizeof(bz_stream));
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ bzerror = BZ2_bzDecompressInit(bzs, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ return NULL;
+ }
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzDecompress(bzs);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_OK) {
+ BZ2_bzDecompressEnd(bzs);
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyString_Resize(&ret, bufsize) < 0) {
+ BZ2_bzDecompressEnd(bzs);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ } else if (bzs->avail_in == 0) {
+ BZ2_bzDecompressEnd(bzs);
+ PyErr_SetString(PyExc_ValueError,
+ "couldn't find end of stream");
+ Py_DECREF(ret);
+ return NULL;
+ }
+ }
+
+ if (bzs->avail_out != 0)
+ _PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs));
+ BZ2_bzDecompressEnd(bzs);
+
+ return ret;
+}
+
+static PyMethodDef bz2_methods[] = {
+ {"compress", (PyCFunction) bz2_compress, METH_VARARGS|METH_KEYWORDS,
+ bz2_compress__doc__},
+ {"decompress", (PyCFunction) bz2_decompress, METH_VARARGS,
+ bz2_decompress__doc__},
+ {NULL, NULL} /* sentinel */
+};
+
+/* ===================================================================== */
+/* Initialization function. */
+
+PyDoc_STRVAR(bz2__doc__,
+"The python bz2 module provides a comprehensive interface for\n\
+the bz2 compression library. It implements a complete file\n\
+interface, one shot (de)compression functions, and types for\n\
+sequential (de)compression.\n\
+");
+
+PyMODINIT_FUNC
+initbz2(void)
+{
+ PyObject *m;
+
+ BZ2File_Type.ob_type = &PyType_Type;
+ BZ2Comp_Type.ob_type = &PyType_Type;
+ BZ2Decomp_Type.ob_type = &PyType_Type;
+
+ m = Py_InitModule3("bz2", bz2_methods, bz2__doc__);
+ if (m == NULL)
+ return;
+
+ PyModule_AddObject(m, "__author__", PyString_FromString(__author__));
+
+ Py_INCREF(&BZ2File_Type);
+ PyModule_AddObject(m, "BZ2File", (PyObject *)&BZ2File_Type);
+
+ Py_INCREF(&BZ2Comp_Type);
+ PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Comp_Type);
+
+ Py_INCREF(&BZ2Decomp_Type);
+ PyModule_AddObject(m, "BZ2Decompressor", (PyObject *)&BZ2Decomp_Type);
+}
Added: pypy/dist/pypy/module/bz2/bzlib.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/bzlib.py Fri Aug 4 16:10:40 2006
@@ -0,0 +1,283 @@
+from ctypes import *
+STRING = c_char_p
+
+
+__darwin_nl_item = c_int
+__darwin_wctrans_t = c_int
+__darwin_wctype_t = c_ulong
+class bz_stream(Structure):
+ pass
+bz_stream._fields_ = [
+ ('next_in', STRING),
+ ('avail_in', c_uint),
+ ('total_in_lo32', c_uint),
+ ('total_in_hi32', c_uint),
+ ('next_out', STRING),
+ ('avail_out', c_uint),
+ ('total_out_lo32', c_uint),
+ ('total_out_hi32', c_uint),
+ ('state', c_void_p),
+ ('bzalloc', CFUNCTYPE(c_void_p, c_void_p, c_int, c_int)),
+ ('bzfree', CFUNCTYPE(None, c_void_p, c_void_p)),
+ ('opaque', c_void_p),
+]
+assert sizeof(bz_stream) == 48, sizeof(bz_stream)
+assert alignment(bz_stream) == 4, alignment(bz_stream)
+BZFILE = None
+__int8_t = c_byte
+__uint8_t = c_ubyte
+__int16_t = c_short
+__uint16_t = c_ushort
+__int32_t = c_int
+__uint32_t = c_uint
+__int64_t = c_longlong
+__uint64_t = c_ulonglong
+__darwin_intptr_t = c_long
+__darwin_natural_t = c_uint
+__darwin_ct_rune_t = c_int
+class __mbstate_t(Union):
+ pass
+__mbstate_t._pack_ = 4
+__mbstate_t._fields_ = [
+ ('__mbstate8', c_char * 128),
+ ('_mbstateL', c_longlong),
+]
+assert sizeof(__mbstate_t) == 128, sizeof(__mbstate_t)
+assert alignment(__mbstate_t) == 4, alignment(__mbstate_t)
+__darwin_mbstate_t = __mbstate_t
+__darwin_ptrdiff_t = c_int
+__darwin_size_t = c_ulong
+__darwin_va_list = STRING
+__darwin_wchar_t = c_int
+__darwin_rune_t = __darwin_wchar_t
+__darwin_wint_t = c_int
+__darwin_clock_t = c_ulong
+__darwin_socklen_t = __uint32_t
+__darwin_ssize_t = c_long
+__darwin_time_t = c_long
+va_list = __darwin_va_list
+size_t = __darwin_size_t
+__darwin_off_t = __int64_t
+fpos_t = __darwin_off_t
+class __sbuf(Structure):
+ pass
+__sbuf._fields_ = [
+ ('_base', POINTER(c_ubyte)),
+ ('_size', c_int),
+]
+assert sizeof(__sbuf) == 8, sizeof(__sbuf)
+assert alignment(__sbuf) == 4, alignment(__sbuf)
+class __sFILEX(Structure):
+ pass
+class __sFILE(Structure):
+ pass
+__sFILE._pack_ = 4
+__sFILE._fields_ = [
+ ('_p', POINTER(c_ubyte)),
+ ('_r', c_int),
+ ('_w', c_int),
+ ('_flags', c_short),
+ ('_file', c_short),
+ ('_bf', __sbuf),
+ ('_lbfsize', c_int),
+ ('_cookie', c_void_p),
+ ('_close', CFUNCTYPE(c_int, c_void_p)),
+ ('_read', CFUNCTYPE(c_int, c_void_p, STRING, c_int)),
+ ('_seek', CFUNCTYPE(fpos_t, c_void_p, c_longlong, c_int)),
+ ('_write', CFUNCTYPE(c_int, c_void_p, STRING, c_int)),
+ ('_ub', __sbuf),
+ ('_extra', POINTER(__sFILEX)),
+ ('_ur', c_int),
+ ('_ubuf', c_ubyte * 3),
+ ('_nbuf', c_ubyte * 1),
+ ('_lb', __sbuf),
+ ('_blksize', c_int),
+ ('_offset', fpos_t),
+]
+assert sizeof(__sFILE) == 88, sizeof(__sFILE)
+assert alignment(__sFILE) == 4, alignment(__sFILE)
+FILE = __sFILE
+class mcontext(Structure):
+ pass
+class mcontext64(Structure):
+ pass
+class __darwin_pthread_handler_rec(Structure):
+ pass
+__darwin_pthread_handler_rec._fields_ = [
+ ('__routine', CFUNCTYPE(None, c_void_p)),
+ ('__arg', c_void_p),
+ ('__next', POINTER(__darwin_pthread_handler_rec)),
+]
+assert sizeof(__darwin_pthread_handler_rec) == 12, sizeof(__darwin_pthread_handler_rec)
+assert alignment(__darwin_pthread_handler_rec) == 4, alignment(__darwin_pthread_handler_rec)
+class _opaque_pthread_attr_t(Structure):
+ pass
+_opaque_pthread_attr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 36),
+]
+assert sizeof(_opaque_pthread_attr_t) == 40, sizeof(_opaque_pthread_attr_t)
+assert alignment(_opaque_pthread_attr_t) == 4, alignment(_opaque_pthread_attr_t)
+class _opaque_pthread_cond_t(Structure):
+ pass
+_opaque_pthread_cond_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 24),
+]
+assert sizeof(_opaque_pthread_cond_t) == 28, sizeof(_opaque_pthread_cond_t)
+assert alignment(_opaque_pthread_cond_t) == 4, alignment(_opaque_pthread_cond_t)
+class _opaque_pthread_condattr_t(Structure):
+ pass
+_opaque_pthread_condattr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 4),
+]
+assert sizeof(_opaque_pthread_condattr_t) == 8, sizeof(_opaque_pthread_condattr_t)
+assert alignment(_opaque_pthread_condattr_t) == 4, alignment(_opaque_pthread_condattr_t)
+class _opaque_pthread_mutex_t(Structure):
+ pass
+_opaque_pthread_mutex_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 40),
+]
+assert sizeof(_opaque_pthread_mutex_t) == 44, sizeof(_opaque_pthread_mutex_t)
+assert alignment(_opaque_pthread_mutex_t) == 4, alignment(_opaque_pthread_mutex_t)
+class _opaque_pthread_mutexattr_t(Structure):
+ pass
+_opaque_pthread_mutexattr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 8),
+]
+assert sizeof(_opaque_pthread_mutexattr_t) == 12, sizeof(_opaque_pthread_mutexattr_t)
+assert alignment(_opaque_pthread_mutexattr_t) == 4, alignment(_opaque_pthread_mutexattr_t)
+class _opaque_pthread_once_t(Structure):
+ pass
+_opaque_pthread_once_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 4),
+]
+assert sizeof(_opaque_pthread_once_t) == 8, sizeof(_opaque_pthread_once_t)
+assert alignment(_opaque_pthread_once_t) == 4, alignment(_opaque_pthread_once_t)
+class _opaque_pthread_rwlock_t(Structure):
+ pass
+_opaque_pthread_rwlock_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 124),
+]
+assert sizeof(_opaque_pthread_rwlock_t) == 128, sizeof(_opaque_pthread_rwlock_t)
+assert alignment(_opaque_pthread_rwlock_t) == 4, alignment(_opaque_pthread_rwlock_t)
+class _opaque_pthread_rwlockattr_t(Structure):
+ pass
+_opaque_pthread_rwlockattr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 12),
+]
+assert sizeof(_opaque_pthread_rwlockattr_t) == 16, sizeof(_opaque_pthread_rwlockattr_t)
+assert alignment(_opaque_pthread_rwlockattr_t) == 4, alignment(_opaque_pthread_rwlockattr_t)
+class _opaque_pthread_t(Structure):
+ pass
+_opaque_pthread_t._fields_ = [
+ ('__sig', c_long),
+ ('__cleanup_stack', POINTER(__darwin_pthread_handler_rec)),
+ ('__opaque', c_char * 596),
+]
+assert sizeof(_opaque_pthread_t) == 604, sizeof(_opaque_pthread_t)
+assert alignment(_opaque_pthread_t) == 4, alignment(_opaque_pthread_t)
+__darwin_blkcnt_t = __int64_t
+__darwin_blksize_t = __int32_t
+__darwin_dev_t = __int32_t
+__darwin_fsblkcnt_t = c_uint
+__darwin_fsfilcnt_t = c_uint
+__darwin_gid_t = __uint32_t
+__darwin_id_t = __uint32_t
+__darwin_ino_t = __uint32_t
+__darwin_mach_port_name_t = __darwin_natural_t
+__darwin_mach_port_t = __darwin_mach_port_name_t
+__darwin_mcontext_t = POINTER(mcontext)
+__darwin_mcontext64_t = POINTER(mcontext64)
+__darwin_mode_t = __uint16_t
+__darwin_pid_t = __int32_t
+__darwin_pthread_attr_t = _opaque_pthread_attr_t
+__darwin_pthread_cond_t = _opaque_pthread_cond_t
+__darwin_pthread_condattr_t = _opaque_pthread_condattr_t
+__darwin_pthread_key_t = c_ulong
+__darwin_pthread_mutex_t = _opaque_pthread_mutex_t
+__darwin_pthread_mutexattr_t = _opaque_pthread_mutexattr_t
+__darwin_pthread_once_t = _opaque_pthread_once_t
+__darwin_pthread_rwlock_t = _opaque_pthread_rwlock_t
+__darwin_pthread_rwlockattr_t = _opaque_pthread_rwlockattr_t
+__darwin_pthread_t = POINTER(_opaque_pthread_t)
+__darwin_sigset_t = __uint32_t
+__darwin_suseconds_t = __int32_t
+__darwin_uid_t = __uint32_t
+__darwin_useconds_t = __uint32_t
+__darwin_uuid_t = c_ubyte * 16
+class sigaltstack(Structure):
+ pass
+sigaltstack._fields_ = [
+ ('ss_sp', c_void_p),
+ ('ss_size', __darwin_size_t),
+ ('ss_flags', c_int),
+]
+assert sizeof(sigaltstack) == 12, sizeof(sigaltstack)
+assert alignment(sigaltstack) == 4, alignment(sigaltstack)
+__darwin_stack_t = sigaltstack
+class ucontext(Structure):
+ pass
+ucontext._fields_ = [
+ ('uc_onstack', c_int),
+ ('uc_sigmask', __darwin_sigset_t),
+ ('uc_stack', __darwin_stack_t),
+ ('uc_link', POINTER(ucontext)),
+ ('uc_mcsize', __darwin_size_t),
+ ('uc_mcontext', __darwin_mcontext_t),
+]
+assert sizeof(ucontext) == 32, sizeof(ucontext)
+assert alignment(ucontext) == 4, alignment(ucontext)
+__darwin_ucontext_t = ucontext
+class ucontext64(Structure):
+ pass
+ucontext64._fields_ = [
+ ('uc_onstack', c_int),
+ ('uc_sigmask', __darwin_sigset_t),
+ ('uc_stack', __darwin_stack_t),
+ ('uc_link', POINTER(ucontext64)),
+ ('uc_mcsize', __darwin_size_t),
+ ('uc_mcontext64', __darwin_mcontext64_t),
+]
+assert sizeof(ucontext64) == 32, sizeof(ucontext64)
+assert alignment(ucontext64) == 4, alignment(ucontext64)
+__darwin_ucontext64_t = ucontext64
+__all__ = ['__uint16_t', '__darwin_off_t', '__uint64_t', '__int16_t',
+ '__darwin_pthread_condattr_t', '__darwin_pthread_key_t',
+ '__darwin_pthread_handler_rec', '__darwin_id_t',
+ '__darwin_ucontext64_t', '__darwin_pthread_mutex_t',
+ '__darwin_wctype_t', '_opaque_pthread_once_t',
+ '__darwin_time_t', '__darwin_nl_item',
+ '_opaque_pthread_condattr_t', '__darwin_pthread_rwlock_t',
+ 'FILE', 'size_t', '__darwin_pthread_rwlockattr_t',
+ '__darwin_fsblkcnt_t', '__darwin_rune_t',
+ '__darwin_intptr_t', 'ucontext64', '_opaque_pthread_t',
+ '__darwin_va_list', '__uint32_t', '__darwin_sigset_t',
+ '__sbuf', 'fpos_t', '_opaque_pthread_mutexattr_t',
+ '__darwin_socklen_t', '__darwin_suseconds_t',
+ '__darwin_gid_t', '__darwin_uuid_t', '__darwin_dev_t',
+ '__darwin_pthread_mutexattr_t', '__darwin_stack_t',
+ '__darwin_pthread_once_t', '__darwin_ucontext_t',
+ '__darwin_blksize_t', '__darwin_mode_t', '__sFILE',
+ '__darwin_wctrans_t', '__darwin_fsfilcnt_t', '__mbstate_t',
+ '__sFILEX', '_opaque_pthread_rwlockattr_t',
+ '__darwin_mach_port_t', '__uint8_t', '__darwin_uid_t',
+ '__int8_t', 'sigaltstack', '__darwin_wchar_t',
+ '_opaque_pthread_mutex_t', '__darwin_pthread_attr_t',
+ '__darwin_mach_port_name_t', 'BZFILE',
+ '__darwin_pthread_t', '_opaque_pthread_attr_t', 'ucontext',
+ '__darwin_wint_t', '__darwin_pthread_cond_t',
+ '__darwin_mcontext64_t', '__darwin_natural_t',
+ '_opaque_pthread_cond_t', '__darwin_blkcnt_t', 'mcontext',
+ '__darwin_ssize_t', 'mcontext64', '__darwin_mcontext_t',
+ '__darwin_size_t', '__darwin_pid_t', '__darwin_ptrdiff_t',
+ '_opaque_pthread_rwlock_t', '__darwin_ct_rune_t',
+ 'va_list', '__darwin_ino_t', '__int32_t', '__int64_t',
+ '__darwin_mbstate_t', '__darwin_useconds_t', 'bz_stream',
+ '__darwin_clock_t']
Added: pypy/dist/pypy/module/bz2/fileobject.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/fileobject.py Fri Aug 4 16:10:40 2006
@@ -0,0 +1,1008 @@
+from ctypes import *
+STRING = c_char_p
+
+
+P_ALL = 0
+OSUnknownByteOrder = 0
+OSBigEndian = 2
+_FP_NAN = 1
+_FP_INFINITE = 2
+P_PGID = 2
+_FP_NORMAL = 4
+OSLittleEndian = 1
+P_PID = 1
+_FP_SUBNORMAL = 5
+_FP_SUPERNORMAL = 6
+_FP_ZERO = 3
+__darwin_nl_item = c_int
+__darwin_wctrans_t = c_int
+__darwin_wctype_t = c_ulong
+float_t = c_float
+double_t = c_double
+
+# values for unnamed enumeration
+__int8_t = c_byte
+__uint8_t = c_ubyte
+__int16_t = c_short
+__uint16_t = c_ushort
+__int32_t = c_int
+__uint32_t = c_uint
+__int64_t = c_longlong
+__uint64_t = c_ulonglong
+__darwin_intptr_t = c_long
+__darwin_natural_t = c_uint
+__darwin_ct_rune_t = c_int
+class __mbstate_t(Union):
+ pass
+__mbstate_t._pack_ = 4
+__mbstate_t._fields_ = [
+ ('__mbstate8', c_char * 128),
+ ('_mbstateL', c_longlong),
+]
+assert sizeof(__mbstate_t) == 128, sizeof(__mbstate_t)
+assert alignment(__mbstate_t) == 4, alignment(__mbstate_t)
+__darwin_mbstate_t = __mbstate_t
+__darwin_ptrdiff_t = c_int
+__darwin_size_t = c_ulong
+__darwin_va_list = STRING
+__darwin_wchar_t = c_int
+__darwin_rune_t = __darwin_wchar_t
+__darwin_wint_t = c_int
+__darwin_clock_t = c_ulong
+__darwin_socklen_t = __uint32_t
+__darwin_ssize_t = c_long
+__darwin_time_t = c_long
+sig_atomic_t = c_int
+class sigcontext(Structure):
+ pass
+sigcontext._fields_ = [
+ ('sc_onstack', c_int),
+ ('sc_mask', c_int),
+ ('sc_eax', c_uint),
+ ('sc_ebx', c_uint),
+ ('sc_ecx', c_uint),
+ ('sc_edx', c_uint),
+ ('sc_edi', c_uint),
+ ('sc_esi', c_uint),
+ ('sc_ebp', c_uint),
+ ('sc_esp', c_uint),
+ ('sc_ss', c_uint),
+ ('sc_eflags', c_uint),
+ ('sc_eip', c_uint),
+ ('sc_cs', c_uint),
+ ('sc_ds', c_uint),
+ ('sc_es', c_uint),
+ ('sc_fs', c_uint),
+ ('sc_gs', c_uint),
+]
+assert sizeof(sigcontext) == 72, sizeof(sigcontext)
+assert alignment(sigcontext) == 4, alignment(sigcontext)
+int8_t = c_byte
+u_int8_t = c_ubyte
+int16_t = c_short
+u_int16_t = c_ushort
+int32_t = c_int
+u_int32_t = c_uint
+int64_t = c_longlong
+u_int64_t = c_ulonglong
+register_t = int32_t
+intptr_t = __darwin_intptr_t
+uintptr_t = c_ulong
+user_addr_t = u_int64_t
+user_size_t = u_int64_t
+user_ssize_t = int64_t
+user_long_t = int64_t
+user_ulong_t = u_int64_t
+user_time_t = int64_t
+syscall_arg_t = u_int64_t
+
+# values for unnamed enumeration
+va_list = __darwin_va_list
+size_t = __darwin_size_t
+__darwin_off_t = __int64_t
+fpos_t = __darwin_off_t
+class __sbuf(Structure):
+ pass
+__sbuf._fields_ = [
+ ('_base', POINTER(c_ubyte)),
+ ('_size', c_int),
+]
+assert sizeof(__sbuf) == 8, sizeof(__sbuf)
+assert alignment(__sbuf) == 4, alignment(__sbuf)
+class __sFILEX(Structure):
+ pass
+class __sFILE(Structure):
+ pass
+__sFILE._pack_ = 4
+__sFILE._fields_ = [
+ ('_p', POINTER(c_ubyte)),
+ ('_r', c_int),
+ ('_w', c_int),
+ ('_flags', c_short),
+ ('_file', c_short),
+ ('_bf', __sbuf),
+ ('_lbfsize', c_int),
+ ('_cookie', c_void_p),
+ ('_close', CFUNCTYPE(c_int, c_void_p)),
+ ('_read', CFUNCTYPE(c_int, c_void_p, STRING, c_int)),
+ ('_seek', CFUNCTYPE(fpos_t, c_void_p, c_longlong, c_int)),
+ ('_write', CFUNCTYPE(c_int, c_void_p, STRING, c_int)),
+ ('_ub', __sbuf),
+ ('_extra', POINTER(__sFILEX)),
+ ('_ur', c_int),
+ ('_ubuf', c_ubyte * 3),
+ ('_nbuf', c_ubyte * 1),
+ ('_lb', __sbuf),
+ ('_blksize', c_int),
+ ('_offset', fpos_t),
+]
+assert sizeof(__sFILE) == 88, sizeof(__sFILE)
+assert alignment(__sFILE) == 4, alignment(__sFILE)
+FILE = __sFILE
+ct_rune_t = __darwin_ct_rune_t
+rune_t = __darwin_rune_t
+class div_t(Structure):
+ pass
+div_t._fields_ = [
+ ('quot', c_int),
+ ('rem', c_int),
+]
+assert sizeof(div_t) == 8, sizeof(div_t)
+assert alignment(div_t) == 4, alignment(div_t)
+class ldiv_t(Structure):
+ pass
+ldiv_t._fields_ = [
+ ('quot', c_long),
+ ('rem', c_long),
+]
+assert sizeof(ldiv_t) == 8, sizeof(ldiv_t)
+assert alignment(ldiv_t) == 4, alignment(ldiv_t)
+class lldiv_t(Structure):
+ pass
+lldiv_t._pack_ = 4
+lldiv_t._fields_ = [
+ ('quot', c_longlong),
+ ('rem', c_longlong),
+]
+assert sizeof(lldiv_t) == 16, sizeof(lldiv_t)
+assert alignment(lldiv_t) == 4, alignment(lldiv_t)
+class mcontext(Structure):
+ pass
+class mcontext64(Structure):
+ pass
+class __darwin_pthread_handler_rec(Structure):
+ pass
+__darwin_pthread_handler_rec._fields_ = [
+ ('__routine', CFUNCTYPE(None, c_void_p)),
+ ('__arg', c_void_p),
+ ('__next', POINTER(__darwin_pthread_handler_rec)),
+]
+assert sizeof(__darwin_pthread_handler_rec) == 12, sizeof(__darwin_pthread_handler_rec)
+assert alignment(__darwin_pthread_handler_rec) == 4, alignment(__darwin_pthread_handler_rec)
+class _opaque_pthread_attr_t(Structure):
+ pass
+_opaque_pthread_attr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 36),
+]
+assert sizeof(_opaque_pthread_attr_t) == 40, sizeof(_opaque_pthread_attr_t)
+assert alignment(_opaque_pthread_attr_t) == 4, alignment(_opaque_pthread_attr_t)
+class _opaque_pthread_cond_t(Structure):
+ pass
+_opaque_pthread_cond_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 24),
+]
+assert sizeof(_opaque_pthread_cond_t) == 28, sizeof(_opaque_pthread_cond_t)
+assert alignment(_opaque_pthread_cond_t) == 4, alignment(_opaque_pthread_cond_t)
+class _opaque_pthread_condattr_t(Structure):
+ pass
+_opaque_pthread_condattr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 4),
+]
+assert sizeof(_opaque_pthread_condattr_t) == 8, sizeof(_opaque_pthread_condattr_t)
+assert alignment(_opaque_pthread_condattr_t) == 4, alignment(_opaque_pthread_condattr_t)
+class _opaque_pthread_mutex_t(Structure):
+ pass
+_opaque_pthread_mutex_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 40),
+]
+assert sizeof(_opaque_pthread_mutex_t) == 44, sizeof(_opaque_pthread_mutex_t)
+assert alignment(_opaque_pthread_mutex_t) == 4, alignment(_opaque_pthread_mutex_t)
+class _opaque_pthread_mutexattr_t(Structure):
+ pass
+_opaque_pthread_mutexattr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 8),
+]
+assert sizeof(_opaque_pthread_mutexattr_t) == 12, sizeof(_opaque_pthread_mutexattr_t)
+assert alignment(_opaque_pthread_mutexattr_t) == 4, alignment(_opaque_pthread_mutexattr_t)
+class _opaque_pthread_once_t(Structure):
+ pass
+_opaque_pthread_once_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 4),
+]
+assert sizeof(_opaque_pthread_once_t) == 8, sizeof(_opaque_pthread_once_t)
+assert alignment(_opaque_pthread_once_t) == 4, alignment(_opaque_pthread_once_t)
+class _opaque_pthread_rwlock_t(Structure):
+ pass
+_opaque_pthread_rwlock_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 124),
+]
+assert sizeof(_opaque_pthread_rwlock_t) == 128, sizeof(_opaque_pthread_rwlock_t)
+assert alignment(_opaque_pthread_rwlock_t) == 4, alignment(_opaque_pthread_rwlock_t)
+class _opaque_pthread_rwlockattr_t(Structure):
+ pass
+_opaque_pthread_rwlockattr_t._fields_ = [
+ ('__sig', c_long),
+ ('__opaque', c_char * 12),
+]
+assert sizeof(_opaque_pthread_rwlockattr_t) == 16, sizeof(_opaque_pthread_rwlockattr_t)
+assert alignment(_opaque_pthread_rwlockattr_t) == 4, alignment(_opaque_pthread_rwlockattr_t)
+class _opaque_pthread_t(Structure):
+ pass
+_opaque_pthread_t._fields_ = [
+ ('__sig', c_long),
+ ('__cleanup_stack', POINTER(__darwin_pthread_handler_rec)),
+ ('__opaque', c_char * 596),
+]
+assert sizeof(_opaque_pthread_t) == 604, sizeof(_opaque_pthread_t)
+assert alignment(_opaque_pthread_t) == 4, alignment(_opaque_pthread_t)
+__darwin_blkcnt_t = __int64_t
+__darwin_blksize_t = __int32_t
+__darwin_dev_t = __int32_t
+__darwin_fsblkcnt_t = c_uint
+__darwin_fsfilcnt_t = c_uint
+__darwin_gid_t = __uint32_t
+__darwin_id_t = __uint32_t
+__darwin_ino_t = __uint32_t
+__darwin_mach_port_name_t = __darwin_natural_t
+__darwin_mach_port_t = __darwin_mach_port_name_t
+__darwin_mcontext_t = POINTER(mcontext)
+__darwin_mcontext64_t = POINTER(mcontext64)
+__darwin_mode_t = __uint16_t
+__darwin_pid_t = __int32_t
+__darwin_pthread_attr_t = _opaque_pthread_attr_t
+__darwin_pthread_cond_t = _opaque_pthread_cond_t
+__darwin_pthread_condattr_t = _opaque_pthread_condattr_t
+__darwin_pthread_key_t = c_ulong
+__darwin_pthread_mutex_t = _opaque_pthread_mutex_t
+__darwin_pthread_mutexattr_t = _opaque_pthread_mutexattr_t
+__darwin_pthread_once_t = _opaque_pthread_once_t
+__darwin_pthread_rwlock_t = _opaque_pthread_rwlock_t
+__darwin_pthread_rwlockattr_t = _opaque_pthread_rwlockattr_t
+__darwin_pthread_t = POINTER(_opaque_pthread_t)
+__darwin_sigset_t = __uint32_t
+__darwin_suseconds_t = __int32_t
+__darwin_uid_t = __uint32_t
+__darwin_useconds_t = __uint32_t
+__darwin_uuid_t = c_ubyte * 16
+class sigaltstack(Structure):
+ pass
+sigaltstack._fields_ = [
+ ('ss_sp', c_void_p),
+ ('ss_size', __darwin_size_t),
+ ('ss_flags', c_int),
+]
+assert sizeof(sigaltstack) == 12, sizeof(sigaltstack)
+assert alignment(sigaltstack) == 4, alignment(sigaltstack)
+__darwin_stack_t = sigaltstack
+class ucontext(Structure):
+ pass
+ucontext._fields_ = [
+ ('uc_onstack', c_int),
+ ('uc_sigmask', __darwin_sigset_t),
+ ('uc_stack', __darwin_stack_t),
+ ('uc_link', POINTER(ucontext)),
+ ('uc_mcsize', __darwin_size_t),
+ ('uc_mcontext', __darwin_mcontext_t),
+]
+assert sizeof(ucontext) == 32, sizeof(ucontext)
+assert alignment(ucontext) == 4, alignment(ucontext)
+__darwin_ucontext_t = ucontext
+class ucontext64(Structure):
+ pass
+ucontext64._fields_ = [
+ ('uc_onstack', c_int),
+ ('uc_sigmask', __darwin_sigset_t),
+ ('uc_stack', __darwin_stack_t),
+ ('uc_link', POINTER(ucontext64)),
+ ('uc_mcsize', __darwin_size_t),
+ ('uc_mcontext64', __darwin_mcontext64_t),
+]
+assert sizeof(ucontext64) == 32, sizeof(ucontext64)
+assert alignment(ucontext64) == 4, alignment(ucontext64)
+__darwin_ucontext64_t = ucontext64
+class timeval(Structure):
+ pass
+timeval._fields_ = [
+ ('tv_sec', __darwin_time_t),
+ ('tv_usec', __darwin_suseconds_t),
+]
+assert sizeof(timeval) == 8, sizeof(timeval)
+assert alignment(timeval) == 4, alignment(timeval)
+rlim_t = __int64_t
+class rusage(Structure):
+ pass
+rusage._fields_ = [
+ ('ru_utime', timeval),
+ ('ru_stime', timeval),
+ ('ru_maxrss', c_long),
+ ('ru_ixrss', c_long),
+ ('ru_idrss', c_long),
+ ('ru_isrss', c_long),
+ ('ru_minflt', c_long),
+ ('ru_majflt', c_long),
+ ('ru_nswap', c_long),
+ ('ru_inblock', c_long),
+ ('ru_oublock', c_long),
+ ('ru_msgsnd', c_long),
+ ('ru_msgrcv', c_long),
+ ('ru_nsignals', c_long),
+ ('ru_nvcsw', c_long),
+ ('ru_nivcsw', c_long),
+]
+assert sizeof(rusage) == 72, sizeof(rusage)
+assert alignment(rusage) == 4, alignment(rusage)
+class rlimit(Structure):
+ pass
+rlimit._pack_ = 4
+rlimit._fields_ = [
+ ('rlim_cur', rlim_t),
+ ('rlim_max', rlim_t),
+]
+assert sizeof(rlimit) == 16, sizeof(rlimit)
+assert alignment(rlimit) == 4, alignment(rlimit)
+mcontext_t = __darwin_mcontext_t
+mcontext64_t = __darwin_mcontext64_t
+sigset_t = __darwin_sigset_t
+ucontext_t = __darwin_ucontext_t
+ucontext64_t = __darwin_ucontext64_t
+class sigval(Union):
+ pass
+sigval._fields_ = [
+ ('sival_int', c_int),
+ ('sival_ptr', c_void_p),
+]
+assert sizeof(sigval) == 4, sizeof(sigval)
+assert alignment(sigval) == 4, alignment(sigval)
+class sigevent(Structure):
+ pass
+pthread_attr_t = __darwin_pthread_attr_t
+sigevent._fields_ = [
+ ('sigev_notify', c_int),
+ ('sigev_signo', c_int),
+ ('sigev_value', sigval),
+ ('sigev_notify_function', CFUNCTYPE(None, sigval)),
+ ('sigev_notify_attributes', POINTER(pthread_attr_t)),
+]
+assert sizeof(sigevent) == 20, sizeof(sigevent)
+assert alignment(sigevent) == 4, alignment(sigevent)
+class __siginfo(Structure):
+ pass
+pid_t = __darwin_pid_t
+uid_t = __darwin_uid_t
+__siginfo._fields_ = [
+ ('si_signo', c_int),
+ ('si_errno', c_int),
+ ('si_code', c_int),
+ ('si_pid', pid_t),
+ ('si_uid', uid_t),
+ ('si_status', c_int),
+ ('si_addr', c_void_p),
+ ('si_value', sigval),
+ ('si_band', c_long),
+ ('pad', c_ulong * 7),
+]
+assert sizeof(__siginfo) == 64, sizeof(__siginfo)
+assert alignment(__siginfo) == 4, alignment(__siginfo)
+siginfo_t = __siginfo
+class __sigaction_u(Union):
+ pass
+__sigaction_u._fields_ = [
+ ('__sa_handler', CFUNCTYPE(None, c_int)),
+ ('__sa_sigaction', CFUNCTYPE(None, c_int, POINTER(__siginfo), c_void_p)),
+]
+assert sizeof(__sigaction_u) == 4, sizeof(__sigaction_u)
+assert alignment(__sigaction_u) == 4, alignment(__sigaction_u)
+class __sigaction(Structure):
+ pass
+__sigaction._fields_ = [
+ ('__sigaction_u', __sigaction_u),
+ ('sa_tramp', CFUNCTYPE(None, c_void_p, c_int, c_int, POINTER(siginfo_t), c_void_p)),
+ ('sa_mask', sigset_t),
+ ('sa_flags', c_int),
+]
+assert sizeof(__sigaction) == 16, sizeof(__sigaction)
+assert alignment(__sigaction) == 4, alignment(__sigaction)
+class sigaction(Structure):
+ pass
+sigaction._fields_ = [
+ ('__sigaction_u', __sigaction_u),
+ ('sa_mask', sigset_t),
+ ('sa_flags', c_int),
+]
+assert sizeof(sigaction) == 12, sizeof(sigaction)
+assert alignment(sigaction) == 4, alignment(sigaction)
+sig_t = CFUNCTYPE(None, c_int)
+stack_t = __darwin_stack_t
+class sigvec(Structure):
+ pass
+sigvec._fields_ = [
+ ('sv_handler', CFUNCTYPE(None, c_int)),
+ ('sv_mask', c_int),
+ ('sv_flags', c_int),
+]
+assert sizeof(sigvec) == 12, sizeof(sigvec)
+assert alignment(sigvec) == 4, alignment(sigvec)
+class sigstack(Structure):
+ pass
+sigstack._fields_ = [
+ ('ss_sp', STRING),
+ ('ss_onstack', c_int),
+]
+assert sizeof(sigstack) == 8, sizeof(sigstack)
+assert alignment(sigstack) == 4, alignment(sigstack)
+class ostat(Structure):
+ pass
+ino_t = __darwin_ino_t
+mode_t = __darwin_mode_t
+nlink_t = __uint16_t
+class timespec(Structure):
+ pass
+time_t = __darwin_time_t
+timespec._fields_ = [
+ ('tv_sec', time_t),
+ ('tv_nsec', c_long),
+]
+assert sizeof(timespec) == 8, sizeof(timespec)
+assert alignment(timespec) == 4, alignment(timespec)
+ostat._fields_ = [
+ ('st_dev', __uint16_t),
+ ('st_ino', ino_t),
+ ('st_mode', mode_t),
+ ('st_nlink', nlink_t),
+ ('st_uid', __uint16_t),
+ ('st_gid', __uint16_t),
+ ('st_rdev', __uint16_t),
+ ('st_size', __int32_t),
+ ('st_atimespec', timespec),
+ ('st_mtimespec', timespec),
+ ('st_ctimespec', timespec),
+ ('st_blksize', __int32_t),
+ ('st_blocks', __int32_t),
+ ('st_flags', __uint32_t),
+ ('st_gen', __uint32_t),
+]
+assert sizeof(ostat) == 64, sizeof(ostat)
+assert alignment(ostat) == 4, alignment(ostat)
+class stat(Structure):
+ pass
+dev_t = __darwin_dev_t
+gid_t = __darwin_gid_t
+off_t = __darwin_off_t
+blkcnt_t = __darwin_blkcnt_t
+blksize_t = __darwin_blksize_t
+stat._pack_ = 4
+stat._fields_ = [
+ ('st_dev', dev_t),
+ ('st_ino', ino_t),
+ ('st_mode', mode_t),
+ ('st_nlink', nlink_t),
+ ('st_uid', uid_t),
+ ('st_gid', gid_t),
+ ('st_rdev', dev_t),
+ ('st_atimespec', timespec),
+ ('st_mtimespec', timespec),
+ ('st_ctimespec', timespec),
+ ('st_size', off_t),
+ ('st_blocks', blkcnt_t),
+ ('st_blksize', blksize_t),
+ ('st_flags', __uint32_t),
+ ('st_gen', __uint32_t),
+ ('st_lspare', __int32_t),
+ ('st_qspare', __int64_t * 2),
+]
+assert sizeof(stat) == 96, sizeof(stat)
+assert alignment(stat) == 4, alignment(stat)
+class _filesec(Structure):
+ pass
+filesec_t = POINTER(_filesec)
+tcflag_t = c_ulong
+cc_t = c_ubyte
+speed_t = c_long
+class termios(Structure):
+ pass
+termios._fields_ = [
+ ('c_iflag', tcflag_t),
+ ('c_oflag', tcflag_t),
+ ('c_cflag', tcflag_t),
+ ('c_lflag', tcflag_t),
+ ('c_cc', cc_t * 20),
+ ('c_ispeed', speed_t),
+ ('c_ospeed', speed_t),
+]
+assert sizeof(termios) == 44, sizeof(termios)
+assert alignment(termios) == 4, alignment(termios)
+class itimerval(Structure):
+ pass
+itimerval._fields_ = [
+ ('it_interval', timeval),
+ ('it_value', timeval),
+]
+assert sizeof(itimerval) == 16, sizeof(itimerval)
+assert alignment(itimerval) == 4, alignment(itimerval)
+class timezone(Structure):
+ pass
+timezone._fields_ = [
+ ('tz_minuteswest', c_int),
+ ('tz_dsttime', c_int),
+]
+assert sizeof(timezone) == 8, sizeof(timezone)
+assert alignment(timezone) == 4, alignment(timezone)
+class clockinfo(Structure):
+ pass
+clockinfo._fields_ = [
+ ('hz', c_int),
+ ('tick', c_int),
+ ('tickadj', c_int),
+ ('stathz', c_int),
+ ('profhz', c_int),
+]
+assert sizeof(clockinfo) == 20, sizeof(clockinfo)
+assert alignment(clockinfo) == 4, alignment(clockinfo)
+class winsize(Structure):
+ pass
+winsize._fields_ = [
+ ('ws_row', c_ushort),
+ ('ws_col', c_ushort),
+ ('ws_xpixel', c_ushort),
+ ('ws_ypixel', c_ushort),
+]
+assert sizeof(winsize) == 8, sizeof(winsize)
+assert alignment(winsize) == 2, alignment(winsize)
+u_char = c_ubyte
+u_short = c_ushort
+u_int = c_uint
+u_long = c_ulong
+ushort = c_ushort
+uint = c_uint
+u_quad_t = u_int64_t
+quad_t = int64_t
+qaddr_t = POINTER(quad_t)
+caddr_t = STRING
+daddr_t = int32_t
+fixpt_t = u_int32_t
+in_addr_t = __uint32_t
+in_port_t = __uint16_t
+key_t = __int32_t
+id_t = __darwin_id_t
+segsz_t = int32_t
+swblk_t = int32_t
+clock_t = __darwin_clock_t
+ssize_t = __darwin_ssize_t
+useconds_t = __darwin_useconds_t
+suseconds_t = __darwin_suseconds_t
+fd_mask = __int32_t
+class fd_set(Structure):
+ pass
+fd_set._fields_ = [
+ ('fds_bits', __int32_t * 32),
+]
+assert sizeof(fd_set) == 128, sizeof(fd_set)
+assert alignment(fd_set) == 4, alignment(fd_set)
+pthread_cond_t = __darwin_pthread_cond_t
+pthread_condattr_t = __darwin_pthread_condattr_t
+pthread_mutex_t = __darwin_pthread_mutex_t
+pthread_mutexattr_t = __darwin_pthread_mutexattr_t
+pthread_once_t = __darwin_pthread_once_t
+pthread_rwlock_t = __darwin_pthread_rwlock_t
+pthread_rwlockattr_t = __darwin_pthread_rwlockattr_t
+pthread_t = __darwin_pthread_t
+pthread_key_t = __darwin_pthread_key_t
+fsblkcnt_t = __darwin_fsblkcnt_t
+fsfilcnt_t = __darwin_fsfilcnt_t
+
+# values for enumeration 'idtype_t'
+idtype_t = c_int # enum
+class wait(Union):
+ pass
+class N4wait3DOLLAR_3E(Structure):
+ pass
+N4wait3DOLLAR_3E._fields_ = [
+ ('w_Termsig', c_uint, 7),
+ ('w_Coredump', c_uint, 1),
+ ('w_Retcode', c_uint, 8),
+ ('w_Filler', c_uint, 16),
+]
+assert sizeof(N4wait3DOLLAR_3E) == 4, sizeof(N4wait3DOLLAR_3E)
+assert alignment(N4wait3DOLLAR_3E) == 4, alignment(N4wait3DOLLAR_3E)
+class N4wait3DOLLAR_4E(Structure):
+ pass
+N4wait3DOLLAR_4E._fields_ = [
+ ('w_Stopval', c_uint, 8),
+ ('w_Stopsig', c_uint, 8),
+ ('w_Filler', c_uint, 16),
+]
+assert sizeof(N4wait3DOLLAR_4E) == 4, sizeof(N4wait3DOLLAR_4E)
+assert alignment(N4wait3DOLLAR_4E) == 4, alignment(N4wait3DOLLAR_4E)
+wait._fields_ = [
+ ('w_status', c_int),
+ ('w_T', N4wait3DOLLAR_3E),
+ ('w_S', N4wait3DOLLAR_4E),
+]
+assert sizeof(wait) == 4, sizeof(wait)
+assert alignment(wait) == 4, alignment(wait)
+class tm(Structure):
+ pass
+tm._fields_ = [
+ ('tm_sec', c_int),
+ ('tm_min', c_int),
+ ('tm_hour', c_int),
+ ('tm_mday', c_int),
+ ('tm_mon', c_int),
+ ('tm_year', c_int),
+ ('tm_wday', c_int),
+ ('tm_yday', c_int),
+ ('tm_isdst', c_int),
+ ('tm_gmtoff', c_long),
+ ('tm_zone', STRING),
+]
+assert sizeof(tm) == 44, sizeof(tm)
+assert alignment(tm) == 4, alignment(tm)
+uint8_t = c_ubyte
+uint16_t = c_ushort
+uint32_t = c_uint
+uint64_t = c_ulonglong
+int_least8_t = int8_t
+int_least16_t = int16_t
+int_least32_t = int32_t
+int_least64_t = int64_t
+uint_least8_t = uint8_t
+uint_least16_t = uint16_t
+uint_least32_t = uint32_t
+uint_least64_t = uint64_t
+int_fast8_t = int8_t
+int_fast16_t = int16_t
+int_fast32_t = int32_t
+int_fast64_t = int64_t
+uint_fast8_t = uint8_t
+uint_fast16_t = uint16_t
+uint_fast32_t = uint32_t
+uint_fast64_t = uint64_t
+intmax_t = c_longlong
+uintmax_t = c_ulonglong
+class PyFileObject(Structure):
+ pass
+Py_ssize_t = ssize_t
+class _typeobject(Structure):
+ pass
+class _object(Structure):
+ pass
+PyObject = _object
+PyFileObject._fields_ = [
+ ('ob_refcnt', Py_ssize_t),
+ ('ob_type', POINTER(_typeobject)),
+ ('f_fp', POINTER(FILE)),
+ ('f_name', POINTER(PyObject)),
+ ('f_mode', POINTER(PyObject)),
+ ('f_close', CFUNCTYPE(c_int, POINTER(FILE))),
+ ('f_softspace', c_int),
+ ('f_binary', c_int),
+ ('f_buf', STRING),
+ ('f_bufend', STRING),
+ ('f_bufptr', STRING),
+ ('f_setbuf', STRING),
+ ('f_univ_newline', c_int),
+ ('f_newlinetypes', c_int),
+ ('f_skipnextlf', c_int),
+ ('f_encoding', POINTER(PyObject)),
+ ('weakreflist', POINTER(PyObject)),
+]
+assert sizeof(PyFileObject) == 68, sizeof(PyFileObject)
+assert alignment(PyFileObject) == 4, alignment(PyFileObject)
+_object._fields_ = [
+ ('ob_refcnt', Py_ssize_t),
+ ('ob_type', POINTER(_typeobject)),
+]
+assert sizeof(_object) == 8, sizeof(_object)
+assert alignment(_object) == 4, alignment(_object)
+class PyVarObject(Structure):
+ pass
+PyVarObject._fields_ = [
+ ('ob_refcnt', Py_ssize_t),
+ ('ob_type', POINTER(_typeobject)),
+ ('ob_size', Py_ssize_t),
+]
+assert sizeof(PyVarObject) == 12, sizeof(PyVarObject)
+assert alignment(PyVarObject) == 4, alignment(PyVarObject)
+unaryfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject))
+binaryfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+ternaryfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+inquiry = CFUNCTYPE(c_int, POINTER(PyObject))
+lenfunc = CFUNCTYPE(Py_ssize_t, POINTER(PyObject))
+coercion = CFUNCTYPE(c_int, POINTER(POINTER(PyObject)), POINTER(POINTER(PyObject)))
+intargfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), c_int)
+intintargfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), c_int, c_int)
+ssizeargfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), c_long)
+ssizessizeargfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), c_long, c_long)
+intobjargproc = CFUNCTYPE(c_int, POINTER(PyObject), c_int, POINTER(PyObject))
+intintobjargproc = CFUNCTYPE(c_int, POINTER(PyObject), c_int, c_int, POINTER(PyObject))
+ssizeobjargproc = CFUNCTYPE(c_int, POINTER(PyObject), c_long, POINTER(PyObject))
+ssizessizeobjargproc = CFUNCTYPE(c_int, POINTER(PyObject), c_long, c_long, POINTER(PyObject))
+objobjargproc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+getreadbufferproc = CFUNCTYPE(c_int, POINTER(PyObject), c_int, POINTER(c_void_p))
+getwritebufferproc = CFUNCTYPE(c_int, POINTER(PyObject), c_int, POINTER(c_void_p))
+getsegcountproc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(c_int))
+getcharbufferproc = CFUNCTYPE(c_int, POINTER(PyObject), c_int, POINTER(STRING))
+readbufferproc = CFUNCTYPE(Py_ssize_t, POINTER(PyObject), c_long, POINTER(c_void_p))
+writebufferproc = CFUNCTYPE(Py_ssize_t, POINTER(PyObject), c_long, POINTER(c_void_p))
+segcountproc = CFUNCTYPE(Py_ssize_t, POINTER(PyObject), POINTER(Py_ssize_t))
+charbufferproc = CFUNCTYPE(Py_ssize_t, POINTER(PyObject), c_long, POINTER(STRING))
+objobjproc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(PyObject))
+visitproc = CFUNCTYPE(c_int, POINTER(PyObject), c_void_p)
+traverseproc = CFUNCTYPE(c_int, POINTER(PyObject), CFUNCTYPE(c_int, POINTER(PyObject), c_void_p), c_void_p)
+class PyNumberMethods(Structure):
+ pass
+PyNumberMethods._fields_ = [
+ ('nb_add', binaryfunc),
+ ('nb_subtract', binaryfunc),
+ ('nb_multiply', binaryfunc),
+ ('nb_divide', binaryfunc),
+ ('nb_remainder', binaryfunc),
+ ('nb_divmod', binaryfunc),
+ ('nb_power', ternaryfunc),
+ ('nb_negative', unaryfunc),
+ ('nb_positive', unaryfunc),
+ ('nb_absolute', unaryfunc),
+ ('nb_nonzero', inquiry),
+ ('nb_invert', unaryfunc),
+ ('nb_lshift', binaryfunc),
+ ('nb_rshift', binaryfunc),
+ ('nb_and', binaryfunc),
+ ('nb_xor', binaryfunc),
+ ('nb_or', binaryfunc),
+ ('nb_coerce', coercion),
+ ('nb_int', unaryfunc),
+ ('nb_long', unaryfunc),
+ ('nb_float', unaryfunc),
+ ('nb_oct', unaryfunc),
+ ('nb_hex', unaryfunc),
+ ('nb_inplace_add', binaryfunc),
+ ('nb_inplace_subtract', binaryfunc),
+ ('nb_inplace_multiply', binaryfunc),
+ ('nb_inplace_divide', binaryfunc),
+ ('nb_inplace_remainder', binaryfunc),
+ ('nb_inplace_power', ternaryfunc),
+ ('nb_inplace_lshift', binaryfunc),
+ ('nb_inplace_rshift', binaryfunc),
+ ('nb_inplace_and', binaryfunc),
+ ('nb_inplace_xor', binaryfunc),
+ ('nb_inplace_or', binaryfunc),
+ ('nb_floor_divide', binaryfunc),
+ ('nb_true_divide', binaryfunc),
+ ('nb_inplace_floor_divide', binaryfunc),
+ ('nb_inplace_true_divide', binaryfunc),
+ ('nb_index', lenfunc),
+]
+assert sizeof(PyNumberMethods) == 156, sizeof(PyNumberMethods)
+assert alignment(PyNumberMethods) == 4, alignment(PyNumberMethods)
+class PySequenceMethods(Structure):
+ pass
+PySequenceMethods._fields_ = [
+ ('sq_length', lenfunc),
+ ('sq_concat', binaryfunc),
+ ('sq_repeat', ssizeargfunc),
+ ('sq_item', ssizeargfunc),
+ ('sq_slice', ssizessizeargfunc),
+ ('sq_ass_item', ssizeobjargproc),
+ ('sq_ass_slice', ssizessizeobjargproc),
+ ('sq_contains', objobjproc),
+ ('sq_inplace_concat', binaryfunc),
+ ('sq_inplace_repeat', ssizeargfunc),
+]
+assert sizeof(PySequenceMethods) == 40, sizeof(PySequenceMethods)
+assert alignment(PySequenceMethods) == 4, alignment(PySequenceMethods)
+class PyMappingMethods(Structure):
+ pass
+PyMappingMethods._fields_ = [
+ ('mp_length', lenfunc),
+ ('mp_subscript', binaryfunc),
+ ('mp_ass_subscript', objobjargproc),
+]
+assert sizeof(PyMappingMethods) == 12, sizeof(PyMappingMethods)
+assert alignment(PyMappingMethods) == 4, alignment(PyMappingMethods)
+class PyBufferProcs(Structure):
+ pass
+PyBufferProcs._fields_ = [
+ ('bf_getreadbuffer', readbufferproc),
+ ('bf_getwritebuffer', writebufferproc),
+ ('bf_getsegcount', segcountproc),
+ ('bf_getcharbuffer', charbufferproc),
+]
+assert sizeof(PyBufferProcs) == 16, sizeof(PyBufferProcs)
+assert alignment(PyBufferProcs) == 4, alignment(PyBufferProcs)
+freefunc = CFUNCTYPE(None, c_void_p)
+destructor = CFUNCTYPE(None, POINTER(PyObject))
+printfunc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(FILE), c_int)
+getattrfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), STRING)
+getattrofunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+setattrfunc = CFUNCTYPE(c_int, POINTER(PyObject), STRING, POINTER(PyObject))
+setattrofunc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+cmpfunc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(PyObject))
+reprfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject))
+hashfunc = CFUNCTYPE(c_long, POINTER(PyObject))
+richcmpfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), POINTER(PyObject), c_int)
+getiterfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject))
+iternextfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject))
+descrgetfunc = CFUNCTYPE(POINTER(PyObject), POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+descrsetfunc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+initproc = CFUNCTYPE(c_int, POINTER(PyObject), POINTER(PyObject), POINTER(PyObject))
+newfunc = CFUNCTYPE(POINTER(PyObject), POINTER(_typeobject), POINTER(PyObject), POINTER(PyObject))
+allocfunc = CFUNCTYPE(POINTER(PyObject), POINTER(_typeobject), c_long)
+class PyMethodDef(Structure):
+ pass
+class PyMemberDef(Structure):
+ pass
+class PyGetSetDef(Structure):
+ pass
+_typeobject._fields_ = [
+ ('ob_refcnt', Py_ssize_t),
+ ('ob_type', POINTER(_typeobject)),
+ ('ob_size', Py_ssize_t),
+ ('tp_name', STRING),
+ ('tp_basicsize', Py_ssize_t),
+ ('tp_itemsize', Py_ssize_t),
+ ('tp_dealloc', destructor),
+ ('tp_print', printfunc),
+ ('tp_getattr', getattrfunc),
+ ('tp_setattr', setattrfunc),
+ ('tp_compare', cmpfunc),
+ ('tp_repr', reprfunc),
+ ('tp_as_number', POINTER(PyNumberMethods)),
+ ('tp_as_sequence', POINTER(PySequenceMethods)),
+ ('tp_as_mapping', POINTER(PyMappingMethods)),
+ ('tp_hash', hashfunc),
+ ('tp_call', ternaryfunc),
+ ('tp_str', reprfunc),
+ ('tp_getattro', getattrofunc),
+ ('tp_setattro', setattrofunc),
+ ('tp_as_buffer', POINTER(PyBufferProcs)),
+ ('tp_flags', c_long),
+ ('tp_doc', STRING),
+ ('tp_traverse', traverseproc),
+ ('tp_clear', inquiry),
+ ('tp_richcompare', richcmpfunc),
+ ('tp_weaklistoffset', Py_ssize_t),
+ ('tp_iter', getiterfunc),
+ ('tp_iternext', iternextfunc),
+ ('tp_methods', POINTER(PyMethodDef)),
+ ('tp_members', POINTER(PyMemberDef)),
+ ('tp_getset', POINTER(PyGetSetDef)),
+ ('tp_base', POINTER(_typeobject)),
+ ('tp_dict', POINTER(PyObject)),
+ ('tp_descr_get', descrgetfunc),
+ ('tp_descr_set', descrsetfunc),
+ ('tp_dictoffset', Py_ssize_t),
+ ('tp_init', initproc),
+ ('tp_alloc', allocfunc),
+ ('tp_new', newfunc),
+ ('tp_free', freefunc),
+ ('tp_is_gc', inquiry),
+ ('tp_bases', POINTER(PyObject)),
+ ('tp_mro', POINTER(PyObject)),
+ ('tp_cache', POINTER(PyObject)),
+ ('tp_subclasses', POINTER(PyObject)),
+ ('tp_weaklist', POINTER(PyObject)),
+ ('tp_del', destructor),
+]
+assert sizeof(_typeobject) == 192, sizeof(_typeobject)
+assert alignment(_typeobject) == 4, alignment(_typeobject)
+PyTypeObject = _typeobject
+class _heaptypeobject(Structure):
+ pass
+_heaptypeobject._fields_ = [
+ ('ht_type', PyTypeObject),
+ ('as_number', PyNumberMethods),
+ ('as_mapping', PyMappingMethods),
+ ('as_sequence', PySequenceMethods),
+ ('as_buffer', PyBufferProcs),
+ ('ht_name', POINTER(PyObject)),
+ ('ht_slots', POINTER(PyObject)),
+]
+assert sizeof(_heaptypeobject) == 424, sizeof(_heaptypeobject)
+assert alignment(_heaptypeobject) == 4, alignment(_heaptypeobject)
+PyHeapTypeObject = _heaptypeobject
+Py_uintptr_t = c_uint
+Py_intptr_t = c_int
+__all__ = ['__uint16_t', 'PyMethodDef', 'objobjargproc', '__int16_t',
+ 'unaryfunc', '__darwin_pthread_condattr_t',
+ 'readbufferproc', '__darwin_id_t', 'key_t',
+ '__darwin_time_t', 'ucontext64_t', '__darwin_nl_item',
+ 'pthread_once_t', '_opaque_pthread_condattr_t', 'FILE',
+ 'pthread_mutexattr_t', 'size_t', 'Py_uintptr_t',
+ '_FP_ZERO', '_FP_SUPERNORMAL', 'cmpfunc', '__uint32_t',
+ 'mcontext_t', 'PySequenceMethods', 'uint8_t', 'fpos_t',
+ 'P_PGID', 'qaddr_t', '__darwin_gid_t', 'blkcnt_t',
+ 'uint_least16_t', '__darwin_dev_t', 'time_t', '_FP_NORMAL',
+ 'allocfunc', 'getiterfunc', 'int32_t',
+ '__darwin_fsfilcnt_t', 'lenfunc', 'intptr_t',
+ 'uint_least64_t', 'blksize_t', 'user_addr_t', 'PyObject',
+ 'int_least32_t', 'sigaltstack', 'Py_intptr_t', 'ostat',
+ '__darwin_pthread_t', 'u_char', 'fixpt_t', 'uid_t',
+ 'u_int64_t', 'u_int16_t', 'register_t',
+ '__darwin_ucontext_t', 'cc_t', 'in_port_t', 'ternaryfunc',
+ '__darwin_ssize_t', 'descrsetfunc', '__darwin_mcontext_t',
+ '__darwin_sigset_t', 'ct_rune_t', 'uint16_t',
+ '__darwin_ptrdiff_t', 'float_t', 'int_fast32_t', 'va_list',
+ 'uint_fast16_t', 'sigset_t', '__int32_t', 'fd_mask',
+ 'binaryfunc', 'fsfilcnt_t', 'ucontext', 'ssizeobjargproc',
+ 'uint_fast32_t', 'freefunc', 'tm', '__uint64_t', 'mode_t',
+ 'timespec', '__darwin_suseconds_t', 'PyNumberMethods',
+ '__sigaction', 'sigevent', 'user_ulong_t', 'user_ssize_t',
+ 'syscall_arg_t', 'reprfunc', 'int16_t', 'getattrofunc',
+ 'clock_t', 'ssizeargfunc', 'ssizessizeargfunc',
+ '__darwin_socklen_t', '__darwin_intptr_t', 'rune_t',
+ '__darwin_va_list', 'caddr_t', 'siginfo_t', 'ucontext_t',
+ '__sbuf', 'setattrfunc', 'coercion', 'int_least8_t',
+ 'getsegcountproc', 'N4wait3DOLLAR_4E', 'div_t', 'newfunc',
+ 'intintobjargproc', 'id_t', '__darwin_blksize_t', 'ldiv_t',
+ 'int_least16_t', '__darwin_wctrans_t', 'uint_least8_t',
+ 'u_int32_t', 'pthread_rwlock_t', 'charbufferproc',
+ '__darwin_wchar_t', 'sigval', 'PyGetSetDef',
+ 'intobjargproc', 'P_PID', 'sigaction',
+ '__darwin_natural_t', 'sig_t', '__darwin_blkcnt_t',
+ 'u_int', '_opaque_pthread_cond_t', '__darwin_size_t',
+ 'ssizessizeobjargproc', 'segsz_t', 'ushort',
+ '__darwin_ct_rune_t', 'pthread_t', '__darwin_ino_t',
+ 'pthread_attr_t', 'fd_set', '__darwin_useconds_t',
+ '__darwin_mcontext64_t', 'ino_t', '__darwin_clock_t',
+ 'uint_fast8_t', '_typeobject', '__darwin_pthread_key_t',
+ 'getwritebufferproc', 'traverseproc',
+ '__darwin_pthread_handler_rec', 'double_t',
+ '__darwin_pthread_mutex_t', 'initproc', 'speed_t',
+ '_object', '__darwin_ucontext64_t', 'getreadbufferproc',
+ 'rlim_t', 'hashfunc', '__darwin_fsblkcnt_t',
+ '__darwin_rune_t', 'fsblkcnt_t', 'u_quad_t',
+ '_opaque_pthread_rwlockattr_t', 'sigvec',
+ '_opaque_pthread_mutexattr_t', 'clockinfo',
+ '__darwin_pthread_rwlock_t', 'pthread_condattr_t',
+ 'destructor', 'rlimit', '__darwin_pthread_mutexattr_t',
+ 'daddr_t', '__darwin_pthread_once_t', 'stack_t',
+ '__darwin_mode_t', 'uint_least32_t', 'wait', 'OSBigEndian',
+ '__mbstate_t', 'uintptr_t', '__darwin_mach_port_t',
+ '__uint8_t', '__darwin_uid_t', 'itimerval', '__int8_t',
+ 'PyMemberDef', '_opaque_pthread_mutex_t', 'int8_t',
+ '__darwin_uuid_t', '_opaque_pthread_attr_t', 'uintmax_t',
+ 'intargfunc', 'off_t', 'gid_t', 'sigstack', 'filesec_t',
+ 'mcontext', 'int_least64_t', '_FP_NAN', 'pid_t',
+ 'visitproc', 'N4wait3DOLLAR_3E', 'quad_t', 'uint_fast64_t',
+ 'u_long', 'intmax_t', 'sigcontext', 'swblk_t', '__siginfo',
+ 'winsize', '__darwin_mbstate_t', 'useconds_t',
+ 'richcmpfunc', 'pthread_key_t', 'uint64_t', 'u_int8_t',
+ 'writebufferproc', 'pthread_cond_t', '_FP_SUBNORMAL',
+ '__darwin_wctype_t', '_opaque_pthread_once_t',
+ 'OSLittleEndian', 'int_fast16_t', 'int64_t',
+ '_FP_INFINITE', 'timezone', '_heaptypeobject', '__sFILE',
+ 'ucontext64', 'sig_atomic_t', 'u_short', 'nlink_t',
+ 'PyVarObject', 'mcontext64', 'lldiv_t',
+ '__darwin_pthread_rwlockattr_t', 'descrgetfunc',
+ 'OSUnknownByteOrder', 'timeval', '__darwin_stack_t',
+ 'inquiry', 'printfunc', 'int_fast64_t', 'int_fast8_t',
+ 'PyTypeObject', '__sFILEX', 'stat', 'getcharbufferproc',
+ 'uint32_t', 'intintargfunc', 'PyBufferProcs',
+ '__darwin_pthread_attr_t', '__darwin_mach_port_name_t',
+ 'user_time_t', 'PyFileObject', 'uint', 'termios',
+ '__darwin_wint_t', '__darwin_pthread_cond_t', 'objobjproc',
+ 'user_size_t', 'pthread_rwlockattr_t', 'rusage',
+ 'PyMappingMethods', 'setattrofunc', 'idtype_t',
+ 'segcountproc', '__darwin_pid_t', '__sigaction_u',
+ '_opaque_pthread_rwlock_t', 'in_addr_t', '__darwin_off_t',
+ '_opaque_pthread_t', 'tcflag_t', 'iternextfunc', 'P_ALL',
+ 'pthread_mutex_t', '__int64_t', 'getattrfunc', 'ssize_t',
+ 'mcontext64_t', 'user_long_t', 'dev_t', '_filesec',
+ 'suseconds_t', 'PyHeapTypeObject', 'Py_ssize_t']
Added: pypy/dist/pypy/module/bz2/interp_bz2.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/interp_bz2.py Fri Aug 4 16:10:40 2006
@@ -0,0 +1,187 @@
+from pypy.rpython.rctypes.tool import ctypes_platform
+import pypy.rpython.rctypes.implementation # this defines rctypes magic
+from pypy.rpython.rctypes.aerrno import geterrno
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app
+from ctypes import *
+import ctypes.util
+
+from bzlib import bz_stream, BZFILE, FILE
+from fileobject import PyFileObject
+
+libbz2 = cdll.LoadLibrary(ctypes.util.find_library("bz2"))
+
+c_void = None
+
+class CConfig:
+ _header_ = """
+ #include <stdio.h>
+ #include <sys/types.h>
+ """
+ off_t = ctypes_platform.SimpleType("off_t", c_longlong)
+ size_t = ctypes_platform.SimpleType("size_t", c_ulong)
+ BUFSIZ = ctypes_platform.ConstantInteger("BUFSIZ")
+
+constants = {}
+constant_names = ['BUFSIZ', 'BZ_RUN', 'BZ_FLUSH', 'BZ_FINISH', 'BZ_OK',
+ 'BZ_RUN_OK', 'BZ_FLUSH_OK', 'BZ_FINISH_OK', 'BZ_STREAM_END',
+ 'BZ_SEQUENCE_ERROR', 'BZ_PARAM_ERROR', 'BZ_MEM_ERROR', 'BZ_DATA_ERROR',
+ 'BZ_DATA_ERROR_MAGIC', 'BZ_IO_ERROR', 'BZ_UNEXPECTED_EOF',
+ 'BZ_OUTBUFF_FULL', 'BZ_CONFIG_ERROR']
+for name in constant_names:
+ setattr(CConfig, name, ctypes_platform.DefinedConstantInteger(name))
+
+class cConfig:
+ pass
+cConfig.__dict__.update(ctypes_platform.configure(CConfig))
+
+for name in constant_names:
+ value = getattr(cConfig, name)
+ if value is not None:
+ constants[name] = value
+locals().update(constants)
+
+off_t = cConfig.off_t
+BZ_OK = cConfig.BZ_OK
+BZ_STREAM_END = cConfig.BZ_STREAM_END
+BZ_CONFIG_ERROR = cConfig.BZ_CONFIG_ERROR
+BZ_PARAM_ERROR = cConfig.BZ_PARAM_ERROR
+BZ_DATA_ERROR = cConfig.BZ_DATA_ERROR
+BZ_DATA_ERROR_MAGIC = cConfig.BZ_DATA_ERROR_MAGIC
+BZ_IO_ERROR = cConfig.BZ_IO_ERROR
+BZ_MEM_ERROR = cConfig.BZ_MEM_ERROR
+BZ_UNEXPECTED_EOF = cConfig.BZ_UNEXPECTED_EOF
+BZ_SEQUENCE_ERROR = cConfig.BZ_SEQUENCE_ERROR
+
+# modes
+MODE_CLOSED = 0
+MODE_READ = 1
+MODE_READ_EOF = 2
+MODE_WRITE = 3
+
+# bits in f_newlinetypes
+NEWLINE_UNKNOWN = 0 # No newline seen, yet
+NEWLINE_CR = 1 # \r newline seen
+NEWLINE_LF = 2 # \n newline seen
+NEWLINE_CRLF = 4 # \r\n newline seen
+
+pythonapi.PyFile_FromString.argtypes = [c_char_p, c_char_p]
+pythonapi.PyFile_FromString.restype = POINTER(PyFileObject)
+pythonapi.PyFile_SetBufSize.argtypes = [POINTER(PyFileObject), c_int]
+pythonapi.PyFile_SetBufSize.restype = c_void
+pythonapi.PyFile_AsFile.argtypes = [POINTER(PyFileObject)]
+pythonapi.PyFile_AsFile.restype = POINTER(FILE)
+libbz2.BZ2_bzReadOpen.argtypes = [POINTER(c_int), POINTER(FILE), c_int,
+ c_int, c_void_p, c_int]
+libbz2.BZ2_bzReadOpen.restype = POINTER(BZFILE)
+libbz2.BZ2_bzWriteOpen.argtypes = [POINTER(c_int), POINTER(FILE), c_int,
+ c_int, c_int]
+libbz2.BZ2_bzWriteOpen.restype = POINTER(BZFILE)
+
+def _catch_bz2_error(space, bzerror):
+ if BZ_CONFIG_ERROR and bzerror == BZ_CONFIG_ERROR:
+ raise OperationError(space.w_SystemError,
+ space.wrap("the bz2 library was not compiled correctly"))
+ if bzerror == BZ_PARAM_ERROR:
+ raise OperationError(space.w_SystemError,
+ space.wrap("the bz2 library has received wrong parameters"))
+ elif bzerror == BZ_MEM_ERROR:
+ raise OperationError(space.w_MemoryError, space.wrap(""))
+ elif bzerror in (BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC):
+ raise OperationError(space.w_IOError, space.wrap("invalid data stream"))
+ elif bzerror == BZ_IO_ERROR:
+ raise OperationError(space.w_IOError, space.wrap("unknown IO error"))
+ elif bzerror == BZ_UNEXPECTED_EOF:
+ raise OperationError(space.w_EOFError,
+ space.wrap(
+ "compressed file ended before the logical end-of-stream was detected"))
+ elif bzerror == BZ_SEQUENCE_ERROR:
+ raise OperationError(space.w_RuntimeError,
+ space.wrap("wrong sequence of bz2 library commands used"))
+
+
+class _BZ2File(Wrappable):
+ def __init__(self, space, filename, mode='r', buffering=-1, compresslevel=9):
+ self.space = space
+
+ self.f_buf = c_char_p() # allocated readahead buffer
+ self.f_bufend = c_char_p() # points after last occupied position
+ self.f_bufptr = c_char_p() # current buffer position
+
+ self.f_softspace = 0 # flag used by print command
+
+ self.f_univ_newline = False # handle any newline convention
+ self.f_newlinetypes = 0 # types of newlines seen
+ self.f_skipnextlf = 0 # skip next \n
+
+ self.mode = 0
+ self.pos = 0
+ self.size = 0
+
+ self._init_bz2file(filename, mode, buffering, compresslevel)
+
+ def _init_bz2file(self, filename, mode_, buffering, compresslevel):
+ self.size = -1
+
+ name = filename
+ mode_char = ""
+ mode_list = mode_
+
+ if compresslevel < 1 or compresslevel > 9:
+ raise OperationError(self.space.w_ValueError,
+ self.space.wrap("compresslevel must be between 1 and 9"))
+
+ i = 1
+ for mode in mode_list:
+ error = False
+
+ if mode in ['r', 'w']:
+ if mode_char:
+ error = True
+ mode_char = mode
+ elif mode == 'b':
+ pass
+ elif mode == 'U':
+ self.f_univ_newline = True
+ else:
+ error = True
+
+ if error:
+ raise OperationError(self.space.w_ValueError,
+ self.space.wrap("invalid mode char %s" % mode))
+
+ if mode_char == 0:
+ mode_char = 'r'
+ mode = ('wb', 'rb')[mode_char == 'r']
+
+ # open the file and set the buffer
+ f = pythonapi.PyFile_FromString(name, mode)
+ if not f:
+ raise OperationError(self.space.w_IOError,
+ self.space.wrap("cannot open file %s" % name))
+ pythonapi.PyFile_SetBufSize(f, buffering)
+
+ # store the FILE object
+ self._file = pythonapi.PyFile_AsFile(f)
+
+ bzerror = c_int()
+ if mode_char == 'r':
+ self.fp = libbz2.BZ2_bzReadOpen(byref(bzerror), self._file,
+ 0, 0, None, 0)
+ else:
+ self.fp = libbz2.BZ2_bzWriteOpen(byref(bzerror), self._file,
+ compresslevel, 0, 0)
+
+ if bzerror != BZ_OK:
+ _catch_bz2_error(self.space, bzerror)
+
+ self.mode = (MODE_WRITE, MODE_READ)[mode_char == 'r']
+
+_BZ2File.typedef = TypeDef("_BZ2File")
+
+def BZ2File(space, filename, mode='r', buffering=-1, compresslevel=9):
+ return _BZ2File(space, filename, mode, buffering, compresslevel)
+BZ2File.unwrap_spec = [ObjSpace, str, str, int, int]
+
Added: pypy/dist/pypy/module/bz2/test/test_bz2.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/bz2/test/test_bz2.py Fri Aug 4 16:10:40 2006
@@ -0,0 +1,386 @@
+from py.test import raises, skip
+from pypy.conftest import gettestobjspace
+import os
+
+def teardown_module(mod):
+ if os.path.exists("foo"):
+ os.unlink("foo")
+
+class AppTestBz2:
+ def setup_class(cls):
+ space = gettestobjspace(usemodules=('bz2',))
+ cls.space = space
+
+ def test_creation(self):
+ from bz2 import BZ2File
+
+ raises(ValueError, BZ2File, "foo", mode='w', compresslevel=10)
+ raises(ValueError, BZ2File, "foo", mode='XYZ')
+ raises(ValueError, BZ2File, "foo", mode='ww')
+
+ BZ2File("foo", mode='wU', buffering=0, compresslevel=8)
+ BZ2File("foo", mode='wb')
+ # a large buf size
+ BZ2File("foo", mode='w', buffering=4096)
+
+
+# #!/usr/bin/python
+# from test import test_support
+# from test.test_support import TESTFN
+#
+# import unittest
+# from cStringIO import StringIO
+# import os
+# import popen2
+# import sys
+#
+# import bz2
+# from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
+#
+# has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx", "riscos")
+#
+# class BaseTest(unittest.TestCase):
+# "Base for other testcases."
+# TEXT = 'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:\ndaemon:x:2:2:daemon:/sbin:\nadm:x:3:4:adm:/var/adm:\nlp:x:4:7:lp:/var/spool/lpd:\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:12:mail:/var/spool/mail:\nnews:x:9:13:news:/var/spool/news:\nuucp:x:10:14:uucp:/var/spool/uucp:\noperator:x:11:0:operator:/root:\ngames:x:12:100:games:/usr/games:\ngopher:x:13:30:gopher:/usr/lib/gopher-data:\nftp:x:14:50:FTP User:/var/ftp:/bin/bash\nnobody:x:65534:65534:Nobody:/home:\npostfix:x:100:101:postfix:/var/spool/postfix:\nniemeyer:x:500:500::/home/niemeyer:/bin/bash\npostgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\nmysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\nwww:x:103:104::/var/www:/bin/false\n'
+# DATA = 'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
+# DATA_CRLF = 'BZh91AY&SY\xaez\xbbN\x00\x01H\xdf\x80\x00\x12@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe0@\x01\xbc\xc6`\x86*\x8d=M\xa9\x9a\x86\xd0L@\x0fI\xa6!\xa1\x13\xc8\x88jdi\x8d@\x03@\x1a\x1a\x0c\x0c\x83 \x00\xc4h2\x19\x01\x82D\x84e\t\xe8\x99\x89\x19\x1ah\x00\r\x1a\x11\xaf\x9b\x0fG\xf5(\x1b\x1f?\t\x12\xcf\xb5\xfc\x95E\x00ps\x89\x12^\xa4\xdd\xa2&\x05(\x87\x04\x98\x89u\xe40%\xb6\x19\'\x8c\xc4\x89\xca\x07\x0e\x1b!\x91UIFU%C\x994!DI\xd2\xfa\xf0\xf1N8W\xde\x13A\xf5\x9cr%?\x9f3;I45A\xd1\x8bT\xb1<l\xba\xcb_\xc00xY\x17r\x17\x88\x08\x08@\xa0\ry@\x10\x04$)`\xf2\xce\x89z\xb0s\xec\x9b.iW\x9d\x81\xb5-+t\x9f\x1a\'\x97dB\xf5x\xb5\xbe.[.\xd7\x0e\x81\xe7\x08\x1cN`\x88\x10\xca\x87\xc3!"\x80\x92R\xa1/\xd1\xc0\xe6mf\xac\xbd\x99\xcca\xb3\x8780>\xa4\xc7\x8d\x1a\\"\xad\xa1\xabyBg\x15\xb9l\x88\x88\x91k"\x94\xa4\xd4\x89\xae*\xa6\x0b\x10\x0c\xd6\xd4m\xe86\xec\xb5j\x8a\x86j\';\xca.\x01I\xf2\xaaJ\xe8\x88\x8cU+t3\xfb\x0c\n\xa33\x13r2\r\x16\xe0\xb3(\xbf\x1d\x83r\xe7M\xf0D\x1365\xd8\x88\xd3\xa4\x92\xcb2\x06\x04\\\xc1\xb0\xea//\xbek&\xd8\xe6+t\xe5\xa1\x13\xada\x16\xder5"w]\xa2i\xb7[\x97R \xe2IT\xcd;Z\x04dk4\xad\x8a\t\xd3\x81z\x10\xf1:^`\xab\x1f\xc5\xdc\x91N\x14$+\x9e\xae\xd3\x80'
+#
+# if has_cmdline_bunzip2:
+# def decompress(self, data):
+# pop = popen2.Popen3("bunzip2", capturestderr=1)
+# pop.tochild.write(data)
+# pop.tochild.close()
+# ret = pop.fromchild.read()
+# pop.fromchild.close()
+# if pop.wait() != 0:
+# ret = bz2.decompress(data)
+# return ret
+#
+# else:
+# # popen2.Popen3 doesn't exist on Windows, and even if it did, bunzip2
+# # isn't available to run.
+# def decompress(self, data):
+# return bz2.decompress(data)
+#
+# class BZ2FileTest(BaseTest):
+# "Test BZ2File type miscellaneous methods."
+#
+# def setUp(self):
+# self.filename = TESTFN
+#
+# def tearDown(self):
+# if os.path.isfile(self.filename):
+# os.unlink(self.filename)
+#
+# def createTempFile(self, crlf=0):
+# f = open(self.filename, "wb")
+# if crlf:
+# data = self.DATA_CRLF
+# else:
+# data = self.DATA
+# f.write(data)
+# f.close()
+#
+# def testRead(self):
+# # "Test BZ2File.read()"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# self.assertRaises(TypeError, bz2f.read, None)
+# self.assertEqual(bz2f.read(), self.TEXT)
+# bz2f.close()
+#
+# def testReadChunk10(self):
+# # "Test BZ2File.read() in chunks of 10 bytes"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# text = ''
+# while 1:
+# str = bz2f.read(10)
+# if not str:
+# break
+# text += str
+# self.assertEqual(text, text)
+# bz2f.close()
+#
+# def testRead100(self):
+# # "Test BZ2File.read(100)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# self.assertEqual(bz2f.read(100), self.TEXT[:100])
+# bz2f.close()
+#
+# def testReadLine(self):
+# # "Test BZ2File.readline()"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# self.assertRaises(TypeError, bz2f.readline, None)
+# sio = StringIO(self.TEXT)
+# for line in sio.readlines():
+# self.assertEqual(bz2f.readline(), line)
+# bz2f.close()
+#
+# def testReadLines(self):
+# # "Test BZ2File.readlines()"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# self.assertRaises(TypeError, bz2f.readlines, None)
+# sio = StringIO(self.TEXT)
+# self.assertEqual(bz2f.readlines(), sio.readlines())
+# bz2f.close()
+#
+# def testIterator(self):
+# # "Test iter(BZ2File)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# sio = StringIO(self.TEXT)
+# self.assertEqual(list(iter(bz2f)), sio.readlines())
+# bz2f.close()
+#
+# def testXReadLines(self):
+# # "Test BZ2File.xreadlines()"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# sio = StringIO(self.TEXT)
+# self.assertEqual(list(bz2f.xreadlines()), sio.readlines())
+# bz2f.close()
+#
+# def testUniversalNewlinesLF(self):
+# # "Test BZ2File.read() with universal newlines (\\n)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename, "rU")
+# self.assertEqual(bz2f.read(), self.TEXT)
+# self.assertEqual(bz2f.newlines, "\n")
+# bz2f.close()
+#
+# def testUniversalNewlinesCRLF(self):
+# # "Test BZ2File.read() with universal newlines (\\r\\n)"
+# self.createTempFile(crlf=1)
+# bz2f = BZ2File(self.filename, "rU")
+# self.assertEqual(bz2f.read(), self.TEXT)
+# self.assertEqual(bz2f.newlines, "\r\n")
+# bz2f.close()
+#
+# def testWrite(self):
+# # "Test BZ2File.write()"
+# bz2f = BZ2File(self.filename, "w")
+# self.assertRaises(TypeError, bz2f.write)
+# bz2f.write(self.TEXT)
+# bz2f.close()
+# f = open(self.filename, 'rb')
+# self.assertEqual(self.decompress(f.read()), self.TEXT)
+# f.close()
+#
+# def testWriteChunks10(self):
+# # "Test BZ2File.write() with chunks of 10 bytes"
+# bz2f = BZ2File(self.filename, "w")
+# n = 0
+# while 1:
+# str = self.TEXT[n*10:(n+1)*10]
+# if not str:
+# break
+# bz2f.write(str)
+# n += 1
+# bz2f.close()
+# f = open(self.filename, 'rb')
+# self.assertEqual(self.decompress(f.read()), self.TEXT)
+# f.close()
+#
+# def testWriteLines(self):
+# # "Test BZ2File.writelines()"
+# bz2f = BZ2File(self.filename, "w")
+# self.assertRaises(TypeError, bz2f.writelines)
+# sio = StringIO(self.TEXT)
+# bz2f.writelines(sio.readlines())
+# bz2f.close()
+# f = open(self.filename, 'rb')
+# self.assertEqual(self.decompress(f.read()), self.TEXT)
+# f.close()
+#
+# def testSeekForward(self):
+# # "Test BZ2File.seek(150, 0)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# self.assertRaises(TypeError, bz2f.seek)
+# bz2f.seek(150)
+# self.assertEqual(bz2f.read(), self.TEXT[150:])
+# bz2f.close()
+#
+# def testSeekBackwards(self):
+# # "Test BZ2File.seek(-150, 1)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# bz2f.read(500)
+# bz2f.seek(-150, 1)
+# self.assertEqual(bz2f.read(), self.TEXT[500-150:])
+# bz2f.close()
+#
+# def testSeekBackwardsFromEnd(self):
+# # "Test BZ2File.seek(-150, 2)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# bz2f.seek(-150, 2)
+# self.assertEqual(bz2f.read(), self.TEXT[len(self.TEXT)-150:])
+# bz2f.close()
+#
+# def testSeekPostEnd(self):
+# # "Test BZ2File.seek(150000)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# bz2f.seek(150000)
+# self.assertEqual(bz2f.tell(), len(self.TEXT))
+# self.assertEqual(bz2f.read(), "")
+# bz2f.close()
+#
+# def testSeekPostEndTwice(self):
+# # "Test BZ2File.seek(150000) twice"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# bz2f.seek(150000)
+# bz2f.seek(150000)
+# self.assertEqual(bz2f.tell(), len(self.TEXT))
+# self.assertEqual(bz2f.read(), "")
+# bz2f.close()
+#
+# def testSeekPreStart(self):
+# # "Test BZ2File.seek(-150, 0)"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename)
+# bz2f.seek(-150)
+# self.assertEqual(bz2f.tell(), 0)
+# self.assertEqual(bz2f.read(), self.TEXT)
+# bz2f.close()
+#
+# def testOpenDel(self):
+# # "Test opening and deleting a file many times"
+# self.createTempFile()
+# for i in xrange(10000):
+# o = BZ2File(self.filename)
+# del o
+#
+# def testOpenNonexistent(self):
+# # "Test opening a nonexistent file"
+# self.assertRaises(IOError, BZ2File, "/non/existent")
+#
+# def testModeU(self):
+# # Bug #1194181: bz2.BZ2File opened for write with mode "U"
+# self.createTempFile()
+# bz2f = BZ2File(self.filename, "U")
+# bz2f.close()
+# f = file(self.filename)
+# f.seek(0, 2)
+# self.assertEqual(f.tell(), len(self.DATA))
+# f.close()
+#
+# def testBug1191043(self):
+# # readlines() for files containing no newline
+# data = 'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t'
+# f = open(self.filename, "wb")
+# f.write(data)
+# f.close()
+# bz2f = BZ2File(self.filename)
+# lines = bz2f.readlines()
+# bz2f.close()
+# self.assertEqual(lines, ['Test'])
+# bz2f = BZ2File(self.filename)
+# xlines = list(bz2f.xreadlines())
+# bz2f.close()
+# self.assertEqual(lines, ['Test'])
+#
+#
+# class BZ2CompressorTest(BaseTest):
+# def testCompress(self):
+# # "Test BZ2Compressor.compress()/flush()"
+# bz2c = BZ2Compressor()
+# self.assertRaises(TypeError, bz2c.compress)
+# data = bz2c.compress(self.TEXT)
+# data += bz2c.flush()
+# self.assertEqual(self.decompress(data), self.TEXT)
+#
+# def testCompressChunks10(self):
+# # "Test BZ2Compressor.compress()/flush() with chunks of 10 bytes"
+# bz2c = BZ2Compressor()
+# n = 0
+# data = ''
+# while 1:
+# str = self.TEXT[n*10:(n+1)*10]
+# if not str:
+# break
+# data += bz2c.compress(str)
+# n += 1
+# data += bz2c.flush()
+# self.assertEqual(self.decompress(data), self.TEXT)
+#
+# class BZ2DecompressorTest(BaseTest):
+# def test_Constructor(self):
+# self.assertRaises(TypeError, BZ2Decompressor, 42)
+#
+# def testDecompress(self):
+# # "Test BZ2Decompressor.decompress()"
+# bz2d = BZ2Decompressor()
+# self.assertRaises(TypeError, bz2d.decompress)
+# text = bz2d.decompress(self.DATA)
+# self.assertEqual(text, self.TEXT)
+#
+# def testDecompressChunks10(self):
+# # "Test BZ2Decompressor.decompress() with chunks of 10 bytes"
+# bz2d = BZ2Decompressor()
+# text = ''
+# n = 0
+# while 1:
+# str = self.DATA[n*10:(n+1)*10]
+# if not str:
+# break
+# text += bz2d.decompress(str)
+# n += 1
+# self.assertEqual(text, self.TEXT)
+#
+# def testDecompressUnusedData(self):
+# # "Test BZ2Decompressor.decompress() with unused data"
+# bz2d = BZ2Decompressor()
+# unused_data = "this is unused data"
+# text = bz2d.decompress(self.DATA+unused_data)
+# self.assertEqual(text, self.TEXT)
+# self.assertEqual(bz2d.unused_data, unused_data)
+#
+# def testEOFError(self):
+# # "Calling BZ2Decompressor.decompress() after EOS must raise EOFError"
+# bz2d = BZ2Decompressor()
+# text = bz2d.decompress(self.DATA)
+# self.assertRaises(EOFError, bz2d.decompress, "anything")
+#
+#
+# class FuncTest(BaseTest):
+# "Test module functions"
+#
+# def testCompress(self):
+# # "Test compress() function"
+# data = bz2.compress(self.TEXT)
+# self.assertEqual(self.decompress(data), self.TEXT)
+#
+# def testDecompress(self):
+# # "Test decompress() function"
+# text = bz2.decompress(self.DATA)
+# self.assertEqual(text, self.TEXT)
+#
+# def testDecompressEmpty(self):
+# # "Test decompress() function with empty string"
+# text = bz2.decompress("")
+# self.assertEqual(text, "")
+#
+# def testDecompressIncomplete(self):
+# # "Test decompress() function with incomplete data"
+# self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10])
+#
+# def test_main():
+# test_support.run_unittest(
+# BZ2FileTest,
+# BZ2CompressorTest,
+# BZ2DecompressorTest,
+# FuncTest
+# )
+# test_support.reap_children()
+#
+# if __name__ == '__main__':
+# test_main()
+#
+# # vim:ts=4:sw=4
More information about the Pypy-commit
mailing list