Patchwork D1198: highlight: hide pygments import error at import time

login
register
mail settings
Submitter phabricator
Date Oct. 19, 2017, 9:46 p.m.
Message ID <differential-rev-PHID-DREV-aij6xswunwvkt3yvpui5-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/25258/
State Superseded
Headers show

Comments

phabricator - Oct. 19, 2017, 9:46 p.m.
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




To: quark, #hg-reviewers
Cc: mercurial-devel
phabricator - Oct. 20, 2017, 1:08 p.m.
yuja requested changes to this revision.
yuja added a comment.
This revision now requires changes to proceed.


  Not work because hgext.highlight is a package module and hgext.highlight.highlight
  is initialized to None.
  
  I'll send another patch that will hopefully fix these test failures.

REPOSITORY
  rHG Mercurial

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

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

Patch

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,