Debugger: added support for the -m option to debug a module as a script.

Fri, 18 Dec 2020 16:48:14 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 18 Dec 2020 16:48:14 +0100
changeset 7895
554fdb07c856
parent 7894
4370a8b30648
child 7896
75ce42b1df23

Debugger: added support for the -m option to debug a module as a script.

eric6/DebugClients/Python/DebugClientBase.py file | annotate | diff | comparison | revisions
eric6/DebugClients/Python/DebugUtilities.py file | annotate | diff | comparison | revisions
--- a/eric6/DebugClients/Python/DebugClientBase.py	Fri Dec 18 12:44:31 2020 +0100
+++ b/eric6/DebugClients/Python/DebugClientBase.py	Fri Dec 18 16:48:14 2020 +0100
@@ -2035,7 +2035,8 @@
     def startProgInDebugger(self, progargs, wd='', host=None,
                             port=None, exceptions=True, tracePython=False,
                             redirect=True, passive=True,
-                            multiprocessSupport=False, codeStr=""):
+                            multiprocessSupport=False, codeStr="",
+                            scriptModule=""):
         """
         Public method used to start the remote debugger.
         
@@ -2057,6 +2058,8 @@
         @type bool
         @param codeStr string containing Python code to execute
         @type str
+        @param scriptModule name of a module to be executed as a script
+        @type str
         @return exit code of the debugged program
         @rtype int
         """
@@ -2111,6 +2114,19 @@
         sys.modules['__main__'] = self.debugMod
         if codeStr:
             code = self.__compileCommand(codeStr)
+        elif scriptModule:
+            import runpy
+            modName, modSpec, code = runpy._get_module_details(scriptModule)
+            self.running = code.co_filename
+            self.debugMod.__dict__.clear()
+            self.debugMod.__dict__.update({
+                "__name__": "__main__",
+                "__file__": self.running,
+                "__package__": modSpec.parent,
+                "__loader__": modSpec.loader,
+                "__spec__": modSpec,
+                "__builtins__": __builtins__,
+            })
         else:
             code = self.__compileFileSource(self.running)
         if code:
@@ -2160,7 +2176,6 @@
                 time.sleep(3)
         return None
     
-    # TODO: add support for the '-m' python invocation => '--module'
     def main(self):
         """
         Public method implementing the main method.
@@ -2175,7 +2190,8 @@
             redirect = True
             passive = True
             multiprocess = False
-            hasCode = False
+            codeStr = ""
+            scriptModule = ""
             while args[0]:
                 if args[0] == '-h':
                     host = args[1]
@@ -2207,8 +2223,13 @@
                 elif args[0] == '--multiprocess':
                     multiprocess = True
                     del args[0]
-                elif args[0] == '--code':
-                    hasCode = True
+                elif args[0] in ('-c', '--code'):
+                    codeStr = args[1]
+                    del args[0]
+                    del args[0]
+                elif args[0] in ('-m', '--module'):
+                    scriptModule = args[1]
+                    del args[0]
                     del args[0]
                 elif args[0] == '--':
                     del args[0]
@@ -2230,15 +2251,11 @@
                 if not self.noencoding:
                     self.__coding = self.defaultCoding
                 patchNewProcessFunctions(multiprocess, self)
-                if hasCode:
-                    codeStr = args.pop(0)
-                else:
-                    codeStr = ""
                 res = self.startProgInDebugger(
                     args, wd, host, port, exceptions=exceptions,
                     tracePython=tracePython, redirect=redirect,
                     passive=passive, multiprocessSupport=multiprocess,
-                    codeStr=codeStr
+                    codeStr=codeStr, scriptModule=scriptModule,
                 )
                 sys.exit(res)
         else:
--- a/eric6/DebugClients/Python/DebugUtilities.py	Fri Dec 18 12:44:31 2020 +0100
+++ b/eric6/DebugClients/Python/DebugUtilities.py	Fri Dec 18 16:48:14 2020 +0100
@@ -316,29 +316,12 @@
             # it is a Python script; insert our interpreter as first argument
             args.insert(0, sys.executable)
     
-    # check for -m invocation => debugging not supported yet
-    if "-m" in args:
-        cm_position = args.index("-m")
-    else:
-        cm_position = 0
-    if cm_position > 0:
-        # check if it belongs to the interpreter or program
-        for pos in range(1, len(args)):
-            if not args[pos].startswith("-"):
-                # first argument not starting with '-' is the program
-                found = True
-                break
-        else:
-            found = False
-        if found and cm_position < pos:
-            # it belongs to the interpreter
-            return quoteArgs(arguments)
-    
     # extract list of interpreter arguments, i.e. all arguments before the
     # first one not starting with '-'.
     interpreter = args.pop(0)
     interpreterArgs = []
     hasCode = False
+    hasScriptModule = False
     while args:
         if args[0].startswith("-"):
             if args[0] in ("-W", "-X"):
@@ -351,6 +334,12 @@
                 args.pop(0)
                 hasCode = True
                 break
+            elif args[0] == "-m":
+                # -m indicates a module to be executed as a script
+                # and ends the arguments list
+                args.pop(0)
+                hasScriptModule = True
+                break
             else:
                 interpreterArgs.append(args.pop(0))
         else:
@@ -382,6 +371,10 @@
         modifiedArguments.append("--multiprocess")
     if hasCode:
         modifiedArguments.append("--code")
+        modifiedArguments.append(args.pop(0))
+    if hasScriptModule:
+        modifiedArguments.append("--module")
+        modifiedArguments.append(args.pop(0))
     modifiedArguments.append("--")
     # end the arguments for DebugClient
     

eric ide

mercurial