eric6/DebugClients/Python/MultiProcessDebugExtension.py

Sun, 05 Jul 2020 11:11:24 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 05 Jul 2020 11:11:24 +0200
branch
multi_processing
changeset 7646
39e3db2b4936
parent 7422
9a008ab4811b
child 7802
eefe954f01e8
permissions
-rw-r--r--

Merged with default to track recent development.

# -*- 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

eric ide

mercurial