[Python-checkins] r80423 - in python/trunk: Lib/ssl.py Lib/test/test_ssl.py Misc/ACKS Misc/NEWS

antoine.pitrou python-checkins at python.org
Sat Apr 24 00:54:59 CEST 2010


Author: antoine.pitrou
Date: Sat Apr 24 00:54:59 2010
New Revision: 80423

Log:
Issue #7943: Fix circular reference created when instantiating an SSL
socket.  Initial patch by Péter Szabó.



Modified:
   python/trunk/Lib/ssl.py
   python/trunk/Lib/test/test_ssl.py
   python/trunk/Misc/ACKS
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/ssl.py
==============================================================================
--- python/trunk/Lib/ssl.py	(original)
+++ python/trunk/Lib/ssl.py	Sat Apr 24 00:54:59 2010
@@ -75,7 +75,7 @@
      SSL_ERROR_EOF, \
      SSL_ERROR_INVALID_ERROR_CODE
 
-from socket import socket, _fileobject, error as socket_error
+from socket import socket, _fileobject, _delegate_methods, error as socket_error
 from socket import getnameinfo as _getnameinfo
 import base64        # for DER-to-PEM translation
 
@@ -91,13 +91,14 @@
                  do_handshake_on_connect=True,
                  suppress_ragged_eofs=True, ciphers=None):
         socket.__init__(self, _sock=sock._sock)
-        # the initializer for socket trashes the methods (tsk, tsk), so...
-        self.send = lambda data, flags=0: SSLSocket.send(self, data, flags)
-        self.sendto = lambda data, addr, flags=0: SSLSocket.sendto(self, data, addr, flags)
-        self.recv = lambda buflen=1024, flags=0: SSLSocket.recv(self, buflen, flags)
-        self.recvfrom = lambda addr, buflen=1024, flags=0: SSLSocket.recvfrom(self, addr, buflen, flags)
-        self.recv_into = lambda buffer, nbytes=None, flags=0: SSLSocket.recv_into(self, buffer, nbytes, flags)
-        self.recvfrom_into = lambda buffer, nbytes=None, flags=0: SSLSocket.recvfrom_into(self, buffer, nbytes, flags)
+        # The initializer for socket overrides the methods send(), recv(), etc.
+        # in the instancce, which we don't need -- but we want to provide the
+        # methods defined in SSLSocket.
+        for attr in _delegate_methods:
+            try:
+                delattr(self, attr)
+            except AttributeError:
+                pass
 
         if certfile and not keyfile:
             keyfile = certfile

Modified: python/trunk/Lib/test/test_ssl.py
==============================================================================
--- python/trunk/Lib/test/test_ssl.py	(original)
+++ python/trunk/Lib/test/test_ssl.py	Sat Apr 24 00:54:59 2010
@@ -11,6 +11,7 @@
 import pprint
 import urllib, urlparse
 import traceback
+import weakref
 
 from BaseHTTPServer import HTTPServer
 from SimpleHTTPServer import SimpleHTTPRequestHandler
@@ -154,6 +155,16 @@
         with self.assertRaisesRegexp(ssl.SSLError, "No cipher can be selected"):
             s.connect(remote)
 
+    @test_support.cpython_only
+    def test_refcycle(self):
+        # Issue #7943: an SSL object doesn't create reference cycles with
+        # itself.
+        s = socket.socket(socket.AF_INET)
+        ss = ssl.wrap_socket(s)
+        wr = weakref.ref(ss)
+        del ss
+        self.assertEqual(wr(), None)
+
 
 class NetworkedTests(unittest.TestCase):
 

Modified: python/trunk/Misc/ACKS
==============================================================================
--- python/trunk/Misc/ACKS	(original)
+++ python/trunk/Misc/ACKS	Sat Apr 24 00:54:59 2010
@@ -743,6 +743,7 @@
 Kalle Svensson
 Paul Swartz
 Thenault Sylvain
+Péter Szabó
 Arfrever Frehtes Taifersar Arahesis
 Geoff Talvola
 William Tanksley

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Apr 24 00:54:59 2010
@@ -25,6 +25,9 @@
 Library
 -------
 
+- Issue #7943: Fix circular reference created when instantiating an SSL
+  socket.  Initial patch by Péter Szabó.
+
 - Issue #8451: Syslog module now uses basename(sys.argv[0]) instead of
   the string "python" as the *ident*.  openlog() arguments are all optional
   and keywords.


More information about the Python-checkins mailing list