[PATCH 1 of 5 V3] perf: import newer modules separately for earlier Mercurial

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Fri Jul 15 11:29:42 EDT 2016

# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1463705255 -32400
#      Fri May 20 09:47:35 2016 +0900
# Node ID cf54693a2b23005be994149cc89579af06224d46
# Parent  9afe1d6924fe28e8521c5a7bf57c5fd2b54ba386
perf: import newer modules separately for earlier Mercurial

demandimport of early Mercurial loads an imported module immediately,
if a module is imported absolutely by "from a import b" style. Recent
perf.py satisfies this condition, because it does:

  - have "from __future__ import absolute_import" line
  - use "from a import b" style for modules in "mercurial" package

Before this patch, importing modules below prevents perf.py from being
loaded by earlier Mercurial, because these aren't available in such
Mercurial, even though there are some code paths for Mercurial earlier
than 1.9.

  - branchmap 2.5 (or bcee63733aad)
  - repoview  2.5 (or 3a6ddacb7198)
  - obsolete  2.3 (or ad0d6c2b3279)
  - scmutil   1.9 (or 8b252e826c68)

For example, setting "_prereadsize" attribute in perfindex() and
perfnodelookup() is effective only with Mercurial earlier than 1.8 (or

After this patch, "mercurial.error" is the only blocker in "from
mercurial import" statement for loading perf.py with Mercurial earlier
than 1.2. This patch ignores it, because just importing it separately
isn't enough.

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -25,7 +25,6 @@ import random
 import sys
 import time
 from mercurial import (
-    branchmap,
@@ -33,14 +32,31 @@ from mercurial import (
-    obsolete,
-    repoview,
-    scmutil,
 # for "historical portability":
+# try to import modules separately (in dict order), and ignore
+# failure, because these aren't available with early Mercurial
+    from mercurial import branchmap # since 2.5 (or bcee63733aad)
+except ImportError:
+    pass
+    from mercurial import obsolete # since 2.3 (or ad0d6c2b3279)
+except ImportError:
+    pass
+    from mercurial import repoview # since 2.5 (or 3a6ddacb7198)
+except ImportError:
+    pass
+    from mercurial import scmutil # since 1.9 (or 8b252e826c68)
+except ImportError:
+    pass
+# for "historical portability":
 # define util.safehasattr forcibly, because util.safehasattr has been
 # available since 1.9.3 (or 94b200a11cf7)
 _undefined = object()

More information about the Mercurial-devel mailing list