Skip to content

Exit status is always 0 when started as a Python module #3156

Closed

Description

@szepeviktor

When started as python -m codespell_lib codespell always exits with 0.

Is it intentional?

Activity

  1. szepeviktor commented on Oct 17, 2023

    @szepeviktor
    ContributorAuthor

    I install it in my user's home pip install --user and don't want to "$(python -m site --user-base)/bin/codespell"

  2. DimitriPapadopoulos commented on Oct 17, 2023

    @DimitriPapadopoulos
    Collaborator

    Indeed:

    $ codespell -I non_existent_file
    ERROR: cannot find ignore-words file: non_existent_file
    [...]
    $ echo $?
    0
    $ 
    $ python -m codespell_lib -I non_existent_file
    ERROR: cannot find ignore-words file: non_existent_file
    [...]
    $ echo $?
    64
    $ 
    

    But the way, isn't $(python -m site --user-base)/bin in the PATH by default on most Linux distributions?

  3. szepeviktor commented on Oct 17, 2023

    @szepeviktor
    ContributorAuthor

    But the way, isn't $(python -m site --user-base)/bin in the PATH by default on most Linux distributions?

    Could be ...
    But I like my computer to be extra simple.

  4. DimitriPapadopoulos commented on Oct 17, 2023

    @DimitriPapadopoulos
    Collaborator

    The codespell executable $(python -m site --user-base)/bin/codespell looks like this:

    from codespell_lib import _script_main
    if __name__ == '__main__':
        sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
        sys.exit(_script_main())

    File codespell_lib/__main__.py looks like this:

    from ._codespell import _script_main
    
    if __name__ == "__main__":
        _script_main()

    Does changing the last line help?

    if __name__ == "__main__":
        sys.exit(_script_main())
  5. szepeviktor commented on Oct 17, 2023

    @szepeviktor
    ContributorAuthor

    Yes.

  6. DimitriPapadopoulos commented on Oct 17, 2023

    @DimitriPapadopoulos
    Collaborator

    Also, do you know if that's right thing to do? I cannot find definitive information in the relevant documentation, although some examples do call sys.exit():

  7. szepeviktor commented on Oct 17, 2023

    @szepeviktor
    ContributorAuthor

    🤔

  8. szepeviktor commented on Oct 17, 2023

    @szepeviktor
    ContributorAuthor

    The only thing I can think of is: when codespell is used as a library it should not exit (terminate)

  9. DimitriPapadopoulos commented on Oct 17, 2023

    @DimitriPapadopoulos
    Collaborator

    My wrong, it is (partially at least) documented:

    main functions are often used to create command-line tools by specifying them as entry points for console scripts. When this is done, pip inserts the function call into a template script, where the return value of main is passed into sys.exit(). For example:

     sys.exit(main())

    Since the call to main is wrapped in sys.exit(), the expectation is that your function will return some value acceptable as an input to sys.exit(); typically, an integer or None (which is implicitly returned if your function does not have a return statement).

    It's just that the above refers to the template script $(python -m site --user-base)/bin/codespell as far as I can tell, not to python -m. Also, I would have expected different wording such as "you MUST".

    Can you please create a pull request that adds sys.exit()? I should be able to merge it rapidly - unlike my own pull requests.

  10. szepeviktor commented on Oct 17, 2023

    @szepeviktor
    ContributorAuthor

    Done 🍏

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions