From dfb299bb95040d5e96690afd93f7212e81b70722 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 23 Apr 2010 22:54:59 +0000 Subject: [PATCH] =?UTF-8?q?Issue=20#7943:=20Fix=20circular=20reference=20c?= =?UTF-8?q?reated=20when=20instantiating=20an=20SSL=20socket.=20=20Initial?= =?UTF-8?q?=20patch=20by=20P=C3=A9ter=20Szab=C3=B3.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/ssl.py | 17 +++++++++-------- Lib/test/test_ssl.py | 11 +++++++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Lib/ssl.py b/Lib/ssl.py index c960aaa76a7..4f291f44869 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -75,7 +75,7 @@ from _ssl import \ 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 @@ class SSLSocket(socket): 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 diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 458db269e5d..5917b513df7 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -11,6 +11,7 @@ import os import pprint import urllib, urlparse import traceback +import weakref from BaseHTTPServer import HTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler @@ -154,6 +155,16 @@ class BasicTests(unittest.TestCase): 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): diff --git a/Misc/ACKS b/Misc/ACKS index cd74072b1ad..cb827da1e21 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -743,6 +743,7 @@ Andrew Svetlov Kalle Svensson Paul Swartz Thenault Sylvain +Péter Szabó Arfrever Frehtes Taifersar Arahesis Geoff Talvola William Tanksley diff --git a/Misc/NEWS b/Misc/NEWS index 6be66b35bb3..26ce4e051f3 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -25,6 +25,9 @@ Core and Builtins 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.