From: Josh Soref <2119212+jsoref@users.noreply.github.com> Date: Thu, 13 Nov 2025 17:40:33 +0000 (-0500) Subject: Use `with open(...) as f:` to ensure file is always closed X-Git-Tag: rec-5.4.0-alpha1~79^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F16500%2Fhead;p=thirdparty%2Fpdns.git Use `with open(...) as f:` to ensure file is always closed Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com> --- diff --git a/.github/scripts/clang-tidy-diff.py b/.github/scripts/clang-tidy-diff.py index c4f6fdb6dd..f00d5cf560 100755 --- a/.github/scripts/clang-tidy-diff.py +++ b/.github/scripts/clang-tidy-diff.py @@ -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, [])) diff --git a/.github/scripts/clang-tidy.py b/.github/scripts/clang-tidy.py index a68c7eec13..b6ea649ba3 100755 --- a/.github/scripts/clang-tidy.py +++ b/.github/scripts/clang-tidy.py @@ -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 diff --git a/build-scripts/docker/repo-test/generate-repo-files.py b/build-scripts/docker/repo-test/generate-repo-files.py index 2ad263cf66..c950d0e230 100755 --- a/build-scripts/docker/repo-test/generate-repo-files.py +++ b/build-scripts/docker/repo-test/generate-repo-files.py @@ -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): diff --git a/pdns/keyroller/setup.py b/pdns/keyroller/setup.py index d89c87953c..fb6d6890ad 100644 --- a/pdns/keyroller/setup.py +++ b/pdns/keyroller/setup.py @@ -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') diff --git a/regression-tests.recursor-dnssec/test_RoutingTag.py b/regression-tests.recursor-dnssec/test_RoutingTag.py index 24ddd5fa6d..d70d13a9dd 100644 --- a/regression-tests.recursor-dnssec/test_RoutingTag.py +++ b/regression-tests.recursor-dnssec/test_RoutingTag.py @@ -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): diff --git a/regression-tests/bulktest-to-json.py b/regression-tests/bulktest-to-json.py index 27a6b1454c..2c83ca6e6b 100755 --- a/regression-tests/bulktest-to-json.py +++ b/regression-tests/bulktest-to-json.py @@ -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('<'): - 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('<'): + 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()))