Skip to content

Commit

Permalink
Merge pull request #480 from takluyver/defer-init-backend
Browse files Browse the repository at this point in the history
Initialise backend on first use instead of on import
  • Loading branch information
jaraco committed Dec 22, 2020
2 parents 9438136 + 876e8d9 commit 0f2d42b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
v21.6.0
-------

* #403: Keyring no longer eagerly initializes the backend
on import, but instead defers the backend initialization
until a keyring is accessed. Any callers reliant on this
early intialization behavior may need to call
``keyring.core.init_backend()`` to explicitly initialize
the detected backend.

v21.5.0
-------

Expand Down
14 changes: 6 additions & 8 deletions keyring/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def set_keyring(keyring):

def get_keyring():
"""Get current keyring backend."""
if _keyring_backend is None:
init_backend()
return _keyring_backend


Expand All @@ -50,22 +52,22 @@ def disable():

def get_password(service_name, username):
"""Get password from the specified service."""
return _keyring_backend.get_password(service_name, username)
return get_keyring().get_password(service_name, username)


def set_password(service_name, username, password):
"""Set password for the user in the specified service."""
_keyring_backend.set_password(service_name, username, password)
get_keyring().set_password(service_name, username, password)


def delete_password(service_name, username):
"""Delete the password for the user in the specified service."""
_keyring_backend.delete_password(service_name, username)
get_keyring().delete_password(service_name, username)


def get_credential(service_name, username):
"""Get a Credential for the specified service."""
return _keyring_backend.get_credential(service_name, username)
return get_keyring().get_credential(service_name, username)


def recommended(backend):
Expand Down Expand Up @@ -187,7 +189,3 @@ def _load_keyring_path(config):
sys.path.insert(0, path)
except (configparser.NoOptionError, configparser.NoSectionError):
pass


# init the _keyring_backend
init_backend()

0 comments on commit 0f2d42b

Please sign in to comment.