scripts/patch_modpython.py

changeset 7785
9978016560ec
parent 7781
607a6098cb44
child 7836
2f0d208b8137
--- a/scripts/patch_modpython.py	Tue Oct 13 19:02:26 2020 +0200
+++ b/scripts/patch_modpython.py	Wed Oct 14 17:50:39 2020 +0200
@@ -82,41 +82,40 @@
     
     try:
         filename = os.path.join(modDir, "apache.py")
-        f = open(filename, "r", encoding="utf-8")
+        with open(filename, "r", encoding="utf-8") as f:
+            lines = f.readlines()
     except EnvironmentError:
         print("The file {0} does not exist. Aborting.".format(filename))
         sys.exit(1)
     
-    lines = f.readlines()
-    f.close()
-    
     pdbFound = False
     ericFound = False
     
     sn = "apache.py"
-    s = open(sn, "w", encoding="utf-8")
-    for line in lines:
-        if not pdbFound and line.startswith("import pdb"):
-            s.write("import eric6.DebugClients.Python.eric6dbgstub as pdb\n")
-            pdbFound = True
-        else:
-            s.write(line)
-            if line.startswith("import eric6"):
-                ericFound = True
-    
-    if not ericFound:
-        s.write("\n")
-        s.write('def initDebugger(name):\n')
-        s.write('    """\n')
-        s.write('    Initialize the debugger and set the script name to be'
-                ' reported \n')
-        s.write('    by the debugger. This is a patch for eric6.\n')
-        s.write('    """\n')
-        s.write('    if not pdb.initDebugger("standard"):\n')
-        s.write('        raise ImportError("Could not initialize debugger")\n')
-        s.write('    pdb.setScriptname(name)\n')
-        s.write("\n")
-    s.close()
+    with open(sn, "w", encoding="utf-8") as s:
+        for line in lines:
+            if not pdbFound and line.startswith("import pdb"):
+                s.write("import eric6.DebugClients.Python.eric6dbgstub as"
+                        " pdb\n")
+                pdbFound = True
+            else:
+                s.write(line)
+                if line.startswith("import eric6"):
+                    ericFound = True
+        
+        if not ericFound:
+            s.write("\n")
+            s.write('def initDebugger(name):\n')
+            s.write('    """\n')
+            s.write('    Initialize the debugger and set the script name to be'
+                    ' reported \n')
+            s.write('    by the debugger. This is a patch for eric6.\n')
+            s.write('    """\n')
+            s.write('    if not pdb.initDebugger("standard"):\n')
+            s.write('        raise ImportError("Could not initialize'
+                    ' debugger")\n')
+            s.write('    pdb.setScriptname(name)\n')
+            s.write("\n")
     
     if ericFound:
         print("Mod_python is already patched for eric6.")

eric ide

mercurial