[Python-checkins] cpython (merge 3.3 -> default): Issue #18817: Fix a resource warning in Lib/aifc.py demo. Patch by

serhiy.storchaka python-checkins at python.org
Sun Aug 25 18:18:28 CEST 2013


http://hg.python.org/cpython/rev/1da223c1b7ed
changeset:   85389:1da223c1b7ed
parent:      85386:f81846c2b746
parent:      85388:e0c33e0c0483
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Aug 25 19:16:01 2013 +0300
summary:
  Issue #18817: Fix a resource warning in Lib/aifc.py demo.  Patch by
Vajrasky Kok.

files:
  Lib/aifc.py |  40 +++++++++++++++++++---------------------
  Misc/NEWS   |   3 +++
  2 files changed, 22 insertions(+), 21 deletions(-)


diff --git a/Lib/aifc.py b/Lib/aifc.py
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -889,24 +889,22 @@
     if not sys.argv[1:]:
         sys.argv.append('/usr/demos/data/audio/bach.aiff')
     fn = sys.argv[1]
-    f = open(fn, 'r')
-    print("Reading", fn)
-    print("nchannels =", f.getnchannels())
-    print("nframes   =", f.getnframes())
-    print("sampwidth =", f.getsampwidth())
-    print("framerate =", f.getframerate())
-    print("comptype  =", f.getcomptype())
-    print("compname  =", f.getcompname())
-    if sys.argv[2:]:
-        gn = sys.argv[2]
-        print("Writing", gn)
-        g = open(gn, 'w')
-        g.setparams(f.getparams())
-        while 1:
-            data = f.readframes(1024)
-            if not data:
-                break
-            g.writeframes(data)
-        g.close()
-        f.close()
-        print("Done.")
+    with open(fn, 'r') as f:
+        print("Reading", fn)
+        print("nchannels =", f.getnchannels())
+        print("nframes   =", f.getnframes())
+        print("sampwidth =", f.getsampwidth())
+        print("framerate =", f.getframerate())
+        print("comptype  =", f.getcomptype())
+        print("compname  =", f.getcompname())
+        if sys.argv[2:]:
+            gn = sys.argv[2]
+            print("Writing", gn)
+            with open(gn, 'w') as g:
+                g.setparams(f.getparams())
+                while 1:
+                    data = f.readframes(1024)
+                    if not data:
+                        break
+                    g.writeframes(data)
+            print("Done.")
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -963,6 +963,9 @@
 Tools/Demos
 -----------
 
+- Issue #18817: Fix a resource warning in Lib/aifc.py demo.  Patch by
+  Vajrasky Kok.
+
 - Issue #18439: Make patchcheck work on Windows for ACKS, NEWS.
 
 - Issue #18448: Fix a typo in Tools/demo/eiffel.py.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list