author | Raphaël Gomès <rgomes@octobus.net> |
Mon, 06 May 2024 12:31:29 +0200 | |
branch | stable |
changeset 51489 | dcb00d5c397a |
parent 50952 | 18c8c18993f0 |
child 51688 | f3bef5eb12f9 |
child 51748 | 39e2b2d062c1 |
permissions | -rw-r--r-- |
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 |
|
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
9 |
import os |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
10 |
import sys |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
11 |
|
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
12 |
# Rules for how modules can be loaded. Values are: |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
13 |
# |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
14 |
# c - require C extensions |
42467
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
15 |
# rust+c - require Rust and C extensions |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
16 |
# 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:
42365
diff
changeset
|
17 |
# for each |
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
18 |
# 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
|
19 |
# cffi - required cffi versions (implemented within pure module) |
b4d117cee636
policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents:
29266
diff
changeset
|
20 |
# cffi-allow - allow pure Python implementation if cffi version is missing |
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
21 |
# py - only load pure Python modules |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
22 |
# |
32291
a04f5c651e52
policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents:
32250
diff
changeset
|
23 |
# 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:
32250
diff
changeset
|
24 |
# 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:
32250
diff
changeset
|
25 |
# __modulepolicy__ generated by setup.py. |
a04f5c651e52
policy: relax the default for in-place build
Yuya Nishihara <yuya@tcha.org>
parents:
32250
diff
changeset
|
26 |
policy = b'allow' |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
27 |
_packageprefs = { |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
28 |
# policy: (versioned package, pure package) |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
29 |
b'c': ('cext', None), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
30 |
b'allow': ('cext', 'pure'), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
31 |
b'cffi': ('cffi', None), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
32 |
b'cffi-allow': ('cffi', 'pure'), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
33 |
b'py': (None, 'pure'), |
42467
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
34 |
# For now, rust policies impact importrust only |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
35 |
b'rust+c': ('cext', None), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
36 |
b'rust+c-allow': ('cext', 'pure'), |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
37 |
} |
29490
b4d117cee636
policy: add cffi policy for PyPy
Maciej Fijalkowski <fijall@gmail.com>
parents:
29266
diff
changeset
|
38 |
|
29266
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
39 |
try: |
b3a677c82a35
debuginstall: expose modulepolicy
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
40 |
from . import __modulepolicy__ |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
41 |
|
29266
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. |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
50 |
if '__pypy__' in sys.builtin_module_names: |
32250
56148133ef36
policy: mark all string literals as sysstr or bytes
Yuya Nishihara <yuya@tcha.org>
parents:
31370
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. |
49029
f98da1349212
policy: remove Python 2.7 compatibility code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48966
diff
changeset
|
54 |
if 'HGMODULEPOLICY' in os.environ: |
f98da1349212
policy: remove Python 2.7 compatibility code
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48966
diff
changeset
|
55 |
policy = os.environ['HGMODULEPOLICY'].encode('utf-8') |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
56 |
|
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
57 |
|
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
58 |
def _importfrom(pkgname, modname): |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
59 |
# 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:
32291
diff
changeset
|
60 |
fakelocals = {} |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
61 |
pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
62 |
try: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
63 |
fakelocals[modname] = mod = getattr(pkg, modname) |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
64 |
except AttributeError: |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
65 |
raise ImportError('cannot import name %s' % modname) |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
66 |
# 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
|
67 |
getattr(mod, '__doc__', None) |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
68 |
return fakelocals[modname] |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
69 |
|
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
70 |
|
32466
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32405
diff
changeset
|
71 |
# keep in sync with "version" in C modules |
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32405
diff
changeset
|
72 |
_cextversions = { |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
73 |
('cext', 'base85'): 1, |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
74 |
('cext', 'bdiff'): 3, |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
75 |
('cext', 'mpatch'): 1, |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
76 |
('cext', 'osutil'): 4, |
49915
3eac92509484
dirstate-entry: add `modified` property
Raphaël Gomès <rgomes@octobus.net>
parents:
49029
diff
changeset
|
77 |
('cext', 'parsers'): 21, |
32466
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32405
diff
changeset
|
78 |
} |
28b773aa3ff2
policy: define C module versions individually
Jun Wu <quark@fb.com>
parents:
32405
diff
changeset
|
79 |
|
33781
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
80 |
# map import request to other package or module |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
81 |
_modredirects = { |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
82 |
('cext', 'charencode'): ('cext', 'parsers'), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
83 |
('cffi', 'base85'): ('pure', 'base85'), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
84 |
('cffi', 'charencode'): ('pure', 'charencode'), |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
85 |
('cffi', 'parsers'): ('pure', 'parsers'), |
33781
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
86 |
} |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
87 |
|
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
88 |
|
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
89 |
def _checkmod(pkgname, modname, mod): |
32544
2e431fb98c6b
policy: extend API version checks for cffi
Yuya Nishihara <yuya@tcha.org>
parents:
32466
diff
changeset
|
90 |
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
|
91 |
actual = getattr(mod, 'version', None) |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
92 |
if actual != expected: |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
93 |
raise ImportError( |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
94 |
'cannot import module %s.%s ' |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
95 |
'(expected version: %d, actual: %r)' |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
96 |
% (pkgname, modname, expected, actual) |
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
97 |
) |
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
98 |
|
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
99 |
|
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
100 |
def importmod(modname): |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
101 |
"""Import module according to policy and check API version""" |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
102 |
try: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
103 |
verpkg, purepkg = _packageprefs[policy] |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
104 |
except KeyError: |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
105 |
raise ImportError('invalid HGMODULEPOLICY %r' % policy) |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
106 |
assert verpkg or purepkg |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
107 |
if verpkg: |
33781
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
108 |
pn, mn = _modredirects.get((verpkg, modname), (verpkg, modname)) |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
109 |
try: |
33781
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
110 |
mod = _importfrom(pn, mn) |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
111 |
if pn == verpkg: |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
112 |
_checkmod(pn, mn, mod) |
32405
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
113 |
return mod |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
114 |
except ImportError: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
115 |
if not purepkg: |
8e0327dae3f4
policy: add helper to import cext/pure module
Yuya Nishihara <yuya@tcha.org>
parents:
32291
diff
changeset
|
116 |
raise |
33781
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
117 |
pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname)) |
cd2aca0808f8
policy: reroute proxy modules internally
Yuya Nishihara <yuya@tcha.org>
parents:
32547
diff
changeset
|
118 |
return _importfrom(pn, mn) |
42467
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
119 |
|
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
120 |
|
42467
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
121 |
def _isrustpermissive(): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
122 |
"""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:
42365
diff
changeset
|
123 |
return policy.endswith(b'-allow') |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
124 |
|
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42467
diff
changeset
|
125 |
|
42467
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
126 |
def importrust(modname, member=None, default=None): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
127 |
"""Import Rust module according to policy and availability. |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
128 |
|
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
129 |
If policy isn't a Rust one, this returns `default`. |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
130 |
|
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
131 |
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:
42365
diff
changeset
|
132 |
if policy is permissive and raises `ImportError` if not. |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
133 |
""" |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
134 |
if not policy.startswith(b'rust'): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
135 |
return default |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
136 |
|
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
137 |
try: |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43551
diff
changeset
|
138 |
mod = _importfrom('rustext', modname) |
42467
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
139 |
except ImportError: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
140 |
if _isrustpermissive(): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
141 |
return default |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
142 |
raise |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
143 |
if member is None: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
144 |
return mod |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
145 |
|
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
146 |
try: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
147 |
return getattr(mod, member) |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
148 |
except AttributeError: |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
149 |
if _isrustpermissive(): |
810f66b468cd
rust: module policy with importrust
Georges Racinet <georges.racinet@octobus.net>
parents:
42365
diff
changeset
|
150 |
return default |
43551
313e3a279828
cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents:
43103
diff
changeset
|
151 |
raise ImportError("Cannot import name %s" % member) |