__init__.py 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# hgdemandimport - global demand-loading of modules for Mercurial
#
# Copyright 2017 Facebook Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

'''demandimport - automatic demand-loading of modules'''

# This is in a separate package from mercurial because in Python 3,
# demand loading is per-package. Keeping demandimport in the mercurial package
# would disable demand loading for any modules in mercurial.

from __future__ import absolute_import

16
import os
17 18
import sys

19 20 21 22
if sys.version_info[0] >= 3:
    from . import demandimportpy3 as demandimport
else:
    from . import demandimportpy2 as demandimport
23

24 25 26
# Full module names which can't be lazy imported.
# Extensions can add to this set.
IGNORES = {
27 28 29 30 31 32
    '__future__',
    '_hashlib',
    # ImportError during pkg_resources/__init__.py:fixup_namespace_package
    '_imp',
    '_xmlplus',
    'fcntl',
33
    'nt',  # pathlib2 tests the existence of built-in 'nt' module
34
    'win32com.gen_py',
35 36
    'win32com.shell',  # 'appdirs' tries to import win32com.shell
    '_winreg',  # 2.7 mimetypes needs immediate ImportError
37 38 39 40 41 42 43 44 45 46 47 48
    'pythoncom',
    # imported by tarfile, not available under Windows
    'pwd',
    'grp',
    # imported by profile, itself imported by hotshot.stats,
    # not available under Windows
    'resource',
    # this trips up many extension authors
    'gtk',
    # setuptools' pkg_resources.py expects "from __main__ import x" to
    # raise ImportError if x not defined
    '__main__',
49 50
    '_ssl',  # conditional imports in the stdlib, issue1964
    '_sre',  # issue4920
51 52
    'rfc822',
    'mimetools',
53
    'sqlalchemy.events',  # has import-time side effects (issue5085)
54 55 56 57
    # setuptools 8 expects this module to explode early when not on windows
    'distutils.msvc9compiler',
    '__builtin__',
    'builtins',
58
    'urwid.command_map',  # for pudb
59
}
60 61 62 63

_pypy = '__pypy__' in sys.builtin_module_names

if _pypy:
64 65
    # _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5)
    IGNORES.add('_ctypes.pointer')
66

67
demandimport.init(IGNORES)
68

69 70 71 72
# Re-export.
isenabled = demandimport.isenabled
disable = demandimport.disable
deactivated = demandimport.deactivated
73

74

75
def enable():
76
    # chg pre-imports modules so do not enable demandimport for it
77 78 79 80
    if (
        'CHGINTERNALMARK' not in os.environ
        and os.environ.get('HGDEMANDIMPORT') != 'disable'
    ):
81
        demandimport.enable()