Skip to content

Commit

Permalink
In Windows backend, rely on ExceptionRaisedContext to catch all excep…
Browse files Browse the repository at this point in the history
…tions during import of pywin32-ctypes (or pywin32). Fixes #319.
  • Loading branch information
jaraco committed Nov 8, 2018
1 parent ec5ff92 commit c778a14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
16.0.2
------

* #319: In Windows backend, trap all exceptions when
attempting to import pywin32.

16.0.1
------

Expand Down
35 changes: 13 additions & 22 deletions keyring/backends/Windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down

1 comment on commit c778a14

@Mekk
Copy link
Contributor

@Mekk Mekk commented on c778a14 Mar 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change somehow introduced completely mysterious problem on non-windows platforms. See
https://bz.mercurial-scm.org/show_bug.cgi?id=6043#c14
Any clues what's up welcome.

Please sign in to comment.