util: check for compression engine availability before returning
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 10 Nov 2016 23:15:02 -0800
changeset 30438 90933e4e44fd
parent 30437 64d7275445d0
child 30439 71b368e3b590
util: check for compression engine availability before returning If a requested compression engine is registered but not available, requesting it will now abort. To be honest, I'm not sure if this is the appropriate mechanism for handling optional compression engines. I won't know until all uses of compression (bundles, wire protocol, revlogs, etc) are using the new API and zstd (our planned optional engine) is implemented. So this API could change.
mercurial/util.py
--- a/mercurial/util.py	Thu Nov 10 23:03:48 2016 -0800
+++ b/mercurial/util.py	Thu Nov 10 23:15:02 2016 -0800
@@ -3021,15 +3021,27 @@
         """Obtain a compression engine registered to a bundle name.
 
         Will raise KeyError if the bundle type isn't registered.
+
+        Will abort if the engine is known but not available.
         """
-        return self._engines[self._bundlenames[bundlename]]
+        engine = self._engines[self._bundlenames[bundlename]]
+        if not engine.available():
+            raise error.Abort(_('compression engine %s could not be loaded') %
+                              engine.name())
+        return engine
 
     def forbundletype(self, bundletype):
         """Obtain a compression engine registered to a bundle type.
 
         Will raise KeyError if the bundle type isn't registered.
+
+        Will abort if the engine is known but not available.
         """
-        return self._engines[self._bundletypes[bundletype]]
+        engine = self._engines[self._bundletypes[bundletype]]
+        if not engine.available():
+            raise error.Abort(_('compression engine %s could not be loaded') %
+                              engine.name())
+        return engine
 
 compengines = compressormanager()