public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Gaius Mulley <gaius@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/modula-2] gcc/m2/tools-src/def2doc.py use with statement to open files.
Date: Sat, 22 Oct 2022 02:16:14 +0000 (GMT)	[thread overview]
Message-ID: <20221022021614.7F42D3858407@sourceware.org> (raw)

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()

                 reply	other threads:[~2022-10-22  2:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221022021614.7F42D3858407@sourceware.org \
    --to=gaius@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).