Skip to content

Commit 20f68a1

Browse files
committed
improve skips
1 parent f2bbd72 commit 20f68a1

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,20 +281,23 @@ def import_module(self, implementation, *, allow_skip=False):
281281
282282
On error, return None if *allow_skip* is false, or raise SkipNoHash.
283283
"""
284-
module = self[implementation].import_module()
284+
target = self[implementation]
285+
module = target.import_module()
285286
if allow_skip and module is None:
286-
raise SkipNoHash(self.canonical_name, implementation)
287+
reason = f"cannot import module {target.module_name}"
288+
raise SkipNoHash(self.canonical_name, implementation, reason)
287289
return module
288290

289291
def import_object_type(self, implementation, *, allow_skip=False):
290292
"""Get the runtime hash object type.
291293
292294
On error, return None if *allow_skip* is false, or raise SkipNoHash.
293295
"""
294-
member = self[implementation].import_member()
296+
target = self[implementation]
297+
member = target.import_member()
295298
if allow_skip and member is None:
296-
raise SkipNoHash(self.name, implementation, interface="class")
297-
assert isinstance(member, type | None), member
299+
reason = f"cannot import class {target.fullname}"
300+
raise SkipNoHash(self.canonical_name, implementation, reason)
298301
return member
299302

300303

@@ -604,13 +607,19 @@ def requires_builtin_hmac():
604607
class SkipNoHash(unittest.SkipTest):
605608
"""A SkipTest exception raised when a hash is not available."""
606609

607-
def __init__(self, digestname, implementation=None, interface=None):
610+
def __init__(self, digestname, implementation=None, reason=None):
608611
parts = ["missing", implementation, f"hash algorithm {digestname!r}"]
609-
if interface is not None:
610-
parts.append(f"for {interface}")
612+
if reason is not None:
613+
parts.insert(0, f"{reason}: ")
611614
super().__init__(" ".join(filter(None, parts)))
612615

613616

617+
class SkipNoHashInCall(SkipNoHash):
618+
619+
def __init__(self, func, digestname, implementation=None):
620+
super().__init__(digestname, implementation, f"cannot use {func}")
621+
622+
614623
def _hashlib_new(digestname, openssl, /, **kwargs):
615624
"""Check availability of [hashlib|_hashlib].new(digestname, **kwargs).
616625
@@ -630,8 +639,7 @@ def _hashlib_new(digestname, openssl, /, **kwargs):
630639
try:
631640
module.new(digestname, **kwargs)
632641
except ValueError as exc:
633-
interface = f"{module.__name__}.new"
634-
raise SkipNoHash(digestname, interface=interface) from exc
642+
raise SkipNoHashInCall(f"{module.__name__}.new", digestname) from exc
635643
return functools.partial(module.new, digestname)
636644

637645

@@ -676,7 +684,7 @@ def _openssl_new(digestname, /, **kwargs):
676684
try:
677685
_hashlib.new(digestname, **kwargs)
678686
except ValueError as exc:
679-
raise SkipNoHash(digestname, interface="_hashlib.new") from exc
687+
raise SkipNoHashInCall("_hashlib.new", digestname) from exc
680688
return functools.partial(_hashlib.new, digestname)
681689

682690

0 commit comments

Comments
 (0)