gogopython

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: Apache-2.0 Imports: 10 Imported by: 2

README

gogopython

Python, but in Go.

CGO_ENABLED=0 go build

Using

gogopython merely exposes the Python C-Api, so you'll need to use it just like you would without Go. For an example of spinning up a sub-interpreter, see the example program in cmd/main.go.

Quick command line test

To run the test program, figure out your python home and paths:

python3 -c 'import sys; print(f"home={sys.prefix}\npaths={sys.path}")'

Run the test program:

go run cmd/main.go [your home] [each path entry]

note: home and path entries are optional, but if you're trying to use a virtualenv you'll need to set them

Known Issues

  • Panics sometimes just happen during library loading, not sure why!

  • Linux requires a shim using the ffi Go module that uses purego to leverage libffi, so on Linux libffi must be available. This is all because some super old Python C API functions decide to return a struct on the stack and purego only supports that on macOS currently.

  • The Python api is super thread local storage oriented. Using it with Go is a small nightmare. Gratuitous use of runtime.LockOSThread() is required.

  • The helper function for finding the Python dynamic library won't work with Python installed via XCode as it's a funky dual-arch binary with some dynamic library funkiness and otool can't resolve the actual Python dylib location (if there even is one!)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Py_DecodeLocale func(string, uint64) WCharPtr
	Py_EncodeLocale func(WCharPtr, uint64) string

	PyPreConfig_InitIsolatedConfig func(*PyPreConfig)

	PyConfig_InitPythonConfig         func(*PyConfig_3_12)
	PyConfig_InitIsolatedPythonConfig func(*PyConfig_3_12)
	PyConfig_Clear                    func(*PyConfig_3_12)

	Py_FinalizeEx func() int32

	Py_EndInterpreter func(PyThreadStatePtr)

	// Check if we have the GIL. 1 if true, 0 if false.
	PyGILState_Check func() int32
	// Take a reference to the GIL. Caution: this is recursive.
	PyGILState_Ensure func() PyGILState
	// Release a reference to the GIL.
	PyGILState_Release func(PyGILState)

	PyEval_AcquireThread func(PyThreadStatePtr)
	PyEval_ReleaseThread func(PyThreadStatePtr)
	PyEval_SaveThread    func() PyThreadStatePtr
	PyEval_RestoreThread func(PyThreadStatePtr)

	PyThreadState_Get            func() PyThreadStatePtr
	PyThreadState_New            func(PyInterpreterStatePtr) PyThreadStatePtr
	PyThreadState_Swap           func(PyThreadStatePtr) PyThreadStatePtr
	PyThreadState_Clear          func(PyThreadStatePtr)
	PyThreadState_Delete         func(PyThreadStatePtr)
	PyThreadState_DeleteCurrent  func()
	PyThreadState_GetInterpreter func(PyThreadStatePtr) PyInterpreterStatePtr

	PyInterpreterState_Get    func() PyInterpreterStatePtr
	PyInterpreterState_GetID  func(PyInterpreterStatePtr) int64
	PyInterpreterState_Clear  func(PyInterpreterStatePtr)
	PyInterpreterState_Delete func(PyInterpreterStatePtr)

	PyRun_SimpleString func(string) int32
	PyRun_String       func(s string, token StartToken, globals, locals PyObjectPtr) PyObjectPtr

	PyModule_New          func(string) PyObjectPtr
	PyModule_AddObjectRef func(module PyObjectPtr, name string, item PyObjectPtr) int32

	PyBool_FromLong func(int) PyObjectPtr

	PyLong_FromLong             func(int) PyObjectPtr
	PyLong_FromUnsignedLong     func(uint) PyObjectPtr
	PyLong_FromLongLong         func(int64) PyObjectPtr
	PyLong_FromUnsignedLongLong func(uint64) PyObjectPtr

	PyTuple_New     func(int64) PyObjectPtr
	PyTuple_SetItem func(tuple PyObjectPtr, pos int64, item PyObjectPtr) int32

	PyDict_New           func() PyObjectPtr
	PyDictProxy_New      func(mapping PyObjectPtr) PyObjectPtr
	PyDict_Clear         func(PyObjectPtr)
	PyDict_SetItem       func(dict, key, val PyObjectPtr) int32
	PyDict_SetItemString func(dict PyObjectPtr, key string, val PyObjectPtr) int

	Py_DecRef func(PyObjectPtr)
	Py_IncRef func(PyObjectPtr)

	PyErr_Clear func()
	PyErr_Print func()
)
View Source
var (
	Py_PreInitialize func(*PyPreConfig) PyStatus

	PyConfig_SetBytesString func(*PyConfig_3_12, *WCharPtr, string) PyStatus
	Py_InitializeFromConfig func(*PyConfig_3_12) PyStatus

	Py_NewInterpreterFromConfig func(state *PyThreadStatePtr, c *PyInterpreterConfig) PyStatus
)
View Source
var TypeStatus = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypeSint32, &ffi.TypePointer, &ffi.TypePointer, &ffi.TypeSint32}[0]}

Functions

func Load_library

func Load_library(exe string) error

func PyBytesToString

func PyBytesToString(b WCharPtr) string

Types

type GilType

type GilType int32
const (
	DefaultGil GilType = 0
	SharedGil  GilType = 1
	OwnGil     GilType = 2
)

type PyConfig_3_12

type PyConfig_3_12 struct {
	ConfigInit int32

	Isolated              int32
	UseEnvironment        int32
	DevMode               int32
	InstallSignalHandlers int32
	UseHashSeed           int32
	HashSeed              uint
	FaultHandler          int32
	TraceMalloc           int32
	PerfProfiling         int32
	ImportTime            int32
	CodeDebugRanges       int32
	ShowRefCount          int32
	DumpRefs              int32
	DumpRefsFile          WCharPtr
	MallocStats           int32
	FilesystemEncoding    WCharPtr
	FilesystemErrors      WCharPtr
	PycachePrefix         WCharPtr
	ParseArgv             int32

	OrigArgv    PyWideStringList
	Argv        PyWideStringList
	XOptions    PyWideStringList
	WarnOptions PyWideStringList

	SiteImport          int32
	BytesWarning        int32
	WarnDefaultEncoding int32
	Inspect             int32
	Interactive         int32
	OptimizationLevel   int32
	ParserDebug         int32
	WriteBytecode       int32
	Verbose             int32
	Quiet               int32
	UserSiteDirectory   int32
	ConfigureCStdio     int32
	BufferedStdio       int32
	StdioEncodings      WCharPtr
	StdioErrors         WCharPtr
	// LegacyWindowsStdio  int32 // if windows
	CheckHashPycsMode WCharPtr
	UseFrozenModules  int32
	SafePath          int32
	IntMaxStrDigits   int32

	/* Path configuration inputs */
	PathConfigWarnings int32
	ProgramName        WCharPtr
	PythonPathEnv      WCharPtr
	Home               WCharPtr
	PlatLibDir         WCharPtr

	/* Path configuration outputs */
	ModuleSearchPathsSet int32
	ModuleSearchPaths    PyWideStringList
	StdlibDir            *byte
	Executable           *byte
	BaseExecutable       *byte
	Prefix               *byte
	BasePrefix           *byte
	ExecPrefix           *byte
	BaseExecPrefix       *byte

	/* Parameter only used by Py_Main */
	SkipSourceFirstLine int32
	RunCommand          *byte
	RunModule           *byte
	RunFilename         *byte

	/* Set by Py_Main */
	SysPath0 *byte

	/* Private Fields */
	InstallImportLib int32
	InitMain         int32
	IsPythonBuild    int32
}

type PyGILState

type PyGILState int32

type PyInterpreterConfig

type PyInterpreterConfig struct {
	UseMainObMalloc            int32
	AllowFork                  int32
	AllowExec                  int32
	AllowThreads               int32
	AllowDaemonThreads         int32
	CheckMultiInterpExtensions int32
	Gil                        GilType
}

type PyInterpreterStatePtr

type PyInterpreterStatePtr uintptr

type PyObjectPtr

type PyObjectPtr uintptr

type PyPreConfig

type PyPreConfig struct {
	ConfigInit        int32
	ParseArgv         int32
	Isolated          int32
	UseEnvironment    int32
	ConfigureLocale   int32
	CoerceCLocale     int32
	CoerceCLocaleWarn int32
	// LegacyWindowsFSEncoding // only on Windows
	Utf8Mode  int32
	DevMode   int32
	Allocator int32
}

type PyStatus

type PyStatus struct {
	Type     int32
	Func     WCharPtr
	ErrMsg   WCharPtr
	ExitCode int32
}

type PyThreadStatePtr

type PyThreadStatePtr uintptr
const NullThreadState PyThreadStatePtr = 0

type PyWideStringList

type PyWideStringList struct {
	Length int64
	Items  uintptr
}

type PythonLibraryPtr

type PythonLibraryPtr = uintptr

type StartToken

type StartToken = int32
const (
	PySingleInput   StartToken = 256 // Used for single statements
	PyFileInput     StartToken = 257 // Used for modules (i.e. many statements)
	PyEvalInput     StartToken = 258 // Used for expressions(?)
	PyFuncTypeInput StartToken = 345 // ??? no idea
)

type WCharPtr

type WCharPtr *byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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