Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Doc/c-api/interp-lifecycle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,48 @@ Initializing and finalizing the interpreter
first initialized).


.. c:function:: int Py_FrozenMain(int argc, char **argv)

.. index:: single: freeze utility

The main program for a "frozen" Python program (an executable that embeds
the interpreter together with the bytecode of the program's modules, as
produced by the :program:`freeze` utility; see :source:`Tools/freeze`).

This function initializes the interpreter, imports the frozen module named
``__main__``, and finalizes the interpreter.
The frozen modules must have been registered in
:c:var:`PyImport_FrozenModules` before calling this function;
it is a fatal error if ``__main__`` is not among them.

*argc* and *argv* are byte strings like the arguments of a C program's
``main`` function, and are passed to the interpreter with
:c:func:`PyConfig_SetBytesArgv`.
Unlike :c:func:`Py_Main`, the arguments are not parsed as Python command
line options (:c:member:`PyConfig.parse_argv` is ``0``), so all of them
are made available to the program in :data:`sys.argv`.
Apart from that, the interpreter uses the
:ref:`Python Configuration <init-python-config>`, so environment variables
such as :envvar:`PYTHONPATH` are honored.
Warnings about the path configuration
(:c:member:`PyConfig.pathconfig_warnings`) are suppressed.
If :envvar:`PYTHONINSPECT` is set and standard input is a terminal, an
interactive prompt is run after ``__main__`` has finished, as with the
:option:`-i` option.

Return ``0`` if ``__main__`` ran without an unhandled exception, ``1``
otherwise, and ``120`` if finalizing the interpreter failed (see
:c:func:`Py_FinalizeEx`).
An unhandled :exc:`SystemExit` terminates the process.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? (I'll need to check or study the code a bit more to verify; but if you already did, I'll believe you :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly, Codex did a lot of the research on the details here. I'll double-check to make sure that it's not hallucinating.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is correct.

If the interpreter cannot be initialized, the process exits via
:c:func:`Py_ExitStatusException`.

On Windows, this function is not exported by the Python DLL.
The :program:`freeze` utility compiles :file:`Python/frozenmain.c` into the
frozen program instead, together with the Windows-specific hooks that the
function requires.


.. c:function:: int Py_RunMain(void)

Executes the main module in a fully configured CPython runtime.
Expand Down
Loading