gcc-changelog: remove file descriptor leaks
authorPierre-Marie de Rodat <derodat@adacore.com>
Tue, 26 May 2020 14:13:28 +0000 (16:13 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Tue, 26 May 2020 15:45:38 +0000 (17:45 +0200)
Currently, running gcc-changelog's unit tests may clutter the output
with tons of warnings such as:

    .../contrib/gcc-changelog/git_email.py:40: ResourceWarning: unclosed
    file <_io.TextIOWrapper name='/tmp/tmpt5okd4qp.patch' mode='r'
    encoding='UTF-8'>
      lines = open(self.filename).read().splitlines()
    ResourceWarning: Enable tracemalloc to get the object allocation
    traceback

This commit fixes these leaks, which restores a clean testsuite output.

contrib/

* gcc-changelog/git_update_version.py: Close file objects after
use.
* gcc-changelog/git_email.py: Likewise.
* gcc-changelog/test_email.py: Likewise.

contrib/gcc-changelog/git_email.py
contrib/gcc-changelog/git_update_version.py
contrib/gcc-changelog/test_email.py

index e1d6b70e80c0b85ce652a6048786343c4f65b367..8c9df293a6610213231ac51e5cedf31005d44446 100755 (executable)
@@ -37,7 +37,8 @@ class GitEmail(GitCommit):
         date = None
         author = None
 
-        lines = open(self.filename).read().splitlines()
+        with open(self.filename, 'r') as f:
+            lines = f.read().splitlines()
         lines = list(takewhile(lambda line: line != '---', lines))
         for line in lines:
             if line.startswith(DATE_PREFIX):
index 3dcc5625eda651d4668ff80e7dcaec5e393089f7..6b6ccf68a5e29d3d4427c5d6bea3808348490724 100755 (executable)
@@ -28,7 +28,8 @@ current_timestamp = datetime.datetime.now().strftime('%Y%m%d\n')
 
 
 def read_timestamp(path):
-    return open(path).read()
+    with open(path) as f:
+        return f.read()
 
 
 def prepend_to_changelog_files(repo, folder, git_commit, add_to_git):
@@ -40,7 +41,8 @@ def prepend_to_changelog_files(repo, folder, git_commit, add_to_git):
         full_path = os.path.join(folder, entry, 'ChangeLog')
         print('writting to %s' % full_path)
         if os.path.exists(full_path):
-            content = open(full_path).read()
+            with open(full_path) as f:
+                content = f.read()
         else:
             content = ''
         with open(full_path, 'w+') as f:
index bf028a3d40a4f82f45ea6a0060393a808aa3709a..1379502e755f7347705f74464b85a0d6e1e98977 100755 (executable)
@@ -33,7 +33,8 @@ class TestGccChangelog(unittest.TestCase):
 
         filename = None
         patch_lines = []
-        lines = open(os.path.join(script_path, 'test_patches.txt')).read()
+        with open(os.path.join(script_path, 'test_patches.txt')) as f:
+            lines = f.read()
         for line in lines.split('\n'):
             if line.startswith('==='):
                 if patch_lines: