py3

package module
v0.0.0-...-a221c7f Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2022 License: MIT Imports: 8 Imported by: 0

README

go python3 bind

  • python version:3.10.5
  • python version:3.8.10 (default supper win7)

support list

  • cogo-less,It does not need CGO support, so the compilation speed is very fast
  • python3 all plugin,include Native module
  • Automatic type conversion Go to PyObject
  • Automatic type conversion PyObject to Go
  • import Any Python package
  • C function is registered to python, painless interaction

cgo-less

install

go get github.com/aadog/py3-go

test


func main(){
	PyImport_AppendInittab("_test", func() *PyObject {
		m := CreateModule("_test", "aa")
		m.AddFunction("add", func(a, b int) int {
			return a + b
		})
		return m.AsObj()
	})
	cpy3.Py_SetProgramName(os.Args[0])
	cpy3.Py_SetPythonHome("./")
	cpy3.Py_Initialize()
	cpy3.PyRun_SimpleString(`
        import _test
        print(_test.Call('add',1,2))
    `)
}

exception


func main(){
	PyImport_AppendInittab("_test", func() *PyObject {
		m := CreateModule("_test", "aa")
		m.AddFunction("add", func(a, b int) int {
		    py3.PyErr_SetString(py3.UserException(),"raise exception")
			return a + b
		})
		return m.AsObj()
	})
	cpy3.Py_SetProgramName(os.Args[0])
	cpy3.Py_SetPythonHome("./")
	cpy3.Py_Initialize()
	cpy3.PyRun_SimpleString(`
        import _test
        print(_test.Call('add',1,2))
    `)
}

Documentation

Index

Constants

View Source
const (
	PYTHON_API_VERSION = 1013
	PYTHON_API_STRING  = "1013"
)

Variables

View Source
var PyClassInstanceMethodCallDef *cpy3.PyMethodDef
View Source
var PyClassInstanceMethodForwardCallBack = syscall.NewCallback(func(self uintptr, args uintptr) uintptr {
	pyArgs := PyTupleFromInst(args)
	arg1 := pyArgs.GetItem(0)
	tp := arg1.Type()
	defer tp.DecRef()
	if tp.Name() == "str" {
		fmt.Println("static")
	} else {
		pyArgsLen := pyArgs.Size()
		if pyArgsLen < 2 {
			return Py_RETURN_NONE().instance
		}
		ForwardCode := pyArgs.GetItem(1).Str()

		newArgs := PyTupleFromObj(pyArgs.GetSlice(2, pyArgsLen))
		defer newArgs.DecRef()
		pySelf := pyArgs.GetItem(0)

		PyClass := PyClassFromObj(pySelf)
		className := PyClass.GetName()
		ifn, ok := PyClass.GoObj.CallMap.Load(ForwardCode)
		if ok == false {
			PyErr_SetString(UserException(), fmt.Sprintf("%s not find method %s ", className, ForwardCode))
			return Py_RETURN_NONE().Instance()
		}

		return PyClassInstanceMethodForward(pySelf, newArgs, ifn).instance
	}
	return Py_RETURN_NONE().instance
})
View Source
var PyModuleMethodForwardCallBack = syscall.NewCallback(func(self uintptr, args uintptr) uintptr {

	pyArgs := PyTupleFromInst(args)
	pyArgsLen := pyArgs.Size()
	if pyArgsLen < 1 {
		return Py_RETURN_NONE().instance
	}
	ForwardCode := pyArgs.GetItem(0).Str()

	pyModule := PyModuleFromInst(self)
	moduleName := pyModule.GetName()
	ifn, ok := pyModule.GoObj.CallMap.Load(ForwardCode)
	if ok == false {
		PyErr_SetString(UserException(), fmt.Sprintf("%s not find method %s ", moduleName, ForwardCode))
		return Py_RETURN_NONE().Instance()
	}

	newArgs := PyTupleFromObj(pyArgs.GetSlice(1, pyArgsLen))
	defer newArgs.DecRef()

	return PyMethodForward(pyModule, newArgs, ifn).Instance()
})
View Source
var PyNil = PyObjectFromInst(0)
View Source
var SystemClassMap = sync.Map{}
View Source
var SystemModuleMap = sync.Map{}

Functions

func Finalize

func Finalize()

func FinalizeEx

func FinalizeEx() int

func InitGoModuleName

func InitGoModuleName(name string, doc string)

func Initialize

func Initialize()

func IsInitialized

func IsInitialized() int

func NewModuleInitFuncCallBack

func NewModuleInitFuncCallBack(moduleName string, fn func() *PyObject) uintptr

func PyErr_SetString

func PyErr_SetString(tp *PyObject, message string)

func PyImport_AppendInittab

func PyImport_AppendInittab(name string, initFunc func() *PyObject) int

func PyObjectToGo

func PyObjectToGo(o *PyObject, to reflect.Type) any

func PyTypeToGoType

func PyTypeToGoType(p *PyObject) any

func RunAnyFile

func RunAnyFile(pathstr string) int

func RunSimpleFile

func RunSimpleFile(pathstr string) int

func RunSimpleString

func RunSimpleString(command string) int

func SetProgramName

func SetProgramName(name string)

func SetPythonHome

func SetPythonHome(home string)

Types

type CObj

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

func (*CObj) ClearPointer

func (i *CObj) ClearPointer()

func (*CObj) Instance

func (i *CObj) Instance() uintptr

Instance 返回对象实例指针。

Return object instance pointer.

func (*CObj) IsValid

func (i *CObj) IsValid() bool

IsValid 检测地址是否为空。

Check if the address is empty.

func (*CObj) UnsafeAddr

func (i *CObj) UnsafeAddr() unsafe.Pointer

UnsafeAddr 获取一个不安全的地址。

Get an unsafe address.

type ICObj

type ICObj interface {
	Instance() uintptr
}

type IPyObject

type IPyObject interface {
	SetObject(object *PyObject)
}

type PyBool

type PyBool struct {
	PyObject
}

func PyBoolFromInst

func PyBoolFromInst(inst uintptr) *PyBool

PyBoolFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyBoolFromObj

func PyBoolFromObj(obj *PyObject) *PyBool

type PyBytes

type PyBytes struct {
	PyObject
}

func PyBytesFromInst

func PyBytesFromInst(inst uintptr) *PyBytes

PyBytesFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyBytes_FromString

func PyBytes_FromString(s string) *PyBytes

type PyClass

type PyClass struct {
	PyObject
	GoObj *PyClassGoObj
}

func CreateClass

func CreateClass(name string, doc string) *PyClass

func PyClassFromInst

func PyClassFromInst(inst uintptr) *PyClass

PyClassFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyClassFromObj

func PyClassFromObj(obj *PyObject) *PyClass

func (*PyClass) AddFunction

func (p *PyClass) AddFunction(name string, fn interface{})

func (*PyClass) GetName

func (p *PyClass) GetName() string

type PyClassGoObj

type PyClassGoObj struct {
	CallMap sync.Map
}

type PyDict

type PyDict struct {
	PyObject
}

func NewPyDict

func NewPyDict() *PyDict

func PyDictFromInst

func PyDictFromInst(inst uintptr) *PyDict

PyDictFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyDictFromObj

func PyDictFromObj(obj *PyObject) *PyDict

func (*PyDict) Clear

func (p *PyDict) Clear()

func (*PyDict) DelItem

func (p *PyDict) DelItem(key *PyObject) int

func (*PyDict) DelItemString

func (p *PyDict) DelItemString(key string) int

func (*PyDict) GetItem

func (p *PyDict) GetItem(key *PyObject) *PyObject

func (*PyDict) GetItemString

func (p *PyDict) GetItemString(key string) *PyObject

func (*PyDict) Keys

func (p *PyDict) Keys() *PyObject

func (*PyDict) SetItem

func (p *PyDict) SetItem(key *PyObject, val *PyObject) int

func (*PyDict) SetItemString

func (p *PyDict) SetItemString(key string, val *PyObject) int

func (*PyDict) Size

func (p *PyDict) Size() int64

type PyFrame

type PyFrame struct {
	PyObject
}

func PyEval_GetFrame

func PyEval_GetFrame() *PyFrame

func PyFrameFromInst

func PyFrameFromInst(inst uintptr) *PyFrame

PyFrameFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func (*PyFrame) GetBack

func (p *PyFrame) GetBack() *PyObject

func (*PyFrame) GetCode

func (p *PyFrame) GetCode() *PyObject

type PyList

type PyList struct {
	PyObject
}

func NewPyList

func NewPyList(len int64) *PyList

func PyListFromInst

func PyListFromInst(inst uintptr) *PyList

PyListFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyListFromObj

func PyListFromObj(obj *PyObject) *PyList

func (*PyList) Append

func (p *PyList) Append(item *PyObject) int

func (*PyList) GetItem

func (p *PyList) GetItem(index int64) *PyObject

func (*PyList) GetSlice

func (p *PyList) GetSlice(low int64, high int64) *PyObject

func (*PyList) Insert

func (p *PyList) Insert(index int64, item *PyObject) int

func (*PyList) SetItem

func (p *PyList) SetItem(index int64, o *PyObject) int

func (*PyList) Size

func (p *PyList) Size() int64

type PyLong

type PyLong struct {
	PyObject
}

func PyLongFromInst

func PyLongFromInst(inst uintptr) *PyLong

PyLongFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyLongFromLong

func PyLongFromLong(n int) *PyLong

func PyLongFromLongLong

func PyLongFromLongLong(n int64) *PyLong

func PyLongFromObj

func PyLongFromObj(obj *PyObject) *PyLong

func PyLong_FromDouble

func PyLong_FromDouble(n float64) *PyLong

type PyModule

type PyModule struct {
	PyObject
	GoObj *PyModuleGoObj
}
var GoModule *PyModule

func CreateModule

func CreateModule(name string, doc string) *PyModule

func PyImport_Import

func PyImport_Import(name string) *PyModule

func PyModuleFromInst

func PyModuleFromInst(inst uintptr) *PyModule

PyModuleFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyModuleFromObj

func PyModuleFromObj(obj *PyObject) *PyModule

func (*PyModule) AddClass

func (p *PyModule) AddClass(class *PyClass)

func (*PyModule) AddFunction

func (p *PyModule) AddFunction(name string, fn interface{})

func (*PyModule) AddIntConstant

func (p *PyModule) AddIntConstant(name string, value int64) int

func (*PyModule) AddObject

func (p *PyModule) AddObject(name string, value *PyObject) int

func (*PyModule) AddObjectRef

func (p *PyModule) AddObjectRef(name string, value *PyObject) int

func (*PyModule) AddStringConstant

func (p *PyModule) AddStringConstant(name string, value string) int

func (*PyModule) GetDict

func (p *PyModule) GetDict() *PyDict

func (*PyModule) GetName

func (p *PyModule) GetName() string

type PyModuleGoObj

type PyModuleGoObj struct {
	CallMap sync.Map
	// contains filtered or unexported fields
}

type PyNone

type PyNone struct {
	PyObject
}

func PyNoneFromInst

func PyNoneFromInst(inst uintptr) *PyNone

PyNoneFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func Py_RETURN_NONE

func Py_RETURN_NONE() *PyNone

type PyObject

type PyObject struct {
	CObj
}

func GoToPyObject

func GoToPyObject(o interface{}) *PyObject

func NewPyBool

func NewPyBool(b bool) *PyObject

func NewPyBoolFromLong

func NewPyBoolFromLong(l int64) *PyObject

func PyClassInstanceMethodForward

func PyClassInstanceMethodForward(self *PyObject, args *PyTuple, method interface{}) *PyObject

func PyErr_NewException

func PyErr_NewException(name string, base *PyObject, dict *PyObject) *PyObject

func PyEval_GetBuiltins

func PyEval_GetBuiltins() *PyObject

func PyEval_GetGlobals

func PyEval_GetGlobals() *PyObject

func PyEval_GetLocals

func PyEval_GetLocals() *PyObject

func PyExc_Exception

func PyExc_Exception() *PyObject

func PyExc_ValueError

func PyExc_ValueError() *PyObject

func PyMethodForward

func PyMethodForward(self *PyModule, args *PyTuple, method interface{}) *PyObject

func PyObjectFromInst

func PyObjectFromInst(inst uintptr) *PyObject

PyObjectFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func UserException

func UserException() *PyObject

func (*PyObject) AsDouble

func (p *PyObject) AsDouble() float64

func (*PyObject) AsInt

func (p *PyObject) AsInt() int

func (*PyObject) AsLong

func (p *PyObject) AsLong() int

func (*PyObject) AsLongLong

func (p *PyObject) AsLongLong() int64

func (*PyObject) AsObj

func (p *PyObject) AsObj() *PyObject

func (*PyObject) AsUTF8

func (p *PyObject) AsUTF8() string

func (*PyObject) Call

func (p *PyObject) Call(args *PyObject, kwargs *PyObject) *PyObject

func (*PyObject) CallNoArgs

func (p *PyObject) CallNoArgs() *PyObject

func (*PyObject) CallObject

func (p *PyObject) CallObject(args *PyObject) *PyObject

func (*PyObject) DecRef

func (o *PyObject) DecRef()

func (*PyObject) DelAttrString

func (p *PyObject) DelAttrString(attr_name string) int

func (*PyObject) GetAttr

func (p *PyObject) GetAttr(attr_name string) *PyObject

func (*PyObject) GetAttrString

func (p *PyObject) GetAttrString(attr_name string) *PyObject

func (*PyObject) HashAttrString

func (p *PyObject) HashAttrString(attr_name string) int

func (*PyObject) IncRef

func (o *PyObject) IncRef()

func (*PyObject) PyObject_Call

func (p *PyObject) PyObject_Call(args *PyObject, kwargs *PyObject) *PyObject

func (*PyObject) RefCount

func (p *PyObject) RefCount() int

func (*PyObject) SetAttrString

func (p *PyObject) SetAttrString(attr_name string, v *PyObject) int

func (*PyObject) Str

func (p *PyObject) Str() string

func (*PyObject) String

func (p *PyObject) String() string

func (*PyObject) Type

func (p *PyObject) Type() *PyType

type PyTuple

type PyTuple struct {
	PyObject
}

func NewPyTuple

func NewPyTuple(l int64) *PyTuple

func PyTupleFromInst

func PyTupleFromInst(inst uintptr) *PyTuple

PyTupleFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyTupleFromObj

func PyTupleFromObj(obj *PyObject) *PyTuple

func (*PyTuple) Check

func (p *PyTuple) Check() int64

func (*PyTuple) GetItem

func (p *PyTuple) GetItem(pos int64) *PyObject

func (*PyTuple) GetSlice

func (p *PyTuple) GetSlice(low int64, high int64) *PyObject

func (*PyTuple) SetItem

func (p *PyTuple) SetItem(pos int64, o *PyObject) *PyObject

func (*PyTuple) Size

func (p *PyTuple) Size() int64

type PyType

type PyType struct {
	PyObject
}

func PyTypeFromInst

func PyTypeFromInst(inst uintptr) *PyType

PyTypeFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyTypeFromObj

func PyTypeFromObj(obj *PyObject) *PyType

func (*PyType) GetModule

func (p *PyType) GetModule() *PyModule

func (*PyType) Name

func (p *PyType) Name() string

type PyUnicode

type PyUnicode struct {
	PyObject
}

func NewPyUnicode

func NewPyUnicode(s string) *PyUnicode

func PyUnicodeFromInst

func PyUnicodeFromInst(inst uintptr) *PyUnicode

PyUnicodeFromInst 新建一个对象来自已经存在的对象实例指针。

Create a new object from an existing object instance pointer.

func PyUnicode_DecodeFSDefault

func PyUnicode_DecodeFSDefault(s string) *PyUnicode

func (*PyUnicode) GetLength

func (p *PyUnicode) GetLength() int64

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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