| 1 |
# hgdemandimport - global demand-loading of modules for Mercurial |
| 2 |
# |
| 3 |
# Copyright 2017 Facebook Inc. |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
'''demandimport - automatic demand-loading of modules''' |
| 9 |
|
| 10 |
# This is in a separate package from mercurial because in Python 3, |
| 11 |
# demand loading is per-package. Keeping demandimport in the mercurial package |
| 12 |
# would disable demand loading for any modules in mercurial. |
| 13 |
|
| 14 |
from __future__ import annotations |
| 15 |
|
| 16 |
import os |
| 17 |
import sys |
| 18 |
|
| 19 |
from . import demandimportpy3 as demandimport |
| 20 |
|
| 21 |
# Full module names which can't be lazy imported. |
| 22 |
# Extensions can add to this set. |
| 23 |
IGNORES = { |
| 24 |
'__future__', |
| 25 |
'_hashlib', |
| 26 |
# ImportError during pkg_resources/__init__.py:fixup_namespace_package |
| 27 |
'_imp', |
| 28 |
'_xmlplus', |
| 29 |
'fcntl', |
| 30 |
'nt', # pathlib2 tests the existence of built-in 'nt' module |
| 31 |
'win32com.gen_py', |
| 32 |
'win32com.shell', # 'appdirs' tries to import win32com.shell |
| 33 |
'_winreg', # 2.7 mimetypes needs immediate ImportError |
| 34 |
'pythoncom', |
| 35 |
# imported by tarfile, not available under Windows |
| 36 |
'pwd', |
| 37 |
'grp', |
| 38 |
# imported by profile, itself imported by hotshot.stats, |
| 39 |
# not available under Windows |
| 40 |
'resource', |
| 41 |
# this trips up many extension authors |
| 42 |
'gtk', |
| 43 |
# setuptools' pkg_resources.py expects "from __main__ import x" to |
| 44 |
# raise ImportError if x not defined |
| 45 |
'__main__', |
| 46 |
'_ast', # https://bugs.python.org/issue41631 |
| 47 |
'_ssl', # conditional imports in the stdlib, issue1964 |
| 48 |
'_sre', # issue4920 |
| 49 |
'rfc822', |
| 50 |
'mimetools', |
| 51 |
'sqlalchemy.events', # has import-time side effects (issue5085) |
| 52 |
'sqlalchemy.dialects', # similar problems as above |
| 53 |
# setuptools 8 expects this module to explode early when not on windows |
| 54 |
'distutils.msvc9compiler', |
| 55 |
'__builtin__', |
| 56 |
'builtins', |
| 57 |
'urwid.command_map', # for pudb |
| 58 |
'lzma', |
| 59 |
# setuptools uses this hack to inject it's own distutils at import time |
| 60 |
'setuptools', |
| 61 |
'_distutils_hack.override', |
| 62 |
# threading is locally imported by importlib.util.LazyLoader.exec_module |
| 63 |
'_contextvars', |
| 64 |
'_weakrefset', |
| 65 |
'warnings', |
| 66 |
'threading', |
| 67 |
# The following modules can’t be lazy-loaded on Python 3.13+ because they |
| 68 |
# are substituted in sys.modules during load. |
| 69 |
'collections.abc', |
| 70 |
'decimal', |
| 71 |
# The following modules can’t be lazy-loaded on Python 3.15.0a7 because |
| 72 |
# they are lazily imported with the PEP 810 mechanism from other modules |
| 73 |
# (in the standard library) and that doesn’t work well together with |
| 74 |
# importlib.util.LazyLoader that demandimportpy3 uses. |
| 75 |
'heapq', |
| 76 |
'copy', |
| 77 |
} |
| 78 |
|
| 79 |
_pypy = '__pypy__' in sys.builtin_module_names |
| 80 |
|
| 81 |
if _pypy: |
| 82 |
# _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5) |
| 83 |
IGNORES.add('_ctypes.pointer') |
| 84 |
# pure Python module on PyPy, must be loaded to raise ModuleNotFoundError |
| 85 |
# on non-Windows platforms |
| 86 |
IGNORES.add('msvcrt') |
| 87 |
|
| 88 |
demandimport.init(IGNORES) |
| 89 |
|
| 90 |
# Re-export. |
| 91 |
isenabled = demandimport.isenabled |
| 92 |
disable = demandimport.disable |
| 93 |
deactivated = demandimport.deactivated |
| 94 |
|
| 95 |
|
| 96 |
def enable(): |
| 97 |
# chg pre-imports modules so do not enable demandimport for it |
| 98 |
if ( |
| 99 |
'CHGINTERNALMARK' not in os.environ |
| 100 |
and os.environ.get('HGDEMANDIMPORT') != 'disable' |
| 101 |
): |
| 102 |
demandimport.enable() |