Make addImage more robust.
authorNeal H. Walfield <neal@walfield.org>
Mon, 5 Sep 2011 20:48:02 +0000 (22:48 +0200)
committerNeal H. Walfield <neal@walfield.org>
Mon, 5 Sep 2011 20:48:02 +0000 (22:48 +0200)
 - Use with to ensure the output file handle is closed.

src/rss_sqlite.py

index b08ff6a..444c918 100644 (file)
@@ -24,6 +24,8 @@
 # Description : Simple RSS Reader
 # ============================================================================
 
+from __future__ import with_statement
+
 import sqlite3
 from os.path import isfile, isdir
 from shutil import rmtree
@@ -113,10 +115,12 @@ class Feed:
 
                 abs_url = urljoin(baseurl,url)
                 f = opener.open(abs_url)
-                outf = open(filename, "w")
-                outf.write(f.read())
-                f.close()
-                outf.close()
+                try:
+                    with open(filename, "w") as outf:
+                        for data in f:
+                            outf.write(data)
+                finally:
+                    f.close()
             except (urllib2.HTTPError, urllib2.URLError, IOError), exception:
                 logger.info("Could not download image %s: %s"
                             % (abs_url, str (exception)))