diff --git a/keyring/backend.py b/keyring/backend.py index 7680e4b3..12c8b7f5 100644 --- a/keyring/backend.py +++ b/keyring/backend.py @@ -11,6 +11,7 @@ import os import typing +from jaraco.context import ExceptionTrap from jaraco.functools import once from . import credentials, errors, util @@ -65,11 +66,13 @@ def priority(self) -> float: """ raise NotImplementedError + # Python 3.8 compatibility + passes = ExceptionTrap().passes + @properties.classproperty + @passes def viable(cls): - with errors.ExceptionRaisedContext() as exc: - cls.priority # noqa: B018 - return not exc + cls.priority # noqa: B018 @classmethod def get_viable_backends( diff --git a/keyring/backends/SecretService.py b/keyring/backends/SecretService.py index 9b180bb5..41aa7884 100644 --- a/keyring/backends/SecretService.py +++ b/keyring/backends/SecretService.py @@ -1,12 +1,13 @@ import logging from contextlib import closing +from jaraco.context import ExceptionTrap + from .. import backend from ..backend import KeyringBackend from ..compat import properties from ..credentials import SimpleCredential from ..errors import ( - ExceptionRaisedContext, InitError, KeyringLocked, PasswordDeleteError, @@ -31,7 +32,7 @@ class Keyring(backend.SchemeSelectable, KeyringBackend): @properties.classproperty def priority(cls) -> float: - with ExceptionRaisedContext() as exc: + with ExceptionTrap() as exc: secretstorage.__name__ # noqa: B018 if exc: raise RuntimeError("SecretStorage required") diff --git a/keyring/backends/Windows.py b/keyring/backends/Windows.py index 035a9e58..f9652487 100644 --- a/keyring/backends/Windows.py +++ b/keyring/backends/Windows.py @@ -1,11 +1,13 @@ import logging +from jaraco.context import ExceptionTrap + from ..backend import KeyringBackend from ..compat import properties from ..credentials import SimpleCredential -from ..errors import ExceptionRaisedContext, PasswordDeleteError +from ..errors import PasswordDeleteError -with ExceptionRaisedContext() as missing_deps: +with ExceptionTrap() as missing_deps: try: # prefer pywin32-ctypes from win32ctypes.pywin32 import pywintypes, win32cred diff --git a/keyring/errors.py b/keyring/errors.py index a793c0d3..ed97cf94 100644 --- a/keyring/errors.py +++ b/keyring/errors.py @@ -1,4 +1,5 @@ import sys +import warnings class KeyringError(Exception): @@ -32,6 +33,11 @@ class ExceptionRaisedContext: """ def __init__(self, ExpectedException=Exception): + warnings.warn( + "ExceptionRaisedContext is deprecated; use `jaraco.context.ExceptionTrap`", + DeprecationWarning, + stacklevel=2, + ) self.ExpectedException = ExpectedException self.exc_info = None diff --git a/newsfragments/+9aa71ff2.feature.rst b/newsfragments/+9aa71ff2.feature.rst new file mode 100644 index 00000000..7e30e999 --- /dev/null +++ b/newsfragments/+9aa71ff2.feature.rst @@ -0,0 +1 @@ +Replace ExceptionRaisedContext with ExceptionTrap. \ No newline at end of file