@@ -1319,18 +1319,22 @@ class ImportPolicy(object):
13191319 :param blocks:
13201320 Prefixes always denied by the responder, only local versions can be
13211321 used.
1322+
1323+ :param unsuitables:
1324+ Prefixes unsuitable to be served, e.g. because they're Python stdlib,
1325+ platform specific. An optimisation to reduce futile round trips.
13221326 """
1323- def __init__ (self , overrides = (), blocks = ()):
1327+ def __init__ (self , overrides = (), blocks = (), unsuitables = () ):
13241328 self .overrides = set (overrides )
13251329 self .blocks = set (blocks )
1326- self ._always = set (Importer . ALWAYS_BLACKLIST )
1330+ self .unsuitables = set (unsuitables )
13271331
13281332 def denied (self , fullname ):
13291333 fullnames = frozenset (module_lineage (fullname ))
13301334 if self .overrides and not self .overrides .intersection (fullnames ):
13311335 return ModuleDeniedByOverridesError
1332- if self .blocks . intersection ( fullnames ) : return ModuleDeniedByBlocksError
1333- if self ._always . intersection ( fullnames ) : return ModuleUnsuitableError
1336+ if self .blocks & fullnames : return ModuleDeniedByBlocksError
1337+ if self .unsuitables & fullnames : return ModuleUnsuitableError
13341338 return False
13351339
13361340 def denied_raise (self , fullname ):
@@ -1381,29 +1385,6 @@ class Importer(object):
13811385 'utils' ,
13821386 ]
13831387
1384- ALWAYS_BLACKLIST = [
1385- # 2.x generates needless imports for 'builtins', while 3.x does the
1386- # same for '__builtin__'. The correct one is built-in, the other always
1387- # a negative round-trip.
1388- 'builtins' ,
1389- '__builtin__' ,
1390-
1391- # On some Python releases (e.g. 3.8, 3.9) the subprocess module tries
1392- # to import of this Windows-only builtin module.
1393- 'msvcrt' ,
1394-
1395- # Python 2.x module that was renamed to _thread in 3.x.
1396- # This entry avoids a roundtrip on 2.x -> 3.x.
1397- 'thread' ,
1398-
1399- # org.python.core imported by copy, pickle, xml.sax; breaks Jython, but
1400- # very unlikely to trigger a bug report.
1401- 'org' ,
1402- ]
1403-
1404- if sys .version_info >= (3 , 0 ):
1405- ALWAYS_BLACKLIST += ['cStringIO' ]
1406-
14071388 def __init__ (self , router , context , core_src , policy ):
14081389 self ._log = logging .getLogger ('mitogen.importer' )
14091390 self ._context = context
@@ -4202,15 +4183,11 @@ def _setup_importer(self):
42024183 else :
42034184 core_src = None
42044185
4205- policy = ImportPolicy (
4206- self .config ['import_overrides' ],
4207- self .config ['import_blocks' ],
4208- )
42094186 importer = Importer (
42104187 self .router ,
42114188 self .parent ,
42124189 core_src ,
4213- policy ,
4190+ ImportPolicy ( * self . config [ ' policy' ]) ,
42144191 )
42154192
42164193 self .importer = importer
0 commit comments