Tue, 11 Feb 2020 18:58:38 +0100
Continued with the multiprocess debugger.
# -*- coding: utf-8 -*- # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a function to patch QProcess to support debugging of the process. """ import os _debugClient = None def patchQProcess(module, debugClient): """ Function to patch the QtCore module's QProcess. @param module reference to the imported module to be patched @type module @param debugClient reference to the debug client object @type DebugClient """ # __IGNORE_WARNING_D234__ global _debugClient class QProcessWrapper(module.QProcess): """ Wrapper class for *.QProcess. """ def __init__(self, parent=None): """ Constructor """ super(QProcessWrapper, self).__init__(parent) def __modifyArgs(self, arguments, multiprocessSupport): """ Private method to modify the arguments given to the start method. @param arguments list of program arguments @type list of str @return modified argument list @rtype list of str """ (wd, host, port, exceptions, tracePython, redirect, noencoding) = _debugClient.startOptions[:7] modifiedArguments = [ os.path.join(os.path.dirname(__file__), "DebugClient.py"), "-h", host, "-p", str(port), "--no-passive", ] if wd: modifiedArguments.extend(["-w", wd]) if not exceptions: modifiedArguments.append("-e") if tracePython: modifiedArguments.append("-t") if not redirect: modifiedArguments.append("-n") if noencoding: modifiedArguments.append("--no-encoding") if multiprocessSupport: modifiedArguments.append("--multiprocess") modifiedArguments.append("--") # end the arguments for DebugClient modifiedArguments.extend(arguments) return modifiedArguments def start(self, *args, **kwargs): """ Public method to start the 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 """ if ( _debugClient.debugging and _debugClient.multiprocessSupport and ((len(args) >= 2 and isinstance(args[1], list)) or (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] if len(args) > 2: mode = args[2] else: mode = module.QIODevice.ReadWrite else: program = self.program() arguments = self.arguments() if len(args) == 1: 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( arguments, _debugClient.multiprocessSupport) super(QProcessWrapper, self).start(program, newArgs, mode) else: super(QProcessWrapper, self).start(*args, **kwargs) else: super(QProcessWrapper, self).start(*args, **kwargs) _debugClient = debugClient module.QProcess = QProcessWrapper