From d12de7e0f984036cbec197eb5031db02f4937ff1 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 29 Oct 2026 03:16:43 +0530 Subject: [PATCH] misc: close leaked files in m.proj, g.extension, G_debug and an r.in.gdal test Close m.proj input/output files, g.extension /dev/null output and the file opened for each G_debug message. Use a context manager in fix_newlines() and in the r.in.gdal testsuite. --- python/grass/pygrass/modules/interface/env.py | 6 +++++- raster/r.in.gdal/testsuite/test_r_in_gdal.py | 3 ++- scripts/g.extension/g.extension.py | 9 +++++++-- scripts/m.proj/m.proj.py | 6 ++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/python/grass/pygrass/modules/interface/env.py b/python/grass/pygrass/modules/interface/env.py index 85107571e86..9f0d4a87e64 100644 --- a/python/grass/pygrass/modules/interface/env.py +++ b/python/grass/pygrass/modules/interface/env.py @@ -33,4 +33,8 @@ def G_debug(level, *msg): if debug_level >= level: dfile = os.environ.get("GRASS_DEBUG_FILE") fd = sys.stderr if dfile is None else open(dfile, mode="a") - print("D%d/%d: " % (level, debug_level), *msg, end="\n", file=fd) + try: + print("D%d/%d: " % (level, debug_level), *msg, end="\n", file=fd) + finally: + if dfile is not None: + fd.close() diff --git a/raster/r.in.gdal/testsuite/test_r_in_gdal.py b/raster/r.in.gdal/testsuite/test_r_in_gdal.py index 477ffca89c8..97ce09b14e9 100644 --- a/raster/r.in.gdal/testsuite/test_r_in_gdal.py +++ b/raster/r.in.gdal/testsuite/test_r_in_gdal.py @@ -307,7 +307,8 @@ def test_netCDF_3d_5(self): test_gdal_import_map.0000000105 """ - text_from_file = open("map_names_file.txt").read() + with open("map_names_file.txt") as file: + text_from_file = file.read() self.assertLooksLike(map_list, text_from_file) diff --git a/scripts/g.extension/g.extension.py b/scripts/g.extension/g.extension.py index 5dbbf884e78..82d72176485 100755 --- a/scripts/g.extension/g.extension.py +++ b/scripts/g.extension/g.extension.py @@ -1665,6 +1665,8 @@ def install_extension_win(name): if fullname not in old_file_list: file_list.append(fullname) + if outdev is not sys.stdout: + outdev.close() return 0, module_list, file_list @@ -1780,8 +1782,9 @@ def is_binary_string(bytes): for root, unused, files in os.walk(directory): for name in files: filename = os.path.join(root, name) - if is_binary_string(open(filename, "rb").read(1024)): - continue # ignore binary files + with open(filename, "rb") as file: + if is_binary_string(file.read(1024)): + continue # ignore binary files # read content of text file data = Path(filename).read_bytes() @@ -2183,6 +2186,8 @@ def install_extension_std_platforms(name, source, url, branch): if fullname not in old_file_list: file_list.append(fullname) + if outdev is not sys.stdout: + outdev.close() return ret, module_list, file_list, os.path.join(srcdir) diff --git a/scripts/m.proj/m.proj.py b/scripts/m.proj/m.proj.py index b9a33238dcd..b9e5d024cf2 100755 --- a/scripts/m.proj/m.proj.py +++ b/scripts/m.proj/m.proj.py @@ -282,8 +282,14 @@ def main(): ) ) + p.stdout.close() p.wait() + if inf is not sys.stdin: + inf.close() + if outf is not sys.stdout: + outf.close() + if p.returncode != 0: gcore.warning(_("Projection transform probably failed, please investigate"))