summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-11-05 21:15:39 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-11-05 21:15:39 (GMT)
commit243757eb79fe4bee33882b1813cf33839117570f (patch)
treea9403be5a75dedbb5c5b6ea4ee563b7155c707fd /Lib
parent4a5f9677f31d75454ec10a0baefef37b63568a31 (diff)
downloadcpython-243757eb79fe4bee33882b1813cf33839117570f.zip
cpython-243757eb79fe4bee33882b1813cf33839117570f.tar.gz
cpython-243757eb79fe4bee33882b1813cf33839117570f.tar.bz2
Issue #10180: Pickling file objects is now explicitly forbidden, since
unpickling them produced nonsensical results.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py4
-rw-r--r--Lib/test/test_io.py18
2 files changed, 22 insertions, 0 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 83bce70..87c833c 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -747,6 +747,10 @@ class _BufferedIOMixin(BufferedIOBase):
def mode(self):
return self.raw.mode
+ def __getstate__(self):
+ raise TypeError("can not serialize a '{0}' object"
+ .format(self.__class__.__name__))
+
def __repr__(self):
clsname = self.__class__.__name__
try:
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 680e36d..91dd803 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -30,6 +30,7 @@ import abc
import signal
import errno
import warnings
+import pickle
from itertools import cycle, count
from collections import deque
from test import support
@@ -2566,6 +2567,23 @@ class MiscIOTest(unittest.TestCase):
self._check_warn_on_dealloc_fd("r")
+ def test_pickling(self):
+ # Pickling file objects is forbidden
+ for kwargs in [
+ {"mode": "w"},
+ {"mode": "wb"},
+ {"mode": "wb", "buffering": 0},
+ {"mode": "r"},
+ {"mode": "rb"},
+ {"mode": "rb", "buffering": 0},
+ {"mode": "w+"},
+ {"mode": "w+b"},
+ {"mode": "w+b", "buffering": 0},
+ ]:
+ for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.open(support.TESTFN, **kwargs) as f:
+ self.assertRaises(TypeError, pickle.dumps, f, protocol)
+
class CMiscIOTest(MiscIOTest):
io = io