# Buggy code
def _set_keychain(self):
    """
    Lazy import to avoid conflict with pytest-xdist.
    """
    import objc
    from Foundation import NSBundle

    Security = NSBundle.bundleWithIdentifier_("com.apple.security")

    S_functions = [
        ("SecKeychainGetTypeID", b"I"),
        ("SecKeychainItemGetTypeID", b"I"),
        (
            "SecKeychainAddGenericPassword",
            b"i^{OpaqueSecKeychainRef=}I*I*I*o^^{OpaqueSecKeychainItemRef}",
        ),
        ("SecKeychainOpen", b"i*o^^{OpaqueSecKeychainRef}"),
        (
            "SecKeychainFindGenericPassword",
            b"i@I*I*o^Io^^{OpaquePassBuff}o^^{OpaqueSecKeychainItemRef}",
        ),
    ]

    objc.loadBundleFunctions(Security, globals(), S_functions)

    SecKeychainRef = objc.registerCFSignature(
        "SecKeychainRef", b"^{OpaqueSecKeychainRef=}", SecKeychainGetTypeID()
    )
    SecKeychainItemRef = objc.registerCFSignature(
        "SecKeychainItemRef",
        b"^{OpaqueSecKeychainItemRef=}",
        SecKeychainItemGetTypeID(),
    )
    PassBuffRef = objc.createOpaquePointerType(
        "PassBuffRef", b"^{OpaquePassBuff=}", None
    )

    # Get the login keychain
    result, login_keychain = SecKeychainOpen(b"login.keychain", None)
    self.login_keychain = login_keychain


# Traceback (dbus.exceptions.DBusException)
2020-08-31 20:53:34,066 - vorta.borg.borg_thread - DEBUG - Using VortaSecretStorageKeyring keyring to store passwords.
2020-08-31 20:53:34,066 - asyncio - DEBUG - Using selector: EpollSelector
Traceback (most recent call last):
File "/home/user/.local/lib/python3.8/site-packages/vorta/views/repo_add_dialog.py", line 64, in run
params = BorgInitThread.prepare(self.values)
File "/home/user/.local/lib/python3.8/site-packages/vorta/borg/init.py", line 20, in prepare
ret = super().prepare(profile)
File "/home/user/.local/lib/python3.8/site-packages/vorta/borg/borg_thread.py", line 121, in prepare
ret['password'] = keyring.get_password('vorta-repo', profile.repo.url)
File "/home/user/.local/lib/python3.8/site-packages/vorta/keyring/secretstorage.py", line 30, in get_password
collection = secretstorage.get_default_collection(self.connection)
File "/usr/lib/python3/dist-packages/secretstorage/collection.py", line 155, in get_default_collection
return Collection(bus)
File "/usr/lib/python3/dist-packages/secretstorage/collection.py", line 42, in __init__
self.collection_props_iface.Get(COLLECTION_IFACE, 'Label',
File "/usr/lib/python3/dist-packages/secretstorage/util.py", line 31, in function_out
return function_in(*args, **kwargs)
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 141, in __call__
return self._connection.call_blocking(self._named_service,
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 652, in call_blocking
reply_message = self.send_message_with_reply_and_block(
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownObject: No such object path '/org/freedesktop/secrets/aliases/default'

# Fixed code
def _set_keychain(self):
    """
    Lazy import to avoid conflict with pytest-xdist.
    """
    import objc
    from Foundation import NSBundle

    Security = NSBundle.bundleWithIdentifier_("com.apple.security")

    # https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
    S_functions = [
        ("SecKeychainGetTypeID", b"I"),
        ("SecKeychainItemGetTypeID", b"I"),
        (
            "SecKeychainAddGenericPassword",
            b"i^{OpaqueSecKeychainRef=}I*I*I*o^^{OpaqueSecKeychainItemRef}",
        ),
        ("SecKeychainOpen", b"i*o^^{OpaqueSecKeychainRef}"),
        (
            "SecKeychainFindGenericPassword",
            b"i@I*I*o^Io^^{OpaquePassBuff}o^^{OpaqueSecKeychainItemRef}",
        ),
        ("SecKeychainGetStatus", b"i^{OpaqueSecKeychainRef=}o^I"),
    ]

    objc.loadBundleFunctions(Security, globals(), S_functions)

    SecKeychainRef = objc.registerCFSignature(
        "SecKeychainRef", b"^{OpaqueSecKeychainRef=}", SecKeychainGetTypeID()
    )
    SecKeychainItemRef = objc.registerCFSignature(
        "SecKeychainItemRef",
        b"^{OpaqueSecKeychainItemRef=}",
        SecKeychainItemGetTypeID(),
    )

    PassBuffRef = objc.createOpaquePointerType(
        "PassBuffRef", b"^{OpaquePassBuff=}", None
    )

    # Get the login keychain
    result, login_keychain = SecKeychainOpen(b"login.keychain", None)
    self.login_keychain = login_keychain