mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #4300 from tk0miya/fix_ImportError_for_autodoc
autodoc: Fix error handling for import_module()
This commit is contained in:
commit
a722c3f2bf
@ -405,23 +405,31 @@ class Documenter(object):
|
|||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.object = obj
|
self.object = obj
|
||||||
return True
|
return True
|
||||||
# this used to only catch SyntaxError, ImportError and AttributeError,
|
except (AttributeError, ImportError) as exc:
|
||||||
# but importing modules with side effects can raise all kinds of errors
|
|
||||||
except (Exception, SystemExit) as e:
|
|
||||||
if self.objpath:
|
if self.objpath:
|
||||||
errmsg = 'autodoc: failed to import %s %r from module %r' % \
|
errmsg = 'autodoc: failed to import %s %r from module %r' % \
|
||||||
(self.objtype, '.'.join(self.objpath), self.modname)
|
(self.objtype, '.'.join(self.objpath), self.modname)
|
||||||
else:
|
else:
|
||||||
errmsg = 'autodoc: failed to import %s %r' % \
|
errmsg = 'autodoc: failed to import %s %r' % \
|
||||||
(self.objtype, self.fullname)
|
(self.objtype, self.fullname)
|
||||||
if isinstance(e, SystemExit):
|
|
||||||
errmsg += ('; the module executes module level statement ' +
|
if isinstance(exc, ImportError):
|
||||||
'and it might call sys.exit().')
|
# import_module() raises ImportError having real exception obj and
|
||||||
elif isinstance(e, ImportError):
|
# traceback
|
||||||
errmsg += '; the following exception was raised:\n%s' % e.args[0]
|
real_exc, traceback_msg = exc.args
|
||||||
|
if isinstance(real_exc, SystemExit):
|
||||||
|
errmsg += ('; the module executes module level statement ' +
|
||||||
|
'and it might call sys.exit().')
|
||||||
|
elif isinstance(real_exc, ImportError):
|
||||||
|
errmsg += ('; the following exception was raised:\n%s' %
|
||||||
|
real_exc.args[0])
|
||||||
|
else:
|
||||||
|
errmsg += ('; the following exception was raised:\n%s' %
|
||||||
|
traceback_msg)
|
||||||
else:
|
else:
|
||||||
errmsg += '; the following exception was raised:\n%s' % \
|
errmsg += ('; the following exception was raised:\n%s' %
|
||||||
traceback.format_exc()
|
traceback.format_exc())
|
||||||
|
|
||||||
if PY2:
|
if PY2:
|
||||||
errmsg = errmsg.decode('utf-8') # type: ignore
|
errmsg = errmsg.decode('utf-8') # type: ignore
|
||||||
logger.debug(errmsg)
|
logger.debug(errmsg)
|
||||||
|
@ -140,7 +140,7 @@ def import_module(modname, warningiserror=False):
|
|||||||
with logging.skip_warningiserror(not warningiserror):
|
with logging.skip_warningiserror(not warningiserror):
|
||||||
__import__(modname)
|
__import__(modname)
|
||||||
return sys.modules[modname]
|
return sys.modules[modname]
|
||||||
except BaseException:
|
except BaseException as exc:
|
||||||
# Importing modules may cause any side effects, including
|
# Importing modules may cause any side effects, including
|
||||||
# SystemExit, so we need to catch all errors.
|
# SystemExit, so we need to catch all errors.
|
||||||
raise ImportError(traceback.format_exc())
|
raise ImportError(exc, traceback.format_exc())
|
||||||
|
Loading…
Reference in New Issue
Block a user