Close the secretstorage connections after using them. Fixes
+from contextlib import closing
import logging
from ..util import properties
@@ -37,8 +38,8 @@ class Keyring(KeyringBackend):
if not hasattr(secretstorage, 'get_default_collection'):
raise RuntimeError("SecretStorage 1.0 or newer required")
try:
- bus = secretstorage.dbus_init()
- list(secretstorage.get_all_collections(bus))
+ with closing(secretstorage.dbus_init()) as connection:
+ list(secretstorage.get_all_collections(connection))
except exceptions.SecretStorageException as e:
raise RuntimeError("Unable to initialize SecretService: %s" % e)
return 5
@@ -72,6 +73,7 @@ class Keyring(KeyringBackend):
"""Get password of the username for the service
"""
collection = self.get_preferred_collection()
+ with closing(collection.connection):
items = collection.search_items({"username": username, "service": service})
for item in items:
self.unlock(item)
@@ -87,12 +89,14 @@ class Keyring(KeyringBackend):
"username": username,
}
label = "Password for '{}' on '{}'".format(username, service)
+ with closing(collection.connection):
collection.create_item(label, attributes, password, replace=True)
def delete_password(self, service, username):
"""Delete the stored password (only the first one)
"""
collection = self.get_preferred_collection()
+ with closing(collection.connection):
items = collection.search_items({"username": username, "service": service})
for item in items:
return item.delete()
@@ -112,6 +116,7 @@ class Keyring(KeyringBackend):
collection = self.get_preferred_collection()
+ with closing(collection.connection):
items = collection.search_items({"service": service})
for item in items:
self.unlock(item)
