##// END OF EJS Templates
rust: module policy with importrust...
Georges Racinet -
r42651:810f66b4 default
parent child Browse files
Show More
@@ -1278,16 +1278,28 b' def debuginstall(ui, **opts):'
1278 fm.write('hgmodules', _("checking installed modules (%s)...\n"),
1278 fm.write('hgmodules', _("checking installed modules (%s)...\n"),
1279 os.path.dirname(pycompat.fsencode(__file__)))
1279 os.path.dirname(pycompat.fsencode(__file__)))
1280
1280
1281 if policy.policy in ('c', 'allow'):
1281 rustandc = policy.policy in ('rust+c', 'rust+c-allow')
1282 rustext = rustandc # for now, that's the only case
1283 cext = policy.policy in ('c', 'allow') or rustandc
1284 nopure = cext or rustext
1285 if nopure:
1282 err = None
1286 err = None
1283 try:
1287 try:
1284 from .cext import (
1288 if cext:
1285 base85,
1289 from .cext import (
1286 bdiff,
1290 base85,
1287 mpatch,
1291 bdiff,
1288 osutil,
1292 mpatch,
1289 )
1293 osutil,
1290 dir(bdiff), dir(mpatch), dir(base85), dir(osutil) # quiet pyflakes
1294 )
1295 # quiet pyflakes
1296 dir(bdiff), dir(mpatch), dir(base85), dir(osutil)
1297 if rustext:
1298 from .rustext import (
1299 ancestor,
1300 dirstate,
1301 )
1302 dir(ancestor), dir(dirstate) # quiet pyflakes
1291 except Exception as inst:
1303 except Exception as inst:
1292 err = stringutil.forcebytestr(inst)
1304 err = stringutil.forcebytestr(inst)
1293 problems += 1
1305 problems += 1
@@ -13,6 +13,9 b' import sys'
13 # Rules for how modules can be loaded. Values are:
13 # Rules for how modules can be loaded. Values are:
14 #
14 #
15 # c - require C extensions
15 # c - require C extensions
16 # rust+c - require Rust and C extensions
17 # rust+c-allow - allow Rust and C extensions with fallback to pure Python
18 # for each
16 # allow - allow pure Python implementation when C loading fails
19 # allow - allow pure Python implementation when C loading fails
17 # cffi - required cffi versions (implemented within pure module)
20 # cffi - required cffi versions (implemented within pure module)
18 # cffi-allow - allow pure Python implementation if cffi version is missing
21 # cffi-allow - allow pure Python implementation if cffi version is missing
@@ -29,6 +32,9 b" policy = b'allow'"
29 b'cffi': (r'cffi', None),
32 b'cffi': (r'cffi', None),
30 b'cffi-allow': (r'cffi', r'pure'),
33 b'cffi-allow': (r'cffi', r'pure'),
31 b'py': (None, r'pure'),
34 b'py': (None, r'pure'),
35 # For now, rust policies impact importrust only
36 b'rust+c': (r'cext', None),
37 b'rust+c-allow': (r'cext', r'pure'),
32 }
38 }
33
39
34 try:
40 try:
@@ -107,3 +113,34 b' def importmod(modname):'
107 raise
113 raise
108 pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname))
114 pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname))
109 return _importfrom(pn, mn)
115 return _importfrom(pn, mn)
116
117 def _isrustpermissive():
118 """Assuming the policy is a Rust one, tell if it's permissive."""
119 return policy.endswith(b'-allow')
120
121 def importrust(modname, member=None, default=None):
122 """Import Rust module according to policy and availability.
123
124 If policy isn't a Rust one, this returns `default`.
125
126 If either the module or its member is not available, this returns `default`
127 if policy is permissive and raises `ImportError` if not.
128 """
129 if not policy.startswith(b'rust'):
130 return default
131
132 try:
133 mod = _importfrom(r'rustext', modname)
134 except ImportError:
135 if _isrustpermissive():
136 return default
137 raise
138 if member is None:
139 return mod
140
141 try:
142 return getattr(mod, member)
143 except AttributeError:
144 if _isrustpermissive():
145 return default
146 raise ImportError(r"Cannot import name %s" % member)
@@ -556,10 +556,17 b' class hgbuildpy(build_py):'
556 if self.distribution.pure:
556 if self.distribution.pure:
557 modulepolicy = 'py'
557 modulepolicy = 'py'
558 elif self.build_lib == '.':
558 elif self.build_lib == '.':
559 # in-place build should run without rebuilding C extensions
559 # in-place build should run without rebuilding C
560 modulepolicy = 'allow'
560 # and Rust extensions
561 if hgrustext == 'cpython':
562 modulepolicy = 'rust+c-allow'
563 else:
564 modulepolicy = 'allow'
561 else:
565 else:
562 modulepolicy = 'c'
566 if hgrustext == 'cpython':
567 modulepolicy = 'rust+c'
568 else:
569 modulepolicy = 'c'
563
570
564 content = b''.join([
571 content = b''.join([
565 b'# this file is autogenerated by setup.py\n',
572 b'# this file is autogenerated by setup.py\n',
General Comments 0
You need to be logged in to leave comments. Login now