Skip to content
Browse files

core: improve importer exception messages.

  • Loading branch information
dw committed Dec 15, 2018
1 parent 1eb08fb commit 824c7931a93f86e5aacce04937ad9512fd4e35b4
Showing with 21 additions and 5 deletions.
  1. +21 −5 mitogen/
@@ -1032,9 +1032,25 @@ def find_module(self, fullname, path=None):
del _tls.running

blacklisted_msg = (
'%r is present in the Mitogen importer blacklist, therefore this '
'context will not attempt to request it from the master, as the '
'request will always be refused.'
pkg_resources_msg = (
'pkg_resources is prohibited from importing __main__, as it causes '
'problems in applications whose main module is not designed to be '
're-imported by children.'
absent_msg = (
'The Mitogen master process was unable to serve %r. It may be a '
'native Python extension, or it may be missing entirely. Check the '
'importer debug logs on the master for more information.'

def _refuse_imports(self, fullname):
if is_blacklisted_import(self, fullname):
raise ImportError('Refused: ' + fullname)
raise ImportError(self.blacklisted_msg % (fullname,))

f = sys._getframe(2)
requestee = f.f_globals['__name__']
@@ -1046,7 +1062,7 @@ def _refuse_imports(self, fullname):
# breaks any app that is not expecting its __main__ to suddenly be
# sucked over a network and injected into a remote process, like
# py.test.
raise ImportError('Refused')
raise ImportError(self.pkg_resources_msg)

if fullname == 'pbr':
# It claims to use pkg_resources to read version information, which
@@ -1106,7 +1122,7 @@ def load_module(self, fullname):

ret = self._cache[fullname]
if ret[2] is None:
raise ImportError('Master does not have %r' % (fullname,))
raise ImportError(self.absent_msg % (fullname,))

pkg_present = ret[1]
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
@@ -1139,14 +1155,14 @@ def get_filename(self, fullname):
# reveals the module can't be loaded, and so load_module()
# throws ImportError, on Python 3.x it is still possible for
# the loader to be called to fetch metadata.
raise ImportError('master cannot serve %r' % (fullname,))
raise ImportError(self.absent_msg % (fullname,))
return u'master:' + self._cache[fullname][2]

def get_source(self, fullname):
if fullname in self._cache:
compressed = self._cache[fullname][3]
if compressed is None:
raise ImportError('master cannot serve %r' % (fullname,))
raise ImportError(self.absent_msg % (fullname,))

source = zlib.decompress(self._cache[fullname][3])
if PY3:

0 comments on commit 824c793

Please sign in to comment.