From: Nadeem Vawda Date: Mon, 28 Oct 2013 20:41:24 +0000 (+0100) Subject: #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor). X-Git-Tag: v3.4.0b1~502 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e6514f533efba25e5aeba50208515d02d528995a;p=thirdparty%2FPython%2Fcpython.git #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor). The underlying C libraries provide no mechanism for serializing compressor and decompressor objects, so actually pickling these classes is impractical. Previously, these objects would be pickled without error, but attempting to use a deserialized instance would segfault the interpreter. --- e6514f533efba25e5aeba50208515d02d528995a diff --cc Modules/_bz2module.c index abc4d5d732bc,3f7a6cf02620..fd05de0a27c6 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@@ -248,24 -250,14 +248,32 @@@ BZ2Compressor_flush(BZ2Compressor *self return result; } + static PyObject * + BZ2Compressor_getstate(BZ2Compressor *self, PyObject *noargs) + { + PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", + Py_TYPE(self)->tp_name); + return NULL; + } + +static void* +BZ2_Malloc(void* ctx, int items, int size) +{ + if (items < 0 || size < 0) + return NULL; + if ((size_t)items > (size_t)PY_SSIZE_T_MAX / (size_t)size) + return NULL; + /* PyMem_Malloc() cannot be used: compress() and decompress() + release the GIL */ + return PyMem_RawMalloc(items * size); +} + +static void +BZ2_Free(void* ctx, void *ptr) +{ + PyMem_RawFree(ptr); +} + static int BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs) {