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_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) { 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)); } } 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_FUNCinitbz2(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);}Lines 2001–2232 of 2232. Previous lines
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_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) { 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)); } } 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); }