#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.
This commit is contained in:
Nadeem Vawda 2013-10-28 21:41:24 +01:00
commit e6514f533e
4 changed files with 57 additions and 2 deletions

View File

@ -5,6 +5,7 @@ from test.support import bigmemtest, _4G
import unittest
from io import BytesIO
import os
import pickle
import random
import subprocess
import sys
@ -628,6 +629,11 @@ class BZ2CompressorTest(BaseTest):
finally:
data = None
def testPickle(self):
with self.assertRaises(TypeError):
pickle.dumps(BZ2Compressor())
class BZ2DecompressorTest(BaseTest):
def test_Constructor(self):
self.assertRaises(TypeError, BZ2Decompressor, 42)
@ -679,6 +685,10 @@ class BZ2DecompressorTest(BaseTest):
compressed = None
decompressed = None
def testPickle(self):
with self.assertRaises(TypeError):
pickle.dumps(BZ2Decompressor())
class CompressDecompressTest(BaseTest):
def testCompress(self):

View File

@ -1,5 +1,6 @@
from io import BytesIO, UnsupportedOperation
import os
import pickle
import random
import unittest
@ -216,6 +217,14 @@ class CompressorDecompressorTestCase(unittest.TestCase):
finally:
input = cdata = ddata = None
# Pickling raises an exception; there's no way to serialize an lzma_stream.
def test_pickle(self):
with self.assertRaises(TypeError):
pickle.dumps(LZMACompressor())
with self.assertRaises(TypeError):
pickle.dumps(LZMADecompressor())
class CompressDecompressFunctionTestCase(unittest.TestCase):

View File

@ -248,6 +248,14 @@ BZ2Compressor_flush(BZ2Compressor *self, PyObject *noargs)
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)
{
@ -317,10 +325,11 @@ BZ2Compressor_dealloc(BZ2Compressor *self)
}
static PyMethodDef BZ2Compressor_methods[] = {
{"compress", (PyCFunction)BZ2Compressor_compress, METH_VARARGS,
{"compress", (PyCFunction)BZ2Compressor_compress, METH_VARARGS,
BZ2Compressor_compress__doc__},
{"flush", (PyCFunction)BZ2Compressor_flush, METH_NOARGS,
{"flush", (PyCFunction)BZ2Compressor_flush, METH_NOARGS,
BZ2Compressor_flush__doc__},
{"__getstate__", (PyCFunction)BZ2Compressor_getstate, METH_NOARGS},
{NULL}
};
@ -471,6 +480,14 @@ BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args)
return result;
}
static PyObject *
BZ2Decompressor_getstate(BZ2Decompressor *self, PyObject *noargs)
{
PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
Py_TYPE(self)->tp_name);
return NULL;
}
static int
BZ2Decompressor_init(BZ2Decompressor *self, PyObject *args, PyObject *kwargs)
{
@ -521,6 +538,7 @@ BZ2Decompressor_dealloc(BZ2Decompressor *self)
static PyMethodDef BZ2Decompressor_methods[] = {
{"decompress", (PyCFunction)BZ2Decompressor_decompress, METH_VARARGS,
BZ2Decompressor_decompress__doc__},
{"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS},
{NULL}
};

View File

@ -564,6 +564,14 @@ Compressor_flush(Compressor *self, PyObject *noargs)
return result;
}
static PyObject *
Compressor_getstate(Compressor *self, PyObject *noargs)
{
PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
Py_TYPE(self)->tp_name);
return NULL;
}
static int
Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset,
PyObject *filterspecs)
@ -735,6 +743,7 @@ static PyMethodDef Compressor_methods[] = {
Compressor_compress_doc},
{"flush", (PyCFunction)Compressor_flush, METH_NOARGS,
Compressor_flush_doc},
{"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS},
{NULL}
};
@ -892,6 +901,14 @@ Decompressor_decompress(Decompressor *self, PyObject *args)
return result;
}
static PyObject *
Decompressor_getstate(Decompressor *self, PyObject *noargs)
{
PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
Py_TYPE(self)->tp_name);
return NULL;
}
static int
Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
{
@ -1019,6 +1036,7 @@ Decompressor_dealloc(Decompressor *self)
static PyMethodDef Decompressor_methods[] = {
{"decompress", (PyCFunction)Decompressor_decompress, METH_VARARGS,
Decompressor_decompress_doc},
{"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
{NULL}
};