Allow providing a custom reason for `importorskip`

This commit is contained in:
Pedro Algarvio 2019-01-04 17:56:13 +00:00
parent 56aecfc081
commit 8b48621687
No known key found for this signature in database
GPG Key ID: BB36BF6584A298FF
1 changed files with 4 additions and 2 deletions

View File

@ -137,7 +137,7 @@ def xfail(reason=""):
xfail.Exception = XFailed
def importorskip(modname, minversion=None):
def importorskip(modname, minversion=None, reason=None):
""" return imported module if it has at least "minversion" as its
__version__ attribute. If no minversion is specified the a skip
is only triggered if the module can not be imported.
@ -159,7 +159,9 @@ def importorskip(modname, minversion=None):
# Do not raise chained exception here(#1485)
should_skip = True
if should_skip:
raise Skipped("could not import %r" % (modname,), allow_module_level=True)
if reason is None:
reason = "could not import %r" % (modname,)
raise Skipped(reason, allow_module_level=True)
mod = sys.modules[modname]
if minversion is None:
return mod