mercurial/__init__.py
author Martin von Zweigbergk <martinvonz@google.com>
Tue, 19 Jan 2016 17:44:25 -0800
branchstable
changeset 27920 da5f23362517
parent 27225 30a20167ae29
child 28430 17b85d739b62
permissions -rw-r--r--
changegroup: cg3 has two empty groups *after* manifests changegroup.getchunks() determines the end of the stream by looking for an empty chunk group (two consecutive empty chunks). It ignores empty groups in the first two groups. Changegroup 3 introduced an empty chunk between the manifests and the files, which confuses getchunks(). Since it comes after the first two, getchunks() will stop there. Fix by rewriting getchunks so it first counts two groups (empty or not) and then keeps antostarts counting empty groups. With this counting, changegroup 1 and 2 have exactly one empty group after the first two groups, while changegroup 3 has two (one for directories and one for files). It's a little hard to test this at this point, but I have verified that this patch fixes narrowhg (which was broken before this patch). Also, future patches will fix "hg strip" with treemanifests, and once that's done, getchunks() will be tested through tests of "hg strip".
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27220
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
27225
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    13
import zipimport
27220
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' '@':
27223
a40c84defd76 mercurial: be more strict about loading dual implemented modules
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27220
diff changeset
    26
    modulepolicy = 'c'
27220
4374d819ccd5 mercurial: implement import hook for handling C/Python modules
Gregory Szorc <gregory.szorc@gmail.com>
parents: 0
diff changeset
    27
27224
d308a9ca9ed7 mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27223
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: 27223
diff changeset
    29
#
d308a9ca9ed7 mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27223
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: 27223
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: 27223
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: 27223
diff changeset
    33
    modulepolicy = 'py'
d308a9ca9ed7 mercurial: don't load C extensions from PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27223
diff changeset
    34
27220
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
27225
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
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: 27224
diff changeset
    65
        # importer to warrant its own code path.
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    66
        loader = getattr(mercurial, '__loader__', None)
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    67
        if isinstance(loader, zipimport.zipimporter):
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    68
            def ziploader(*paths):
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
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: 27224
diff changeset
    70
                path = os.path.join(loader.archive, *paths)
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    71
                zl = sys.path_importer_cache.get(path)
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    72
                if not zl:
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    73
                    zl = zipimport.zipimporter(path)
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    74
                return zl
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    75
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    76
            try:
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    77
                if modulepolicy == 'py':
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    78
                    raise ImportError()
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    79
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    80
                zl = ziploader('mercurial')
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    81
                mod = zl.load_module(name)
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
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: 27224
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: 27224
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: 27224
diff changeset
    85
            except ImportError:
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    86
                if modulepolicy == 'c':
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    87
                    raise
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    88
                zl = ziploader('mercurial', 'pure')
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    89
                mod = zl.load_module(name)
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    90
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    91
            sys.modules[name] = mod
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    92
            return mod
30a20167ae29 mercurial: support loading modules from zipimporter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27224
diff changeset
    93
27220
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.
27223
a40c84defd76 mercurial: be more strict about loading dual implemented modules
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27220
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: 27220
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: 27220
diff changeset
   121
                                  'version should exist' % name)
27220
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())