eric6/DebugClients/Python/QProcessExtension.py

branch
multi_processing
changeset 7410
401791e6f50f
parent 7409
1413bfe73d41
child 7411
6d8dcb3551b3
diff -r 1413bfe73d41 -r 401791e6f50f eric6/DebugClients/Python/QProcessExtension.py
--- a/eric6/DebugClients/Python/QProcessExtension.py	Tue Feb 11 18:58:38 2020 +0100
+++ b/eric6/DebugClients/Python/QProcessExtension.py	Wed Feb 12 20:04:31 2020 +0100
@@ -13,6 +13,25 @@
 _debugClient = None
 
 
+# TODO: extend this with first line logic
+def _isPythonProgram(program, arguments):
+    """
+    Protected function to check, if program is a Python interpreter and
+    arguments don't include '-m'.
+    
+    @param program program to be executed
+    @type str
+    @param arguments list of command line arguments
+    @type list of str
+    @return flag indicating a python program and a tuple containing the
+        interpreter to be used and the arguments
+    @rtype tuple of (bool, tuple of (str, list of str))
+    """
+    prog = program.lower()
+    ok = "python" in prog and arguments[0] != '-m'
+    return ok, (program, arguments[:])
+
+
 def patchQProcess(module, debugClient):
     """
     Function to patch the QtCore module's QProcess.
@@ -28,13 +47,16 @@
         """
         Wrapper class for *.QProcess.
         """
+        _origQProcessStartDetached = module.QProcess.startDetached
+        
         def __init__(self, parent=None):
             """
             Constructor
             """
             super(QProcessWrapper, self).__init__(parent)
         
-        def __modifyArgs(self, arguments, multiprocessSupport):
+        @classmethod
+        def modifyArgs(cls, arguments, multiprocessSupport):
             """
             Private method to modify the arguments given to the start method.
             
@@ -71,6 +93,10 @@
             
             return modifiedArguments
         
+        ###################################################################
+        ## Handling of 'start(...)' below
+        ###################################################################
+        
         def start(self, *args, **kwargs):
             """
             Public method to start the process.
@@ -91,7 +117,6 @@
                  (len(args) == 1 and not isinstance(args[0], str)) or
                  len(args) == 0)
             ):
-                # TODO: implement this
                 if len(args) >= 2:
                     program = args[0]
                     arguments = args[1]
@@ -106,16 +131,115 @@
                         mode = args[0]
                     else:
                         mode = module.QIODevice.ReadWrite
-                if "python" in program.lower() and arguments[0] != "-m":
-                    # assume a Python script is to be started
-                    # '-m' option to python is not (yet) supported
-                    newArgs = self.__modifyArgs(
+                ok, (program, arguments) = _isPythonProgram(program, arguments)
+                if ok:
+                    newArgs = self.modifyArgs(
                         arguments, _debugClient.multiprocessSupport)
                     super(QProcessWrapper, self).start(program, newArgs, mode)
                 else:
                     super(QProcessWrapper, self).start(*args, **kwargs)
             else:
                 super(QProcessWrapper, self).start(*args, **kwargs)
+        
+        ###################################################################
+        ## Handling of 'startDetached(...)' below
+        ###################################################################
+        
+        def startDetached(self, *args, **kwargs):
+            """
+            Public method to start the detached process.
+            
+            This method patches the arguments such, that a debug client is
+            started for the Python script. A Python script is assumed, if the
+            program to be started contains the string 'python'.
+            
+            @param args arguments of the start call
+            @type list
+            @param kwargs keyword arguments of the start call
+            @type dict
+            @return flag indicating a successful start
+            @rtype bool
+            """
+            if isinstance(self, str):
+                return QProcessWrapper.startDetachedStatic(
+                    self, *args)
+            else:
+                return self.__startDetached(*args, **kwargs)
+        
+        def __startDetached(self, *args, **kwargs):
+            """
+            Private method to start the detached process.
+            
+            This method patches the arguments such, that a debug client is
+            started for the Python script. A Python script is assumed, if the
+            program to be started contains the string 'python'.
+            
+            @param args arguments of the start call
+            @type list
+            @param kwargs keyword arguments of the start call
+            @type dict
+            @return flag indicating a successful start
+            @rtype bool
+            """
+            if (
+                _debugClient.debugging and
+                _debugClient.multiprocessSupport and
+                len(args) == 0
+            ):
+                program = self.program()
+                arguments = self.arguments()
+                wd = self.workingDirectory()
+                
+                ok, (program, arguments) = _isPythonProgram(program, arguments)
+                if ok:
+                    return QProcessWrapper.startDetachedStatic(
+                        program, arguments, wd)
+                else:
+                    return super(QProcessWrapper, self).startDetached(
+                        *args, **kwargs)
+            else:
+                return super(QProcessWrapper, self).startDetached(
+                    *args, **kwargs)
+        
+        @staticmethod
+        def startDetachedStatic(*args, **kwargs):
+            """
+            Static method to start the detached process.
+            
+            This method patches the arguments such, that a debug client is
+            started for the Python script. A Python script is assumed, if the
+            program to be started contains the string 'python'.
+            
+            @param args arguments of the start call
+            @type list
+            @param kwargs keyword arguments of the start call
+            @type dict
+            @return flag indicating a successful start
+            @rtype bool
+            """
+            if (
+                _debugClient.debugging and
+                _debugClient.multiprocessSupport and
+                (len(args) >= 2 and isinstance(args[1], list))
+            ):
+                program = args[0]
+                arguments = args[1]
+                if len(args) >= 3:
+                    wd = args[2]
+                else:
+                    wd = ""
+                ok, (program, arguments) = _isPythonProgram(program, arguments)
+                if ok:
+                    newArgs = QProcessWrapper.modifyArgs(
+                        arguments, _debugClient.multiprocessSupport)
+                    return QProcessWrapper._origQProcessStartDetached(
+                        program, newArgs, wd)
+                else:
+                    return QProcessWrapper._origQProcessStartDetached(
+                        *args, **kwargs)
+            else:
+                return QProcessWrapper._origQProcessStartDetached(
+                    *args, **kwargs)
     
     _debugClient = debugClient
     module.QProcess = QProcessWrapper

eric ide

mercurial