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

All sys attributes (.argv, ...) should exist in embedded environments #76754

Closed
pgacv2 mannequin opened this issue Jan 16, 2018 · 11 comments
Closed

All sys attributes (.argv, ...) should exist in embedded environments #76754

pgacv2 mannequin opened this issue Jan 16, 2018 · 11 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pgacv2
Copy link
Mannequin

pgacv2 mannequin commented Jan 16, 2018

BPO 32573
Nosy @terryjreedy, @ncoghlan, @vstinner, @bitdancer
PRs
  • bpo-32573: Handle the case when sys.argv is not set #12463
  • [3.7] bpo-32573: sys.argv always exists and is non-empty #13553
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-06-02.21:59:27.688>
    created_at = <Date 2018-01-16.19:46:28.259>
    labels = ['3.8', 'type-feature', 'library']
    title = 'All sys attributes (.argv, ...) should exist in embedded environments'
    updated_at = <Date 2019-06-02.21:59:27.687>
    user = 'https://bugs.python.org/pgacv2'

    bugs.python.org fields:

    activity = <Date 2019-06-02.21:59:27.687>
    actor = 'vstinner'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-06-02.21:59:27.688>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2018-01-16.19:46:28.259>
    creator = 'pgacv2'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32573
    keywords = ['patch']
    message_count = 11.0
    messages = ['310105', '310108', '310127', '310128', '310139', '310287', '310304', '310309', '342342', '342343', '344329']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'vstinner', 'r.david.murray', 'docs@python', 'pgacv2']
    pr_nums = ['12463', '13553']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32573'
    versions = ['Python 3.8']

    @pgacv2
    Copy link
    Mannequin Author

    pgacv2 mannequin commented Jan 16, 2018

    Embedded Python interpreters, such as Boost.Python, do not have sys.argv available. (sys itself works fine.) This causes the interpreter to crash with the following exception when you try to access argv:

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

    I'm not sure how closely related this is to bpo-15577 or PEP-432. A simple workaround is to manually assign a list with an empty string to argv, as suggested by googleapis/oauth2client#642. However, the documentation for the sys module makes no mention of this special case for argv, and the line at the top that says "It is always available" can easily be interpreted to mean that *all* of sys's attributes will always be available. I suggest adding the following to the documentation for sys.argv:

    ========

    Since argv contains the list of command line arguments passed to the Python interpreter, argv is not available within embedded interpreters, and scripts that run in embedded environments will raise an AttributeError when they attempt to access argv. As a workaround, assign a list with an empty string manually:

    import sys
    
    if not hasattr(sys, 'argv'):
        sys.argv  = ['']
    

    @pgacv2 pgacv2 mannequin added docs Documentation in the Doc dir 3.7 (EOL) end of life labels Jan 16, 2018
    @pgacv2 pgacv2 mannequin assigned docspython Jan 16, 2018
    @pgacv2 pgacv2 mannequin added the type-feature A feature request or enhancement label Jan 16, 2018
    @bitdancer
    Copy link
    Member

    A better question might be: is there something in the embedding framework that should initialize argv to the empty list? embedding framework here could have two meanings: either the third party code, or the code that we provide for supporting embedding.

    @pgacv2
    Copy link
    Mannequin Author

    pgacv2 mannequin commented Jan 17, 2018

    My first inclination would be no. An argv value of [''] means "zero command-line arguments" (not even a filename), which implies the following as far as I know:

    1. Python was called as an executable rather than a library, because you can't pass command-line arguments to a library
    2. No arguments were passed to the executable
    3. You are running from inside the REPL

    All three are false in an embedded context.

    A not-much-better-but-maybe-worth-considering question might also be: should scripts be able to tell whether they are being run from an embedded interpreter? If so, do they have any other way of doing so if an initialization is forced for argv?

    @pgacv2
    Copy link
    Mannequin Author

    pgacv2 mannequin commented Jan 17, 2018

    My comment above uses "code that we provide for supporting embedding," not "third party code," as the definition of embedded.

    @ncoghlan
    Copy link
    Contributor

    There are actually 3 attributes that may be absent when CPython itself isn't managing the C level entry point: sys.argv, sys.warnoptions, and sys._xoptions.

    I'd be more inclined to make the implementation match the docs though, and have these all be empty lists in the not-configured case, rather than absent entirely. (Accessing sys.argv[0] would still be an error in that situation, just an IndexError instead of an AttributeError)

    @terryjreedy
    Copy link
    Member

    Pedro: David and Nick are proposing initializing sys.argv to [] rather than [''] for embedded interpreters. This would say 'running embedded'. The .argv entry would need an additional sentence.

    I like Nick's proposal, except that _xoptions should be {}. Documenting absence should only be a fallback is there is no sensible default.

    @terryjreedy terryjreedy added 3.8 only security fixes stdlib Python modules in the Lib dir and removed docs Documentation in the Doc dir labels Jan 19, 2018
    @terryjreedy terryjreedy changed the title sys.argv documentation should include caveat for embedded environments All sys attributes (.argv, ...) should exist in embedded environments Jan 19, 2018
    @pgacv2
    Copy link
    Mannequin Author

    pgacv2 mannequin commented Jan 20, 2018

    I agree that an empty list, instead of a list with an empty string, makes more sense. Leaving _xoptions as {} would be the same as when it runs non-embedded without any -X arguments, but I guess there's no way of making {} any more empty.

    @terryjreedy
    Copy link
    Member

    For detecting 'embedded', it is enough that only one documented attribute value be unique to embedded situations.

    @vstinner
    Copy link
    Member

    In Python 3.8, sys.argv is now always created. It's initialized to sys.argv = [""] if (argc, argv) are not set in _PyCoreConfig.

    So PR 12463 looks useless to me, except if someone wants to change the behavior.

    @vstinner
    Copy link
    Member

    See also the PEP-587.

    @vstinner
    Copy link
    Member

    vstinner commented Jun 2, 2019

    The issue has been fixed in Python 3.8. See PEP-587 and http://docs.python.org/dev/c-api/init_config.html for the larger scope.

    For Python 3.7, the fix is trivial: don't add the following 2 lines in your application:

    if not hasattr(sys, 'argv'):
        sys.argv = ['']

    Python 3.7 Release Manager, Ned Deily, was opposed to change the behavior in a minor release:
    #13553 (comment)

    @vstinner vstinner removed the 3.7 (EOL) end of life label Jun 2, 2019
    @vstinner vstinner closed this as completed Jun 2, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants