vm

package
v0.12.3 Latest Latest
Warning

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

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

Documentation

Overview

__build_class__ is the builtin every `class C(...): ...` statement invokes through LOAD_BUILD_CLASS. The compiler emits the call as

__build_class__(<body fn>, "C", *bases, **kwds)

and the implementation here:

  1. allocates a fresh namespace dict (this is the class's tp_dict),
  2. runs the body fn with that dict as f_locals so STORE_NAME lands in the namespace,
  3. resolves the metaclass (kwds['metaclass'] if present, else `type`),
  4. dispatches to metaclass(name, bases_tuple, ns, **other_kwds), which builds the class object via NewUserType.

Lives in vm/ because step 2 needs to push a frame and drive Eval; builtins/ cannot do that without an import cycle. The hook back into builtins/ is registered in init().

CPython: Python/bltinmodule.c:131 builtin___build_class__

Wires objects.FunctionType.Call so a Python-defined function can be invoked through objects.Call. Lives in the vm package because the call needs to push a new frame and drive the eval loop, which the objects package can't reach without an import cycle.

CPython: Objects/funcobject.c function_call

Small helpers the dispatch arms call into. The action translator (1621/B6) emits some of these; the hand-written panel uses them directly. CPython has them as macros / inline helpers in Python/ceval_macros.h and Python/ceval.c.

IMPORT_NAME and IMPORT_FROM bytecode arms. Added to the hand-written panel rather than the generated arms because the import machinery depends on the imp package which is only available after v0.8.

CPython: Python/bytecodes.c IMPORT_NAME / IMPORT_FROM

RESUME / RETURN_GENERATOR / generator re-entry. CPython handles these in Python/ceval.c around the eval-loop entry. v0.6 keeps a minimal RESUME that polls the eval breaker and otherwise no-ops; the generator path lives behind the objects.Generator port (1687) and is wired in v0.7.

CPython: Python/ceval.c RESUME

Index

Constants

View Source
const (
	PyTraceCall       = 0
	PyTraceException  = 1
	PyTraceLine       = 2
	PyTraceReturn     = 3
	PyTraceCCall      = 4
	PyTraceCException = 5
	PyTraceCReturn    = 6
	PyTraceOpcode     = 7
)

PyTrace_* are the legacy event kinds the bridge feeds to a LegacyTraceFunc. The integer values match the public Include/cpython/pystate.h constants so a port of pdb / cProfile behaves like the real thing.

CPython: Include/cpython/pystate.h:Py_tracefunc constants

Variables

View Source
var ErrNotImplemented = errors.New("vm: opcode not implemented in v0.6")

ErrNotImplemented is returned by dispatch arms that have not yet been generated. The eval loop surfaces it as a runtime error so tests can pin the surface without crashing.

Functions

func BreakerFor

func BreakerFor(ts *state.Thread) *gil.Breaker

BreakerFor is the public form of breakerFor for the same reason.

func Eval

func Eval(ts *state.Thread, f *frame.Frame) (objects.Object, error)

Eval runs f to completion under ts and returns the value the frame produced (RETURN_VALUE) or the error that escaped.

CPython: Python/ceval.c _PyEval_EvalFrameDefault

func EvalCode

func EvalCode(ts *state.Thread, co *objects.Code, globals, locals objects.Object) (objects.Object, error)

EvalCode is the convenience wrapper that builds a frame from a code object plus globals/locals and calls Eval.

CPython: Python/ceval.c PyEval_EvalCode

func PendingFor

func PendingFor(ts *state.Thread) *gil.Pending

PendingFor returns the pending-call queue for ts. Public so the signal bridge in user code (1651) can wire SIGINT into it.

func RegisterSysTraceBuiltins added in v0.11.0

func RegisterSysTraceBuiltins(d *objects.Dict) error

RegisterSysTraceBuiltins installs settrace, setprofile, gettrace, and getprofile in the supplied sys dict. Lifecycle wiring calls this once it has built the per-interpreter sys module.

CPython: Python/sysmodule.c:3360 sys_methods (settrace / setprofile rows)

func SetGIL added in v0.9.0

func SetGIL(ts *state.Thread, g *gil.GIL)

SetGIL attaches g to ts so the eval loop can drive the switch-interval handshake. v0.9 leaves this nil except in tests; v0.13's sub-interpreter scheduler will set it during interpreter creation.

CPython: pycore_interp.h _gil_runtime_state lives on PyInterpreterState

func SetProfile added in v0.11.0

func SetProfile(ts *state.Thread, fn LegacyTraceFunc, arg objects.Object) (objects.Object, error)

SetProfile is the Go form of CPython's _PyEval_SetProfile. The caller passes in a thread, the legacy profile function (or nil to clear), and the user-visible argument that gets handed back to every callback. Returns the previously-installed argument so the caller can drop its reference.

CPython: Python/legacy_tracing.c:531 _PyEval_SetProfile

func SetTrace added in v0.11.0

func SetTrace(ts *state.Thread, fn LegacyTraceFunc, arg objects.Object) (objects.Object, error)

SetTrace is the Go form of CPython's _PyEval_SetTrace.

CPython: Python/legacy_tracing.c:696 _PyEval_SetTrace

func SysGetProfile added in v0.11.0

func SysGetProfile() objects.Object

SysGetProfile returns the builtin that backs sys.getprofile.

CPython: Python/sysmodule.c:1283 sys_getprofile_impl

func SysGetTrace added in v0.11.0

func SysGetTrace() objects.Object

SysGetTrace returns the builtin that backs sys.gettrace. It hands back the user-installed trace callable, or None when no trace is active.

CPython: Python/sysmodule.c:1202 sys_gettrace_impl

func SysSetProfile added in v0.11.0

func SysSetProfile() objects.Object

SysSetProfile returns the builtin function that backs sys.setprofile.

CPython: Python/sysmodule.c:1226 sys_setprofile

func SysSetTrace added in v0.11.0

func SysSetTrace() objects.Object

SysSetTrace returns the builtin function that backs sys.settrace. Calling it installs callable as the trace function on the current thread, or clears tracing when callable is None.

CPython: Python/sysmodule.c:1145 sys_settrace

Types

type LegacyTraceFunc added in v0.11.0

type LegacyTraceFunc func(obj objects.Object, f *frame.Frame, kind int, arg objects.Object) error

LegacyTraceFunc is the Go shape of CPython's Py_tracefunc. The runtime hands the trace object the user installed, the current frame, the legacy event ID (one of the PyTrace_* constants), and the per-event argument the bridge synthesized. Returning a non-nil error aborts the trace path.

CPython: Include/cpython/pystate.h:Py_tracefunc

func CProfileFunc added in v0.11.0

func CProfileFunc(ts *state.Thread) (LegacyTraceFunc, objects.Object)

CProfileFunc returns the thread's legacy profilefunc.

func CTraceFunc added in v0.11.0

func CTraceFunc(ts *state.Thread) (LegacyTraceFunc, objects.Object)

CTraceFunc returns the thread's legacy tracefunc, or nil if sys.settrace has not been called on this thread.

Jump to

Keyboard shortcuts

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