From 3e58be1551ef40b5122ed4575a1eb71cb2e00c33 Mon Sep 17 00:00:00 2001 From: dmiller Date: Mon, 30 Dec 2019 06:46:34 +0000 Subject: [PATCH] Use 'with' statements for file open/close. #1834 --- ndiff/setup.py | 5 +--- zenmap/setup.py | 5 +--- .../zenmap/locale/xgettext-profile_editor.py | 5 +--- zenmap/zenmapCore/NmapParser.py | 5 +--- zenmap/zenmapCore/ScriptMetadata.py | 25 ++++--------------- 5 files changed, 9 insertions(+), 36 deletions(-) diff --git a/ndiff/setup.py b/ndiff/setup.py index b5e254c5b..cb35459e4 100644 --- a/ndiff/setup.py +++ b/ndiff/setup.py @@ -238,13 +238,10 @@ for dir in dirs: if INSTALLED_FILES_NAME == self.record: distutils.log.warn("warning: installation record is overwriting " "--record file '%s'." % self.record) - f = open(INSTALLED_FILES_NAME, "w") - try: + with open(INSTALLED_FILES_NAME, "w") as f: for output in self.get_installed_files(): assert "\n" not in output print >> f, output - finally: - f.close() class my_uninstall(distutils.cmd.Command): diff --git a/zenmap/setup.py b/zenmap/setup.py index 0b975fb8e..7daae5035 100755 --- a/zenmap/setup.py +++ b/zenmap/setup.py @@ -485,13 +485,10 @@ for dir in dirs: if INSTALLED_FILES_NAME == self.record: distutils.log.warn("warning: installation record is overwriting " "--record file '%s'." % self.record) - f = open(INSTALLED_FILES_NAME, "w") - try: + with open(INSTALLED_FILES_NAME, "w") as f: for output in self.get_installed_files(): assert "\n" not in output print >> f, output - finally: - f.close() class my_uninstall(Command): diff --git a/zenmap/share/zenmap/locale/xgettext-profile_editor.py b/zenmap/share/zenmap/locale/xgettext-profile_editor.py index 6ac69da99..3c498e69a 100755 --- a/zenmap/share/zenmap/locale/xgettext-profile_editor.py +++ b/zenmap/share/zenmap/locale/xgettext-profile_editor.py @@ -48,13 +48,10 @@ if directory is not None: os.chdir(directory) for fn in filenames: - f = open(fn, "r") - try: + with open(fn, "r") as f: parser = xml.sax.make_parser() parser.setContentHandler(Handler()) parser.parse(f) - finally: - f.close() if len(filenames) < 2: parser = xml.sax.make_parser() diff --git a/zenmap/zenmapCore/NmapParser.py b/zenmap/zenmapCore/NmapParser.py index dde80a826..4b72e51a2 100644 --- a/zenmap/zenmapCore/NmapParser.py +++ b/zenmap/zenmapCore/NmapParser.py @@ -817,12 +817,9 @@ class NmapParserSAX(ParserBasics, ContentHandler): def parse_file(self, filename): """Parse an Nmap XML file from the named file.""" - f = open(filename, "r") - try: + with open(filename, "r") as f: self.parse(f) self.filename = filename - finally: - f.close() def _parse_nmaprun(self, attrs): run_tag = "nmaprun" diff --git a/zenmap/zenmapCore/ScriptMetadata.py b/zenmap/zenmapCore/ScriptMetadata.py index f3c1394ea..d39be1b39 100644 --- a/zenmap/zenmapCore/ScriptMetadata.py +++ b/zenmap/zenmapCore/ScriptMetadata.py @@ -151,13 +151,10 @@ class ScriptDB (object): def __init__(self, script_db_path=None): self.unget_buf = "" - self.f = open(script_db_path, "r") self.lineno = 1 self.line = "" - try: + with open(script_db_path, "r") as self.f: self.entries_list = self.parse() - finally: - self.f.close() def syntax_error(self, message): e = ScriptDBSyntaxError(message) @@ -368,15 +365,12 @@ class ScriptMetadata (object): self.get_string_variable(filename, "author")] filepath = os.path.join(self.scripts_dir, filename) - f = open(filepath, "r") - try: + with open(filepath, "r") as f: for tag_name, tag_text in nsedoc_tags_iter(f): if tag_name == "output" and not entry.output: entry.output = tag_text elif tag_name == "usage" and not entry.usage: entry.usage = tag_text - finally: - f.close() except IOError as e: entry.description = "Error getting metadata: {}".format(e) @@ -384,11 +378,8 @@ class ScriptMetadata (object): @staticmethod def get_file_contents(filename): - f = open(filename, "r") - try: + with open(filename, "r") as f: contents = f.read() - finally: - f.close() return contents def get_string_variable(self, filename, varname): @@ -421,11 +412,8 @@ class ScriptMetadata (object): @staticmethod def get_requires(filename): - f = open(filename, "r") - try: + with open(filename, "r") as f: requires = ScriptMetadata.get_requires_from_file(f) - finally: - f.close() return requires @staticmethod @@ -440,11 +428,8 @@ class ScriptMetadata (object): @staticmethod def get_script_args(filename): - f = open(filename, "r") - try: + with open(filename, "r") as f: args = ScriptMetadata.get_script_args_from_file(f) - finally: - f.close() return args @staticmethod