From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1499) id 7F42D3858407; Sat, 22 Oct 2022 02:16:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F42D3858407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666404974; bh=Pf5oePQo5GHpbnDHq6DewD42kKr6i68Boa1xB8BKLTU=; h=From:To:Subject:Date:From; b=eTOd/Gz+FsblHxjKY7f8bTM685rU7aUnx6SmiBNEwnkkQSSvMUD6fgo9as1vxPw+m JtUTzgfJp9+vUk7fj6nKdcknP9WhalZ+vnm9RDac8j9Tk0q5+JVNxJ6A9uEsdRLw23 mGB+KoAM7n2WF3aoHVfNxvEQOcLSyYPHXx8IZCAc= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Gaius Mulley To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/modula-2] gcc/m2/tools-src/def2doc.py use with statement to open files. X-Act-Checkin: gcc X-Git-Author: Gaius Mulley X-Git-Refname: refs/heads/devel/modula-2 X-Git-Oldrev: b66b8be4a0bd6724581640cf577677bdc06b050b X-Git-Newrev: a0fa580fb07a0dedaa0598e97a643349d10a0e1e Message-Id: <20221022021614.7F42D3858407@sourceware.org> Date: Sat, 22 Oct 2022 02:16:14 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a0fa580fb07a0dedaa0598e97a643349d10a0e1e commit a0fa580fb07a0dedaa0598e97a643349d10a0e1e Author: Gaius Mulley 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 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()