Mon, 17 Feb 2020 19:23:27 +0100
Implemented multi process debugging support for the 'subprocess' module.
# -*- coding: utf-8 -*- # Copyright (c) 2002 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a function to patch the process creation functions to support multiprocess debugging. """ import sys from DebugUtilities import isWindowsPlatform, patchArguments, isPythonProgram _debugClient = None def patchModule(module, functionName, createFunction): """ Function to replace a function of a module with a modified one. @param module reference to the module @type types.ModuleType @param functionName name of the function to be replaced @type str @param createFunction function creating the replacement @type types.FunctionType """ if hasattr(module, functionName): originalName = 'original_' + functionName if not hasattr(module, originalName): setattr(module, originalName, getattr(module, functionName)) setattr(module, functionName, createFunction(originalName)) def createExecl(originalName): """ Function to patch the 'execl' process creation functions. <ul> <li>os.execl(path, arg0, arg1, ...)</li> <li>os.execle(path, arg0, arg1, ..., env)</li> <li>os.execlp(file, arg0, arg1, ...)</li> <li>os.execlpe(file, arg0, arg1, ..., env)</li> </ul> """ def newExecl(path, *args): """ Function replacing the 'execl' functions of the os module. """ print(args) import os if ( _debugClient.debugging and _debugClient.multiprocessSupport ): args = patchArguments(_debugClient, args) if isPythonProgram(args[0]): path = args[0] print(args) return getattr(os, originalName)(path, *args) return newExecl def patchNewProcessFunctions(multiprocessEnabled, debugClient): """ Function to patch the process creation functions to support multiprocess debugging. @param multiprocessEnabled flag indicating multiprocess support @type bool @param debugClient reference to the debug client object @type DebugClient """ global _debugClient if not multiprocessEnabled: # return without patching return import os # patch 'os.exec...()' functions patchModule(os, "execl", createExecl) patchModule(os, "execle", createExecl) patchModule(os, "execlp", createExecl) patchModule(os, "execlpe", createExecl) # TODO: implement patching of the various functions of the os module _debugClient = debugClient