##// END OF EJS Templates
policy: remove unused policynoc and policynocffi constants
Yuya Nishihara -
r32514:3f5d675f default
parent child Browse files
Show More
@@ -1,106 +1,104
1 1 # policy.py - module policy logic for Mercurial.
2 2 #
3 3 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import os
11 11 import sys
12 12
13 13 # Rules for how modules can be loaded. Values are:
14 14 #
15 15 # c - require C extensions
16 16 # allow - allow pure Python implementation when C loading fails
17 17 # cffi - required cffi versions (implemented within pure module)
18 18 # cffi-allow - allow pure Python implementation if cffi version is missing
19 19 # py - only load pure Python modules
20 20 #
21 21 # By default, fall back to the pure modules so the in-place build can
22 22 # run without recompiling the C extensions. This will be overridden by
23 23 # __modulepolicy__ generated by setup.py.
24 24 policy = b'allow'
25 policynoc = (b'cffi', b'cffi-allow', b'py')
26 policynocffi = (b'c', b'py')
27 25 _packageprefs = {
28 26 # policy: (versioned package, pure package)
29 27 b'c': (r'cext', None),
30 28 b'allow': (r'cext', r'pure'),
31 29 b'cffi': (r'cffi', None),
32 30 b'cffi-allow': (r'cffi', r'pure'),
33 31 b'py': (None, r'pure'),
34 32 }
35 33
36 34 try:
37 35 from . import __modulepolicy__
38 36 policy = __modulepolicy__.modulepolicy
39 37 except ImportError:
40 38 pass
41 39
42 40 # PyPy doesn't load C extensions.
43 41 #
44 42 # The canonical way to do this is to test platform.python_implementation().
45 43 # But we don't import platform and don't bloat for it here.
46 44 if r'__pypy__' in sys.builtin_module_names:
47 45 policy = b'cffi'
48 46
49 47 # Our C extensions aren't yet compatible with Python 3. So use pure Python
50 48 # on Python 3 for now.
51 49 if sys.version_info[0] >= 3:
52 50 policy = b'py'
53 51
54 52 # Environment variable can always force settings.
55 53 if sys.version_info[0] >= 3:
56 54 if r'HGMODULEPOLICY' in os.environ:
57 55 policy = os.environ[r'HGMODULEPOLICY'].encode(r'utf-8')
58 56 else:
59 57 policy = os.environ.get(r'HGMODULEPOLICY', policy)
60 58
61 59 def _importfrom(pkgname, modname):
62 60 # from .<pkgname> import <modname> (where . is looked through this module)
63 61 fakelocals = {}
64 62 pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1)
65 63 try:
66 64 fakelocals[modname] = mod = getattr(pkg, modname)
67 65 except AttributeError:
68 66 raise ImportError(r'cannot import name %s' % modname)
69 67 # force import; fakelocals[modname] may be replaced with the real module
70 68 getattr(mod, r'__doc__', None)
71 69 return fakelocals[modname]
72 70
73 71 # keep in sync with "version" in C modules
74 72 _cextversions = {
75 73 (r'cext', r'base85'): 1,
76 74 (r'cext', r'bdiff'): 1,
77 75 (r'cext', r'diffhelpers'): 1,
78 76 (r'cext', r'mpatch'): 1,
79 77 (r'cext', r'osutil'): 1,
80 78 (r'cext', r'parsers'): 1,
81 79 }
82 80
83 81 def _checkmod(pkgname, modname, mod):
84 82 expected = _cextversions.get((pkgname, modname))
85 83 actual = getattr(mod, r'version', None)
86 84 if actual != expected:
87 85 raise ImportError(r'cannot import module %s.%s '
88 86 r'(expected version: %d, actual: %r)'
89 87 % (pkgname, modname, expected, actual))
90 88
91 89 def importmod(modname):
92 90 """Import module according to policy and check API version"""
93 91 try:
94 92 verpkg, purepkg = _packageprefs[policy]
95 93 except KeyError:
96 94 raise ImportError(r'invalid HGMODULEPOLICY %r' % policy)
97 95 assert verpkg or purepkg
98 96 if verpkg:
99 97 try:
100 98 mod = _importfrom(verpkg, modname)
101 99 _checkmod(verpkg, modname, mod)
102 100 return mod
103 101 except ImportError:
104 102 if not purepkg:
105 103 raise
106 104 return _importfrom(purepkg, modname)
General Comments 0
You need to be logged in to leave comments. Login now