Documentation
¶
Overview ¶
Package bind provides tools to generate bindings to use Go from Python.
Index ¶
- Constants
- Variables
- func CXX() string
- func CXXArchive(name string) string
- func CXXArgs(name, modlib string, pycfg PyConfig, libflags, srcs, ldflags []string) []string
- func CgoLDFlags(dir, tags string, pkgs ...string) ([]string, error)
- func CmdStrToMakefile(cmdstr string) string
- func ExtModuleName(name, libext string, pycfg PyConfig) string
- func GenPyBind(mode BuildMode, libext string, lang int, dynamicLink bool, cfg *BindCfg) error
- func ResetPackages()
- type Backend
- type BindCfg
- type BuildMode
- type Const
- type Enum
- type ErrorList
- type Func
- type Interface
- type Map
- type Object
- type Package
- type Protocol
- type PyConfig
- type Signature
- type Slice
- type Struct
- type Type
- type Var
Constants ¶
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" )
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
const BackendEnvVar = "GOPY_BACKEND"
BackendEnvVar is the environment variable that selects which tool is used to bind the generated cgo shim to CPython.
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 ¶
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.
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.
var NoMake = false
NoMake turns off generation of Makefiles
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.
var Packages []*Package
Packages accumulates all the packages processed
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
CXXArchive returns the file name of the static archive the cgo shim for package name builds as.
func CXXArgs ¶ added in v0.6.0
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
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
CmdStrToMakefile does what is needed to make the command string suitable for makefiles * removes -output
func ExtModuleName ¶ added in v0.5.2
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
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.
func BackendFromEnv ¶ added in v0.6.0
BackendFromEnv returns the backend selected by GOPY_BACKEND. An unset or empty variable selects pybindgen.
func (Backend) EnvPrefix ¶ added in v0.6.0
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
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 Enum ¶ added in v0.3.2
type Enum struct {
// contains filtered or unexported fields
}
func (*Enum) SortConsts ¶ added in v0.3.2
func (e *Enum) SortConsts()
type Func ¶
type Func struct {
// contains filtered or unexported fields
}
Func collects information about a go func/method.
type Interface ¶ added in v0.3.0
type Interface struct {
// contains filtered or unexported fields
}
Interface collects information about a go interface.
type Map ¶ added in v0.3.0
type Map struct {
// contains filtered or unexported fields
}
Map collects information about a go map.
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 ¶
NewPackage creates a new Package, tying types.Package and ast.Package together.
func (*Package) AddPyImport ¶ added in v0.3.2
add given path to python imports -- these packages were referenced
func (*Package) ImportPath ¶
ImportPath returns the package import path.
type Protocol ¶
type Protocol int
Protocol encodes the various protocols a python type may implement
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
GetPythonConfig returns the needed python configuration for the given python VM (python, python2, python3, pypy, etc...)
type Signature ¶
type Signature struct {
// contains filtered or unexported fields
}
A Signature represents a (non-builtin) function or method type.
type Slice ¶ added in v0.3.0
type Slice struct {
// contains filtered or unexported fields
}
Slice collects information about a go slice.
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct collects information about a go struct.
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