Extract _id_from_argv and also trap TypeError. Fixes
import sys
import os
+import contextlib
from ..backend import KeyringBackend
from ..errors import PasswordDeleteError
@@ -16,18 +17,21 @@ except AttributeError:
pass
+def _id_from_argv():
+ """
+ Safely infer an app id from sys.argv. Ref #445.
+ """
+ allowed = AttributeError, IndexError, TypeError
+ with contextlib.suppress(allowed):
+ return sys.argv[0]
+
+
class DBusKeyring(KeyringBackend):
"""
KDE KWallet 5 via D-Bus
"""
- # See https://github.com/jaraco/keyring/issues/445
- default_appid = 'Python keyring library'
- appid = (
- sys.argv[0] or default_appid
- if hasattr(sys, 'argv') and len(sys.argv) > 0
- else default_appid
- )
+ appid = _id_from_argv() or 'Python keyring library'
wallet = None
bus_name = 'org.kde.kwalletd5'
object_path = '/modules/kwalletd5'
