Mercurial > public > mercurial-scm > hg
annotate mercurial/policy.py @ 42451:810f66b468cd
rust: module policy with importrust
We introduce two rust+c module policies and a new
`policy.importrust()` that makes use of them.
This simple approach provides runtime switching of
implementations, which is crucial for the performance
measurements such as those Octobus does with ASV.
It can also be useful for bug analysis.
It also has the advantage of making conditionals in
Rust callers more uniform, in particular
abstracting over specifics like `demandimport`
At this point, the build stays unchanged, with the rust-cpython based
`rustext` module being built if HGWITHRUSTEXT=cpython.
More transparency for the callers, i.e., just using
`policy.importmod` would be a much longer term and riskier
effort for the following reasons:
1. It would require to define common module boundaries
for the three or four cases (pure, c, rust+ext, cffi) and that
is premature with the Rust extension currently under heavy
development in areas that are outside the scope of the C extensions.
2. It would imply internal API changes that are not currently wished,
as the case of ancestors demonstrates.
3. The lack of data or property-like attributes (tp_member
and tp_getset) in current `rust-cpython` makes it impossible to
achieve direct transparent replacement of pure Python classes by
Rust extension code, meaning that the caller sometimes has to be able
to make adjustments or provide additional wrapping.
author | Georges Racinet <georges.racinet@octobus.net> |
---|---|
date | Wed, 29 May 2019 13:27:56 +0200 |
parents | d8e55c0c642c |
children | 57875cf423c9 |
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 |
42451
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
16 # rust+c - require Rust and C extensions |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
17 # 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
|
18 # for each |
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
19 # 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
|
20 # cffi - required cffi versions (implemented within pure module) |
b4d117cee636
policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents:
29266
diff
changeset
|
21 # cffi-allow - allow pure Python implementation if cffi version is missing |
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
22 # py - only load pure Python modules |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
23 # |
32251
a04f5c651e52
policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents:
32210
diff
changeset
|
24 # 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
|
25 # 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
|
26 # __modulepolicy__ generated by setup.py. |
a04f5c651e52
policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents:
32210
diff
changeset
|
27 policy = b'allow' |
32366
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
28 _packageprefs = { |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
29 # policy: (versioned package, pure package) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
30 b'c': (r'cext', None), |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
31 b'allow': (r'cext', r'pure'), |
32512
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32511
diff
changeset
|
32 b'cffi': (r'cffi', None), |
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32511
diff
changeset
|
33 b'cffi-allow': (r'cffi', r'pure'), |
32366
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
34 b'py': (None, r'pure'), |
42451
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
35 # For now, rust policies impact importrust only |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
36 b'rust+c': (r'cext', None), |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
37 b'rust+c-allow': (r'cext', r'pure'), |
32366
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
38 } |
29490
b4d117cee636
policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents:
29266
diff
changeset
|
39 |
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
40 try: |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
41 from . import __modulepolicy__ |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
42 policy = __modulepolicy__.modulepolicy |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
43 except ImportError: |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
44 pass |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
45 |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
46 # PyPy doesn't load C extensions. |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
47 # |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
48 # The canonical way to do this is to test platform.python_implementation(). |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
49 # 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
|
50 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
|
51 policy = b'cffi' |
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
52 |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
53 # 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
|
54 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
|
55 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
|
56 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
|
57 else: |
32210
56148133ef36
policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31361
diff
changeset
|
58 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
|
59 |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
60 def _importfrom(pkgname, modname): |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
61 # 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
|
62 fakelocals = {} |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
63 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
|
64 try: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
65 fakelocals[modname] = mod = getattr(pkg, modname) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
66 except AttributeError: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
67 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
|
68 # 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
|
69 getattr(mod, r'__doc__', None) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
70 return fakelocals[modname] |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
71 |
32428
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32366
diff
changeset
|
72 # keep in sync with "version" in C modules |
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32366
diff
changeset
|
73 _cextversions = { |
32511
2e431fb98c6b
policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents:
32428
diff
changeset
|
74 (r'cext', r'base85'): 1, |
36675 | 75 (r'cext', r'bdiff'): 3, |
32511
2e431fb98c6b
policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents:
32428
diff
changeset
|
76 (r'cext', r'mpatch'): 1, |
36780
f3c314020beb
osutil: implement minimal __getitem__ compatibility on our custom listdir type
Augie Fackler <augie@google.com>
parents:
36675
diff
changeset
|
77 (r'cext', r'osutil'): 4, |
42343
d8e55c0c642c
util: make util.dirs() and util.finddirs() include root directory (API)
Martin von Zweigbergk <martinvonz@google.com>
parents:
40708
diff
changeset
|
78 (r'cext', r'parsers'): 13, |
32428
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32366
diff
changeset
|
79 } |
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32366
diff
changeset
|
80 |
33760
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
81 # map import request to other package or module |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
82 _modredirects = { |
33761
f5fc54e7e467
encoding: drop circular import by proxying through '<policy>.charencode'
Yuya Nishihara <yuya@tcha.org>
parents:
33760
diff
changeset
|
83 (r'cext', r'charencode'): (r'cext', r'parsers'), |
33760
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
84 (r'cffi', r'base85'): (r'pure', r'base85'), |
33761
f5fc54e7e467
encoding: drop circular import by proxying through '<policy>.charencode'
Yuya Nishihara <yuya@tcha.org>
parents:
33760
diff
changeset
|
85 (r'cffi', r'charencode'): (r'pure', r'charencode'), |
33760
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
86 (r'cffi', r'parsers'): (r'pure', r'parsers'), |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
87 } |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
88 |
32366
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
89 def _checkmod(pkgname, modname, mod): |
32511
2e431fb98c6b
policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents:
32428
diff
changeset
|
90 expected = _cextversions.get((pkgname, modname)) |
32366
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
91 actual = getattr(mod, r'version', None) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
92 if actual != expected: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
93 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
|
94 r'(expected version: %d, actual: %r)' |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
95 % (pkgname, modname, expected, actual)) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
96 |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
97 def importmod(modname): |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
98 """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
|
99 try: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
100 verpkg, purepkg = _packageprefs[policy] |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
101 except KeyError: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
102 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
|
103 assert verpkg or purepkg |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
104 if verpkg: |
33760
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
105 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
|
106 try: |
33760
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
107 mod = _importfrom(pn, mn) |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
108 if pn == verpkg: |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
109 _checkmod(pn, mn, mod) |
32366
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
110 return mod |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
111 except ImportError: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
112 if not purepkg: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32251
diff
changeset
|
113 raise |
33760
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
114 pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname)) |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32514
diff
changeset
|
115 return _importfrom(pn, mn) |
42451
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
116 |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
117 def _isrustpermissive(): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
118 """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
|
119 return policy.endswith(b'-allow') |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
120 |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
121 def importrust(modname, member=None, default=None): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
122 """Import Rust module according to policy and availability. |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
123 |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
124 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
|
125 |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
126 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
|
127 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
|
128 """ |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
129 if not policy.startswith(b'rust'): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
130 return default |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
131 |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
132 try: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
133 mod = _importfrom(r'rustext', modname) |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
134 except ImportError: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
135 if _isrustpermissive(): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
136 return default |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
137 raise |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
138 if member is None: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
139 return mod |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
140 |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
141 try: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
142 return getattr(mod, member) |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
143 except AttributeError: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
144 if _isrustpermissive(): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
145 return default |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42343
diff
changeset
|
146 raise ImportError(r"Cannot import name %s" % member) |