2232 lines · 53.4 KB · C
RawDownload
        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_in == 0)			break; /* no more input data */		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));		}	} 	_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 intBZ2Comp_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) {		PyErr_SetString(PyExc_MemoryError, "unable to allocate 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);		self->lock = NULL;	}#endif	return -1;} static voidBZ2Comp_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_in == 0)			break; /* no more input data */		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));		}	} 	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 intBZ2Decomp_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) {		PyErr_SetString(PyExc_MemoryError, "unable to allocate 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);		self->lock = NULL;	}#endif	Py_CLEAR(self->unused_data);	return -1;} static voidBZ2Decomp_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*/

Lines 1501–2000 of 2232. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t