summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-04-18 18:33:08 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-04-18 18:33:08 (GMT)
commit172f025bed3cca992c1d4d9fe93d4cd8bee92392 (patch)
treecfed69633555d4a905faa1f4da90fbfcdc63b5a2
parent1226db46517f21cba52bd3efe8b206ddaefd7504 (diff)
downloadcpython-172f025bed3cca992c1d4d9fe93d4cd8bee92392.zip
cpython-172f025bed3cca992c1d4d9fe93d4cd8bee92392.tar.gz
cpython-172f025bed3cca992c1d4d9fe93d4cd8bee92392.tar.bz2
Issue #21068: The ssl.PROTOCOL* constants are now enum members.
-rw-r--r--Lib/ssl.py27
-rw-r--r--Lib/test/test_ssl.py8
-rw-r--r--Misc/NEWS2
3 files changed, 18 insertions, 19 deletions
diff --git a/Lib/ssl.py b/Lib/ssl.py
index d2be3ce..d6a63db 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -92,7 +92,7 @@ import re
import sys
import os
from collections import namedtuple
-from enum import Enum as _Enum
+from enum import Enum as _Enum, IntEnum as _IntEnum
import _ssl # if we can't import it, let the error propagate
@@ -119,30 +119,19 @@ _import_symbols('SSL_ERROR_')
from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN
-from _ssl import PROTOCOL_SSLv3, PROTOCOL_SSLv23, PROTOCOL_TLSv1
from _ssl import _OPENSSL_API_VERSION
+_SSLMethod = _IntEnum('_SSLMethod',
+ {name: value for name, value in vars(_ssl).items()
+ if name.startswith('PROTOCOL_')})
+globals().update(_SSLMethod.__members__)
+
+_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
-_PROTOCOL_NAMES = {
- PROTOCOL_TLSv1: "TLSv1",
- PROTOCOL_SSLv23: "SSLv23",
- PROTOCOL_SSLv3: "SSLv3",
-}
try:
- from _ssl import PROTOCOL_SSLv2
_SSLv2_IF_EXISTS = PROTOCOL_SSLv2
-except ImportError:
+except NameError:
_SSLv2_IF_EXISTS = None
-else:
- _PROTOCOL_NAMES[PROTOCOL_SSLv2] = "SSLv2"
-
-try:
- from _ssl import PROTOCOL_TLSv1_1, PROTOCOL_TLSv1_2
-except ImportError:
- pass
-else:
- _PROTOCOL_NAMES[PROTOCOL_TLSv1_1] = "TLSv1.1"
- _PROTOCOL_NAMES[PROTOCOL_TLSv1_2] = "TLSv1.2"
if sys.platform == "win32":
from _ssl import enum_certificates, enum_crls
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 9f5bd09..a2ab795 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -134,6 +134,14 @@ class BasicSocketTests(unittest.TestCase):
self.assertIn(ssl.HAS_SNI, {True, False})
self.assertIn(ssl.HAS_ECDH, {True, False})
+ def test_str_for_enums(self):
+ # Make sure that the PROTOCOL_* constants have enum-like string
+ # reprs.
+ proto = ssl.PROTOCOL_SSLv3
+ self.assertEqual(str(proto), '_SSLMethod.PROTOCOL_SSLv3')
+ ctx = ssl.SSLContext(proto)
+ self.assertIs(ctx.protocol, proto)
+
def test_random(self):
v = ssl.RAND_status()
if support.verbose:
diff --git a/Misc/NEWS b/Misc/NEWS
index 0c1a573..76fc436 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,8 @@ Core and Builtins
Library
-------
+- Issue #21068: The ssl.PROTOCOL* constants are now enum members.
+
- Issue #21262: New method assert_not_called for Mock.
It raises AssertionError if the mock has been called.