From 07cd669dca50785d7607b646944d0db80937b210 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 29 Oct 2026 03:16:34 +0530 Subject: [PATCH] vector: close files and pipes in v.rast.stats, v.db.*, v.report and v.in.e00 Close gs.pipe_command() stdout and the /dev/null handles these scripts open, so running them with PYTHONDEVMODE=1 no longer reports ResourceWarning. The unused nuldev in v.rast.stats is removed with its os import. --- scripts/v.db.droptable/v.db.droptable.py | 23 +++++++++++-------- .../v.db.reconnect.all/v.db.reconnect.all.py | 5 +++- scripts/v.db.univar/v.db.univar.py | 5 +++- scripts/v.in.e00/v.in.e00.py | 1 + scripts/v.rast.stats/v.rast.stats.py | 7 +++--- scripts/v.report/v.report.py | 6 +++++ 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/scripts/v.db.droptable/v.db.droptable.py b/scripts/v.db.droptable/v.db.droptable.py index c9c8a0f1049..ea851bfcb1a 100755 --- a/scripts/v.db.droptable/v.db.droptable.py +++ b/scripts/v.db.droptable/v.db.droptable.py @@ -106,15 +106,20 @@ def main(): gs.message(_("Current attribute table link(s):")) # silently test first to avoid confusing error messages - nuldev = open(os.devnull, "w") - try: - gs.run_command( - "v.db.connect", flags="p", map=map, quiet=True, stdout=nuldev, stderr=nuldev - ) - except CalledModuleError: - gs.message(_("(No database links remaining)")) - else: - gs.run_command("v.db.connect", flags="p", map=map) + with open(os.devnull, "w") as nuldev: + try: + gs.run_command( + "v.db.connect", + flags="p", + map=map, + quiet=True, + stdout=nuldev, + stderr=nuldev, + ) + except CalledModuleError: + gs.message(_("(No database links remaining)")) + else: + gs.run_command("v.db.connect", flags="p", map=map) # write cmd history: gs.vector_history(map) diff --git a/scripts/v.db.reconnect.all/v.db.reconnect.all.py b/scripts/v.db.reconnect.all/v.db.reconnect.all.py index bff499178c9..078da0f0701 100755 --- a/scripts/v.db.reconnect.all/v.db.reconnect.all.py +++ b/scripts/v.db.reconnect.all/v.db.reconnect.all.py @@ -322,4 +322,7 @@ def main(): if __name__ == "__main__": options, flags = gs.parser() nuldev = open(os.devnull, "w") - sys.exit(main()) + try: + sys.exit(main()) + finally: + nuldev.close() diff --git a/scripts/v.db.univar/v.db.univar.py b/scripts/v.db.univar/v.db.univar.py index 4bad9363bba..88c82c8e89a 100755 --- a/scripts/v.db.univar/v.db.univar.py +++ b/scripts/v.db.univar/v.db.univar.py @@ -117,4 +117,7 @@ def main(): if __name__ == "__main__": options, flags = gs.parser() nuldev = open(os.devnull, "w") - main() + try: + main() + finally: + nuldev.close() diff --git a/scripts/v.in.e00/v.in.e00.py b/scripts/v.in.e00/v.in.e00.py index d026f32028a..6e0b0d3c49a 100755 --- a/scripts/v.in.e00/v.in.e00.py +++ b/scripts/v.in.e00/v.in.e00.py @@ -130,6 +130,7 @@ def main(): gcore.call(["e00conv", filename, e00tmp + ".e00"]) gcore.message(_("...converted to Arc Coverage in current directory")) gcore.call(["avcimport", e00tmp + ".e00", e00shortname], stderr=nuldev) + nuldev.close() # SQL name fix: name = name.replace("-", "_") diff --git a/scripts/v.rast.stats/v.rast.stats.py b/scripts/v.rast.stats/v.rast.stats.py index 467d299cfa2..5ddb29d13b6 100644 --- a/scripts/v.rast.stats/v.rast.stats.py +++ b/scripts/v.rast.stats/v.rast.stats.py @@ -71,7 +71,6 @@ # %end import sys -import os import atexit import grass.script as gs from grass.script.utils import decode @@ -88,7 +87,7 @@ def cleanup(): def main(): - global tmp, sqltmp, tmpname, nuldev, vector, rastertmp + global tmp, sqltmp, tmpname, vector, rastertmp rastertmp = False # setup temporary files tmp = gs.tempfile() @@ -96,8 +95,6 @@ def main(): # we need a random name tmpname = gs.basename(tmp) - nuldev = open(os.devnull, "w") - rasters = options["raster"].split(",") colprefixes = options["column_prefix"].split(",") vector = options["map"] @@ -296,6 +293,7 @@ def get_nr_of_categories( for line in p.stdout: line = decode(line) cats.append(line.rstrip("\r\n").split(";")[0]) + p.stdout.close() p.wait() number = len(cats) @@ -482,6 +480,7 @@ def perform_stats( f.write(" WHERE %s=%s;\n" % (fi["key"], vars[0])) f.write("{0}\n".format(gs.db_commit_transaction(fi["driver"]))) + p.stdout.close() p.wait() diff --git a/scripts/v.report/v.report.py b/scripts/v.report/v.report.py index a8b42ccec6d..e7a37447bd1 100755 --- a/scripts/v.report/v.report.py +++ b/scripts/v.report/v.report.py @@ -127,6 +127,7 @@ def main(): if cols[catcol] == "-1" or cols[catcol] == "0": continue records1.append(cols[:catcol] + [int(cols[catcol])] + cols[(catcol + 1) :]) + p.stdout.close() p.wait() if p.returncode != 0: sys.exit(1) @@ -162,6 +163,7 @@ def main(): if fields[0] in {"cat", "-1", "0"}: continue records2.append([int(fields[0])] + fields[1:]) + p.stdout.close() p.wait() records2.sort() @@ -186,6 +188,7 @@ def main(): field = int(decode(line).rstrip()) if field > 0: records1.append(field) + p.stdout.close() p.wait() records1.sort() records1 = uniq(records1) @@ -206,6 +209,7 @@ def main(): if fields[0] in {"cat", "-1", "0"}: continue records3.append([int(fields[0])] + fields[1:]) + p.stdout.close() p.wait() records3.sort() @@ -240,6 +244,8 @@ def main(): for r in records3: sys.stdout.write(fs.join(map(str, r)) + "\n") + nuldev.close() + if __name__ == "__main__": options, flags = gs.parser()