]> granicus.if.org Git - python/commitdiff
Issue #11127: Raise a TypeError when trying to pickle a socket object.
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 20 Mar 2011 22:56:36 +0000 (23:56 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 20 Mar 2011 22:56:36 +0000 (23:56 +0100)
Lib/socket.py
Lib/test/test_socket.py
Misc/NEWS

index 1e285493c48e289687d9c73e98ab35f92219c34f..57150349a5363faca83dda5023ceb681955046d5 100644 (file)
@@ -112,6 +112,9 @@ class socket(_socket.socket):
                                 s[7:])
         return s
 
+    def __getstate__(self):
+        raise TypeError("Cannot serialize socket object")
+
     def dup(self):
         """dup() -> socket object
 
index d761a73f2ff48e0d326ab2ebf40b8db2878b3592..8b23ae9924ac456b7117bbcda135a5597cad9f34 100644 (file)
@@ -18,6 +18,7 @@ import contextlib
 from weakref import proxy
 import signal
 import math
+import pickle
 try:
     import fcntl
 except ImportError:
@@ -764,6 +765,12 @@ class GeneralModuleTests(unittest.TestCase):
             fp.close()
             self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>")
 
+    def test_pickle(self):
+        sock = socket.socket()
+        with sock:
+            for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+                self.assertRaises(TypeError, pickle.dumps, sock, protocol)
+
 
 @unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicTCPTest(SocketConnectedTest):
index d55c88869ffe7555a1ad80263a1f0d469a2703a6..b2e7a56bf77e80707263ba36398b25154b57ce58 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -83,6 +83,8 @@ Library
 
 - Issue #4391: Use proper gettext plural forms in optparse.
 
+- Issue #11127: Raise a TypeError when trying to pickle a socket object.
+
 - Issue #11563: Connection:close header is sent by requests using URLOpener
   class which helps in closing of sockets after connection is over. Patch
   contributions by Jeff McNeil and Nadeem Vawda.