annotate mercurial/policy.py @ 32511:2e431fb98c6b

policy: extend API version checks for cffi This is just a stub for future extension. I could add a version constant to CFFI modules by putting it to both ffi.set_source() and ffi.cdef(), but that doesn't seem right. So for now, cffi modules will be explicitly unversioned (i.e. version constant must be undefined or set to None.) We can revisit it later when we need to consider CFFI support more seriously.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 28 May 2017 15:45:52 +0900
parents 28b773aa3ff2
children 0e8b0b9a7acc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
1 # policy.py - module policy logic for Mercurial.
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
2 #
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
3 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com>
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
4 #
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
5 # This software may be used and distributed according to the terms of the
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
6 # GNU General Public License version 2 or any later version.
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
7
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
8 from __future__ import absolute_import
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
9
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
10 import os
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
11 import sys
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
12
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
13 # Rules for how modules can be loaded. Values are:
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
14 #
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
15 # c - require C extensions
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
16 # allow - allow pure Python implementation when C loading fails
29490
b4d117cee636 policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents: 29266
diff changeset
17 # cffi - required cffi versions (implemented within pure module)
b4d117cee636 policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents: 29266
diff changeset
18 # cffi-allow - allow pure Python implementation if cffi version is missing
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
19 # py - only load pure Python modules
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
20 #
32251
a04f5c651e52 policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents: 32210
diff changeset
21 # By default, fall back to the pure modules so the in-place build can
a04f5c651e52 policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents: 32210
diff changeset
22 # run without recompiling the C extensions. This will be overridden by
a04f5c651e52 policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents: 32210
diff changeset
23 # __modulepolicy__ generated by setup.py.
a04f5c651e52 policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents: 32210
diff changeset
24 policy = b'allow'
31361
8a17c541177f py3: add "b" prefix to string literals related to module policy
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31308
diff changeset
25 policynoc = (b'cffi', b'cffi-allow', b'py')
8a17c541177f py3: add "b" prefix to string literals related to module policy
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31308
diff changeset
26 policynocffi = (b'c', b'py')
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
27 _packageprefs = {
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
28 # policy: (versioned package, pure package)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
29 b'c': (r'cext', None),
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
30 b'allow': (r'cext', r'pure'),
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
31 b'cffi': (None, r'pure'), # TODO: (r'cffi', None)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
32 b'cffi-allow': (None, r'pure'), # TODO: (r'cffi', r'pure')
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
33 b'py': (None, r'pure'),
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
34 }
29490
b4d117cee636 policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents: 29266
diff changeset
35
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
36 try:
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
37 from . import __modulepolicy__
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
38 policy = __modulepolicy__.modulepolicy
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
39 except ImportError:
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
40 pass
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
41
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
42 # PyPy doesn't load C extensions.
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
43 #
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
44 # The canonical way to do this is to test platform.python_implementation().
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
45 # But we don't import platform and don't bloat for it here.
32210
56148133ef36 policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31361
diff changeset
46 if r'__pypy__' in sys.builtin_module_names:
56148133ef36 policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31361
diff changeset
47 policy = b'cffi'
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
48
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
49 # Our C extensions aren't yet compatible with Python 3. So use pure Python
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
50 # on Python 3 for now.
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
51 if sys.version_info[0] >= 3:
31308
62939e0148f1 policy: try and always have a bytes for module policy
Augie Fackler <raf@durin42.com>
parents: 29490
diff changeset
52 policy = b'py'
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
53
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
54 # Environment variable can always force settings.
31361
8a17c541177f py3: add "b" prefix to string literals related to module policy
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31308
diff changeset
55 if sys.version_info[0] >= 3:
32210
56148133ef36 policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31361
diff changeset
56 if r'HGMODULEPOLICY' in os.environ:
56148133ef36 policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31361
diff changeset
57 policy = os.environ[r'HGMODULEPOLICY'].encode(r'utf-8')
31361
8a17c541177f py3: add "b" prefix to string literals related to module policy
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31308
diff changeset
58 else:
32210
56148133ef36 policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31361
diff changeset
59 policy = os.environ.get(r'HGMODULEPOLICY', policy)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
60
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
61 def _importfrom(pkgname, modname):
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
62 # from .<pkgname> import <modname> (where . is looked through this module)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
63 fakelocals = {}
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
64 pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
65 try:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
66 fakelocals[modname] = mod = getattr(pkg, modname)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
67 except AttributeError:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
68 raise ImportError(r'cannot import name %s' % modname)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
69 # force import; fakelocals[modname] may be replaced with the real module
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
70 getattr(mod, r'__doc__', None)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
71 return fakelocals[modname]
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
72
32428
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
73 # keep in sync with "version" in C modules
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
74 _cextversions = {
32511
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
75 (r'cext', r'base85'): 1,
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
76 (r'cext', r'bdiff'): 1,
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
77 (r'cext', r'diffhelpers'): 1,
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
78 (r'cext', r'mpatch'): 1,
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
79 (r'cext', r'osutil'): 1,
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
80 (r'cext', r'parsers'): 1,
32428
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
81 }
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
82
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
83 def _checkmod(pkgname, modname, mod):
32511
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
84 expected = _cextversions.get((pkgname, modname))
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
85 actual = getattr(mod, r'version', None)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
86 if actual != expected:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
87 raise ImportError(r'cannot import module %s.%s '
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
88 r'(expected version: %d, actual: %r)'
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
89 % (pkgname, modname, expected, actual))
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
90
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
91 def importmod(modname):
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
92 """Import module according to policy and check API version"""
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
93 try:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
94 verpkg, purepkg = _packageprefs[policy]
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
95 except KeyError:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
96 raise ImportError(r'invalid HGMODULEPOLICY %r' % policy)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
97 assert verpkg or purepkg
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
98 if verpkg:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
99 try:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
100 mod = _importfrom(verpkg, modname)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
101 _checkmod(verpkg, modname, mod)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
102 return mod
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
103 except ImportError:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
104 if not purepkg:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
105 raise
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
106 return _importfrom(purepkg, modname)