D1198: highlight: hide pygments import error at import time

quark (Jun Wu) phabricator at mercurial-scm.org
Thu Oct 19 21:46:09 UTC 2017


quark created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Commands like `hg help` has a side effect of loading every extension. If
  `pygments` is not installed, loading the `highlight` could fail immediately
  with chg or HGDEMANDIMPORT=disable.
  
  This patch special handles the ImportError so `import highlight from hgext`
  does not raise an exception if pygments is missing. That makes the following
  tests pass with `--chg` and without pygments:
  
  - test-bad-extension.t
  - test-convert-bzr-directories.t
  - test-run-tests.t
  - test-extdiff.t

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1198

AFFECTED FILES
  hgext/highlight/__init__.py

CHANGE DETAILS

diff --git a/hgext/highlight/__init__.py b/hgext/highlight/__init__.py
--- a/hgext/highlight/__init__.py
+++ b/hgext/highlight/__init__.py
@@ -28,7 +28,6 @@
 
 from __future__ import absolute_import
 
-from . import highlight
 from mercurial.hgweb import (
     common,
     webcommands,
@@ -47,6 +46,21 @@
 # leave the attribute unspecified.
 testedwith = 'ships-with-hg-core'
 
+# Hide highlight ImportError at import time, so this module does not have
+# unwanted side-effect if pygments is not installed. This affects `hg help`
+# which loads every extension.
+highlight = None
+
+def _loadhighlight():
+    global highlight
+    if highlight is None:
+        from . import highlight
+
+try:
+    _loadhighlight()
+except ImportError:
+    pass
+
 def pygmentize(web, field, fctx, tmpl):
     style = web.config('web', 'pygments_style', 'colorful')
     expr = web.config('web', 'highlightfiles', "size('<5M')")
@@ -56,6 +70,7 @@
     tree = fileset.parse(expr)
     mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None)
     if fctx.path() in fileset.getset(mctx, tree):
+        _loadhighlight()
         highlight.pygmentize(field, fctx, style, tmpl,
                 guessfilenameonly=filenameonly)
 
@@ -83,6 +98,7 @@
 
 def generate_css(web, req, tmpl):
     pg_style = web.config('web', 'pygments_style', 'colorful')
+    _loadhighlight()
     fmter = highlight.HtmlFormatter(style=pg_style)
     req.respond(common.HTTP_OK, 'text/css')
     return ['/* pygments_style = %s */\n\n' % pg_style,



To: quark, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list