Index: Objects/fileobject.c =================================================================== --- Objects/fileobject.c (revision 73576) +++ Objects/fileobject.c (working copy) @@ -1889,6 +1889,14 @@ Py_RETURN_NONE; } +static PyObject * +file_reduce(PyObject *f) +{ + PyErr_Format(PyExc_TypeError, "cannot pickle %.200s objects", + Py_TYPE(f)->tp_name); + return NULL; +} + PyDoc_STRVAR(readline_doc, "readline([size]) -> next line from the file, as a string.\n" "\n" @@ -1998,6 +2006,7 @@ {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc}, {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc}, + {"__reduce__", (PyCFunction)file_reduce, METH_NOARGS, NULL}, {NULL, NULL} /* sentinel */ }; Index: Lib/test/test_file2k.py =================================================================== --- Lib/test/test_file2k.py (revision 73576) +++ Lib/test/test_file2k.py (working copy) @@ -2,6 +2,7 @@ import os import unittest import itertools +import pickle import time import threading from array import array @@ -123,6 +124,11 @@ def testReadWhenWriting(self): self.assertRaises(IOError, self.f.read) + def testPicklingIsDisabled(self): + for protocol in range(pickle.HIGHEST_PROTOCOL + 1): + self.assertRaises(TypeError, pickle.dumps, self.f, protocol) + + class OtherFileTests(unittest.TestCase): def testOpenDir(self):