[Python-checkins] cpython: Issue #21068: The ssl.PROTOCOL* constants are now enum members.

antoine.pitrou python-checkins at python.org
Fri Apr 18 20:33:15 CEST 2014


http://hg.python.org/cpython/rev/f776771ab0ee
changeset:   90404:f776771ab0ee
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Fri Apr 18 20:33:08 2014 +0200
summary:
  Issue #21068: The ssl.PROTOCOL* constants are now enum members.

files:
  Lib/ssl.py           |  29 +++++++++--------------------
  Lib/test/test_ssl.py |   8 ++++++++
  Misc/NEWS            |   2 ++
  3 files changed, 19 insertions(+), 20 deletions(-)


diff --git a/Lib/ssl.py b/Lib/ssl.py
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -92,7 +92,7 @@
 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 @@
 
 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 = {
-    PROTOCOL_TLSv1: "TLSv1",
-    PROTOCOL_SSLv23: "SSLv23",
-    PROTOCOL_SSLv3: "SSLv3",
-}
-try:
-    from _ssl import PROTOCOL_SSLv2
-    _SSLv2_IF_EXISTS = PROTOCOL_SSLv2
-except ImportError:
-    _SSLv2_IF_EXISTS = None
-else:
-    _PROTOCOL_NAMES[PROTOCOL_SSLv2] = "SSLv2"
+_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
 
 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"
+    _SSLv2_IF_EXISTS = PROTOCOL_SSLv2
+except NameError:
+    _SSLv2_IF_EXISTS = None
 
 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
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -134,6 +134,14 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,8 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list