[Python-checkins] bpo-34500: Fix ResourceWarning in difflib.py (GH-8926)

Terry Jan Reedy webhook-mailer at python.org
Sun Sep 2 21:48:17 EDT 2018


https://github.com/python/cpython/commit/30af2e737aad427d4da97f8dadeeecff6c2b28f5
commit: 30af2e737aad427d4da97f8dadeeecff6c2b28f5
branch: 2.7
author: Mickaël Schoentgen <contact at tiger-222.fr>
committer: Terry Jan Reedy <tjreedy at udel.edu>
date: 2018-09-02T21:48:08-04:00
summary:

bpo-34500: Fix ResourceWarning in difflib.py (GH-8926)

The change to Tools/scripts/diff.py effectively backports part of
a2637729f23dc993e820fd92f0d1759ad714c9b2.
The test code changed in Doc/library/difflib.rst is not present in current 3.x.

files:
A Misc/NEWS.d/next/Tools-Demos/2018-08-25-17-13-24.bpo-34500.Edz41x.rst
M Doc/library/difflib.rst
M Tools/scripts/diff.py

diff --git a/Doc/library/difflib.rst b/Doc/library/difflib.rst
index c6bf3ef6775d..01a3bfc2cd13 100644
--- a/Doc/library/difflib.rst
+++ b/Doc/library/difflib.rst
@@ -757,8 +757,10 @@ It is also contained in the Python source distribution, as
        # we're passing these as arguments to the diff function
        fromdate = time.ctime(os.stat(fromfile).st_mtime)
        todate = time.ctime(os.stat(tofile).st_mtime)
-       fromlines = open(fromfile, 'U').readlines()
-       tolines = open(tofile, 'U').readlines()
+       with open(fromfile, 'U') as f:
+           fromlines = f.readlines()
+       with open(tofile, 'U') as f:
+           tolines = f.readlines()
 
        if options.u:
            diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
diff --git a/Misc/NEWS.d/next/Tools-Demos/2018-08-25-17-13-24.bpo-34500.Edz41x.rst b/Misc/NEWS.d/next/Tools-Demos/2018-08-25-17-13-24.bpo-34500.Edz41x.rst
new file mode 100644
index 000000000000..27ca06f1cbcd
--- /dev/null
+++ b/Misc/NEWS.d/next/Tools-Demos/2018-08-25-17-13-24.bpo-34500.Edz41x.rst
@@ -0,0 +1 @@
+Fix 2 ResourceWarning in difflib.py. Patch by Mickaël Schoentgen.
diff --git a/Tools/scripts/diff.py b/Tools/scripts/diff.py
index 513e2a7112de..c4c2e101c60e 100755
--- a/Tools/scripts/diff.py
+++ b/Tools/scripts/diff.py
@@ -32,8 +32,10 @@ def main():
 
     fromdate = time.ctime(os.stat(fromfile).st_mtime)
     todate = time.ctime(os.stat(tofile).st_mtime)
-    fromlines = open(fromfile, 'U').readlines()
-    tolines = open(tofile, 'U').readlines()
+    with open(fromfile, 'U') as f:
+        fromlines = f.readlines()
+    with open(tofile, 'U') as f:
+        tolines = f.readlines()
 
     if options.u:
         diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)



More information about the Python-checkins mailing list