public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/modula-2] gcc/m2/tools-src/def2doc.py use with statement to open files.
@ 2022-10-22  2:16 Gaius Mulley
  0 siblings, 0 replies; only message in thread
From: Gaius Mulley @ 2022-10-22  2:16 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a0fa580fb07a0dedaa0598e97a643349d10a0e1e

commit a0fa580fb07a0dedaa0598e97a643349d10a0e1e
Author: Gaius Mulley <gaiusmod2@gmail.com>
Date:   Sat Oct 22 03:15:54 2022 +0100

    gcc/m2/tools-src/def2doc.py use with statement to open files.
    
    Use with statement to open files.
    
    gcc/m2/ChangeLog:
    
            * tools-src/def2doc.py (parseDefinition): use with statement.
            (doCat) Use with statement.  (handleFile) New function.
            (main) Use with statement.
    
    Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>

Diff:
---
 gcc/m2/tools-src/def2doc.py | 85 +++++++++++++++++++++++----------------------
 1 file changed, 43 insertions(+), 42 deletions(-)

diff --git a/gcc/m2/tools-src/def2doc.py b/gcc/m2/tools-src/def2doc.py
index 6f8f934ee62..163987ee910 100755
--- a/gcc/m2/tools-src/def2doc.py
+++ b/gcc/m2/tools-src/def2doc.py
@@ -234,41 +234,40 @@ def checkIndex(line):
 
 def parseDefinition(dir, source, build, file, needPage):
     output.write("\n")
-    f = open(findFile(dir, build, source, file), "r")
-    initState()
-    line = f.readline()
-    while (str.find(line, "(*") != -1):
-        removeInitialComments(f, line)
+    with open(findFile(dir, build, source, file), "r") as f:
+        initState()
         line = f.readline()
+        while (str.find(line, "(*") != -1):
+            removeInitialComments(f, line)
+            line = f.readline()
 
-    while (str.find(line, "DEFINITION") == -1):
-        line = f.readline()
+        while (str.find(line, "DEFINITION") == -1):
+            line = f.readline()
 
-    output.write("@example\n")
-    output.write(str.rstrip(line) + "\n")
-    line = f.readline()
-    if len(str.rstrip(line)) == 0:
-        output.write(str.replace(str.replace(str.rstrip(line),
-                                             "{", "@{"), "}", "@}") + "\n")
+        output.write("@example\n")
+        output.write(str.rstrip(line) + "\n")
         line = f.readline()
-        if (str.find(line, "(*") != -1):
-            removeFields(f, line)
+        if len(str.rstrip(line)) == 0:
+            output.write(str.replace(str.replace(str.rstrip(line),
+                                                 "{", "@{"), "}", "@}") + "\n")
+            line = f.readline()
+            if (str.find(line, "(*") != -1):
+                removeFields(f, line)
+            else:
+                output.write(str.rstrip(line) + "\n")
         else:
             output.write(str.rstrip(line) + "\n")
-    else:
-        output.write(str.rstrip(line) + "\n")
 
-    line = f.readline()
-    while line:
-        line = str.rstrip(line)
-        checkIndex(line)
-        output.write(str.replace(str.replace(line, "{", "@{"), "}", "@}"))
-        output.write("\n")
-        line = f.readline()
-    output.write("@end example\n")
-    if needPage:
-        output.write("@page\n")
-    f.close()
+            line = f.readline()
+        while line:
+            line = str.rstrip(line)
+            checkIndex(line)
+            output.write(str.replace(str.replace(line, "{", "@{"), "}", "@}"))
+            output.write("\n")
+            line = f.readline()
+        output.write("@end example\n")
+        if needPage:
+            output.write("@page\n")
 
 
 def parseModules(up, dir, build, source, listOfModules):
@@ -294,12 +293,11 @@ def parseModules(up, dir, build, source, listOfModules):
 #  doCat - displays the contents of file, name, to stdout
 
 def doCat(name):
-    file = open(name, "r")
-    line = file.readline()
-    while line:
-        output.write(str.rstrip(line) + "\n")
+    with open(name, "r") as file:
         line = file.readline()
-    file.close()
+        while line:
+            output.write(str.rstrip(line) + "\n")
+            line = file.readline()
 
 
 #  moduleMenu - generates a simple menu for all definition modules
@@ -423,13 +421,7 @@ def collectArgs():
     return args
 
 
-def main():
-    global args, output
-    args = collectArgs()
-    if args.outputfile is None:
-        output = sys.stdout
-    else:
-        output = open(args.outputfile, "w")
+def handleFile():
     if args.inputfile is None:
         displayCopyright()
         displayMenu()
@@ -437,8 +429,17 @@ def main():
     else:
         parseDefinition(".", args.sourcedir, args.builddir,
                         args.inputfile, False)
-    if not (args.outputfile is None):
-        output.close()
+
+
+def main():
+    global args, output
+    args = collectArgs()
+    if args.outputfile is None:
+        output = sys.stdout
+        handleFile()
+    else:
+        with open(args.outputfile, "w") as output:
+            handleFile()
 
 
 main()

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-22  2:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22  2:16 [gcc/devel/modula-2] gcc/m2/tools-src/def2doc.py use with statement to open files Gaius Mulley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).