Skip to main content

Include/pythonrun.h

cpython 3.14 @ ab2d84fe1023/Include/pythonrun.h

Include/pythonrun.h declares the high-level execution entry points. These are the functions an embedding application calls to run Python source without going through the compiler pipeline manually.

Map

LinesSymbolRole
1-40Py_CompileString, Py_CompileStringExFlagsCompile source to a code object
41-90PyRun_SimpleString, PyRun_SimpleStringFlagsRun source in __main__
91-120PyRun_FileExFlags, PyRun_InteractiveLoopFlagsRun from a file object
121-140PyErr_Print, PyErr_PrintEx, PyErr_DisplayException reporting

Reading

Py_CompileString

Py_CompileString takes a NUL-terminated source string, a filename (for tracebacks), and a start token (Py_eval_input, Py_file_input, Py_single_input). It returns a code object or NULL on error.

// CPython: Include/pythonrun.h:18 Py_CompileString
#define Py_CompileString(str, p, s) \
Py_CompileStringExFlags(str, p, s, NULL, -1)

PyRun_SimpleString

PyRun_SimpleString calls Py_CompileString with Py_file_input, then evaluates the resulting code object in __main__'s namespace. Return value: 0 on success, -1 if an unhandled exception occurred.

PyErr_Print and PyErr_Display

PyErr_Print calls sys.excepthook if it is set; PyErr_Display is the low-level fallback that writes the traceback to sys.stderr directly. Embedders that want to capture exception output should call PyErr_Fetch/PyErr_Normalize before formatting.

gopy notes

pythonrun/runstring.go mirrors this header. RunString compiles and evaluates a source string in the given namespace. The start argument distinction (eval vs file vs single) is handled by the compile layer in compile/.