From c778a14ecccccb2407edc9347f0cdf40b97b3413 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 8 Nov 2018 11:59:51 -0500 Subject: [PATCH] In Windows backend, rely on ExceptionRaisedContext to catch all exceptions during import of pywin32-ctypes (or pywin32). Fixes #319. --- CHANGES.rst | 6 ++++++ keyring/backends/Windows.py | 35 +++++++++++++---------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3cfeaef9..0c34f870 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +16.0.2 +------ + +* #319: In Windows backend, trap all exceptions when + attempting to import pywin32. + 16.0.1 ------ diff --git a/keyring/backends/Windows.py b/keyring/backends/Windows.py index f56a9e40..ec88de5e 100644 --- a/keyring/backends/Windows.py +++ b/keyring/backends/Windows.py @@ -8,32 +8,23 @@ from ..credentials import SimpleCredential from ..errors import PasswordDeleteError, ExceptionRaisedContext -try: - # prefer pywin32-ctypes - from win32ctypes.pywin32 import pywintypes - from win32ctypes.pywin32 import win32cred - # force demand import to raise ImportError - win32cred.__name__ -except ImportError: - # fallback to pywin32 + +with ExceptionRaisedContext() as missing_deps: try: + # prefer pywin32-ctypes + from win32ctypes.pywin32 import pywintypes + from win32ctypes.pywin32 import win32cred + # force demand import to raise ImportError + win32cred.__name__ + except ImportError: + # fallback to pywin32 import pywintypes import win32cred - except ImportError: - pass - -__metaclass__ = type + # force demand import to raise ImportError + win32cred.__name__ -def has_pywin32(): - """ - Does this environment have pywin32? - Should return False even when Mercurial's Demand Import allowed import of - win32cred. - """ - with ExceptionRaisedContext() as exc: - win32cred.__name__ - return not bool(exc) +__metaclass__ = type class WinVaultKeyring(KeyringBackend): @@ -60,7 +51,7 @@ def priority(cls): """ If available, the preferred backend on Windows. """ - if not has_pywin32(): + if missing_deps: raise RuntimeError("Requires Windows and pywin32") return 5