[PATCH 2 of 8] localrepo: use absolute import

Gregory Szorc gregory.szorc at gmail.com
Mon May 25 14:23:46 CDT 2015

# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1432580350 25200
#      Mon May 25 11:59:10 2015 -0700
# Node ID c2b845cda536ef0abc665befb19319f53bb6216e
# Parent  f085ecda3b0e11341ecc119683d5fc47038540d4
localrepo: use absolute import

Python 3 changes the behavior of "import" to only be absolute. In Python
2.x "import foo" could import a top-level package or a module from the
current package. This resulted in ambiguity when the name of a module
conflicted with the name of a package. It also resulted in a slight
performance overhead. Since the name of a module could be ambiguous,
Python would stat() multiple paths until a module was located. With
absolute import, the module name is not ambiguous and excessive stat()
calls can be avoided.

Python 2.5 introduced the "absolute_import" feature to __future__ to
enable Python 2.x to import like Python 3. Adopting "absolute_import"
makes dual compatibility with Python 3 easier to achieve. In my
opinion, it also makes code easier to read since there is no ambiguity
as to where a module is coming from. With a project of Mercurial's
size (~120 .py files), this can be a real concern, especially for
new contributors.

PEP-0328 also introduced the multiple line import statement. Simply
add parenthesis and you get more readable import statements. This
PEP also introduced relative imports ("from . import foo").

This patch switches localrepo.py to use absolute imports as well as
modern import syntax. While I was here, I also reordered imports:
standard library first, Mercurial second, each alphabetized within its

It is worth noting that the "from X import Y" syntax is not used. This
is because it breaks Mercurial's lazy import mechanism. While "from X
import Y" syntax would be easier to read (especially when multiple
imports are used), the performance side-effects would be unacceptable.

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -3,23 +3,57 @@
 # Copyright 2005-2007 Matt Mackall <mpm at selenic.com>
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
-from node import hex, nullid, short
-from i18n import _
+from __future__ import absolute_import
+import errno
+import inspect
+import os
+import time
 import urllib
-import peer, changegroup, subrepo, pushkey, obsolete, repoview
-import changelog, dirstate, filelog, manifest, context, bookmarks, phases
-import lock as lockmod
-import transaction, store, encoding, exchange, bundle2
-import scmutil, util, extensions, hook, error, revset
-import match as matchmod
-import merge as mergemod
-import tags as tagsmod
-from lock import release
-import weakref, errno, os, time, inspect
-import branchmap, pathutil
-import namespaces
+import weakref
+import mercurial.bookmarks as bookmarks
+import mercurial.branchmap as branchmap
+import mercurial.bundle2 as bundle2
+import mercurial.changegroup as changegroup
+import mercurial.changelog as changelog
+import mercurial.context as context
+import mercurial.dirstate as dirstate
+import mercurial.encoding as encoding
+import mercurial.error as error
+import mercurial.exchange as exchange
+import mercurial.extensions as extensions
+import mercurial.filelog as filelog
+import mercurial.hook as hook
+from mercurial.i18n import _
+import mercurial.lock as lockmod
+import mercurial.manifest as manifest
+import mercurial.match as matchmod
+import mercurial.merge as mergemod
+import mercurial.namespaces as namespaces
+from mercurial.node import (
+    hex,
+    nullid,
+    short,
+import mercurial.obsolete as obsolete
+import mercurial.pathutil as pathutil
+import mercurial.peer as peer
+import mercurial.phases as phases
+import mercurial.pushkey as pushkey
+import mercurial.repoview as repoview
+import mercurial.revset as revset
+import mercurial.scmutil as scmutil
+import mercurial.store as store
+import mercurial.subrepo as subrepo
+import mercurial.tags as tagsmod
+import mercurial.transaction as transaction
+import mercurial.util as util
+release = lockmod.release
 propertycache = util.propertycache
 filecache = scmutil.filecache
 class repofilecache(filecache):

More information about the Mercurial-devel mailing list