bind

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: BSD-3-Clause Imports: 20 Imported by: 8 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bind provides tools to generate bindings to use Go from Python.

Index

Constants

View Source
const (
	// GoHandle is the type to use for the Handle map key, go-side
	GoHandle = "int64"
	// CGoHandle is Handle for cgo files
	CGoHandle = "C.longlong"
	// PyHandle is within python
	PyHandle = "int64_t"
)
View Source
const (

	// 3 = gopy version
	PyBuildPreamble = `` /* 1895-byte string literal not displayed */

	// 3 = specific package name, 4 = spec pkg path, 5 = doc, 6 = imports, 8 = gopy version
	PyWrapPreamble = `` /* 2617-byte string literal not displayed */

	// exe version of preamble -- doesn't need complex code to load _ module
	// 3 = specific package name, 4 = spec pkg path, 5 = doc, 6 = imports, 8 = gopy version
	PyWrapExePreamble = `` /* 528-byte string literal not displayed */

	GoPkgDefs = `` /* 524-byte string literal not displayed */

	// 3 = gencmd, 4 = vm, 5 = libext, 6 = module file name,
	// 7 = windows special declspec hack, 8 = gopy version
	MakefileTemplate = `` /* 980-byte string literal not displayed */

	// 1 = package name, 2 = gopy command, 3 = gencmd, 4 = vm, 5 = libext,
	// 6 = gopy version: cffi needs no %[1]s.c, and none of the CPython flags
	// that building one would.
	MakefileTemplateCFFI = `` /* 705-byte string literal not displayed */

	// exe version of template: 3 = gencmd, 4 = vm, 5 = libext, 8 = gopy version
	MakefileExeTemplate = `` /* 1301-byte string literal not displayed */

)

3 = libcfg, 4 = GoHandle, 5 = CGoHandle, 6 = all imports, 7 = mainstr, 8 = exe pre C, 9 = exe pre go, 10 = gopy version

View Source
const BackendEnvVar = "GOPY_BACKEND"

BackendEnvVar is the environment variable that selects which tool is used to bind the generated cgo shim to CPython.

View Source
const MakefileTemplateCXX = `` /* 883-byte string literal not displayed */

MakefileTemplateCXX is the Makefile for the C++ backends: 1 = package name, 2 = gopy command, 3 = gencmd, 4 = vm, 5 = C++ compiler, 6 = the backend's own make variables (CXXLIBFOUND, CXXLIBFLAGS, CXXLIBSRCS), 7 = C++ compiler arguments, 8 = gopy version, 9 = backend name, 10 = module file name.

Variables

View Source
var ClearGoTLS = false

ClearGoTLS controls whether generated wrappers emit a _gopy_clear_go_tls() call before every CGo entry point (issue #370). It is opt-in and off by default. The clear performs a hardcoded TLS store (movq $0, %gs:0x30 on darwin/amd64, movq $0, %fs:-8 on linux/amd64); with a single gopy extension in the process it is unnecessary, and on glibc + CPython 3.12+ that offset overlaps the TLS slot CPython uses for the current thread state, so the store nulls it and the interpreter segfaults on the first call (issue #395). Loading each extension without RTLD_GLOBAL, done unconditionally, already keeps each runtime's goroutine-pointer TLS local to its own .so.

View Source
var NanobindCXXFlags = []string{"-DNDEBUG", "-DNB_COMPACT_ASSERTIONS", "-fno-strict-aliasing"}

NanobindCXXFlags are the flags nanobind's own build uses for libnanobind (see the comment at the top of its nb_combined.cpp), which the nanobind backend compiles into every module; harmless for the generated .cpp too.

View Source
var NoMake = false

NoMake turns off generation of Makefiles

View Source
var NoWarn = false

NoWarn turns off warnings -- this must be a global as it is relevant during initial package parsing before e.g., thePyGen is present.

View Source
var Packages []*Package

Packages accumulates all the packages processed

View Source
var WindowsOS = false

set this to true if OS is windows

Functions

func CXX added in v0.6.0

func CXX() string

CXX returns the C++ compiler to use: $CXX, or else c++.

func CXXArchive added in v0.6.0

func CXXArchive(name string) string

CXXArchive returns the file name of the static archive the cgo shim for package name builds as.

func CXXArgs added in v0.6.0

func CXXArgs(name, modlib string, pycfg PyConfig, libflags, srcs, ldflags []string) []string

CXXArgs returns the C++ compiler arguments that compile <name>.cpp and srcs (the C++ library's own sources, if any) with libflags (its include directories and flags of its own), and link them with the archive and ldflags (the archive's own link flags, see CgoLDFlags) into modlib.

func CgoLDFlags added in v0.6.0

func CgoLDFlags(dir, tags string, pkgs ...string) ([]string, error)

CgoLDFlags returns the linker flags that the cgo code in pkgs, and in everything they import, asks for: each package's own #cgo LDFLAGS, and the standard library's //go:cgo_ldflag directives (e.g. the frameworks crypto/x509 needs on macOS), as go list finds them when run in dir with build tags tags. go build passes these on to the final link itself, but a c-archive leaves that link to the C++ backends (see CXXArgs).

func CmdStrToMakefile added in v0.3.0

func CmdStrToMakefile(cmdstr string) string

CmdStrToMakefile does what is needed to make the command string suitable for makefiles * removes -output

func ExtModuleName added in v0.5.2

func ExtModuleName(name, libext string, pycfg PyConfig) string

ExtModuleName returns the file name of the extension module _<name>, preferring the interpreter's own suffix (e.g. .cpython-312-x86_64-linux-gnu.so) over libext.

func GenPyBind added in v0.3.0

func GenPyBind(mode BuildMode, libext string, lang int, dynamicLink bool, cfg *BindCfg) error

GenPyBind generates a .go file, build.py file to enable pybindgen to create python bindings, and wrapper .py file(s) that are loaded as the interface to the package with shadow python-side classes mode = gen, build, pkg, exe

func ResetPackages added in v0.3.0

func ResetPackages()

ResetPackages resets any accumulated packages -- needed when doing tests

Types

type Backend added in v0.6.0

type Backend string

Backend names a CPython binding tool.

const (
	BackendPyBindGen Backend = "pybindgen" // default
	BackendCFFI      Backend = "cffi"
	BackendPyBind11  Backend = "pybind11"
	BackendNanobind  Backend = "nanobind"
	BackendCAPI      Backend = "capi"
)

func BackendFromEnv added in v0.6.0

func BackendFromEnv() (Backend, error)

BackendFromEnv returns the backend selected by GOPY_BACKEND. An unset or empty variable selects pybindgen.

func (Backend) EnvPrefix added in v0.6.0

func (b Backend) EnvPrefix() string

EnvPrefix returns the environment assignment that selects b, to put ahead of a gopy command in a generated Makefile recipe, so that make's gen target regenerates code for the same backend the rest of that Makefile builds; it is empty for pybindgen, the default.

func (Backend) ForExe added in v0.6.0

func (b Backend) ForExe() Backend

ForExe returns the backend gopy exe builds with when b is selected: b itself, or capi in place of a plain-C backend. exe mode embeds the Python interpreter into the Go binary via the CPython C API (see goExePreambleC/Go in gen.go), which a plain-C backend's shim avoids. An exe links libpython and calls the C API anyway, which is all capi needs, and capi binds every function a plain-C backend does. An unknown b is returned as is, for GenPyBind to refuse.

type BindCfg added in v0.3.5

type BindCfg struct {
	// output directory for bindings
	OutputDir string
	// name of output package (otherwise name of first package is used)
	Name string
	// code string to run in the go main() function in the cgo library
	Main string
	// the full command args as a string, without path to exe
	Cmd string
	// path to python interpreter
	VM string
	// package prefix used when generating python import statements
	PkgPrefix string
	// rename Go exported symbols to python PEP snake_case
	RenameCase bool
	// gopy version string embedded in this binary, stamped into generated
	// file headers so output can be traced back to the release that produced it
	Version string
	// tool used to bind the cgo shim to CPython, see BackendFromEnv
	Backend Backend
}

BindCfg is a configuration used during binding generation

type BuildMode added in v0.3.0

type BuildMode string
const (
	ModeGen   BuildMode = "gen"
	ModeBuild BuildMode = "build"
	ModeExe   BuildMode = "exe"
	ModePkg   BuildMode = "pkg"
)

type Const

type Const struct {
	// contains filtered or unexported fields
}

func (*Const) Doc

func (c *Const) Doc() string

func (*Const) GoName

func (c *Const) GoName() string

func (*Const) GoType

func (c *Const) GoType() types.Type

func (*Const) ID

func (c *Const) ID() string

type Enum added in v0.3.2

type Enum struct {
	// contains filtered or unexported fields
}

func (*Enum) AddConst added in v0.3.2

func (e *Enum) AddConst(p *Package, o *types.Const) (*Const, error)

func (*Enum) Doc added in v0.3.2

func (e *Enum) Doc() string

func (*Enum) GoName added in v0.3.2

func (e *Enum) GoName() string

func (*Enum) GoType added in v0.3.2

func (e *Enum) GoType() types.Type

func (*Enum) ID added in v0.3.2

func (e *Enum) ID() string

func (*Enum) SortConsts added in v0.3.2

func (e *Enum) SortConsts()

type ErrorList

type ErrorList []error

ErrorList is a list of errors

func (*ErrorList) Add added in v0.3.0

func (list *ErrorList) Add(err error)

func (*ErrorList) Error

func (list *ErrorList) Error() error

type Func

type Func struct {
	// contains filtered or unexported fields
}

Func collects information about a go func/method.

func (*Func) Doc

func (f *Func) Doc() string

func (*Func) GoFmt added in v0.3.0

func (f *Func) GoFmt() string

func (*Func) GoName

func (f *Func) GoName() string

func (*Func) GoType

func (f *Func) GoType() types.Type

func (*Func) ID

func (f *Func) ID() string

func (*Func) Obj added in v0.3.0

func (f *Func) Obj() types.Object

func (*Func) Package

func (f *Func) Package() *Package

func (*Func) Return

func (f *Func) Return() types.Type

func (*Func) Signature

func (f *Func) Signature() *Signature

type Interface added in v0.3.0

type Interface struct {
	// contains filtered or unexported fields
}

Interface collects information about a go interface.

func (*Interface) Doc added in v0.3.0

func (it *Interface) Doc() string

func (*Interface) GoName added in v0.3.0

func (it *Interface) GoName() string

func (*Interface) GoType added in v0.3.0

func (it *Interface) GoType() types.Type

func (*Interface) ID added in v0.3.0

func (it *Interface) ID() string

func (*Interface) Interface added in v0.3.0

func (it *Interface) Interface() *types.Interface

func (*Interface) Package added in v0.3.0

func (it *Interface) Package() *Package

type Map added in v0.3.0

type Map struct {
	// contains filtered or unexported fields
}

Map collects information about a go map.

func (*Map) Doc added in v0.3.0

func (it *Map) Doc() string

func (*Map) GoName added in v0.3.0

func (it *Map) GoName() string

func (*Map) GoType added in v0.3.0

func (it *Map) GoType() types.Type

func (*Map) ID added in v0.3.0

func (it *Map) ID() string

func (*Map) Map added in v0.3.0

func (it *Map) Map() *types.Map

func (*Map) Package added in v0.3.0

func (it *Map) Package() *Package

type Object

type Object interface {
	Package() *Package
	ID() string
	Doc() string
	GoName() string
}

type Package

type Package struct {
	// contains filtered or unexported fields
}

Package ties types.Package and ast.Package together. Package also collects information about specific types (structs, ifaces, etc)

func NewPackage

func NewPackage(pkg *types.Package, doc *doc.Package) (*Package, error)

NewPackage creates a new Package, tying types.Package and ast.Package together.

func (*Package) AddPyImport added in v0.3.2

func (p *Package) AddPyImport(ipath string, extra bool)

add given path to python imports -- these packages were referenced

func (*Package) ImportPath

func (p *Package) ImportPath() string

ImportPath returns the package import path.

func (*Package) Lookup

func (p *Package) Lookup(o types.Object) (Object, bool)

Lookup returns the bind.Object corresponding to a types.Object

func (*Package) Name

func (p *Package) Name() string

Name returns the package name.

type Protocol

type Protocol int

Protocol encodes the various protocols a python type may implement

const (
	ProtoStringer Protocol = 1 << iota
)

type PyConfig added in v0.3.3

type PyConfig struct {
	Version        int
	CFlags         string
	LdFlags        string
	LdDynamicFlags string
	ExtSuffix      string
}

func GetPythonConfig added in v0.3.3

func GetPythonConfig(vm string) (PyConfig, error)

GetPythonConfig returns the needed python configuration for the given python VM (python, python2, python3, pypy, etc...)

func (*PyConfig) AllFlags added in v0.3.3

func (pc *PyConfig) AllFlags() string

AllFlags returns CFlags + " " + LdFlags

type Signature

type Signature struct {
	// contains filtered or unexported fields
}

A Signature represents a (non-builtin) function or method type.

func (*Signature) Params

func (sig *Signature) Params() []*Var

func (*Signature) Recv

func (sig *Signature) Recv() *Var

func (*Signature) Results

func (sig *Signature) Results() []*Var

type Slice added in v0.3.0

type Slice struct {
	// contains filtered or unexported fields
}

Slice collects information about a go slice.

func (*Slice) Doc added in v0.3.0

func (it *Slice) Doc() string

func (*Slice) GoName added in v0.3.0

func (it *Slice) GoName() string

func (*Slice) GoType added in v0.3.0

func (it *Slice) GoType() types.Type

func (*Slice) ID added in v0.3.0

func (it *Slice) ID() string

func (*Slice) Package added in v0.3.0

func (it *Slice) Package() *Package

func (*Slice) Slice added in v0.3.0

func (it *Slice) Slice() *types.Slice

type Struct

type Struct struct {
	// contains filtered or unexported fields
}

Struct collects information about a go struct.

func (*Struct) Doc

func (s *Struct) Doc() string

func (*Struct) FirstEmbed added in v0.3.0

func (s *Struct) FirstEmbed() *symbol

FirstEmbed returns the first field if it is embedded, supporting convention of placing embedded "parent" types first

func (*Struct) GoName

func (s *Struct) GoName() string

func (*Struct) GoType

func (s *Struct) GoType() types.Type

func (*Struct) ID

func (s *Struct) ID() string

func (*Struct) Obj added in v0.3.0

func (s *Struct) Obj() *types.TypeName

func (*Struct) Package

func (s *Struct) Package() *Package

func (*Struct) Struct

func (s *Struct) Struct() *types.Struct

type Type

type Type interface {
	Object
	GoType() types.Type
}

type Var

type Var struct {
	// contains filtered or unexported fields
}

func (*Var) CGoType

func (v *Var) CGoType() string

func (*Var) CType

func (v *Var) CType() string

func (*Var) GoType

func (v *Var) GoType() types.Type

func (*Var) Name

func (v *Var) Name() string

Jump to

Keyboard shortcuts

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