Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kwallet backend fails in environments where sys.argv is not present #445

Closed
AndrewGrossman opened this issue Jul 7, 2020 · 4 comments
Closed

Comments

@AndrewGrossman
Copy link

Kwallet backend fails in environments where sys does not have argv available.

In my particular case, I was trying to utilize the Snowflake connector for SQLAlchemy through PostgreSQL Multicorn FDW. I encountered the following stacktrace in my log:

  File "/usr/lib/python3/dist-packages/multicorn/sqlalchemyfdw.py", line 282, in __init__
    self.engine = create_engine(url)

  File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)

  File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/strategies.py", line 61, in create
    entrypoint = u._get_entrypoint()

  File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/url.py", line 172, in _get_entrypoint
    cls = registry.load(name)

  File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/util/langhelpers.py", line 265, in load
    return impl.load()

  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2324, in load
    return self.resolve()

  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2330, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)

  File "/usr/local/lib/python3.6/dist-packages/snowflake/sqlalchemy/__init__.py", line 8, in <module>
    from . import snowdialect

  File "/usr/local/lib/python3.6/dist-packages/snowflake/sqlalchemy/snowdialect.py", line 27, in <module>
    from snowflake.connector.constants import UTF8

  File "/usr/local/lib/python3.6/dist-packages/snowflake/connector/__init__.py", line 17, in <module>
    from .connection import SnowflakeConnection

  File "/usr/local/lib/python3.6/dist-packages/snowflake/connector/connection.py", line 18, in <module>
    from .auth import Auth

  File "/usr/local/lib/python3.6/dist-packages/snowflake/connector/auth.py", line 51, in <module>
    import keyring

  File "/usr/lib/python3/dist-packages/keyring/__init__.py", line 3, in <module>
    from .core import (set_keyring, get_keyring, set_password, get_password,

  File "/usr/lib/python3/dist-packages/keyring/core.py", line 153, in <module>
    init_backend()

  File "/usr/lib/python3/dist-packages/keyring/core.py", line 66, in init_backend
    keyrings = filter(limit, backend.get_all_keyring())

  File "/usr/lib/python3/dist-packages/keyring/util/__init__.py", line 21, in wrapper
    func.always_returns = func(*args, **kwargs)

  File "/usr/lib/python3/dist-packages/keyring/backend.py", line 184, in get_all_keyring
    _load_backends()

  File "/usr/lib/python3/dist-packages/keyring/backend.py", line 142, in _load_backends
    list(map(_load_backend, backends))

  File "/usr/lib/python3/dist-packages/keyring/backend.py", line 133, in _load_backend
    mod = importlib.import_module('.' + name, package)

  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)

  File "/usr/lib/python3/dist-packages/keyring/backends/kwallet.py", line 21, in <module>
    class DBusKeyring(KeyringBackend):

  File "/usr/lib/python3/dist-packages/keyring/backends/kwallet.py", line 26, in DBusKeyring
    appid = os.path.basename(sys.argv[0]) or 'Python keyring library'

AttributeError: module 'sys' has no attribute 'argv'

I was able to successfully work around this by setting sys.argv to [''] when absent.

sezna pushed a commit to sezna/keyring that referenced this issue Jul 15, 2020
Added a check that argv exists and has at least some value before accessing it.
@sezna
Copy link
Contributor

sezna commented Jul 15, 2020

I had the same issue running Python from pio3. I just submitted PR #447 to fix this.

sezna pushed a commit to sezna/keyring that referenced this issue Jul 15, 2020
Added a check that argv exists and has at least some value before accessing it.
@jaraco
Copy link
Owner

jaraco commented Aug 2, 2020

It seems to me you've found a bug in those Python implementations. Even the docs of Python reference sys.argv[0]. I'd recommend reporting these non-compliant implementations or updating the Python docs to describe what interface users can expect.

@AndrewGrossman
Copy link
Author

I don't think it's necessarily a matter of the Python implementation. Looking at the discussion on https://bugs.python.org/issue839151 and an example usage in a C++ interface at https://stackoverflow.com/questions/12230210/attributeerror-module-object-has-no-attribute-argv-when-using-python-h , it's not clear to me that Python defines the presence of argv as canonical. The interface provides for it and the linked docs seem to be but a simplified general usage example. Yes, I agree that the docs should probably note this.

For my particular use case, I'm looking at CPython via Postgres plpython code at https://github.com/postgres/postgres/blob/master/src/pl/plpython/plpy_main.c

I think there's a valid argument that a script name in that context isn't appropriate and that CPython within Postgresql isn't that cornery of a corner case.

@jaraco
Copy link
Owner

jaraco commented Aug 22, 2020

Thanks for that additional evidence. I've filed bpo-41615 to track the issue upstream. In the meantime, I agree it would be worthwhile to address this issue in keyring.

jaraco added a commit that referenced this issue Aug 22, 2020
…as indicated by the Python docs and might in fact be empty or uninitialized or not a list. Ref #445.
@jaraco jaraco closed this as completed in 7b2d633 Aug 22, 2020
jaraco added a commit that referenced this issue Aug 22, 2020
Use default appid if sys.argv doesn't exist. Ref #445
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants