[all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Wed, 29 Jun 2011 18:26:29 +0200
changeset 7577 9892937d9041
parent 7575 335f14e8e5a7
child 7578 49be8619b745
[all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
cwctl.py
ext/rest.py
mttransforms.py
server/serverctl.py
--- a/cwctl.py	Wed Jun 29 16:13:09 2011 +0200
+++ b/cwctl.py	Wed Jun 29 18:26:29 2011 +0200
@@ -727,11 +727,9 @@
         config = cwcfg.config_for(appid)
         config.repairing = True # notice we're not starting the server
         config.verbosity = self.config.verbosity
-        try:
-            config.set_sources_mode(self.config.ext_sources or ('migration',))
-        except AttributeError:
-            # not a server config
-            pass
+        set_sources_mode = getattr(config, 'set_sources_mode', None)
+        if set_sources_mode is not None:
+            set_sources_mode(self.config.ext_sources or ('migration',))
         # get instance and installed versions for the server and the componants
         mih = config.migration_handler()
         repo = mih.repo_connect()
--- a/ext/rest.py	Wed Jun 29 16:13:09 2011 +0200
+++ b/ext/rest.py	Wed Jun 29 18:26:29 2011 +0200
@@ -200,7 +200,7 @@
             context = state.document.settings.context
             context._cw.add_css('pygments.css')
         except AttributeError:
-            # used outside cubicweb
+            # used outside cubicweb XXX use hasattr instead
             pass
         return [nodes.raw('', parsed, format='html')]
 
--- a/mttransforms.py	Wed Jun 29 16:13:09 2011 +0200
+++ b/mttransforms.py	Wed Jun 29 18:26:29 2011 +0200
@@ -99,10 +99,10 @@
 
     def patch_convert(cls):
         def _convert(self, trdata, origconvert=cls._convert):
-            try:
-                trdata.appobject._cw.add_css('pygments.css')
-            except AttributeError: # session has no add_css, only http request
-                pass
+            add_css = getattr(trdata.appobject._cw, 'add_css', None)
+            if add_css is not None:
+                # session has no add_css, only http request
+                add_css('pygments.css')
             return origconvert(self, trdata)
         cls._convert = _convert
     patch_convert(pygmentstransforms.PygmentsHTMLTransform)
--- a/server/serverctl.py	Wed Jun 29 16:13:09 2011 +0200
+++ b/server/serverctl.py	Wed Jun 29 18:26:29 2011 +0200
@@ -123,11 +123,10 @@
                             interactive=interactive)
     # disable autocommit (isolation_level(1)) because DROP and
     # CREATE DATABASE can't be executed in a transaction
-    try:
-        cnx.set_isolation_level(0)
-    except AttributeError:
+    set_isolation_level = getattr(cnx, 'set_isolation_level', None)
+    if set_isolation_level is not None:
         # set_isolation_level() is psycopg specific
-        pass
+        set_isolation_level(0)
     return cnx
 
 def repo_cnx(config):