Mercurial > hg > mercurial-crew
annotate mercurial/__init__.py @ 27865:30a20167ae29
mercurial: support loading modules from zipimporter
The previous refactor to module importing broke module loading when
mercurial.* modules were loaded from a zipfile (using a zipimporter).
This scenario is likely encountered when using py2exe.
Supporting zipimporter and the traditional importer side-by-side
turns out to be quite a pain. In Python 2.x, the standard, file-based
import mechanism is partially implemented in C. The sys.meta_path
and sys.path_hooks hook points exist to allow custom importers in
Python/userland. zipimport.zipimporter and our "hgimporter" class
from earlier in this patch series are 2 of these.
In a standard Python installation (no matter if running in py2exe
or similar or not), zipimport.zipimporter appears to be registered
in sys.path_hooks. This means that as each sys.path entry is
consulted, it will ask zipimporter if it supports that path and
zipimporter will be used if that entry is a zip file. In a
py2exe environment, sys.path contains an entry with the path to
the zip file containing the Python standard library along with
Mercurial's Python files.
The way the importer mechanism works is the first importer that
declares knowledge of a module (via find_module() returning an
object) gets to load it. Since our "hgimporter" is registered
in sys.meta_path and returns an interest in specific mercurial.*
modules, the zipimporter registered on sys.path_hooks never comes
into play for these modules. So, we need to be zipimporter aware
and call into zipimporter to load modules.
This patch teaches "hgimporter" how to call out into zipimporter
when necessary. We detect the necessity of zipimporter by looking
at the loader for the "mercurial" module. If it is a zipimporter
instance, we load via zipimporter.
The behavior of zipimporter is a bit wonky.
You appear to need separate zipimporter instances for each directory
in the zip file. I'm not sure why this is. I suspect it has
something to do with the low-level importing mechanism (implemented
in C) operating on a per-directory basis. PEP-302 makes some
references to this. I was not able to get a zipimporter to
import modules outside of its immediate directory no matter how
I specified the module name. This is why we use separate
zipimporter instances for the ".zip/mercurial" and
".zip/mercurial/pure" locations.
The zipimporter documentation for Python 2.7 explicitly states that
zipimporter does not import dynamic modules (C extensions). Yet from
a py2exe distribution on Windows - where the .pyd files are *not*
in the zip archive - zipimporter imported these dynamic modules
just fine! I'm not sure if dynamic modules can't be imported from
*inside* the zip archive or whether zipimporter looks for dynamic
modules outside the zip archive. All I know is zipimporter does
manage to import the .pyd files on Windows and this patch makes
our new importer compatible with py2exe.
In the ideal world, We'd probably reimplement or fall back to parts
of the built-in import mechanism instead of handling zipimporter
specially. After all, if we're loading Mercurial modules via
something that isn't the built-in file-based importer or zipimporter,
our custom importer will likely fail because it doesn't know how to
call into it. I'd like to think that we'll never encounter this
in the wild, but you never know. If we do encounter it, we can
come up with another solution.
It's worth nothing that Python 3 has moved a lot of the importing
code from C to Python. Python 3 gives you near total control over
the import mechanism. So in the very distant future when Mercurial
drops Python 2 support, it's likely that our custom importer code
can be refactored to something a bit saner.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 03 Dec 2015 21:25:05 -0800 (2015-12-04) |
parents | d308a9ca9ed7 |
children |
rev | line source |
---|---|
27860
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
1 # __init__.py - Startup and module loading logic for Mercurial. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
2 # |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
3 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
4 # |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
7 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
8 from __future__ import absolute_import |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
9 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
10 import imp |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
11 import os |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
12 import sys |
27865
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
13 import zipimport |
27860
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
14 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
15 __all__ = [] |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
16 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
17 # Rules for how modules can be loaded. Values are: |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
18 # |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
19 # c - require C extensions |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
20 # allow - allow pure Python implementation when C loading fails |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
21 # py - only load pure Python modules |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
22 modulepolicy = '@MODULELOADPOLICY@' |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
23 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
24 # By default, require the C extensions for performance reasons. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
25 if modulepolicy == '@' 'MODULELOADPOLICY' '@': |
27863
a40c84defd76
mercurial: be more strict about loading dual implemented modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27860
diff
changeset
|
26 modulepolicy = 'c' |
27860
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
27 |
27864
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
28 # PyPy doesn't load C extensions. |
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
29 # |
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
30 # The canonical way to do this is to test platform.python_implementation(). |
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
31 # But we don't import platform and don't bloat for it here. |
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
32 if '__pypy__' in sys.builtin_module_names: |
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
33 modulepolicy = 'py' |
d308a9ca9ed7
mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27863
diff
changeset
|
34 |
27860
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
35 # Environment variable can always force settings. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
36 modulepolicy = os.environ.get('HGMODULEPOLICY', modulepolicy) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
37 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
38 # Modules that have both Python and C implementations. See also the |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
39 # set of .py files under mercurial/pure/. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
40 _dualmodules = set([ |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
41 'mercurial.base85', |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
42 'mercurial.bdiff', |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
43 'mercurial.diffhelpers', |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
44 'mercurial.mpatch', |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
45 'mercurial.osutil', |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
46 'mercurial.parsers', |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
47 ]) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
48 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
49 class hgimporter(object): |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
50 """Object that conforms to import hook interface defined in PEP-302.""" |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
51 def find_module(self, name, path=None): |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
52 # We only care about modules that have both C and pure implementations. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
53 if name in _dualmodules: |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
54 return self |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
55 return None |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
56 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
57 def load_module(self, name): |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
58 mod = sys.modules.get(name, None) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
59 if mod: |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
60 return mod |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
61 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
62 mercurial = sys.modules['mercurial'] |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
63 |
27865
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
64 # The zip importer behaves sufficiently differently from the default |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
65 # importer to warrant its own code path. |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
66 loader = getattr(mercurial, '__loader__', None) |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
67 if isinstance(loader, zipimport.zipimporter): |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
68 def ziploader(*paths): |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
69 """Obtain a zipimporter for a directory under the main zip.""" |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
70 path = os.path.join(loader.archive, *paths) |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
71 zl = sys.path_importer_cache.get(path) |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
72 if not zl: |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
73 zl = zipimport.zipimporter(path) |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
74 return zl |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
75 |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
76 try: |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
77 if modulepolicy == 'py': |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
78 raise ImportError() |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
79 |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
80 zl = ziploader('mercurial') |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
81 mod = zl.load_module(name) |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
82 # Unlike imp, ziploader doesn't expose module metadata that |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
83 # indicates the type of module. So just assume what we found |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
84 # is OK (even though it could be a pure Python module). |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
85 except ImportError: |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
86 if modulepolicy == 'c': |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
87 raise |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
88 zl = ziploader('mercurial', 'pure') |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
89 mod = zl.load_module(name) |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
90 |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
91 sys.modules[name] = mod |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
92 return mod |
30a20167ae29
mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27864
diff
changeset
|
93 |
27860
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
94 # Unlike the default importer which searches special locations and |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
95 # sys.path, we only look in the directory where "mercurial" was |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
96 # imported from. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
97 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
98 # imp.find_module doesn't support submodules (modules with "."). |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
99 # Instead you have to pass the parent package's __path__ attribute |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
100 # as the path argument. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
101 stem = name.split('.')[-1] |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
102 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
103 try: |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
104 if modulepolicy == 'py': |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
105 raise ImportError() |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
106 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
107 modinfo = imp.find_module(stem, mercurial.__path__) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
108 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
109 # The Mercurial installer used to copy files from |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
110 # mercurial/pure/*.py to mercurial/*.py. Therefore, it's possible |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
111 # for some installations to have .py files under mercurial/*. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
112 # Loading Python modules when we expected C versions could result |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
113 # in a) poor performance b) loading a version from a previous |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
114 # Mercurial version, potentially leading to incompatibility. Either |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
115 # scenario is bad. So we verify that modules loaded from |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
116 # mercurial/* are C extensions. If the current policy allows the |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
117 # loading of .py modules, the module will be re-imported from |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
118 # mercurial/pure/* below. |
27863
a40c84defd76
mercurial: be more strict about loading dual implemented modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27860
diff
changeset
|
119 if modinfo[2][2] != imp.C_EXTENSION: |
a40c84defd76
mercurial: be more strict about loading dual implemented modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27860
diff
changeset
|
120 raise ImportError('.py version of %s found where C ' |
a40c84defd76
mercurial: be more strict about loading dual implemented modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27860
diff
changeset
|
121 'version should exist' % name) |
27860
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
122 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
123 except ImportError: |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
124 if modulepolicy == 'c': |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
125 raise |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
126 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
127 # Could not load the C extension and pure Python is allowed. So |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
128 # try to load them. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
129 from . import pure |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
130 modinfo = imp.find_module(stem, pure.__path__) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
131 if not modinfo: |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
132 raise ImportError('could not find mercurial module %s' % |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
133 name) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
134 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
135 mod = imp.load_module(name, *modinfo) |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
136 sys.modules[name] = mod |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
137 return mod |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
138 |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
139 # We automagically register our custom importer as a side-effect of loading. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
140 # This is necessary to ensure that any entry points are able to import |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
141 # mercurial.* modules without having to perform this registration themselves. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
142 if not any(isinstance(x, hgimporter) for x in sys.meta_path): |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
143 # meta_path is used before any implicit finders and before sys.path. |
4374d819ccd5
mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents:
0
diff
changeset
|
144 sys.meta_path.insert(0, hgimporter()) |