[pitivi] Make sure the file objects are closed



commit e9fa63e849a4c478fc0d48727af783943c8e2f32
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Tue May 5 23:46:25 2015 +0200

    Make sure the file objects are closed

 pitivi/preset.py              |   16 +++++++++-------
 pitivi/timeline/previewers.py |    7 ++++---
 2 files changed, 13 insertions(+), 10 deletions(-)
---
diff --git a/pitivi/preset.py b/pitivi/preset.py
index 60d1e94..f1ff147 100644
--- a/pitivi/preset.py
+++ b/pitivi/preset.py
@@ -305,7 +305,8 @@ class VideoPresetManager(PresetManager):
     user_path = os.path.join(xdg_data_home(), 'video_presets')
 
     def _loadSection(self, filepath):
-        parser = json.loads(open(filepath).read())
+        with open(filepath) as section:
+            parser = json.loads(section.read())
 
         name = parser["name"]
         width = parser["width"]
@@ -347,10 +348,10 @@ class AudioPresetManager(PresetManager):
     user_path = os.path.join(xdg_data_home(), 'audio_presets')
 
     def _loadSection(self, filepath):
-        parser = json.loads(open(filepath).read())
+        with open(filepath) as section:
+            parser = json.loads(section.read())
 
         name = parser["name"]
-
         channels = parser["channels"]
         sample_rate = parser["sample-rate"]
 
@@ -376,7 +377,8 @@ class RenderPresetManager(PresetManager):
     user_path = os.path.join(xdg_data_home(), 'render_presets')
 
     def _loadSection(self, filepath):
-        parser = json.loads(open(filepath).read())
+        with open(filepath) as section:
+            parser = json.loads(section.read())
 
         name = parser["name"]
         container = parser["container"]
@@ -384,9 +386,9 @@ class RenderPresetManager(PresetManager):
         vcodec = parser["vcodec"]
 
         cached_encs = CachedEncoderList()
-        if (acodec not in [fact.get_name() for fact in cached_encs.aencoders]
-           or vcodec not in [fact.get_name() for fact in cached_encs.vencoders]
-           or container not in [fact.get_name() for fact in cached_encs.muxers]):
+        if (acodec not in [fact.get_name() for fact in cached_encs.aencoders] or
+                vcodec not in [fact.get_name() for fact in cached_encs.vencoders] or
+                container not in [fact.get_name() for fact in cached_encs.muxers]):
             return
 
         try:
diff --git a/pitivi/timeline/previewers.py b/pitivi/timeline/previewers.py
index dddd71b..937fa8f 100644
--- a/pitivi/timeline/previewers.py
+++ b/pitivi/timeline/previewers.py
@@ -740,7 +740,8 @@ class AudioPreviewer(Gtk.Layout, PreviewGenerator, Zoomable, Loggable):
         filename = os.path.join(cache_dir, filename)
 
         if os.path.exists(filename):
-            self.samples = pickle.load(open(filename, "rb"))
+            with open(filename, "rb") as samples:
+                self.samples = pickle.load(samples)
             self._startRendering()
         else:
             self.wavefile = filename
@@ -780,8 +781,8 @@ class AudioPreviewer(Gtk.Layout, PreviewGenerator, Zoomable, Loggable):
             samples = numpy.array(self.peaks[0])
 
         self.samples = samples.tolist()
-        f = open(self.wavefile, 'wb')
-        pickle.dump(self.samples, f)
+        with open(self.wavefile, 'wb') as wavefile:
+            pickle.dump(self.samples, wavefile)
 
     def _startRendering(self):
         self.nbSamples = len(self.samples)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]