eric6/E5Network/E5SslErrorHandler.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8176
31965986ecd1
parent 8268
6b8128e0c9d1
--- a/eric6/E5Network/E5SslErrorHandler.py	Fri Apr 02 11:59:41 2021 +0200
+++ b/eric6/E5Network/E5SslErrorHandler.py	Sat May 01 14:27:20 2021 +0200
@@ -7,6 +7,8 @@
 Module implementing a SSL error handler.
 """
 
+import contextlib
+import enum
 import platform
 
 from PyQt5.QtCore import QObject, QByteArray
@@ -21,6 +23,15 @@
 import Globals
 
 
+class E5SslErrorState(enum.Enum):
+    """
+    Class defining the SSL error handling states.
+    """
+    NOT_IGNORED = 0
+    SYSTEM_IGNORED = 1
+    USER_IGNORED = 2
+
+
 class E5SslErrorHandler(QObject):
     """
     Class implementing a handler for SSL errors.
@@ -28,17 +39,13 @@
     It also initializes the default SSL configuration with certificates
     permanently accepted by the user already.
     """
-    NotIgnored = 0
-    SystemIgnored = 1
-    UserIgnored = 2
-    
     def __init__(self, parent=None):
         """
         Constructor
         
         @param parent reference to the parent object (QObject)
         """
-        super(E5SslErrorHandler, self).__init__(parent)
+        super().__init__(parent)
         
         caList = self.__getSystemCaCertificates()
         if Preferences.Prefs.settings.contains("Help/CaCertificatesDict"):
@@ -64,11 +71,9 @@
         except AttributeError:
             sslProtocol = QSsl.SslProtocol.SecureProtocols
         sslCfg.setProtocol(sslProtocol)
-        try:
+        with contextlib.suppress(AttributeError):
             sslCfg.setSslOption(QSsl.SslOption.SslOptionDisableCompression,
                                 True)
-        except AttributeError:
-            pass
         QSslConfiguration.setDefaultConfiguration(sslCfg)
     
     def sslErrorsReplySlot(self, reply, errors):
@@ -106,12 +111,15 @@
         """
         Public method to handle SSL errors.
         
-        @param errors list of SSL errors (list of QSslError)
-        @param server name of the server (string)
-        @param port value of the port (integer)
-        @return tuple indicating to ignore the SSL errors (one of NotIgnored,
-            SystemIgnored or UserIgnored) and indicating a change of the
-            default SSL configuration (boolean)
+        @param errors list of SSL errors
+        @type list of QSslError
+        @param server name of the server
+        @type str
+        @param port value of the port
+        @type int
+        @return tuple indicating to ignore the SSL errors and indicating a
+            change of the default SSL configuration
+        @rtype tuple of (E5SslErrorState, bool)
         """
         caMerge = {}
         certificateDict = Globals.toDict(
@@ -136,7 +144,7 @@
                     if cert not in caNew:
                         caNew.append(cert)
         if not errorStrings:
-            return E5SslErrorHandler.SystemIgnored, False
+            return E5SslErrorState.SYSTEM_IGNORED, False
         
         errorString = '.</li><li>'.join(errorStrings)
         ret = E5MessageBox.yesNo(
@@ -177,12 +185,10 @@
                         sslCfg.setProtocol(QSsl.SslProtocol.TlsV1_1OrLater)
                     except AttributeError:
                         sslCfg.setProtocol(QSsl.SslProtocol.SecureProtocols)
-                    try:
+                    with contextlib.suppress(AttributeError):
                         sslCfg.setSslOption(
                             QSsl.SslOption.SslOptionDisableCompression,
                             True)
-                    except AttributeError:
-                        pass
                     QSslConfiguration.setDefaultConfiguration(sslCfg)
                     
                     certificateDict = {}
@@ -195,10 +201,10 @@
                         "Ssl/CaCertificatesDict",
                         certificateDict)
             
-            return E5SslErrorHandler.UserIgnored, caRet
+            return E5SslErrorState.USER_IGNORED, caRet
         
         else:
-            return E5SslErrorHandler.NotIgnored, False
+            return E5SslErrorState.NOT_IGNORED, False
     
     def __certToString(self, cert):
         """

eric ide

mercurial