py

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Linking Python to Go

TODO

Demo

The _demo directory contains our demos (it start with _ to prevent the go command from compiling it):

  • hellopy: link Python to Go and say Hello world
  • clpy: compile Python code and eval.
  • callpy: call Python standard library function math.sqrt.
How to run demos

To run the demos in directory _demo, you need to set the LLGO_LIB_PYTHON environment variable first. Assuming you use Python 3.12, and the libpython3.12.so (or libpython3.12.dylib or python3.12.lib) file is in the /foo/bar directory, then you need to set LLGO_LIB_PYTHON to:

export LLGO_LIB_PYTHON=/foo/bar/python3.12

For example, /opt/homebrew/Frameworks/Python.framework/Versions/3.12/libpython3.12.dylib is a typical python lib location under macOS. So we should set it like this:

export LLGO_LIB_PYTHON=/opt/homebrew/Frameworks/Python.framework/Versions/3.12/python3.12

Then you can run the demos in directory _demo:

cd <demo-directory>  # eg. cd _demo/hellopy
llgo run .

Documentation

Index

Constants

View Source
const (
	LLGoPackage = "link: $LLGO_LIB_PYTHON"
)

Variables

This section is empty.

Functions

func Finalize

func Finalize()

func Initialize

func Initialize()

func InitializeEx

func InitializeEx(initsigs c.Int)

This function works like Initialize() if initsigs is 1. If initsigs is 0, it skips initialization registration of signal handlers, which might be useful when Python is embedded.

func RunSimpleFile

func RunSimpleFile(fp c.FilePtr, filename *c.Char) c.Int

func RunSimpleFileFlags

func RunSimpleFileFlags(fp c.FilePtr, filename *c.Char, flags *CompilerFlags) c.Int

func RunSimpleString

func RunSimpleString(command *c.Char) c.Int

func RunSimpleStringFlags

func RunSimpleStringFlags(command *c.Char, flags *CompilerFlags) c.Int

func SetProgramName

func SetProgramName(name *c.Char)

Types

type CompilerFlags

type CompilerFlags struct {
	CfFlags c.Int
}

llgo:type C

type InputType

type InputType c.Int
const (
	SingleInput InputType = 256 // read code from i/o
	FileInput   InputType = 257 // read code from filename
	EvalInput   InputType = 258 // read code from string

)

type Object

type Object struct {
	Unused [8]byte
}

Object represents a Python object.

func BuildValue

func BuildValue(format *c.Char, __llgo_va_list ...any) *Object

Create a new value based on a format string similar to those accepted by the PyArg_Parse* family of functions and a sequence of values. Returns the value or nil in the case of an error; an exception will be raised if nil is returned. See https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue

func CompileString

func CompileString(str, filename *c.Char, start InputType) *Object

func CompileStringExFlags

func CompileStringExFlags(str, filename *c.Char, start InputType, flags *CompilerFlags, optimize c.Int) *Object

func CompileStringFlags

func CompileStringFlags(str, filename *c.Char, start InputType, flags *CompilerFlags) *Object

func CompileStringObject

func CompileStringObject(str *c.Char, filename *Object, start InputType, flags *CompilerFlags, optimize c.Int) *Object

Parse and compile the Python source code in str, returning the resulting code object. The start token is given by start; this can be used to constrain the code which can be compiled and should be py.EvalInput, py.FileInput, or py.SingleInput. The filename specified by filename is used to construct the code object and may appear in tracebacks or SyntaxError exception messages. This returns NULL if the code cannot be parsed or compiled.

The integer optimize specifies the optimization level of the compiler; a value of -1 selects the optimization level of the interpreter as given by -O options. Explicit levels are 0 (no optimization; __debug__ is true), 1 (asserts are removed, __debug__ is false) or 2 (docstrings are removed too).

func EvalCode

func EvalCode(code, globals, locals *Object) *Object

This is a simplified interface to EvalCodeEx, with just the code object, and global and local variables. The other arguments are set to nil.

func EvalCodeEx

func EvalCodeEx(
	code, globals, locals *Object,
	args *Object, argcount c.Int, kws *Object, kwcount c.Int,
	defs *Object, defcount c.Int, kwdefs, closure *Object) *Object

Evaluate a precompiled code object, given a particular environment for its evaluation. This environment consists of a dictionary of global variables, a mapping object of local variables, arrays of arguments, keywords and defaults, a dictionary of default values for keyword-only arguments and a closure tuple of cells.

func Float

func Float(v float64) *Object

func FloatFromSring

func FloatFromSring(v *Object) *Object

func FromCStr

func FromCStr(s *c.Char) *Object

String returns a new bytes object from a C string.

func FromString

func FromString(s string) *Object

FromString returns a new bytes object from a Go string.

func Import

func Import(name *Object) *Object

This is a higher-level interface that calls the current “import hook function” (with an explicit level of 0, meaning absolute import). It invokes the __import__() function from the __builtins__ of the current globals. This means that the import is done using whatever import hooks are installed in the current environment.

This function always uses absolute imports.

func ImportModule

func ImportModule(name *c.Char) *Object

This is a wrapper around py.Import which takes a const char* as an argument instead of an Object.

func (*Object) CStr

func (o *Object) CStr() *c.Char

CStr returns the content of a bytes object as a C string.

llgo:link (*Object).CStr C.PyBytes_AsString

func (*Object) Call

func (o *Object) Call(args, kwargs *Object) *Object

Call a callable Python object o, with arguments given by the tuple args, and named arguments given by the dictionary kwargs.

args must not be nil; use an empty tuple if no arguments are needed. If no named arguments are needed, kwargs can be nil.

Return the result of the call on success, or raise an exception and return nil on failure.

This is the equivalent of the Python expression: o(*args, **kwargs).

llgo:link (*Object).Call C.PyObject_Call

func (*Object) CallFunction

func (o *Object) CallFunction(format *c.Char, __llgo_va_list ...any) *Object

Call a callable Python object o, with a variable number of C arguments. The C arguments are described using a py.BuildValue style format string. The format can be nil, indicating that no arguments are provided.

Return the result of the call on success, or raise an exception and return nil on failure.

This is the equivalent of the Python expression: o(*args).

Note that if you only pass PyObject* args, (*Object).CallFunctionObjArgs is a faster alternative.

llgo:link (*Object).CallFunction C.PyObject_CallFunction

func (*Object) CallFunctionObjArgs

func (o *Object) CallFunctionObjArgs(__llgo_va_list ...any) *Object

Call a callable Python object o, with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by nil.

Return the result of the call on success, or raise an exception and return nil on failure.

This is the equivalent of the Python expression: o(arg1, arg2, ...).

llgo:link (*Object).CallFunctionObjArgs C.PyObject_CallFunctionObjArgs

func (*Object) CallMethod

func (o *Object) CallMethod(name *c.Char, format *c.Char, __llgo_va_list ...any) *Object

llgo:link (*Object).CallMethod C.PyObject_CallMethod

func (*Object) CallMethodNoArgs

func (o *Object) CallMethodNoArgs(name *Object) *Object

llgo:link (*Object).CallMethodNoArgs C.PyObject_CallMethodNoArgs

func (*Object) CallMethodObjArgs

func (o *Object) CallMethodObjArgs(name *Object, __llgo_va_list ...any) *Object

llgo:link (*Object).CallMethodObjArgs C.PyObject_CallMethodObjArgs

func (*Object) CallMethodOneArg

func (o *Object) CallMethodOneArg(name, arg *Object) *Object

llgo:link (*Object).CallMethodOneArg C.PyObject_CallMethodOneArg

func (*Object) CallNoArgs

func (o *Object) CallNoArgs() *Object

Call a callable Python object callable without any arguments. It is the most efficient way to call a callable Python object without any argument.

Return the result of the call on success, or raise an exception and return nil on failure.

llgo:link (*Object).CallNoArgs C.PyObject_CallNoArgs

func (*Object) CallObject

func (o *Object) CallObject(callable, args *Object) *Object

Call a callable Python object o, with arguments given by the tuple args. If no arguments are needed, then args can be nil.

Return the result of the call on success, or raise an exception and return nil on failure.

This is the equivalent of the Python expression: o(*args).

llgo:link (*Object).CallObject C.PyObject_CallObject

func (*Object) CallOneArg

func (o *Object) CallOneArg(arg *Object) *Object

Call a callable Python object callable with exactly 1 positional argument arg and no keyword arguments.

Return the result of the call on success, or raise an exception and return nil on failure.

llgo:link (*Object).CallOneArg C.PyObject_CallOneArg

func (*Object) Callable

func (o *Object) Callable() c.Int

Determine if the object o is callable. Return 1 if the object is callable and 0 otherwise. This function always succeeds.

llgo:link (*Object).Callable C.PyCallable_Check

func (*Object) DecRef

func (o *Object) DecRef()

llgo:link (*Object).DecRef C.Py_DecRef

func (*Object) Float64

func (o *Object) Float64() float64

llgo:link (*Object).Float64 C.PyFloat_AsDouble

func (*Object) GetAttr

func (o *Object) GetAttr(attrName *Object) *Object

Retrieve an attribute named attrName from object o. Returns the attribute value on success, or nil on failure. This is the equivalent of the Python expression o.attrName.

llgo:link (*Object).GetAttr C.PyObject_GetAttr

func (*Object) GetAttrString

func (o *Object) GetAttrString(attrName *c.Char) *Object

llgo:link (*Object).GetAttrString C.PyObject_GetAttrString

func (*Object) ModuleGetDict

func (m *Object) ModuleGetDict() *Object

Return the dictionary object that implements module’s namespace; this object is the same as the __dict__ attribute of the module object. If module is not a module object (or a subtype of a module object), SystemError is raised and nil is returned.

It is recommended extensions use other Module and Object functions rather than directly manipulate a module’s __dict__.

llgo:link (*Object).ModuleGetDict C.PyModule_GetDict

func (*Object) ModuleLoadSyms

func (m *Object) ModuleLoadSyms(__llgo_va_list ...any)

llgo:link (*Object).ModuleLoadSyms C.llgoLoadPyModSyms

func (*Object) Strlen

func (o *Object) Strlen() uintptr

llgo:link (*Object).Strlen C.PyBytes_Size

func (*Object) Vectorcall

func (o *Object) Vectorcall(args **Object, nargs uintptr, kwnames *Object) *Object

llgo:link (*Object).Vectorcall C.PyObject_Vectorcall

func (*Object) VectorcallDict

func (o *Object) VectorcallDict(args **Object, nargs uintptr, kwdict *Object) *Object

llgo:link (*Object).VectorcallDict C.PyObject_VectorcallDict

func (*Object) VectorcallMethod

func (o *Object) VectorcallMethod(name *Object, args **Object, nargs uintptr, kwnames *Object) *Object

llgo:link (*Object).VectorcallMethod C.PyObject_VectorcallMethod

Directories

Path Synopsis
_demo
callpy command
clpy command
hellopy command

Jump to

Keyboard shortcuts

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