gh-122559: Synchronize C and Python implementation of the io module about pickling by serhiy-storchaka · Pull Request #122628 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Remove :meth:`!__reduce__` and :meth:`!__reduce_ex__` methods that always
raise :exc:`TypeError` in the C implementation of :class:`io.FileIO`,
:class:`io.BufferedReader`, :class:`io.BufferedWriter` and
:class:`io.BufferedRandom` and replace them with default
:meth:`!__getstate__` methods that raise :exc:`!TypeError`.
This restores fine details of behavior of Python 3.11 and older versions.
9 changes: 3 additions & 6 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2555,8 +2555,7 @@ static PyMethodDef bufferedreader_methods[] = {
_IO__BUFFERED_TRUNCATE_METHODDEF
_IO__BUFFERED___SIZEOF___METHODDEF

{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},
{"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
{NULL, NULL}
};

Expand Down Expand Up @@ -2615,8 +2614,7 @@ static PyMethodDef bufferedwriter_methods[] = {
_IO__BUFFERED_TELL_METHODDEF
_IO__BUFFERED___SIZEOF___METHODDEF

{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},
{"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
{NULL, NULL}
};

Expand Down Expand Up @@ -2733,8 +2731,7 @@ static PyMethodDef bufferedrandom_methods[] = {
_IO_BUFFEREDWRITER_WRITE_METHODDEF
_IO__BUFFERED___SIZEOF___METHODDEF

{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},
{"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
{NULL, NULL}
};

Expand Down
3 changes: 1 addition & 2 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1262,8 +1262,7 @@ static PyMethodDef fileio_methods[] = {
_IO_FILEIO_ISATTY_METHODDEF
{"_isatty_open_only", _io_FileIO_isatty_open_only, METH_NOARGS},
{"_dealloc_warn", fileio_dealloc_warn, METH_O, NULL},
{"__reduce__", _PyIOBase_cannot_pickle, METH_NOARGS},
{"__reduce_ex__", _PyIOBase_cannot_pickle, METH_O},
{"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
3 changes: 1 addition & 2 deletions Modules/_io/textio.c