[totem] plugins: Fix up exception handling in the Python plugins



commit d43f3ee0a3876139dc4739006bc3bdb980e33fab
Author: Philip Withnall <philip tecnocode co uk>
Date:   Tue Apr 5 00:41:14 2011 +0100

    plugins: Fix up exception handling in the Python plugins
    
    Pretty much none of the plugins were specifying which exceptions to catch,
    which is discouraged practice, and it made pylint upset.
    
    Helps: bgo#645739

 src/plugins/iplayer/iplayer.py             |    4 +-
 src/plugins/iplayer/iplayer2.py            |    1 +
 src/plugins/jamendo/jamendo.py             |   79 ++++++++++++++--------------
 src/plugins/opensubtitles/opensubtitles.py |    9 ++--
 src/plugins/pythonconsole/pythonconsole.py |    2 +-
 5 files changed, 48 insertions(+), 47 deletions(-)
---
diff --git a/src/plugins/iplayer/iplayer.py b/src/plugins/iplayer/iplayer.py
index 720ffa9..de6c5a4 100644
--- a/src/plugins/iplayer/iplayer.py
+++ b/src/plugins/iplayer/iplayer.py
@@ -187,7 +187,7 @@ class PopulateChannelsThread (threading.Thread):
                     GObject.idle_add (self.callback,
                                       self.tree_model, parent_path,
                                       [name, category_id, None])
-            except:
+            except StandardError:
                 # Only show the error once, rather than for each channel
                 # (it gets a bit grating)
                 if not shown_error:
@@ -234,7 +234,7 @@ class PopulateProgrammesThread (threading.Thread):
         # Get the programmes
         try:
             programmes = feed.list ()
-        except:
+        except StandardError:
             GObject.idle_add (self.callback,
                               self.tree_model, self.category_path, None, False)
             self.download_lock.release ()
diff --git a/src/plugins/iplayer/iplayer2.py b/src/plugins/iplayer/iplayer2.py
index 728e917..3853141 100644
--- a/src/plugins/iplayer/iplayer2.py
+++ b/src/plugins/iplayer/iplayer2.py
@@ -290,6 +290,7 @@ HTTP_OBJECT = httplib2.Http ()
 
 class NoItemsError (Exception):
     def __init__ (self, reason=None):
+        Exception.__init__ (self)
         self.reason = reason
 
     def __str__ (self):
diff --git a/src/plugins/jamendo/jamendo.py b/src/plugins/jamendo/jamendo.py
index 02070a6..60f792b 100644
--- a/src/plugins/jamendo/jamendo.py
+++ b/src/plugins/jamendo/jamendo.py
@@ -215,10 +215,7 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
         JamendoService.NUM_PER_PAGE = self.settings.get_int ('num-per-page')
 
     def __on_config_widget_destroy (self, _widget):
-        try:
-            self._reset ()
-        except:
-            pass
+        self._reset ()
 
     def _reset (self):
         """
@@ -294,10 +291,14 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
                 pixbuf = GdkPixbuf.Pixbuf.new_from_file (album['image'])
                 os.unlink (album['image'])
                 album['image'] = pixbuf
-            except:
+            except GObject.GError:
                 # do not fail for this, just display a dummy pixbuf
                 album['image'] = GdkPixbuf.Pixbuf.new (GdkPixbuf.Colorspace.RGB,
                                                        True, 8, 1, 1)
+            except OSError:
+                # Ignore if we fail to delete the temporary file
+                pass
+
         # format title
         title  = '<b>%s</b>\n' % self._format_str (album['name'])
         title += _(u'Artist: %s') % self._format_str (album['artist_name'])
@@ -312,8 +313,9 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
             # Translators: this is the release time of an album in Python
             # strftime format
             release = time.strftime (_(u'%x'), release)
-        except:
+        except (LookupError, ValueError):
             release = ''
+
         tip = '\n'.join ([
             '<b>%s</b>' % self._format_str (album['name']),
             _(u'Artist: %s') % self._format_str (album['artist_name']),
@@ -451,10 +453,10 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
         if hasattr (exc, 'reason'):
             try:
                 reason = exc.reason[1]
-            except:
+            except IndexError:
                 try:
                     reason = exc.reason[0]
-                except:
+                except IndexError:
                     reason = str (exc)
             reason = reason.capitalize ()
             msg = _(u'Failed to connect to Jamendo server.\n%s.') % reason
@@ -510,7 +512,7 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
         """
         try:
             item = self._get_selection ()[0] # first item selected
-        except:
+        except IndexError:
             return
 
         if path.get_depth () == 1:
@@ -522,34 +524,31 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
         """
         Called when the user clicked on a treeview element.
         """
-        try:
-            if evt.button == 3:
-                (path, _col, _cell_x, _cell_y) = tree_view.get_path_at_pos (
-                    int (evt.x),
-                    int (evt.y)
-                )
-                sel  = tree_view.get_selection ()
-                (_model, rows) = sel.get_selected_rows ()
-                if path not in rows:
-                    sel.unselect_all ()
-                    sel.select_path (path)
-                tree_view.grab_focus ()
-                self.popup.popup_for_device (None, None, None, None, None,
-                                             evt.button, evt.time)
-                return True
-
-            (event_x, event_y) = evt.get_coords ()
+        if evt.button == 3:
             (path, _col, _cell_x, _cell_y) = tree_view.get_path_at_pos (
-                int (event_x),
-                int (event_y)
+                int (evt.x),
+                int (evt.y)
             )
-            if path.get_depth () == 1:
-                if tree_view.row_expanded (path):
-                    tree_view.collapse_row (path)
-                else:
-                    tree_view.expand_row (path, False)
-        except:
-            pass
+            sel  = tree_view.get_selection ()
+            (_model, rows) = sel.get_selected_rows ()
+            if path not in rows:
+                sel.unselect_all ()
+                sel.select_path (path)
+            tree_view.grab_focus ()
+            self.popup.popup_for_device (None, None, None, None, None,
+                                         evt.button, evt.time)
+            return True
+
+        (event_x, event_y) = evt.get_coords ()
+        (path, _col, _cell_x, _cell_y) = tree_view.get_path_at_pos (
+            int (event_x),
+            int (event_y)
+        )
+        if path.get_depth () == 1:
+            if tree_view.row_expanded (path):
+                tree_view.collapse_row (path)
+            else:
+                tree_view.expand_row (path, False)
 
     def __on_treeview_selection_changed (self, selection):
         (_model, rows) = selection.get_selected_rows ()
@@ -593,7 +592,7 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
         try:
             url = self._get_selection (True)[0]['url']
             os.spawnlp (os.P_NOWAIT, "xdg-open", "xdg-open", url)
-        except:
+        except IndexError:
             pass
 
     def __on_add_to_playlist_activate (self, *_args):
@@ -646,7 +645,7 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
         (model, rows) = sel.get_selected_rows ()
         try:
             itera = model.get_iter (rows[0])
-        except:
+        except IndexError:
             itera = None
         pindex = self.treeviews.index (self.current_treeview)
         self.previous_button.set_sensitive (self.current_page[pindex] > 1)
@@ -663,7 +662,7 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
             return ''
         try:
             return escape (unicode (string))
-        except:
+        except ValueError:
             return string
 
     @classmethod
@@ -680,7 +679,7 @@ class JamendoPlugin (GObject.Object, Peas.Activatable, PeasGtk.Configurable):
             # Translators: time formatting (in Python strftime format) for the
             # Jamendo plugin for times shorter than an hour
             return time.strftime (_(u'%M:%S'), time.gmtime (secs))
-        except:
+        except (LookupError, ValueError):
             return ''
 
 
@@ -737,7 +736,7 @@ class JamendoService (threading.Thread):
                 album['url'] = url.replace ('/en/', '/' + _('en') + '/')
                 GObject.idle_add (self.loop_cb[0], self.loop_cb[1], album)
             GObject.idle_add (self.done_cb[0], self.done_cb[1], albums)
-        except Exception as exc:
+        except Exception as exc: # pylint: disable-msg=W0703
             GObject.idle_add (self.error_cb[0], self.error_cb[1], exc)
         finally:
             self.lock.release ()
diff --git a/src/plugins/opensubtitles/opensubtitles.py b/src/plugins/opensubtitles/opensubtitles.py
index c8c998a..341cc43 100644
--- a/src/plugins/opensubtitles/opensubtitles.py
+++ b/src/plugins/opensubtitles/opensubtitles.py
@@ -206,8 +206,9 @@ class OpenSubtitlesModel (object):
         try:
             import locale
             self.lang = LANGUAGES[locale.getlocale ()[0].split ('_')[0]]
-        except:
+        except (ImportError, IndexError):
             self.lang = 'eng'
+
         self.hash = None
         self.size = 0
 
@@ -232,14 +233,14 @@ class OpenSubtitlesModel (object):
             # We have already logged-in before, check the connection
             try:
                 result = self._server.NoOperation (self._token)
-            except:
+            except (xmlrpclib.Fault, xmlrpclib.ProtocolError):
                 pass
             if result and result['status'] != OK200:
                 return True
         try:
             result = self._server.LogIn (username, password, self.lang,
                                          USER_AGENT)
-        except:
+        except (xmlrpclib.Fault, xmlrpclib.ProtocolError):
             pass
         if result and result.get ('status') == OK200:
             self._token = result.get ('token')
@@ -283,7 +284,7 @@ class OpenSubtitlesModel (object):
             if result and result.get ('status') == OK200:
                 try:
                     subtitle64 = result['data'][0]['data']
-                except:
+                except LookupError:
                     self.message = error_message
                     return None
 
diff --git a/src/plugins/pythonconsole/pythonconsole.py b/src/plugins/pythonconsole/pythonconsole.py
index b395bbc..e740a68 100644
--- a/src/plugins/pythonconsole/pythonconsole.py
+++ b/src/plugins/pythonconsole/pythonconsole.py
@@ -43,7 +43,7 @@ from gi.repository import Gio # pylint: disable-msg=E0611
 try:
     import rpdb2
     HAVE_RPDB2 = True
-except:
+except ImportError:
     HAVE_RPDB2 = False
 
 import gettext



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