Skip to content

Py_Main and Py_RunMain SystemExit documentation does not match actual behavior #133264

Description

@mhsmith

A few months ago, #8023 added some details to the docs about how Py_RunMain determines its exit status. Specifically, it wrote thatPy_RunMain exits the process on an unhandled SystemExit.

But this isn't the case, at least not when running via the pymain_run_module path (the other paths aren't so clear to me). The relevant code is in pymain_err_print, which specifically checks for SystemExit in order to avoid passing it to PyErr_Print, which is where exit would actually be called.

My understanding is that Py_RunMain was intended for an embedded context, in which case exiting the process is probably not desirable.

The docs of Py_Main have specified the process exiting behavior for much longer, but right now it looks like both functions share the same implementation.

@vstinner @ncoghlan

Linked PRs

Activity

  1. changed the title [-]Py_RunMain SystemExit documentation does not match actual behavior[/-] [+]Py_Main and Py_RunMain SystemExit documentation does not match actual behavior[/+] on May 1, 2025
  2. vstinner commented on May 2, 2025

    @vstinner
    Member

    Do you want to propose a PR to update the documentation?

  3. mhsmith commented on Jun 8, 2025

    @mhsmith
    MemberAuthor

    I considered it, but it's not clear to me whether the documentation or the implementation is wrong.

  4. vstinner commented on Jun 9, 2025

    @vstinner
    Member

    You're correct that Py_RunMain() is designed to embed Python in an application. Calling exit(code) would be bad in this case. Py_RunMain() return value is an exit code. SystemExit(code) becomes an exit code, it doesn't call exit(code).

  5. mhsmith commented on Jun 9, 2025

    @mhsmith
    MemberAuthor

    But what about Py_Main? Its documentation has stated for many years that an unhandled SystemExit will exit the process, but the implementation no longer does that.

  6. vstinner commented on Jun 9, 2025

    @vstinner
    Member

    Py_Main() calls Py_RunMain().

  7. mhsmith commented on Jun 9, 2025

    @mhsmith
    MemberAuthor

    Yes, that's my point. Neither function currently exits the process on an unhandled SystemExit, but in the case of Py_Main I suspect this behavior has changed unintentionally at some point in the past.

  8. vstinner commented on Jun 9, 2025

    @vstinner
    Member

    Correct, the behavior changed. The documentation should be updated.

  9. vstinner commented on Jun 9, 2025

    @vstinner
    Member

    this behavior has changed unintentionally at some point in the past.

    Correction: these changes were made on purpose.

  10. mhsmith commented on Jun 10, 2025

    @mhsmith
    MemberAuthor

    OK, it looks like this was done in #13390 in 2019, so anyone who was affected by the change has already adapted to it. So I'll make a PR to update the documentation.

  11. added a commit that references this issue on Jun 11, 2025
  12. added 2 commits that reference this issue on Jun 11, 2025
  13. added 2 commits that reference this issue on Jun 11, 2025
  14. ncoghlan commented on Jun 12, 2025

    @ncoghlan
    Contributor

    As far as why #8023 didn't get this right: I forgot the behaviour had changed, and added the outdated info from the Py_Main docs to the Py_RunMain docs when expanding the latter (without checking it was still true).

    Thanks for picking up and resolving the oversight!

  15. added a commit that references this issue on Jun 19, 2025
  16. added a commit that references this issue on Jul 12, 2025
  17. added a commit that references this issue on Jul 14, 2025
  18. added a commit that references this issue on Aug 4, 2025
  19. added a commit that references this issue on Aug 19, 2025
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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions