bpo-44362: ssl: improve deprecation warnings and docs (GH-26646)
Signed-off-by: Christian Heimes <christian@python.org>
(cherry picked from commit e26014f1c47d26d6097ff7a0f25384bfbde714a9)
Co-authored-by: Christian Heimes <christian@python.org>
diff --git a/Lib/ssl.py b/Lib/ssl.py
index 2b131de..a16ebd7 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -94,7 +94,7 @@
import os
from collections import namedtuple
from enum import Enum as _Enum, IntEnum as _IntEnum, IntFlag as _IntFlag
-from enum import _simple_enum, _test_simple_enum
+from enum import _simple_enum
import _ssl # if we can't import it, let the error propagate
@@ -387,7 +387,7 @@ def match_hostname(cert, hostname):
returns nothing.
"""
warnings.warn(
- "ssl module: match_hostname() is deprecated",
+ "ssl.match_hostname() is deprecated",
category=DeprecationWarning,
stacklevel=2
)
@@ -492,8 +492,7 @@ class SSLContext(_SSLContext):
def __new__(cls, protocol=None, *args, **kwargs):
if protocol is None:
warnings.warn(
- "ssl module: "
- "SSLContext() without protocol argument is deprecated.",
+ "ssl.SSLContext() without protocol argument is deprecated.",
category=DeprecationWarning,
stacklevel=2
)
@@ -536,7 +535,11 @@ def wrap_bio(self, incoming, outgoing, server_side=False,
)
def set_npn_protocols(self, npn_protocols):
- warnings.warn("NPN is deprecated, use ALPN instead", stacklevel=2)
+ warnings.warn(
+ "ssl NPN is deprecated, use ALPN instead",
+ DeprecationWarning,
+ stacklevel=2
+ )
protos = bytearray()
for protocol in npn_protocols:
b = bytes(protocol, 'ascii')
@@ -940,7 +943,9 @@ def selected_npn_protocol(self):
if a next protocol was not negotiated or if NPN is not supported by one
of the peers."""
warnings.warn(
- "ssl module: NPN is deprecated, use ALPN instead", stacklevel=2
+ "ssl NPN is deprecated, use ALPN instead",
+ DeprecationWarning,
+ stacklevel=2
)
def selected_alpn_protocol(self):
@@ -1157,7 +1162,9 @@ def getpeercert(self, binary_form=False):
def selected_npn_protocol(self):
self._checkClosed()
warnings.warn(
- "ssl module: NPN is deprecated, use ALPN instead", stacklevel=2
+ "ssl NPN is deprecated, use ALPN instead",
+ DeprecationWarning,
+ stacklevel=2
)
return None
@@ -1419,7 +1426,7 @@ def wrap_socket(sock, keyfile=None, certfile=None,
suppress_ragged_eofs=True,
ciphers=None):
warnings.warn(
- "ssl module: wrap_socket is deprecated, use SSLContext.wrap_socket()",
+ "ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket()",
category=DeprecationWarning,
stacklevel=2
)
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 9bd8e22..5dc27df 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -1754,7 +1754,7 @@ class MySSLObject(ssl.SSLObject):
with ctx.wrap_socket(socket.socket(), server_side=True) as sock:
self.assertIsInstance(sock, MySSLSocket)
- obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
+ obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(), server_side=True)
self.assertIsInstance(obj, MySSLObject)
def test_num_tickest(self):
@@ -2888,24 +2888,29 @@ def test_echo(self):
server_context=client_context,
chatty=True, connectionchatty=True,
sni_name=hostname)
- self.assertIn('called a function you should not call',
- str(e.exception))
+ self.assertIn(
+ 'Cannot create a client socket with a PROTOCOL_TLS_SERVER context',
+ str(e.exception)
+ )
with self.subTest(client=ssl.PROTOCOL_TLS_SERVER, server=ssl.PROTOCOL_TLS_SERVER):
with self.assertRaises(ssl.SSLError) as e:
server_params_test(client_context=server_context,
server_context=server_context,
chatty=True, connectionchatty=True)
- self.assertIn('called a function you should not call',
- str(e.exception))
+ self.assertIn(
+ 'Cannot create a client socket with a PROTOCOL_TLS_SERVER context',
+ str(e.exception)
+ )
with self.subTest(client=ssl.PROTOCOL_TLS_CLIENT, server=ssl.PROTOCOL_TLS_CLIENT):
with self.assertRaises(ssl.SSLError) as e:
server_params_test(client_context=server_context,
server_context=client_context,
chatty=True, connectionchatty=True)
- self.assertIn('called a function you should not call',
- str(e.exception))
+ self.assertIn(
+ 'Cannot create a client socket with a PROTOCOL_TLS_SERVER context',
+ str(e.exception))
def test_getpeercert(self):
if support.verbose: