]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33138: Change standard error message for non-pickleable and non-copyable types...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 31 Oct 2018 00:28:07 +0000 (02:28 +0200)
committerGitHub <noreply@github.com>
Wed, 31 Oct 2018 00:28:07 +0000 (02:28 +0200)
Lib/_pyio.py
Lib/copyreg.py
Lib/socket.py
Misc/NEWS.d/next/Core and Builtins/2018-03-25-19-25-14.bpo-33138.aSqudH.rst [new file with mode: 0644]
Modules/_bz2module.c
Modules/_io/bufferedio.c
Modules/_io/fileio.c
Modules/_io/textio.c
Modules/_io/winconsoleio.c
Modules/_lzmamodule.c
Objects/typeobject.c

index b8975ff533d7eb8dcfb6dd3d1bd5931724cee97f..e4a879941e1d4b1d95f0d9109fe47bff4b5a97f1 100644 (file)
@@ -814,8 +814,7 @@ class _BufferedIOMixin(BufferedIOBase):
         return self.raw.mode
 
     def __getstate__(self):
-        raise TypeError("can not serialize a '{0}' object"
-                        .format(self.__class__.__name__))
+        raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
 
     def __repr__(self):
         modname = self.__class__.__module__
@@ -1554,7 +1553,7 @@ class FileIO(RawIOBase):
             self.close()
 
     def __getstate__(self):
-        raise TypeError("cannot serialize '%s' object", self.__class__.__name__)
+        raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
 
     def __repr__(self):
         class_name = '%s.%s' % (self.__class__.__module__,
index bbe1af4e2e7e7178b68faf234a428b5bd8eaf2d8..dfc463c49a389d59789c982846c3697c456646f1 100644 (file)
@@ -53,7 +53,8 @@ _HEAPTYPE = 1<<9
 
 def _reduce_ex(self, proto):
     assert proto < 2
-    for base in self.__class__.__mro__:
+    cls = self.__class__
+    for base in cls.__mro__:
         if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
             break
     else:
@@ -61,16 +62,18 @@ def _reduce_ex(self, proto):
     if base is object:
         state = None
     else:
-        if base is self.__class__:
-            raise TypeError("can't pickle %s objects" % base.__name__)
+        if base is cls:
+            raise TypeError(f"cannot pickle {cls.__name__!r} object")
         state = base(self)
-    args = (self.__class__, base, state)
+    args = (cls, base, state)
     try:
         getstate = self.__getstate__
     except AttributeError:
         if getattr(self, "__slots__", None):
-            raise TypeError("a class that defines __slots__ without "
-                            "defining __getstate__ cannot be pickled") from None
+            raise TypeError(f"cannot pickle {cls.__name__!r} object: "
+                            f"a class that defines __slots__ without "
+                            f"defining __getstate__ cannot be pickled "
+                            f"with protocol {proto}") from None
         try:
             dict = self.__dict__
         except AttributeError:
index 385844b58532021f8042b6f2d7c23e9757344a9f..772b9e185bf1c6e3bfc493d8a76a35d7262795c9 100644 (file)
@@ -189,7 +189,7 @@ class socket(_socket.socket):
         return s
 
     def __getstate__(self):
-        raise TypeError("Cannot serialize socket object")
+        raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
 
     def dup(self):
         """dup() -> socket object
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-03-25-19-25-14.bpo-33138.aSqudH.rst b/Misc/NEWS.d/next/Core and Builtins/2018-03-25-19-25-14.bpo-33138.aSqudH.rst
new file mode 100644 (file)
index 0000000..6f44526
--- /dev/null
@@ -0,0 +1,2 @@
+Changed standard error message for non-pickleable and non-copyable types. It
+now says "cannot pickle" instead of "can't pickle" or "cannot serialize".
index f0d9588fe55d6f9f8e50cf54cb3885359106f886..b5e5a79d50a5bb48300b40f19e781eb634675f6f 100644 (file)
@@ -264,14 +264,6 @@ _bz2_BZ2Compressor_flush_impl(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)
 {
@@ -347,7 +339,6 @@ BZ2Compressor_dealloc(BZ2Compressor *self)
 static PyMethodDef BZ2Compressor_methods[] = {
     _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF
     _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF
-    {"__getstate__", (PyCFunction)BZ2Compressor_getstate, METH_NOARGS},
     {NULL}
 };
 
@@ -612,14 +603,6 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
     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;
-}
-
 /*[clinic input]
 _bz2.BZ2Decompressor.__init__
 
@@ -679,7 +662,6 @@ BZ2Decompressor_dealloc(BZ2Decompressor *self)
 
 static PyMethodDef BZ2Decompressor_methods[] = {
     _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF
-    {"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS},
     {NULL}
 };
 
index 9d3b4466493c7f9824c7b661922194e99dced2e1..2eb5262f0f7bc239f932714c47432f672ce9e282 100644 (file)
@@ -608,16 +608,6 @@ buffered_isatty(buffered *self, PyObject *Py_UNUSED(ignored))
     return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_isatty, NULL);
 }
 
-/* Serialization */
-
-static PyObject *
-buffered_getstate(buffered *self, PyObject *Py_UNUSED(ignored))
-{
-    PyErr_Format(PyExc_TypeError,
-                 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
-    return NULL;
-}
-
 /* Forward decls */
 static PyObject *
 _bufferedwriter_flush_unlocked(buffered *);
@@ -2394,7 +2384,6 @@ static PyMethodDef bufferedreader_methods[] = {
     {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
     {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
     {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
-    {"__getstate__", (PyCFunction)buffered_getstate, METH_NOARGS},
 
     _IO__BUFFERED_READ_METHODDEF
     _IO__BUFFERED_PEEK_METHODDEF
@@ -2485,7 +2474,6 @@ static PyMethodDef bufferedwriter_methods[] = {
     {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
     {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
     {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
-    {"__getstate__", (PyCFunction)buffered_getstate, METH_NOARGS},
 
     _IO_BUFFEREDWRITER_WRITE_METHODDEF
     _IO__BUFFERED_TRUNCATE_METHODDEF
@@ -2579,8 +2567,6 @@ static PyMethodDef bufferedrwpair_methods[] = {
     {"close", (PyCFunction)bufferedrwpair_close, METH_NOARGS},
     {"isatty", (PyCFunction)bufferedrwpair_isatty, METH_NOARGS},
 
-    {"__getstate__", (PyCFunction)buffered_getstate, METH_NOARGS},
-
     {NULL, NULL}
 };
 
@@ -2652,7 +2638,6 @@ static PyMethodDef bufferedrandom_methods[] = {
     {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
     {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
     {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
-    {"__getstate__", (PyCFunction)buffered_getstate, METH_NOARGS},
 
     {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
 
index 44d51c9faf5d1fcacaa2a3453fdfba0f478395f8..ffcb7301295354d35627d5ceb7619a77d3668b11 100644 (file)
@@ -1120,14 +1120,6 @@ _io_FileIO_isatty_impl(fileio *self)
     return PyBool_FromLong(res);
 }
 
-static PyObject *
-fileio_getstate(fileio *self, PyObject *Py_UNUSED(ignored))
-{
-    PyErr_Format(PyExc_TypeError,
-                 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
-    return NULL;
-}
-
 #include "clinic/fileio.c.h"
 
 static PyMethodDef fileio_methods[] = {
@@ -1145,7 +1137,6 @@ static PyMethodDef fileio_methods[] = {
     _IO_FILEIO_FILENO_METHODDEF
     _IO_FILEIO_ISATTY_METHODDEF
     {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
-    {"__getstate__", (PyCFunction)fileio_getstate, METH_NOARGS, NULL},
     {NULL,           NULL}             /* sentinel */
 };
 
index a466d3a03a5ba313f303e1d89649027721b1938e..3a3667b39da86c75447b5db14a60412d7eca909d 100644 (file)
@@ -2891,14 +2891,6 @@ _io_TextIOWrapper_isatty_impl(textio *self)
     return _PyObject_CallMethodId(self->buffer, &PyId_isatty, NULL);
 }
 
-static PyObject *
-textiowrapper_getstate(textio *self, PyObject *args)
-{
-    PyErr_Format(PyExc_TypeError,
-                 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
-    return NULL;
-}
-
 /*[clinic input]
 _io.TextIOWrapper.flush
 [clinic start generated code]*/
@@ -3132,7 +3124,6 @@ static PyMethodDef textiowrapper_methods[] = {
     _IO_TEXTIOWRAPPER_READABLE_METHODDEF
     _IO_TEXTIOWRAPPER_WRITABLE_METHODDEF
     _IO_TEXTIOWRAPPER_ISATTY_METHODDEF
-    {"__getstate__", (PyCFunction)textiowrapper_getstate, METH_NOARGS},
 
     _IO_TEXTIOWRAPPER_SEEK_METHODDEF
     _IO_TEXTIOWRAPPER_TELL_METHODDEF
index 13342ec239d6d6687f8b490b67b6015887433c9a..148255c354a495a5279f0f80e934c0245e9b1bae 100644 (file)
@@ -1060,14 +1060,6 @@ _io__WindowsConsoleIO_isatty_impl(winconsoleio *self)
     Py_RETURN_TRUE;
 }
 
-static PyObject *
-winconsoleio_getstate(winconsoleio *self, PyObject *Py_UNUSED(ignored))
-{
-    PyErr_Format(PyExc_TypeError,
-                 "cannot serialize '%s' object", Py_TYPE(self)->tp_name);
-    return NULL;
-}
-
 #include "clinic/winconsoleio.c.h"
 
 static PyMethodDef winconsoleio_methods[] = {
@@ -1080,7 +1072,6 @@ static PyMethodDef winconsoleio_methods[] = {
     _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
     _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
     _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
-    {"__getstate__", (PyCFunction)winconsoleio_getstate, METH_NOARGS, NULL},
     {NULL,           NULL}             /* sentinel */
 };
 
index bb7a7ec50ce059bfff621ea273478ba5a0704634..b5f9561df2ad0a980c1801acef065f3346fb5224 100644 (file)
@@ -591,14 +591,6 @@ _lzma_LZMACompressor_flush_impl(Compressor *self)
     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)
@@ -794,7 +786,6 @@ Compressor_dealloc(Compressor *self)
 static PyMethodDef Compressor_methods[] = {
     _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF
     _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF
-    {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS},
     {NULL}
 };
 
@@ -1078,14 +1069,6 @@ _lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data,
     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)
 {
@@ -1235,7 +1218,6 @@ Decompressor_dealloc(Decompressor *self)
 
 static PyMethodDef Decompressor_methods[] = {
     _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF
-    {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
     {NULL}
 };
 
index 8e91e33d2d106faa8e61cd83afb2b6b0127d0f58..722fe5f94735c0554f047c22b444bb45ce74cbcb 100644 (file)
@@ -4122,7 +4122,7 @@ _PyObject_GetState(PyObject *obj, int required)
 
         if (required && obj->ob_type->tp_itemsize) {
             PyErr_Format(PyExc_TypeError,
-                         "can't pickle %.200s objects",
+                         "cannot pickle '%.200s' object",
                          Py_TYPE(obj)->tp_name);
             return NULL;
         }
@@ -4163,7 +4163,7 @@ _PyObject_GetState(PyObject *obj, int required)
                 Py_DECREF(slotnames);
                 Py_DECREF(state);
                 PyErr_Format(PyExc_TypeError,
-                             "can't pickle %.200s objects",
+                             "cannot pickle '%.200s' object",
                              Py_TYPE(obj)->tp_name);
                 return NULL;
             }
@@ -4400,7 +4400,7 @@ reduce_newobj(PyObject *obj)
 
     if (Py_TYPE(obj)->tp_new == NULL) {
         PyErr_Format(PyExc_TypeError,
-                     "can't pickle %.200s objects",
+                     "cannot pickle '%.200s' object",
                      Py_TYPE(obj)->tp_name);
         return NULL;
     }