Issue #9729: Fix the signature of SSLSocket.recvfrom() and

SSLSocket.sendto() to match the corresponding socket methods.  Also,
fix various SSLSocket methods to raise socket.error rather than an
unhelpful TypeError when called on an unconnected socket.  Original patch
by Andrew Bennetts.

NOTE: obviously, these methods are untested and unused in the real world...
This commit is contained in:
Antoine Pitrou 2010-09-14 14:37:18 +00:00
parent 7a84877de1
commit f7f390a251
3 changed files with 29 additions and 8 deletions

View File

@ -184,14 +184,16 @@ class SSLSocket(socket):
else: else:
return v return v
else: else:
return socket.send(self, data, flags) return self._sock.send(data, flags)
def sendto(self, data, addr, flags=0): def sendto(self, data, flags_or_addr, addr=None):
if self._sslobj: if self._sslobj:
raise ValueError("sendto not allowed on instances of %s" % raise ValueError("sendto not allowed on instances of %s" %
self.__class__) self.__class__)
elif addr is None:
return self._sock.sendto(data, flags_or_addr)
else: else:
return socket.sendto(self, data, addr, flags) return self._sock.sendto(data, flags_or_addr, addr)
def sendall(self, data, flags=0): def sendall(self, data, flags=0):
if self._sslobj: if self._sslobj:
@ -216,7 +218,7 @@ class SSLSocket(socket):
self.__class__) self.__class__)
return self.read(buflen) return self.read(buflen)
else: else:
return socket.recv(self, buflen, flags) return self._sock.recv(buflen, flags)
def recv_into(self, buffer, nbytes=None, flags=0): def recv_into(self, buffer, nbytes=None, flags=0):
if buffer and (nbytes is None): if buffer and (nbytes is None):
@ -233,21 +235,21 @@ class SSLSocket(socket):
buffer[:v] = tmp_buffer buffer[:v] = tmp_buffer
return v return v
else: else:
return socket.recv_into(self, buffer, nbytes, flags) return self._sock.recv_into(buffer, nbytes, flags)
def recvfrom(self, addr, buflen=1024, flags=0): def recvfrom(self, buflen=1024, flags=0):
if self._sslobj: if self._sslobj:
raise ValueError("recvfrom not allowed on instances of %s" % raise ValueError("recvfrom not allowed on instances of %s" %
self.__class__) self.__class__)
else: else:
return socket.recvfrom(self, addr, buflen, flags) return self._sock.recvfrom(buflen, flags)
def recvfrom_into(self, buffer, nbytes=None, flags=0): def recvfrom_into(self, buffer, nbytes=None, flags=0):
if self._sslobj: if self._sslobj:
raise ValueError("recvfrom_into not allowed on instances of %s" % raise ValueError("recvfrom_into not allowed on instances of %s" %
self.__class__) self.__class__)
else: else:
return socket.recvfrom_into(self, buffer, nbytes, flags) return self._sock.recvfrom_into(buffer, nbytes, flags)
def pending(self): def pending(self):
if self._sslobj: if self._sslobj:

View File

@ -179,6 +179,19 @@ class BasicSocketTests(unittest.TestCase):
del ss del ss
self.assertEqual(wr(), None) self.assertEqual(wr(), None)
def test_wrapped_unconnected(self):
# The _delegate_methods in socket.py are correctly delegated to by an
# unconnected SSLSocket, so they will raise a socket.error rather than
# something unexpected like TypeError.
s = socket.socket(socket.AF_INET)
ss = ssl.wrap_socket(s)
self.assertRaises(socket.error, ss.recv, 1)
self.assertRaises(socket.error, ss.recv_into, bytearray(b'x'))
self.assertRaises(socket.error, ss.recvfrom, 1)
self.assertRaises(socket.error, ss.recvfrom_into, bytearray(b'x'), 1)
self.assertRaises(socket.error, ss.send, b'x')
self.assertRaises(socket.error, ss.sendto, b'x', ('0.0.0.0', 0))
class NetworkedTests(unittest.TestCase): class NetworkedTests(unittest.TestCase):

View File

@ -43,6 +43,12 @@ Core and Builtins
Library Library
------- -------
- Issue #9729: Fix the signature of SSLSocket.recvfrom() and
SSLSocket.sendto() to match the corresponding socket methods. Also,
fix various SSLSocket methods to raise socket.error rather than an
unhelpful TypeError when called on an unconnected socket. Original patch
by Andrew Bennetts.
- Issue #9826: OrderedDict.__repr__ can now handle self-referential - Issue #9826: OrderedDict.__repr__ can now handle self-referential
values: d['x'] = d. values: d['x'] = d.