src/eric7/Debugger/DebugServer.py

branch
eric7-maintenance
changeset 9549
67295777d9fe
parent 9442
906485dcd210
parent 9521
a663a8be64da
child 9654
7328efba128b
--- a/src/eric7/Debugger/DebugServer.py	Mon Oct 31 14:07:57 2022 +0100
+++ b/src/eric7/Debugger/DebugServer.py	Wed Nov 30 09:19:51 2022 +0100
@@ -7,28 +7,35 @@
 Module implementing the debug server.
 """
 
+import contextlib
+import importlib
 import os
 import shlex
-import contextlib
+
+from PyQt6.QtCore import QModelIndex, pyqtSignal, pyqtSlot
+from PyQt6.QtNetwork import QHostAddress, QHostInfo, QNetworkInterface, QTcpServer
 
-from PyQt6.QtCore import pyqtSignal, pyqtSlot, QModelIndex
-from PyQt6.QtNetwork import QTcpServer, QHostAddress, QHostInfo, QNetworkInterface
+from eric7 import Preferences
+from eric7.EricWidgets import EricMessageBox
+from eric7.EricWidgets.EricApplication import ericApp
 
-from eric7.EricWidgets.EricApplication import ericApp
-from eric7.EricWidgets import EricMessageBox
-
+from . import DebugClientCapabilities
 from .BreakPointModel import BreakPointModel
 from .WatchPointModel import WatchPointModel
-from . import DebugClientCapabilities
-
-from eric7 import Preferences
-
 
 DebuggerInterfaces = {
     "Python": "DebuggerInterfacePython",
     "None": "DebuggerInterfaceNone",
 }
 
+NetworkInterfaceMapping = {
+    "all": QHostAddress.SpecialAddress.Any,
+    "allv4": QHostAddress.SpecialAddress.AnyIPv4,
+    "allv6": QHostAddress.SpecialAddress.AnyIPv6,
+    "localv4": QHostAddress.SpecialAddress.LocalHost,
+    "localv6": QHostAddress.SpecialAddress.LocalHostIPv6,
+}
+
 
 class DebugServer(QTcpServer):
     """
@@ -191,28 +198,34 @@
         self.__reportedWatchpointIssues = []
 
         self.networkInterface = Preferences.getDebugger("NetworkInterface")
-        if self.networkInterface == "all":
-            hostAddress = QHostAddress("0.0.0.0")
-            # QHostAddress.SpecialAddress.Any)  # secok
-        elif self.networkInterface == "allv6":
-            hostAddress = QHostAddress("::")
-            # QHostAddress.SpecialAddress.AnyIPv6)
-        else:
-            hostAddress = QHostAddress(self.networkInterface)
+        hostAddress = (
+            QHostAddress(NetworkInterfaceMapping[self.networkInterface])
+            if self.networkInterface in NetworkInterfaceMapping
+            else QHostAddress(self.networkInterface)
+        )
         (
             self.networkInterfaceName,
             self.networkInterfaceIndex,
         ) = self.__getNetworkInterfaceAndIndex(self.networkInterface)
 
         if not preventPassiveDebugging and Preferences.getDebugger("PassiveDbgEnabled"):
-            sock = Preferences.getDebugger("PassiveDbgPort")  # default: 42424
-            self.listen(hostAddress, sock)
+            port = Preferences.getDebugger("PassiveDbgPort")  # default: 42424
+            self.listen(hostAddress, port)
             self.passive = True
             self.passiveClientExited = False
         else:
             if hostAddress.toString().lower().startswith("fe80"):
                 hostAddress.setScopeId(self.networkInterfaceName)
-            self.listen(hostAddress)
+            if Preferences.getDebugger("NetworkPortFixed"):
+                port = Preferences.getDebugger("NetworkPort")
+                res = self.listen(hostAddress, port)
+                if not res and Preferences.getDebugger("NetworkPortIncrement"):
+                    maxPort = port + 100  # try a maximum of 100 ports
+                    while not res and port < maxPort:
+                        port += 1
+                        res = self.listen(hostAddress, port)
+            else:
+                self.listen(hostAddress)
             self.passive = False
 
         self.debuggerInterface = None
@@ -262,13 +275,13 @@
         @return IP address or hostname
         @rtype str
         """
-        if self.networkInterface == "all":
-            if localhost:
+        if self.networkInterface in ("allv4", "localv4"):
+            if localhost or self.networkInterface == "localv4":
                 return "127.0.0.1"
             else:
                 return "{0}@@v4".format(QHostInfo.localHostName())
-        elif self.networkInterface == "allv6":
-            if localhost:
+        elif self.networkInterface in ("all", "allv6", "localv6"):
+            if localhost or self.networkInterface == "localv6":
                 return "::1"
             else:
                 return "{0}@@v6".format(QHostInfo.localHostName())
@@ -400,12 +413,7 @@
         Private method to register the available internal debugger interfaces.
         """
         for name, interface in DebuggerInterfaces.items():
-            modName = "eric7.Debugger.{0}".format(interface)
-            mod = __import__(modName)
-            components = modName.split(".")
-            for comp in components[1:]:
-                mod = getattr(mod, comp)
-
+            mod = importlib.import_module(".{0}".format(interface), __package__)
             self.registerDebuggerInterface(name, mod.getRegistryData)
 
     def getSupportedLanguages(self, shellOnly=False):
@@ -2187,7 +2195,11 @@
         @return string for the project environment
         @rtype str
         """
-        if ericApp().getObject("Project").isOpen():
-            return self.tr("<project>")
-        else:
+        try:
+            if ericApp().getObject("Project").isOpen():
+                return self.tr("<project>")
+            else:
+                return ""
+        except KeyError:
+            # The project object is not present
             return ""

eric ide

mercurial