]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Use `with open(...) as f:` to ensure file is always closed 16500/head
authorJosh Soref <2119212+jsoref@users.noreply.github.com>
Thu, 13 Nov 2025 17:40:33 +0000 (12:40 -0500)
committerJosh Soref <2119212+jsoref@users.noreply.github.com>
Thu, 13 Nov 2025 17:40:33 +0000 (12:40 -0500)
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
.github/scripts/clang-tidy-diff.py
.github/scripts/clang-tidy.py
build-scripts/docker/repo-test/generate-repo-files.py
pdns/keyroller/setup.py
regression-tests.recursor-dnssec/test_RoutingTag.py
regression-tests/bulktest-to-json.py

index c4f6fdb6dd0cfd8a62a4d03c92402a2b70894176..f00d5cf5602107ad7f7ab2983ce6b377610b4a7c 100755 (executable)
@@ -99,7 +99,8 @@ def merge_replacement_files(tmpdir, mergefile):
   mergekey = "Diagnostics"
   merged = []
   for replacefile in glob.iglob(os.path.join(tmpdir, '*.yaml')):
-    content = yaml.safe_load(open(replacefile, 'r'))
+    with open(replacefile, 'r') as f:
+      content = yaml.safe_load(f)
     if not content:
       continue # Skip empty files.
     merged.extend(content.get(mergekey, []))
index a68c7eec13c045fdf87fe9207194e2b21374a142..b6ea649ba3c2f2557054b1eddfdd67e19556713d 100755 (executable)
@@ -48,8 +48,8 @@ def main():
     gh_step_summary = os.getenv("GITHUB_STEP_SUMMARY")
     if gh_step_summary:
         # Print Markdown summary
-        summary_fp = open(gh_step_summary, "a", encoding="utf-8")
-        print("### clang-tidy summary", file=summary_fp)
+        with open(gh_step_summary, "a", encoding="utf-8") as summary_fp:
+            print("### clang-tidy summary", file=summary_fp)
 
     fixes = fixes["Diagnostics"]
     have_warnings = False
index 2ad263cf6614b075c65b368aa24a4ffda58a2261..c950d0e2300b8a772bb3c84cc0f07e38aeb88aa6 100755 (executable)
@@ -89,31 +89,28 @@ def write_dockerfile (os, os_version, release):
         pkg = 'dnsdist'
         cmd = 'dnsdist'
 
-    f = open('{}{}.{}-{}'.format(g_dockerfile, release, os, os_version), 'w')
-
-    # This comment was in the template for the `--nobest` part but that makes
-    # the template look even more different than the final output, so:
-    #
-    # > When should the logic be in the code and when in the template? :shrug:
-    # > I prefer it to be in the code but I also do not want to add extra vars
-    # > and logic to the code unless necessary.
-    f.write(tpl.render({ "os": os,
-                         "os_image": os_image,
-                         "os_version": os_version,
-                         "release": release,
-                         "cmd": cmd,
-                         "pkg": pkg }))
-    f.close()
+    with open('{}{}.{}-{}'.format(g_dockerfile, release, os, os_version), 'w') as f:
+        # This comment was in the template for the `--nobest` part but that makes
+        # the template look even more different than the final output, so:
+        #
+        # > When should the logic be in the code and when in the template? :shrug:
+        # > I prefer it to be in the code but I also do not want to add extra vars
+        # > and logic to the code unless necessary.
+        f.write(tpl.render({ "os": os,
+                            "os_image": os_image,
+                            "os_version": os_version,
+                            "release": release,
+                            "cmd": cmd,
+                            "pkg": pkg }))
 
 
 def write_list_file (os, os_version, release):
     tpl = g_env.get_template('pdns-list.jinja2')
 
-    f = open('pdns.list.{}.{}-{}'.format(release, os, os_version), 'w')
-    f.write(tpl.render({ "os": os,
-                         "os_version": os_version,
-                         "release": release }))
-    f.close()
+    with open('pdns.list.{}.{}-{}'.format(release, os, os_version), 'w') as f:
+        f.write(tpl.render({ "os": os,
+                            "os_version": os_version,
+                            "release": release }))
 
 
 def write_pkg_pin_file (release):
@@ -124,9 +121,8 @@ def write_pkg_pin_file (release):
     elif release.startswith('dnsdist-'):
         pkg = 'dnsdist'
 
-    f = open('pkg-pin', 'w')
-    f.write(tpl.render({ "pkg": pkg }))
-    f.close()
+    with open('pkg-pin', 'w') as f:
+       f.write(tpl.render({ "pkg": pkg }))
 
 
 def write_release_files (release):
index d89c87953cd237aa604870c2efeca48e6af12f2f..fb6d6890ad48306dc90f7a3113c5cfa753731d05 100644 (file)
@@ -29,8 +29,9 @@ def exists(fname):
 # README file and 2) it's easier to type in the README file than to put a raw
 # string in below ...
 def read(fname):
-    return open(os.path.join(os.path.dirname(__file__), fname),
-                'r', encoding='utf-8').read()
+    with open(os.path.join(os.path.dirname(__file__), fname),
+              'r', encoding='utf-8') as f:
+        return f.read()
 
 
 version = os.environ.get('BUILDER_VERSION', '0.0.0')
index 24ddd5fa6d06ca4059480a8d04191facaf6cf361..d70d13a9dd7aacdca2494816676fbe418f1bdae6 100644 (file)
@@ -64,10 +64,9 @@ ecs-add-for=0.0.0.0/0
 
     def setRoutingTag(self, tag):
         # This value is picked up by the gettag()
-        file = open('tagfile', 'w')
-        if tag:
-            file.write(tag)
-        file.close();
+        with open('tagfile', 'w') as file:
+            if tag:
+                file.write(tag)
 
     @classmethod
     def startResponders(cls):
index 27a6b1454c4c5db8bf6a4183eb5db3f96d1b584e..2c83ca6e6b8945172552701c335f4ff2a136cbed 100755 (executable)
@@ -13,12 +13,13 @@ for fname in glob.glob('testresults-*.xml'):
        vars['tag'] = tag
        varnames.update(vars.keys())
        stats=dict()
-       for line in open(fname):
-               if line.startswith('&lt;'):
-                       sname = line.split(';')[4][:-3]
-                       sval = line.split(';')[8][:-3]
-                       stats[sname]=sval
-                       statnames.add(sname)
+       with open(fname) as f
+               for line in f:
+                       if line.startswith('&lt;'):
+                               sname = line.split(';')[4][:-3]
+                               sval = line.split(';')[8][:-3]
+                               stats[sname]=sval
+                               statnames.add(sname)
        # print fname, vars, stats
        runs.append(dict(vars.items()+stats.items()))