pythonrun

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package pythonrun ports cpython/Python/pythonrun.c. It is the consumer layer that sits on top of parser, compile, and vm: take a chunk of source, drive it through the pipeline, and surface the result or the traceback. v0.7 lands the string-evaluation entries (PyRun_SimpleString, PyRun_String); the file and REPL arms join in 1624-B and 1624-C.

CPython: Python/pythonrun.c

Index

Constants

View Source
const PromptPS1 = ">>> "

PromptPS1 is the primary prompt displayed before each line of input. CPython grabs sys.ps1 first; that wiring lands with 1651-sys-D.

CPython: Python/pythonrun.c:122 sys.ps1 default

View Source
const PromptPS2 = "... "

PromptPS2 is the continuation prompt. The v0.7 line-buffered REPL does not ask for continuation input, so this is held for the multi-line work in 1645.

Variables

View Source
var ErrFileEmpty = errors.New("pythonrun: source file is empty")

ErrFileEmpty is returned by RunFile when the source file is empty. CPython treats this as a no-op module; the gopy port keeps the same shape but surfaces the case so callers can distinguish.

Functions

func InteractiveLoop

func InteractiveLoop(ts *state.Thread, stdin io.Reader, stdout, stderr io.Writer, globals objects.Object) int

InteractiveLoop is the basic REPL: read a line from stdin, run it against globals, print any expression value, repeat until EOF. Mirrors PyRun_InteractiveLoopFlags. The v0.7 port uses bufio for line input; readline editing comes with 1645.

Each line is tried in eval mode first (so "1 + 2" prints "3"); on a parse failure, the line is retried in single-statement mode so assignments, print() calls, and other statements still run.

CPython: Python/pythonrun.c:128 PyRun_InteractiveLoopFlags

func RunAnyFile

func RunAnyFile(ts *state.Thread, filename string, globals objects.Object, stderr io.Writer) int

RunAnyFile is the top-level dispatch entry. It mirrors PyRun_AnyFileExFlags: pick interactive vs simple-file, pass through. v0.7 lacks the REPL (1624-C) so the entry collapses to RunSimpleFile; the dispatch shape stays so the call site does not need to move when 1624-C lands.

CPython: Python/pythonrun.c:91 PyRun_AnyFileExFlags

func RunFile

func RunFile(ts *state.Thread, filename string, globals, locals objects.Object) (objects.Object, error)

RunFile reads filename, parses it as a Python module, and runs the resulting code object against globals/locals. Returns the value the frame produced (None for module mode) or the error that escaped.

The caller owns the globals dict; RunFile sets `__file__` on it for the duration of the run if the slot is missing, mirroring _PyRun_SimpleFileObject. Restoration on the way out matches the CPython "set_file_name" branch.

CPython: Python/pythonrun.c:1276 pyrun_file

func RunSimpleFile

func RunSimpleFile(ts *state.Thread, filename string, globals objects.Object, stderr io.Writer) int

RunSimpleFile parses, compiles, and runs filename as a module against globals. The result is discarded; on failure the traceback (or Go-level error) is rendered to stderr.

Returns the exit code: 0 on success, 1 on a generic exception, or the int the caller passed to SystemExit. See RunSimpleString for the rationale on returning the exit code instead of CPython's 0/-1 plus Py_Exit long jump.

The .pyc detect-and-dispatch branch and the interactive-tty fallback are deferred to 1624-C and pyc.go.

CPython: Python/pythonrun.c:462 _PyRun_SimpleFileObject

func RunSimpleString

func RunSimpleString(ts *state.Thread, command string, globals objects.Object, stderr io.Writer) int

RunSimpleString parses, compiles, and runs command as a Python module against globals. The result is discarded; on failure the traceback (or Go-level error from the parse/compile arm) is rendered to stderr.

Returns the exit code: 0 on success, 1 on a generic exception, or the int the caller passed to SystemExit. CPython's PyRun_SimpleStringFlags returns -1 and Py_Exit jumps inside PyErr_Print; we surface the exit code through the return so lifecycle.Main propagates it without long-jumping the runtime.

CPython owns __main__ in the import system. Once 1623 lands the globals argument disappears and RunSimpleString grabs the dict itself via PyImport_AddModuleRef("__main__").

CPython: Python/pythonrun.c:592 PyRun_SimpleStringFlags

func RunString

func RunString(ts *state.Thread, src, filename string, mode parser.Mode, globals, locals objects.Object) (objects.Object, error)

RunString parses src as Python source under mode (file or eval), compiles it, and runs the resulting code object against globals and locals. Returns the value the frame produced (None for ModeFile, the expression result for ModeEval) or the error that escaped.

CPython: Python/pythonrun.c:1219 _PyRun_StringFlagsWithName

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL