From patchwork Sun May 21 20:48:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [8, of, 8, demandimport-py3] python3: allow hgloader to work with lazy loaders From: Siddharth Agarwal X-Patchwork-Id: 20802 Message-Id: <902d8e08f7e5bc6114e2.1495399683@devvm31800.prn1.facebook.com> To: Date: Sun, 21 May 2017 13:48:03 -0700 # HG changeset patch # User Siddharth Agarwal # Date 1495398377 25200 # Sun May 21 13:26:17 2017 -0700 # Node ID 902d8e08f7e5bc6114e2fd9285be6ef75225da9e # Parent 1bf07f6a0c323a4a842d7e76d14e4e893a2b68ca python3: allow hgloader to work with lazy loaders Don't clobber the loader returned from find_spec. This brings `hg version` down from 0.27 seconds to 0.17. diff --git a/mercurial/__init__.py b/mercurial/__init__.py --- a/mercurial/__init__.py +++ b/mercurial/__init__.py @@ -53,7 +53,14 @@ if sys.version_info[0] >= 3: # TODO need to support loaders from alternate specs, like zip # loaders. - spec.loader = hgloader(spec.name, spec.origin) + loader = hgloader(spec.name, spec.origin) + # Can't use util.safehasattr here because that would require + # importing util, and we're in import code. + if hasattr(spec.loader, 'loader'): # hasattr-py3-only + # This is a nested loader (maybe a lazy loader?) + spec.loader.loader = loader + else: + spec.loader = loader return spec def replacetokens(tokens, fullname):