hgdemandimport/demandimportpy3.py
branchstable
changeset 49366 288de6f5d724
parent 48958 b8eb29ab3906
child 49536 87516dd774d0
child 49774 48e38b179106
equal deleted inserted replaced
49364:e8ea403b1c46 49366:288de6f5d724
    22   practice, we only expect builtins to be loaded before demandimport is
    22   practice, we only expect builtins to be loaded before demandimport is
    23   enabled.
    23   enabled.
    24 """
    24 """
    25 
    25 
    26 # This line is unnecessary, but it satisfies test-check-py3-compat.t.
    26 # This line is unnecessary, but it satisfies test-check-py3-compat.t.
    27 from __future__ import absolute_import
       
    28 
    27 
    29 import contextlib
    28 import contextlib
    30 import importlib.util
    29 import importlib.util
    31 import sys
    30 import sys
    32 
    31 
    33 from . import tracing
    32 from . import tracing
    34 
    33 
    35 _deactivated = False
    34 _deactivated = False
    36 
       
    37 # Python 3.5's LazyLoader doesn't work for some reason.
       
    38 # https://bugs.python.org/issue26186 is a known issue with extension
       
    39 # importing. But it appears to not have a meaningful effect with
       
    40 # Mercurial.
       
    41 _supported = sys.version_info[0:2] >= (3, 6)
       
    42 
    35 
    43 
    36 
    44 class _lazyloaderex(importlib.util.LazyLoader):
    37 class _lazyloaderex(importlib.util.LazyLoader):
    45     """This is a LazyLoader except it also follows the _deactivated global and
    38     """This is a LazyLoader except it also follows the _deactivated global and
    46     the ignore list.
    39     the ignore list.
    53                 self.loader.exec_module(module)
    46                 self.loader.exec_module(module)
    54             else:
    47             else:
    55                 super().exec_module(module)
    48                 super().exec_module(module)
    56 
    49 
    57 
    50 
    58 class LazyFinder(object):
    51 class LazyFinder:
    59     """A wrapper around a ``MetaPathFinder`` that makes loaders lazy.
    52     """A wrapper around a ``MetaPathFinder`` that makes loaders lazy.
    60 
    53 
    61     ``sys.meta_path`` finders have their ``find_spec()`` called to locate a
    54     ``sys.meta_path`` finders have their ``find_spec()`` called to locate a
    62     module. This returns a ``ModuleSpec`` if found or ``None``. The
    55     module. This returns a ``ModuleSpec`` if found or ``None``. The
    63     ``ModuleSpec`` has a ``loader`` attribute, which is called to actually
    56     ``ModuleSpec`` has a ``loader`` attribute, which is called to actually
   143         )
   136         )
   144     sys.meta_path[:] = new_finders
   137     sys.meta_path[:] = new_finders
   145 
   138 
   146 
   139 
   147 def enable():
   140 def enable():
   148     if not _supported:
       
   149         return
       
   150 
       
   151     new_finders = []
   141     new_finders = []
   152     for finder in sys.meta_path:
   142     for finder in sys.meta_path:
   153         new_finders.append(
   143         new_finders.append(
   154             LazyFinder(finder) if not isinstance(finder, LazyFinder) else finder
   144             LazyFinder(finder) if not isinstance(finder, LazyFinder) else finder
   155         )
   145         )