Skip to content

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

Closed

Description

@pgacv2
mannequin
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']

    Activity

    1. pgacv2 commented on Jan 16, 2018

      pgacv2mannequin
      MannequinAuthor

      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  = ['']
      
    2. bitdancer commented on Jan 16, 2018

      @bitdancer
      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.

    3. pgacv2 commented on Jan 17, 2018

      pgacv2mannequin
      MannequinAuthor

      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?

    4. pgacv2 commented on Jan 17, 2018

      pgacv2mannequin
      MannequinAuthor

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

    5. ncoghlan commented on Jan 17, 2018

      @ncoghlan
      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)

    6. terryjreedy commented on Jan 19, 2018

      @terryjreedy
      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.

    7. added
      stdlibStandard Library Python modules in the Lib/ directory
      and removed
      docsDocumentation in the Doc dir
      on Jan 19, 2018
    8. changed the title [-]sys.argv documentation should include caveat for embedded environments[/-] [+]All sys attributes (.argv, ...) should exist in embedded environments[/+] on Jan 19, 2018
    9. pgacv2 commented on Jan 20, 2018

      pgacv2mannequin
      MannequinAuthor

      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.

    10. terryjreedy commented on Jan 20, 2018

      @terryjreedy
      Member

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

    11. vstinner commented on May 13, 2019

      @vstinner
      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.

    12. vstinner commented on May 13, 2019

      @vstinner
      Member

      See also the PEP-587.

    13. vstinner commented on Jun 2, 2019

      @vstinner
      Member

      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)

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions