Allow providing a custom reason for `importorskip`
This commit is contained in:
parent
56aecfc081
commit
8b48621687
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue