Repository navigation
Py_Main and Py_RunMain SystemExit documentation does not match actual behavior #133264
Description
Activity
- 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 Do you want to propose a PR to update the documentation?
I considered it, but it's not clear to me whether the documentation or the implementation is wrong.
You're correct that
Py_RunMain()is designed to embed Python in an application. Callingexit(code)would be bad in this case.Py_RunMain()return value is an exit code.SystemExit(code)becomes an exit code, it doesn't callexit(code).But what about
Py_Main? Its documentation has stated for many years that an unhandledSystemExitwill exit the process, but the implementation no longer does that.Py_Main()callsPy_RunMain().Yes, that's my point. Neither function currently exits the process on an unhandled
SystemExit, but in the case ofPy_MainI suspect this behavior has changed unintentionally at some point in the past.Correct, the behavior changed. The documentation should be updated.
this behavior has changed unintentionally at some point in the past.
Correction: these changes were made on purpose.
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.
Reacted by Victor Stinner- added a commit that references this issue
on Jun 11, 2025 As far as why #8023 didn't get this right: I forgot the behaviour had changed, and added the outdated info from the
Py_Maindocs to thePy_RunMaindocs when expanding the latter (without checking it was still true).Thanks for picking up and resolving the oversight!
Reacted by Victor Stinner and Malcolm Smith
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
A few months ago, #8023 added some details to the docs about how
Py_RunMaindetermines its exit status. Specifically, it wrote thatPy_RunMainexits the process on an unhandledSystemExit.But this isn't the case, at least not when running via the
pymain_run_modulepath (the other paths aren't so clear to me). The relevant code is inpymain_err_print, which specifically checks forSystemExitin order to avoid passing it toPyErr_Print, which is whereexitwould actually be called.My understanding is that
Py_RunMainwas intended for an embedded context, in which case exiting the process is probably not desirable.The docs of
Py_Mainhave specified the process exiting behavior for much longer, but right now it looks like both functions share the same implementation.@vstinner @ncoghlan
Linked PRs