annotate mercurial/policy.py @ 43913:4b7d5d10c45d

exchange: ensure all outgoing subrepo references are present before pushing We've run into occasional problems with people committing a repo, and then amending or rebasing in the subrepo. That makes it so that the revision in the parent can't be checked out, and the problem gets propagated on push. Mercurial already tries to defend against this sort of dangling reference by pushing *all* subrepo revisions first. This reuses the checks that trigger warnings in `hg verify` to bail on the push unless using `--force`. I thought about putting this on the server side, but at that point, all of the data has been transferred, only to bail out. Additionally, SCM Manager hosts subrepos in a location that isn't nested in the parent, so normal subrepo code would complain that the subrepo is missing when run on the server. Because the push command pushes subrepos before calling this exchange code, a subrepo will be pushed before the parent is verified. Not great, but no dangling references are exchanged, so it solves the problem. This code isn't in the loop that pushes the subrepos because: 1) the list of outgoing revisions is needed to limit the scope of the check 2) the loop only accesses the current revision, and therefore can miss subrepos that were dropped in previous commits 3) this code is called when pushing a subrepo, so the protection is recursive I'm not sure if there's a cheap check for the list of files in the outgoing bundle. If there is, that would provide a fast path to bypass this check for people not using subrepos (or if no subrepo changes were made). There's probably also room for verifying other references like tags. But since that doesn't break checkouts, it's much less of a problem. Differential Revision: https://phab.mercurial-scm.org/D7616
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 12 Dec 2019 12:30:15 -0500
parents b56de57c45ce
children 61e7464477ac
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
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43075
diff changeset
13 from .pycompat import getattr
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43075
diff changeset
14
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
15 # Rules for how modules can be loaded. Values are:
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
16 #
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
17 # c - require C extensions
42451
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
18 # rust+c - require Rust and C extensions
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
19 # rust+c-allow - allow Rust and C extensions with fallback to pure Python
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
20 # for each
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
21 # 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
22 # cffi - required cffi versions (implemented within pure module)
b4d117cee636 policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents: 29266
diff changeset
23 # cffi-allow - allow pure Python implementation if cffi version is missing
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
24 # py - only load pure Python modules
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
25 #
32251
a04f5c651e52 policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents: 32210
diff changeset
26 # 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
27 # 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
28 # __modulepolicy__ generated by setup.py.
a04f5c651e52 policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents: 32210
diff changeset
29 policy = b'allow'
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
30 _packageprefs = {
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
31 # policy: (versioned package, pure package)
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
32 b'c': ('cext', None),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
33 b'allow': ('cext', 'pure'),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
34 b'cffi': ('cffi', None),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
35 b'cffi-allow': ('cffi', 'pure'),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
36 b'py': (None, 'pure'),
42451
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
37 # For now, rust policies impact importrust only
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
38 b'rust+c': ('cext', None),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
39 b'rust+c-allow': ('cext', 'pure'),
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
40 }
29490
b4d117cee636 policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents: 29266
diff changeset
41
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
42 try:
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
43 from . import __modulepolicy__
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
44
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
45 policy = __modulepolicy__.modulepolicy
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
46 except ImportError:
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
47 pass
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
48
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
49 # PyPy doesn't load C extensions.
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
50 #
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
51 # The canonical way to do this is to test platform.python_implementation().
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
52 # But we don't import platform and don't bloat for it here.
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
53 if '__pypy__' in sys.builtin_module_names:
32210
56148133ef36 policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31361
diff changeset
54 policy = b'cffi'
29266
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
55
b3a677c82a35 debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff changeset
56 # 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
57 if sys.version_info[0] >= 3:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
58 if 'HGMODULEPOLICY' in os.environ:
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
59 policy = os.environ['HGMODULEPOLICY'].encode('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
60 else:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
61 policy = os.environ.get('HGMODULEPOLICY', policy)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
62
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
63
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
64 def _importfrom(pkgname, modname):
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
65 # 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
66 fakelocals = {}
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
67 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
68 try:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
69 fakelocals[modname] = mod = getattr(pkg, modname)
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
70 except AttributeError:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
71 raise ImportError('cannot import name %s' % modname)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
72 # force import; fakelocals[modname] may be replaced with the real module
43103
c95b2f40db7c py3: stop normalizing 2nd argument of *attr() to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43089
diff changeset
73 getattr(mod, '__doc__', None)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
74 return fakelocals[modname]
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
75
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
76
32428
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
77 # keep in sync with "version" in C modules
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
78 _cextversions = {
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
79 ('cext', 'base85'): 1,
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
80 ('cext', 'bdiff'): 3,
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
81 ('cext', 'mpatch'): 1,
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
82 ('cext', 'osutil'): 4,
43554
b56de57c45ce index: add a `get_rev` method (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43552
diff changeset
83 ('cext', 'parsers'): 16,
32428
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
84 }
28b773aa3ff2 policy: define C module versions individually
Jun Wu <quark@fb.com>
parents: 32366
diff changeset
85
33760
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
86 # map import request to other package or module
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
87 _modredirects = {
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
88 ('cext', 'charencode'): ('cext', 'parsers'),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
89 ('cffi', 'base85'): ('pure', 'base85'),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
90 ('cffi', 'charencode'): ('pure', 'charencode'),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
91 ('cffi', 'parsers'): ('pure', 'parsers'),
33760
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
92 }
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
93
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
94
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
95 def _checkmod(pkgname, modname, mod):
32511
2e431fb98c6b policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents: 32428
diff changeset
96 expected = _cextversions.get((pkgname, modname))
43103
c95b2f40db7c py3: stop normalizing 2nd argument of *attr() to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43089
diff changeset
97 actual = getattr(mod, 'version', None)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
98 if actual != expected:
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
99 raise ImportError(
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
100 'cannot import module %s.%s '
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
101 '(expected version: %d, actual: %r)'
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
102 % (pkgname, modname, expected, actual)
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
103 )
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
104
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
105
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
106 def importmod(modname):
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
107 """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
108 try:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
109 verpkg, purepkg = _packageprefs[policy]
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
110 except KeyError:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
111 raise ImportError('invalid HGMODULEPOLICY %r' % policy)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
112 assert verpkg or purepkg
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
113 if verpkg:
33760
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
114 pn, mn = _modredirects.get((verpkg, modname), (verpkg, modname))
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
115 try:
33760
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
116 mod = _importfrom(pn, mn)
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
117 if pn == verpkg:
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
118 _checkmod(pn, mn, mod)
32366
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
119 return mod
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
120 except ImportError:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
121 if not purepkg:
8e0327dae3f4 policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents: 32251
diff changeset
122 raise
33760
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
123 pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname))
cd2aca0808f8 policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents: 32514
diff changeset
124 return _importfrom(pn, mn)
42451
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
125
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
126
42451
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
127 def _isrustpermissive():
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
128 """Assuming the policy is a Rust one, tell if it's permissive."""
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
129 return policy.endswith(b'-allow')
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
130
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42451
diff changeset
131
42451
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
132 def importrust(modname, member=None, default=None):
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
133 """Import Rust module according to policy and availability.
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
134
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
135 If policy isn't a Rust one, this returns `default`.
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
136
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
137 If either the module or its member is not available, this returns `default`
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
138 if policy is permissive and raises `ImportError` if not.
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
139 """
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
140 if not policy.startswith(b'rust'):
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
141 return default
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
142
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
143 try:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
144 mod = _importfrom('rustext', modname)
42451
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
145 except ImportError:
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
146 if _isrustpermissive():
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
147 return default
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
148 raise
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
149 if member is None:
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
150 return mod
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
151
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
152 try:
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
153 return getattr(mod, member)
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
154 except AttributeError:
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
155 if _isrustpermissive():
810f66b468cd rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents: 42343
diff changeset
156 return default
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43103
diff changeset
157 raise ImportError("Cannot import name %s" % member)