Documentation
¶
Index ¶
- Variables
- func Forked() bool
- type Borrowed
- type Error
- type Exception
- type Func
- type GIL
- func (g *GIL) Attr(o uintptr, name string) (uintptr, error)
- func (g *GIL) Call(callable uintptr, args []any, kwargs map[string]any) (uintptr, error)
- func (g *GIL) CallMethod(o uintptr, name string, args []any, kwargs map[string]any) (uintptr, error)
- func (g *GIL) Callable(o uintptr) bool
- func (g *GIL) Decref(o uintptr)
- func (g *GIL) DictDel(d uintptr, key string)
- func (g *GIL) DictKeys(d uintptr) ([]string, error)
- func (g *GIL) DictSet(d uintptr, key string, value any) error
- func (g *GIL) Drain()
- func (g *GIL) ExceptionMatches(err error, cls uintptr) (bool, error)
- func (g *GIL) ExceptionObject(err error) (uintptr, bool)
- func (g *GIL) FromPython(o uintptr) (any, error)
- func (g *GIL) HasAttr(o uintptr, name string) bool
- func (g *GIL) Host() *Host
- func (g *GIL) Import(name string) (uintptr, error)
- func (g *GIL) IsAwaitable(o uintptr) (bool, error)
- func (g *GIL) IsInstance(o, cls uintptr) (bool, error)
- func (g *GIL) IsNone(o uintptr) bool
- func (g *GIL) IsSubclass(o, cls uintptr) (bool, error)
- func (g *GIL) Iterate(o uintptr, fn func(item uintptr) error) error
- func (g *GIL) KindOf(o uintptr) Kind
- func (g *GIL) Loop() uintptr
- func (g *GIL) NewClass(module, name string, bases []any, members map[string]any) (uintptr, error)
- func (g *GIL) NewDocFunc(name, doc string, fn RawFunc) (uintptr, error)
- func (g *GIL) NewFunc(name string, fn Func) (uintptr, error)
- func (g *GIL) NewObject(impl Object) (uintptr, error)
- func (g *GIL) NewRawFunc(name string, fn RawFunc) (uintptr, error)
- func (g *GIL) NewRef(o uintptr) *Ref
- func (g *GIL) None() uintptr
- func (g *GIL) Own(o uintptr) *Ref
- func (g *GIL) RaiseFrom(exc uintptr, cause error) error
- func (g *GIL) Raising(exc *Ref) error
- func (g *GIL) Repr(o uintptr) (string, error)
- func (g *GIL) SetAttr(o uintptr, name string, value any) error
- func (g *GIL) Str(o uintptr) (string, error)
- func (g *GIL) SysModules() uintptr
- func (g *GIL) ToPython(v any) (uintptr, error)
- func (g *GIL) Truthy(o uintptr) (bool, error)
- func (g *GIL) TupleItem(t uintptr, i int) uintptr
- func (g *GIL) Unblocked(fn func())
- type Host
- type InitError
- type Interpreter
- type Kind
- type NamedFunc
- type NoSharedLibraryError
- type Object
- type Options
- type Property
- type RawFunc
- type Ref
- func (r *Ref) Await(ctx context.Context) (any, error)
- func (r *Ref) AwaitIn(ctx context.Context, pyctx *Ref) (*Ref, error)
- func (r *Ref) Call(args []any, kwargs map[string]any) (any, error)
- func (r *Ref) CallAsync(ctx context.Context, args []any, kwargs map[string]any) (any, error)
- func (r *Ref) CallLocked(g *GIL, args []any, kwargs map[string]any) (any, error)
- func (r *Ref) CallMethod(name string, args []any, kwargs map[string]any) (any, error)
- func (r *Ref) GetAttr(name string) (any, error)
- func (r *Ref) GetAttrLocked(g *GIL, name string) (any, error)
- func (r *Ref) Ptr() uintptr
- func (r *Ref) Release()
- func (r *Ref) TypeName() string
- type SymbolError
Constants ¶
This section is empty.
Variables ¶
var ( // Returned for work asked of a host once shutdown has begun ErrClosed = errors.New("embedded Python host is shut down") // Start called after an interpreter was started in this process, running or finalized, since the process-wide object caches belong to that one ErrStarted = errors.New("an embedded Python interpreter was already started in this process") // Async work asked of a host whose asyncio loop thread is not running ErrNoLoop = errors.New("the host asyncio loop is not running") // Finalize called on a thread other than the one Start locked ErrWrongThread = errors.New("finalize must run on the goroutine that called Start") // A Go value with no Python form or a Python object with no Go form ErrUnconvertible = errors.New("value has no conversion") // Work that needs other OS threads asked of a child created by os.fork ErrForkedChild = errors.New("this call needs Go runtime threads, which do not exist in a child created by os.fork, use multiprocessing spawn or forkserver") // Start on a platform where purego cannot call libpython or take callbacks from it, matched by errors.ErrUnsupported too ErrUnsupported = fmt.Errorf("no embedded CPython support for %s/%s: %w", runtime.GOOS, runtime.GOARCH, errors.ErrUnsupported) )
Functions ¶
Types ¶
type Borrowed ¶
type Borrowed uintptr
Existing object pointer passed as an argument, the callee taking its own reference
type Error ¶
Python exception carried into Go with its qualified class name and message, the exception instance kept for re-raising and matching
func (*Error) Error ¶
Class and message the way the last line of a Python traceback shows them, the class alone for an empty message
func (*Error) Matches ¶
Whether the carried exception is an instance of any named builtin or module.Class exception class
type GIL ¶
type GIL struct {
// contains filtered or unexported fields
}
Proof that the goroutine holds an attached thread state of the interpreter, valid inside the call that handed it out and never shared between goroutines
func (*GIL) CallMethod ¶
func (g *GIL) CallMethod(o uintptr, name string, args []any, kwargs map[string]any) (uintptr, error)
Calls a method by name, a new reference
func (*GIL) Drain ¶
func (g *GIL) Drain()
Drops every reference Go released since the last GIL entry
func (*GIL) ExceptionMatches ¶
Whether err carries a Python exception that is an instance of cls, as an except clause naming cls would catch it
func (*GIL) ExceptionObject ¶
Borrowed exception object err carries, ok false when err holds no Python exception
func (*GIL) FromPython ¶
Converts a Python object to Go: None, bool, int, float, str, bytes and bytearray to Go values, dicts, lists and tuples by value, every other object as an owned *Ref
func (*GIL) HasAttr ¶
Whether the object has the attribute, any lookup error counting as absent and from 3.13 handed to sys.unraisablehook by CPython
func (*GIL) IsAwaitable ¶
Whether inspect.isawaitable reports the object awaitable, ErrNoLoop once the loop thread is gone
func (*GIL) IsInstance ¶
Whether o is an instance of cls, with the exception of a failing check as the error
func (*GIL) IsSubclass ¶
Whether class o derives from cls, with the exception of a failing check as the error
func (*GIL) Iterate ¶
Calls fn with each item of the iterable as a borrowed pointer until the iterator ends or fn fails
func (*GIL) KindOf ¶
Builtin kind of an object, KindOther for every class outside the builtin scalars and containers
func (*GIL) NewClass ¶
New class built by type(name, bases, namespace) whose RawFunc and Property members run Go code, a new reference
func (*GIL) NewDocFunc ¶
Native callable named name with ml_doc set to doc, whose leading name(params) line becomes its text signature, a new reference
func (*GIL) NewRawFunc ¶
Native callable over raw pointers, a new reference whose self object carries the Go function
func (*GIL) RaiseFrom ¶
Error raising the exception object exc chained to cause, as raise exc from cause inside its except block does
func (*GIL) Raising ¶
Error that re-raises the given exception instance when it reaches Python again
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
The one embedded interpreter of the process
func Start ¶
Loads libpython and initializes it once per process, leaving the calling goroutine locked to the OS thread that must call Finalize
func (*Host) Do ¶
Runs fn with the GIL held on a locked OS thread, refusing new work once shutdown began
func (*Host) Finalize ¶
Stops the loop, waits for in flight calls and finalizes; runs on the goroutine that called Start, ctx bounding every wait before Py_FinalizeEx
func (*Host) Interpreter ¶
func (h *Host) Interpreter() *Interpreter
The interpreter the host was started from
type InitError ¶
Failed step of interpreter initialization with the C function and message CPython reported, or the exit it asked for
type Interpreter ¶
type Interpreter struct {
GOOS string
Executable string
Prefix string
BasePrefix string
Major, Minor int
ABIFlags string
Library string
IncludeDir string
StdlibDir string
SitePackages string
Venv bool
Macros map[string]bool
Sysconfig map[string]string
}
CPython installation resolved from an executable path without running it
func Discover ¶
func Discover(exe string) (*Interpreter, error)
Resolves the interpreter of exe for the running platform
func DiscoverFor ¶
func DiscoverFor(exe, goos string) (*Interpreter, error)
Resolves the interpreter of exe for goos, which lets foreign layouts be checked on this machine
func (*Interpreter) Version ¶
func (in *Interpreter) Version() string
The major.minor text of the interpreter's version
type Kind ¶
type Kind int
Builtin class an object belongs to, subclasses included, the way conversion to Go reads it
const ( // Any class outside the builtin scalars and containers KindOther Kind = iota // The None singleton KindNone // True or False KindBool // int and its subclasses other than bool KindInt // float and its subclasses KindFloat // str and its subclasses KindStr // bytes and its subclasses KindBytes // bytearray and its subclasses KindByteArray // dict and its subclasses KindDict // list and its subclasses KindList // tuple and its subclasses KindTuple )
type NoSharedLibraryError ¶
type NoSharedLibraryError struct {
}
Interpreter linked without libpython, naming the uv commands that supply a shared build
func (*NoSharedLibraryError) Error ¶
func (e *NoSharedLibraryError) Error() string
The interpreter, why it has no usable libpython and how to get one
type Object ¶
type Object interface {
TypeName() (module, name string)
GetAttr(g *GIL, name string) (value any, ok bool, err error)
}
Go value exposed to Python, its attribute lookups answered by GetAttr
type Options ¶
type Options struct {
Interpreter *Interpreter
PycachePrefix string
ExtraPaths []string
Environment bool
Argv []string
}
Interpreter and environment the embedded runtime starts with
type RawFunc ¶
Go function over the borrowed argument tuple and keyword dict, 0 for no keywords, that returns a new reference
type Ref ¶
type Ref struct {
// contains filtered or unexported fields
}
Owned reference held by Go, usable from any goroutine and released under the GIL
func (*Ref) Await ¶
Runs the referenced awaitable on the loop thread and waits without holding the GIL, a plain value converting as it is
func (*Ref) AwaitIn ¶
Runs the referenced awaitable as a task created inside the contextvars context pyctx and returns its raw result
func (*Ref) CallAsync ¶
Calls an async callable, runs the awaitable on the loop thread and waits without holding the GIL until it finishes or ctx ends
func (*Ref) CallLocked ¶
Calls the referenced callable on the current thread while the GIL is already held
func (*Ref) CallMethod ¶
Calls a method of the referenced object from any goroutine
func (*Ref) GetAttrLocked ¶
Reads an attribute while the caller already holds the GIL
func (*Ref) Ptr ¶
Borrowed pointer of the referenced object, valid while the Ref is held, 0 for a nil Ref
type SymbolError ¶
C API function or data symbol the loaded libpython does not export
func (*SymbolError) Error ¶
func (e *SymbolError) Error() string
The missing symbol and the loader's reason when it gave one
func (*SymbolError) Unwrap ¶
func (e *SymbolError) Unwrap() error
Loader failure behind the missing symbol