From b00c0674ec98dbb817a95dbb0e71648f56e13a6c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 25 Mar 2018 19:25:21 +0300 Subject: [PATCH 1/3] bpo-33138: Chang standard error message for non-pickleable and non-copyable types. It now says "cannot serialize" instead of "can't pickle". --- Lib/_pyio.py | 5 ++--- Lib/copyreg.py | 15 +++++++++------ Lib/socket.py | 2 +- .../2018-03-25-19-25-14.bpo-33138.aSqudH.rst | 2 ++ Modules/_bz2module.c | 18 ------------------ Modules/_io/bufferedio.c | 13 ------------- Modules/_io/fileio.c | 9 --------- Modules/_io/textio.c | 9 --------- Modules/_io/winconsoleio.c | 9 --------- Modules/_lzmamodule.c | 18 ------------------ Objects/typeobject.c | 6 +++--- 11 files changed, 17 insertions(+), 89 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-03-25-19-25-14.bpo-33138.aSqudH.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index c91a647a2f67b0..c9a8ceef8d5620 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -809,8 +809,7 @@ def mode(self): return self.raw.mode def __getstate__(self): - raise TypeError("can not serialize a '{0}' object" - .format(self.__class__.__name__)) + raise TypeError(f"cannot serialize '{self.__class__.__name__}' object") def __repr__(self): modname = self.__class__.__module__ @@ -1549,7 +1548,7 @@ def __del__(self): self.close() def __getstate__(self): - raise TypeError("cannot serialize '%s' object", self.__class__.__name__) + raise TypeError(f"cannot serialize '{self.__class__.__name__}' object") def __repr__(self): class_name = '%s.%s' % (self.__class__.__module__, diff --git a/Lib/copyreg.py b/Lib/copyreg.py index bbe1af4e2e7e71..2eabfe85bdb8a8 100644 --- a/Lib/copyreg.py +++ b/Lib/copyreg.py @@ -53,7 +53,8 @@ def _reconstructor(cls, base, state): 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 serialize '{cls.__name__}' 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 serialize '{cls.__name__}' 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: diff --git a/Lib/socket.py b/Lib/socket.py index cfa605a22ada92..f1298961987c68 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -189,7 +189,7 @@ def __repr__(self): return s def __getstate__(self): - raise TypeError("Cannot serialize socket object") + raise TypeError(f"cannot serialize '{self.__class__.__name__}' 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 index 00000000000000..1562cb45788fb7 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-03-25-19-25-14.bpo-33138.aSqudH.rst @@ -0,0 +1,2 @@ +Changed standard error message for non-pickleable and non-copyable types. It +now says "cannot serialize" instead of "can't pickle". diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c index 0789b6179e52be..f63aa2f2f51834 100644 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@ -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__ @@ -675,7 +658,6 @@ BZ2Decompressor_dealloc(BZ2Decompressor *self) static PyMethodDef BZ2Decompressor_methods[] = { _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF - {"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS}, {NULL} }; diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 358a654a98ca58..007a0ff21f1cdc 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -610,14 +610,6 @@ buffered_isatty(buffered *self, PyObject *args) /* Serialization */ -static PyObject * -buffered_getstate(buffered *self, PyObject *args) -{ - 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 +2386,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 +2476,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 +2569,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 +2640,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}, diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 0d5bf3b22114e5..59a2c862bd3ba3 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -1122,14 +1122,6 @@ _io_FileIO_isatty_impl(fileio *self) return PyBool_FromLong(res); } -static PyObject * -fileio_getstate(fileio *self) -{ - PyErr_Format(PyExc_TypeError, - "cannot serialize '%s' object", Py_TYPE(self)->tp_name); - return NULL; -} - #include "clinic/fileio.c.h" static PyMethodDef fileio_methods[] = { @@ -1147,7 +1139,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 */ }; diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 717b56ab319b61..c6e7e113d4d6e7 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2879,14 +2879,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]*/ @@ -3120,7 +3112,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 diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index b85c11b3405a73..0dfea9117ac7fd 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -1056,14 +1056,6 @@ _io__WindowsConsoleIO_isatty_impl(winconsoleio *self) Py_RETURN_TRUE; } -static PyObject * -winconsoleio_getstate(winconsoleio *self) -{ - PyErr_Format(PyExc_TypeError, - "cannot serialize '%s' object", Py_TYPE(self)->tp_name); - return NULL; -} - #include "clinic/winconsoleio.c.h" static PyMethodDef winconsoleio_methods[] = { @@ -1076,7 +1068,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 */ }; diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c index 5bcd088d772188..1d9b49ff7b6441 100644 --- a/Modules/_lzmamodule.c +++ b/Modules/_lzmamodule.c @@ -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) { @@ -1231,7 +1214,6 @@ Decompressor_dealloc(Decompressor *self) static PyMethodDef Decompressor_methods[] = { _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF - {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS}, {NULL} }; diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 1dd534866b4d65..a54a051d232d47 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4103,7 +4103,7 @@ _PyObject_GetState(PyObject *obj, int required) if (required && obj->ob_type->tp_itemsize) { PyErr_Format(PyExc_TypeError, - "can't pickle %.200s objects", + "cannot serialize '%.200s' object", Py_TYPE(obj)->tp_name); return NULL; } @@ -4144,7 +4144,7 @@ _PyObject_GetState(PyObject *obj, int required) Py_DECREF(slotnames); Py_DECREF(state); PyErr_Format(PyExc_TypeError, - "can't pickle %.200s objects", + "cannot serialize '%.200s' object", Py_TYPE(obj)->tp_name); return NULL; } @@ -4381,7 +4381,7 @@ reduce_newobj(PyObject *obj) if (Py_TYPE(obj)->tp_new == NULL) { PyErr_Format(PyExc_TypeError, - "can't pickle %.200s objects", + "cannot serialize '%.200s' object", Py_TYPE(obj)->tp_name); return NULL; } From 74476932377426bb47cba238a2cde176fabd8cd8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 26 Jul 2018 13:18:38 +0300 Subject: [PATCH 2/3] Update copyreg.py --- Lib/copyreg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/copyreg.py b/Lib/copyreg.py index 2eabfe85bdb8a8..419d3fc8a9e809 100644 --- a/Lib/copyreg.py +++ b/Lib/copyreg.py @@ -72,7 +72,7 @@ def _reduce_ex(self, proto): if getattr(self, "__slots__", None): raise TypeError(f"cannot serialize '{cls.__name__}' object: " f"a class that defines __slots__ without " - f"defining __getstate__ cannot be pickled" + f"defining __getstate__ cannot be pickled " f"with protocol {proto}") from None try: dict = self.__dict__ From f617849f48087c1bae0b9179978aa92f4c10b3d0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 30 Oct 2018 09:37:40 +0200 Subject: [PATCH 3/3] Update wording. --- Lib/_pyio.py | 4 ++-- Lib/copyreg.py | 4 ++-- Lib/socket.py | 2 +- .../2018-03-25-19-25-14.bpo-33138.aSqudH.rst | 2 +- Modules/_io/bufferedio.c | 2 -- Objects/typeobject.c | 6 +++--- 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 78e36a033be6c4..e4a879941e1d4b 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -814,7 +814,7 @@ def mode(self): return self.raw.mode def __getstate__(self): - raise TypeError(f"cannot serialize '{self.__class__.__name__}' object") + raise TypeError(f"cannot pickle {self.__class__.__name__!r} object") def __repr__(self): modname = self.__class__.__module__ @@ -1553,7 +1553,7 @@ def __del__(self): self.close() def __getstate__(self): - raise TypeError(f"cannot serialize '{self.__class__.__name__}' object") + raise TypeError(f"cannot pickle {self.__class__.__name__!r} object") def __repr__(self): class_name = '%s.%s' % (self.__class__.__module__, diff --git a/Lib/copyreg.py b/Lib/copyreg.py index 419d3fc8a9e809..dfc463c49a389d 100644 --- a/Lib/copyreg.py +++ b/Lib/copyreg.py @@ -63,14 +63,14 @@ def _reduce_ex(self, proto): state = None else: if base is cls: - raise TypeError(f"cannot serialize '{cls.__name__}' object") + raise TypeError(f"cannot pickle {cls.__name__!r} object") state = base(self) args = (cls, base, state) try: getstate = self.__getstate__ except AttributeError: if getattr(self, "__slots__", None): - raise TypeError(f"cannot serialize '{cls.__name__}' object: " + 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 diff --git a/Lib/socket.py b/Lib/socket.py index a82e887c1dedef..772b9e185bf1c6 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -189,7 +189,7 @@ def __repr__(self): return s def __getstate__(self): - raise TypeError(f"cannot serialize '{self.__class__.__name__}' 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 index 1562cb45788fb7..6f445261843f30 100644 --- 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 @@ -1,2 +1,2 @@ Changed standard error message for non-pickleable and non-copyable types. It -now says "cannot serialize" instead of "can't pickle". +now says "cannot pickle" instead of "can't pickle" or "cannot serialize". diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index f7e89b79aa3089..2eb5262f0f7bc2 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -608,8 +608,6 @@ buffered_isatty(buffered *self, PyObject *Py_UNUSED(ignored)) return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_isatty, NULL); } -/* Serialization */ - /* Forward decls */ static PyObject * _bufferedwriter_flush_unlocked(buffered *); diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 6f3f23667a7205..722fe5f94735c0 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4122,7 +4122,7 @@ _PyObject_GetState(PyObject *obj, int required) if (required && obj->ob_type->tp_itemsize) { PyErr_Format(PyExc_TypeError, - "cannot serialize '%.200s' object", + "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, - "cannot serialize '%.200s' object", + "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, - "cannot serialize '%.200s' object", + "cannot pickle '%.200s' object", Py_TYPE(obj)->tp_name); return NULL; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy