37 b'rust+c-allow': (r'cext', r'pure'), |
37 b'rust+c-allow': (r'cext', r'pure'), |
38 } |
38 } |
39 |
39 |
40 try: |
40 try: |
41 from . import __modulepolicy__ |
41 from . import __modulepolicy__ |
|
42 |
42 policy = __modulepolicy__.modulepolicy |
43 policy = __modulepolicy__.modulepolicy |
43 except ImportError: |
44 except ImportError: |
44 pass |
45 pass |
45 |
46 |
46 # PyPy doesn't load C extensions. |
47 # PyPy doesn't load C extensions. |
55 if r'HGMODULEPOLICY' in os.environ: |
56 if r'HGMODULEPOLICY' in os.environ: |
56 policy = os.environ[r'HGMODULEPOLICY'].encode(r'utf-8') |
57 policy = os.environ[r'HGMODULEPOLICY'].encode(r'utf-8') |
57 else: |
58 else: |
58 policy = os.environ.get(r'HGMODULEPOLICY', policy) |
59 policy = os.environ.get(r'HGMODULEPOLICY', policy) |
59 |
60 |
|
61 |
60 def _importfrom(pkgname, modname): |
62 def _importfrom(pkgname, modname): |
61 # from .<pkgname> import <modname> (where . is looked through this module) |
63 # from .<pkgname> import <modname> (where . is looked through this module) |
62 fakelocals = {} |
64 fakelocals = {} |
63 pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1) |
65 pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1) |
64 try: |
66 try: |
66 except AttributeError: |
68 except AttributeError: |
67 raise ImportError(r'cannot import name %s' % modname) |
69 raise ImportError(r'cannot import name %s' % modname) |
68 # force import; fakelocals[modname] may be replaced with the real module |
70 # force import; fakelocals[modname] may be replaced with the real module |
69 getattr(mod, r'__doc__', None) |
71 getattr(mod, r'__doc__', None) |
70 return fakelocals[modname] |
72 return fakelocals[modname] |
|
73 |
71 |
74 |
72 # keep in sync with "version" in C modules |
75 # keep in sync with "version" in C modules |
73 _cextversions = { |
76 _cextversions = { |
74 (r'cext', r'base85'): 1, |
77 (r'cext', r'base85'): 1, |
75 (r'cext', r'bdiff'): 3, |
78 (r'cext', r'bdiff'): 3, |
84 (r'cffi', r'base85'): (r'pure', r'base85'), |
87 (r'cffi', r'base85'): (r'pure', r'base85'), |
85 (r'cffi', r'charencode'): (r'pure', r'charencode'), |
88 (r'cffi', r'charencode'): (r'pure', r'charencode'), |
86 (r'cffi', r'parsers'): (r'pure', r'parsers'), |
89 (r'cffi', r'parsers'): (r'pure', r'parsers'), |
87 } |
90 } |
88 |
91 |
|
92 |
89 def _checkmod(pkgname, modname, mod): |
93 def _checkmod(pkgname, modname, mod): |
90 expected = _cextversions.get((pkgname, modname)) |
94 expected = _cextversions.get((pkgname, modname)) |
91 actual = getattr(mod, r'version', None) |
95 actual = getattr(mod, r'version', None) |
92 if actual != expected: |
96 if actual != expected: |
93 raise ImportError(r'cannot import module %s.%s ' |
97 raise ImportError( |
94 r'(expected version: %d, actual: %r)' |
98 r'cannot import module %s.%s ' |
95 % (pkgname, modname, expected, actual)) |
99 r'(expected version: %d, actual: %r)' |
|
100 % (pkgname, modname, expected, actual) |
|
101 ) |
|
102 |
96 |
103 |
97 def importmod(modname): |
104 def importmod(modname): |
98 """Import module according to policy and check API version""" |
105 """Import module according to policy and check API version""" |
99 try: |
106 try: |
100 verpkg, purepkg = _packageprefs[policy] |
107 verpkg, purepkg = _packageprefs[policy] |
112 if not purepkg: |
119 if not purepkg: |
113 raise |
120 raise |
114 pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname)) |
121 pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname)) |
115 return _importfrom(pn, mn) |
122 return _importfrom(pn, mn) |
116 |
123 |
|
124 |
117 def _isrustpermissive(): |
125 def _isrustpermissive(): |
118 """Assuming the policy is a Rust one, tell if it's permissive.""" |
126 """Assuming the policy is a Rust one, tell if it's permissive.""" |
119 return policy.endswith(b'-allow') |
127 return policy.endswith(b'-allow') |
|
128 |
120 |
129 |
121 def importrust(modname, member=None, default=None): |
130 def importrust(modname, member=None, default=None): |
122 """Import Rust module according to policy and availability. |
131 """Import Rust module according to policy and availability. |
123 |
132 |
124 If policy isn't a Rust one, this returns `default`. |
133 If policy isn't a Rust one, this returns `default`. |