[Python-checkins] cpython (3.3): #19395: Raise exception when pickling a (BZ2|LZMA)(Compressor|Decompressor).
nadeem.vawda
python-checkins at python.org
Mon Oct 28 21:46:06 CET 2013
http://hg.python.org/cpython/rev/be363c1e94fe
changeset: 86704:be363c1e94fe
branch: 3.3
parent: 86699:543c66410547
user: Nadeem Vawda <nadeem.vawda at gmail.com>
date: Mon Oct 28 21:35:23 2013 +0100
summary:
#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.
files:
Lib/test/test_bz2.py | 10 ++++++++++
Lib/test/test_lzma.py | 9 +++++++++
Misc/NEWS | 4 ++++
Modules/_bz2module.c | 22 ++++++++++++++++++++--
Modules/_lzmamodule.c | 18 ++++++++++++++++++
5 files changed, 61 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -5,6 +5,7 @@
import unittest
from io import BytesIO
import os
+import pickle
import random
import subprocess
import sys
@@ -621,6 +622,11 @@
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)
@@ -672,6 +678,10 @@
compressed = None
decompressed = None
+ def testPickle(self):
+ with self.assertRaises(TypeError):
+ pickle.dumps(BZ2Decompressor())
+
class CompressDecompressTest(BaseTest):
def testCompress(self):
diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py
--- a/Lib/test/test_lzma.py
+++ b/Lib/test/test_lzma.py
@@ -1,5 +1,6 @@
from io import BytesIO, UnsupportedOperation
import os
+import pickle
import random
import unittest
@@ -216,6 +217,14 @@
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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -92,6 +92,10 @@
Library
-------
+- Issue #19395: Raise an exception when attempting to pickle a bz2 or lzma
+ compressor/decompressor object, rather than creating a pickle that would
+ cause a segfault when loaded and used.
+
- Issue #19227: Try to fix deadlocks caused by re-seeding then OpenSSL
pseudo-random number generator on fork().
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -250,6 +250,14 @@
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 int
BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs)
{
@@ -298,10 +306,11 @@
}
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}
};
@@ -452,6 +461,14 @@
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)
{
@@ -502,6 +519,7 @@
static PyMethodDef BZ2Decompressor_methods[] = {
{"decompress", (PyCFunction)BZ2Decompressor_decompress, METH_VARARGS,
BZ2Decompressor_decompress__doc__},
+ {"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS},
{NULL}
};
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -546,6 +546,14 @@
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)
@@ -712,6 +720,7 @@
Compressor_compress_doc},
{"flush", (PyCFunction)Compressor_flush, METH_NOARGS,
Compressor_flush_doc},
+ {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS},
{NULL}
};
@@ -869,6 +878,14 @@
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)
{
@@ -991,6 +1008,7 @@
static PyMethodDef Decompressor_methods[] = {
{"decompress", (PyCFunction)Decompressor_decompress, METH_VARARGS,
Decompressor_decompress_doc},
+ {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
{NULL}
};
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list