|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a function to patch subprocess.Popen to support debugging |
|
8 of the process. |
|
9 """ |
|
10 |
|
11 import os |
|
12 import shlex |
|
13 |
|
14 from DebugUtilities import isPythonProgram, patchArguments |
|
15 |
|
16 _debugClient = None |
|
17 |
|
18 |
|
19 def patchSubprocess(module, debugClient): |
|
20 """ |
|
21 Function to patch the subprocess module. |
|
22 |
|
23 @param module reference to the imported module to be patched |
|
24 @type module |
|
25 @param debugClient reference to the debug client object |
|
26 @type DebugClient |
|
27 """ # __IGNORE_WARNING_D234__ |
|
28 global _debugClient |
|
29 |
|
30 class PopenWrapper(module.Popen): |
|
31 """ |
|
32 Wrapper class for subprocess.Popen. |
|
33 """ |
|
34 def __init__(self, arguments, *args, **kwargs): |
|
35 """ |
|
36 Constructor |
|
37 |
|
38 @param arguments command line arguments for the new process |
|
39 @type list of str or str |
|
40 @param args constructor arguments of Popen |
|
41 @type list |
|
42 @param kwargs constructor keyword only arguments of Popen |
|
43 @type dict |
|
44 """ |
|
45 if ( |
|
46 _debugClient.debugging and |
|
47 _debugClient.multiprocessSupport and |
|
48 isinstance(arguments, (str, list)) |
|
49 ): |
|
50 if isinstance(arguments, str): |
|
51 # convert to arguments list |
|
52 arguments = shlex.split(arguments) |
|
53 else: |
|
54 # create a copy of the arguments |
|
55 arguments = arguments[:] |
|
56 ok = isPythonProgram(arguments[0]) |
|
57 if ok: |
|
58 scriptName = os.path.basename(arguments[0]) |
|
59 if not _debugClient.skipMultiProcessDebugging(scriptName): |
|
60 arguments = patchArguments( |
|
61 _debugClient, arguments, noRedirect=True |
|
62 ) |
|
63 |
|
64 super().__init__(arguments, *args, **kwargs) |
|
65 |
|
66 _debugClient = debugClient |
|
67 module.Popen = PopenWrapper |