Documentation
¶
Index ¶
- Variables
- func Forked() bool
- type Borrowed
- 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 Interpreter
- type Kind
- type NamedFunc
- type NoSharedLibraryError
- type Object
- type Options
- type Property
- type PyError
- 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
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("python host is shut down")
ErrClosed is returned once shutdown has begun
var 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")
ErrForkedChild is returned for work that needs other OS threads inside a child created by os.fork
Functions ¶
Types ¶
type Borrowed ¶
type Borrowed uintptr
Borrowed passes an existing object pointer as an argument, the callee takes its own reference
type Exception ¶
Exception is a Go error raised into Python as the named class, a builtin name or module.Class
type GIL ¶
type GIL struct {
// contains filtered or unexported fields
}
GIL proves the goroutine holds an attached thread state of the interpreter
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, while the GIL is held
func (*GIL) ExceptionMatches ¶
Reports 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, containers by value and every other object as an owned *Ref
func (*GIL) HasAttr ¶
Whether the object has the attribute, lookup errors count as absent the way hasattr reports them
func (*GIL) IsAwaitable ¶
Whether inspect.isawaitable reports the object awaitable
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
func (*GIL) NewDocFunc ¶
Native callable named name with ml_doc set to doc, whose leading name(params) line becomes its text signature
func (*GIL) NewRawFunc ¶
Native callable over raw pointers, the PyMethodDef is shared per name and self carries the handle
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 ¶
Python error that re-raises the given exception instance when it reaches Python again
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host owns the one embedded interpreter of the process
func Start ¶
Loads libpython, initializes it and leaves 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, run on the goroutine that called Start
func (*Host) Interpreter ¶
func (h *Host) Interpreter() *Interpreter
Interpreter the host was started from
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
}
Interpreter is a 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
type Kind ¶
type Kind int
Kind classifies an object by the builtin type flags of its class, subclasses included
type NoSharedLibraryError ¶
type NoSharedLibraryError struct {
}
NoSharedLibraryError names an interpreter linked without libpython and the uv commands that supply one
func (*NoSharedLibraryError) Error ¶
func (e *NoSharedLibraryError) Error() string
type Object ¶
type Object interface {
TypeName() (module, name string)
GetAttr(g *GIL, name string) (value any, ok bool, err error)
}
Object is a Go value exposed to Python, attribute lookup reaches GetAttr
type Options ¶
type Options struct {
Interpreter *Interpreter
PycachePrefix string
ExtraPaths []string
Environment bool
Argv []string
}
Options select the interpreter and the environment the embedded runtime starts with
type PyError ¶
type PyError struct {
Type string
Message string
Traceback string
// contains filtered or unexported fields
}
PyError is a Python exception carried into Go with its type, message and formatted traceback
type RawFunc ¶
RawFunc receives the argument tuple and keyword dict as borrowed pointers and returns a new reference
type Ref ¶
type Ref struct {
// contains filtered or unexported fields
}
Ref is an 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 converts
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
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