Patchwork [4,of,8] util: check for compression engine availability before returning

login
register
mail settings
Submitter Gregory Szorc
Date Nov. 11, 2016, 9:23 a.m.
Message ID <bf84b10919fc5869097a.1478856216@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/17471/
State Accepted
Headers show

Comments

Gregory Szorc - Nov. 11, 2016, 9:23 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1478848502 28800
#      Thu Nov 10 23:15:02 2016 -0800
# Node ID bf84b10919fc5869097a7ba64e15b38859064f62
# Parent  13fccc619c0d8627d05d07f4a51d67a87bf2ca62
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.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2929,15 +2929,27 @@  class compressormanager(object):
         """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()