Skip to content

Commit

Permalink
Replace ExceptionRaisedContext with ExceptionTrap.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 2, 2024
1 parent 1899770 commit a85a7cb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
9 changes: 6 additions & 3 deletions keyring/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import typing

from jaraco.context import ExceptionTrap
from jaraco.functools import once

from . import credentials, errors, util
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions keyring/backends/SecretService.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions keyring/backends/Windows.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions keyring/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import warnings


class KeyringError(Exception):
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions newsfragments/+9aa71ff2.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace ExceptionRaisedContext with ExceptionTrap.

0 comments on commit a85a7cb

Please sign in to comment.