Rev 3768: Fix reporting of incompatible api plugin load errors, fixing bug 279451. in http://people.ubuntu.com/~robertc/baz2.0/api

Robert Collins robertc at robertcollins.net
Tue Oct 7 07:41:51 BST 2008


At http://people.ubuntu.com/~robertc/baz2.0/api

------------------------------------------------------------
revno: 3768
revision-id: robertc at robertcollins.net-20081007064146-d1w8jhf0zqn1mb3y
parent: robertc at robertcollins.net-20081007045125-4h9com3yii5h6zpx
committer: Robert Collins <robertc at robertcollins.net>
branch nick: api
timestamp: Tue 2008-10-07 17:41:46 +1100
message:
  Fix reporting of incompatible api plugin load errors, fixing bug 279451.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/plugin.py               plugin.py-20050622060424-829b654519533d69
  bzrlib/tests/test_plugins.py   plugins.py-20050622075746-32002b55e5e943e9
=== modified file 'NEWS'
--- a/NEWS	2008-10-07 04:51:25 +0000
+++ b/NEWS	2008-10-07 06:41:46 +0000
@@ -82,6 +82,9 @@
     * API versioning support now has a multiple-version checking api
       ``require_any_api``. (Robert Collins, #279447)
 
+    * A failure to load a plugin due to an IncompatibleAPI exception is
+      now correctly reported. (Robert Collins, #279451)
+
     * Branching from a shared repository on a smart server into a new
       repository now preserves the repository format.
       (Andrew Bennetts, #269214)

=== modified file 'bzrlib/plugin.py'
--- a/bzrlib/plugin.py	2008-09-08 12:59:00 +0000
+++ b/bzrlib/plugin.py	2008-10-07 06:41:46 +0000
@@ -43,6 +43,7 @@
 from bzrlib import (
     config,
     debug,
+    errors,
     osutils,
     trace,
     )
@@ -172,7 +173,10 @@
 
 
 def load_from_dir(d):
-    """Load the plugins in directory d."""
+    """Load the plugins in directory d.
+    
+    d must be in the plugins module path already.
+    """
     # Get the list of valid python suffixes for __init__.py?
     # this includes .py, .pyc, and .pyo (depending on if we are running -O)
     # but it doesn't include compiled modules (.so, .dll, etc)
@@ -210,7 +214,13 @@
             exec "import bzrlib.plugins.%s" % name in {}
         except KeyboardInterrupt:
             raise
+        except errors.IncompatibleAPI, e:
+            trace.warning("Unable to load plugin %r. It requested API version "
+                "%s of module %s but the minimum exported version is %s, and "
+                "the maximum is %s" %
+                (name, e.wanted, e.api, e.minimum, e.current))
         except Exception, e:
+            trace.warning("%s" % e)
             ## import pdb; pdb.set_trace()
             if re.search('\.|-| ', name):
                 sanitised_name = re.sub('[-. ]', '_', name)

=== modified file 'bzrlib/tests/test_plugins.py'
--- a/bzrlib/tests/test_plugins.py	2008-08-11 12:50:16 +0000
+++ b/bzrlib/tests/test_plugins.py	2008-10-07 06:41:46 +0000
@@ -193,30 +193,60 @@
                 del bzrlib.plugins.ts_plugin
         self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
 
-    def test_plugin_with_bad_name_does_not_load(self):
-        # Create badly-named plugin
-        file('bzr-bad plugin-name..py', 'w').close()
-
+    def load_and_capture(self, name):
+        """Load plugins from '.' capturing the output.
+        
+        :param name: The name of the plugin.
+        :return: A string with the log from the plugin loading call.
+        """
         # Capture output
         stream = StringIO()
-        handler = logging.StreamHandler(stream)
-        log = logging.getLogger('bzr')
-        log.addHandler(handler)
-
-        bzrlib.plugin.load_from_dir('.')
-
-        # Stop capturing output
-        handler.flush()
-        handler.close()
-        log.removeHandler(handler)
-
-        self.assertContainsRe(stream.getvalue(),
+        try:
+            handler = logging.StreamHandler(stream)
+            log = logging.getLogger('bzr')
+            log.addHandler(handler)
+            try:
+                try:
+                    bzrlib.plugin.load_from_path(['.'])
+                finally:
+                    if 'bzrlib.plugins.%s' % name in sys.modules:
+                        del sys.modules['bzrlib.plugins.%s' % name]
+                    if getattr(bzrlib.plugins, name, None):
+                        delattr(bzrlib.plugins, name)
+            finally:
+                # Stop capturing output
+                handler.flush()
+                handler.close()
+                log.removeHandler(handler)
+            return stream.getvalue()
+        finally:
+            stream.close()
+    
+    def test_plugin_with_bad_api_version_reports(self):
+        # This plugin asks for bzrlib api version 1.0.0, which is not supported
+        # anymore.
+        name = 'wants100.py'
+        f = file(name, 'w')
+        try:
+            f.write("import bzrlib.api\n"
+                "bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
+        finally:
+            f.close()
+
+        log = self.load_and_capture(name)
+        self.assertContainsRe(log,
+            r"It requested API version")
+
+    def test_plugin_with_bad_name_does_not_load(self):
+        # The file name here invalid for a python module.
+        name = 'bzr-bad plugin-name..py'
+        file(name, 'w').close()
+        log = self.load_and_capture(name)
+        self.assertContainsRe(log,
             r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
             "because the file path isn't a valid module name; try renaming "
             "it to 'bad_plugin_name_'\.")
 
-        stream.close()
-
 
 class TestPlugins(TestCaseInTempDir):
 




More information about the bazaar-commits mailing list