Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42382fe

Browse files
committed
fix issue #8807: adds a context parameter to POP3_SSL class.
1 parent ccfb91c commit 42382fe

5 files changed

Lines changed: 53 additions & 8 deletions

File tree

Doc/library/poplib.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ A single class is provided by the :mod:`poplib` module:
3232
be used).
3333

3434

35-
.. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None[, timeout])
35+
.. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)
3636

3737
This is a subclass of :class:`POP3` that connects to the server over an SSL
3838
encrypted socket. If *port* is not specified, 995, the standard POP3-over-SSL
3939
port is used. *keyfile* and *certfile* are also optional - they can contain a
4040
PEM formatted private key and certificate chain file for the SSL connection.
41-
*timeout* works as in the :class:`POP3` constructor.
41+
*timeout* works as in the :class:`POP3` constructor. *context* parameter is a
42+
:class:`ssl.SSLContext` object which allows bundling SSL configuration
43+
options, certificates and private keys into a single (potentially long-lived)
44+
structure.
45+
46+
.. versionchanged:: 3.2
47+
*context* parameter added.
4248

4349

4450
One exception is defined as an attribute of the :mod:`poplib` module:

Doc/whatsnew/3.2.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ New, Improved, and Deprecated Modules
186186

187187
(Contributed by Giampaolo Rodolà; :issue:`8866`.)
188188

189+
* :class:`~poplib.POP3_SSL` class now accepts a *context* parameter, which is a
190+
:class:`ssl.SSLContext` object allowing bundling SSL configuration options,
191+
certificates and private keys into a single (potentially long-lived)
192+
structure.
193+
194+
(Contributed by Giampaolo Rodolà; :issue:`8807`.)
195+
189196
Multi-threading
190197
===============
191198

Lib/poplib.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,26 @@ class POP3_SSL(POP3):
331331
See the methods of the parent class POP3 for more documentation.
332332
"""
333333

334-
def __init__(self, host, port=POP3_SSL_PORT,
335-
keyfile=None, certfile=None,
336-
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
334+
def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None,
335+
timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None):
336+
if context is not None and keyfile is not None:
337+
raise ValueError("context and keyfile arguments are mutually "
338+
"exclusive")
339+
if context is not None and certfile is not None:
340+
raise ValueError("context and certfile arguments are mutually "
341+
"exclusive")
337342
self.keyfile = keyfile
338343
self.certfile = certfile
344+
self.context = context
339345
POP3.__init__(self, host, port, timeout)
340346

341347
def _create_socket(self, timeout):
342348
sock = POP3._create_socket(self, timeout)
343-
return ssl.wrap_socket(sock, self.keyfile, self.certfile)
349+
if self.context is not None:
350+
sock = self.context.wrap_socket(sock)
351+
else:
352+
sock = ssl.wrap_socket(sock, self.keyfile, self.certfile)
353+
return sock
344354

345355
__all__.append("POP3_SSL")
346356

Lib/test/test_poplib.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def __init__(self, address, af=socket.AF_INET):
122122
self.active = False
123123
self.active_lock = threading.Lock()
124124
self.host, self.port = self.socket.getsockname()[:2]
125+
self.handler_instance = None
125126

126127
def start(self):
127128
assert not self.active
@@ -145,8 +146,7 @@ def stop(self):
145146

146147
def handle_accept(self):
147148
conn, addr = self.accept()
148-
self.handler = self.handler(conn)
149-
self.close()
149+
self.handler_instance = self.handler(conn)
150150

151151
def handle_connect(self):
152152
self.close()
@@ -287,6 +287,23 @@ def setUp(self):
287287
def test__all__(self):
288288
self.assertIn('POP3_SSL', poplib.__all__)
289289

290+
def test_context(self):
291+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
292+
self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host,
293+
self.server.port, keyfile=CERTFILE, context=ctx)
294+
self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host,
295+
self.server.port, certfile=CERTFILE, context=ctx)
296+
self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host,
297+
self.server.port, keyfile=CERTFILE,
298+
certfile=CERTFILE, context=ctx)
299+
300+
self.client.quit()
301+
self.client = poplib.POP3_SSL(self.server.host, self.server.port,
302+
context=ctx)
303+
self.assertIsInstance(self.client.sock, ssl.SSLSocket)
304+
self.assertIs(self.client.sock.context, ctx)
305+
self.assertTrue(self.client.noop().startswith(b'+OK'))
306+
290307

291308
class TestTimeouts(TestCase):
292309

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ Extensions
9393
Library
9494
-------
9595

96+
- Issue #8807: poplib.POP3_SSL class now accepts a context parameter, which is a
97+
ssl.SSLContext object allowing bundling SSL configuration options,
98+
certificates and private keys into a single (potentially long-lived)
99+
structure.
100+
96101
- Issue #8866: parameters passed to socket.getaddrinfo can now be specified as
97102
single keyword arguments.
98103

0 commit comments

Comments
 (0)