https://github.com/goplus/py
Golang bindings to the CPython C-API
https://github.com/goplus/py
Last synced: 2 months ago
JSON representation
Golang bindings to the CPython C-API
- Host: GitHub
- URL: https://github.com/goplus/py
- Owner: goplus
- License: mit
- Created: 2013-03-10T11:16:16.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2024-06-07T00:17:38.000Z (about 1 year ago)
- Last Synced: 2025-03-30T01:06:34.620Z (2 months ago)
- Language: Go
- Size: 54.7 KB
- Stars: 323
- Watchers: 36
- Forks: 57
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
py - Golang bindings to the CPython C-API
==**NOTE**: This project is **unmaintained**. Maybe [https://github.com/go-python/cpy3](https://github.com/go-python/cpy3) is a good replacement.
py is Golang bindings to the CPython C-API.
py project's homepage is: https://github.com/qiniu/py
# Install
```
go get github.com/qiniu/py
```# Example
```{go}
package mainimport (
"fmt"
"github.com/qiniu/log"
"github.com/qiniu/py"
)// -------------------------------------------------------------------
type FooModule struct {
}func (r *FooModule) Py_bar(args *py.Tuple) (ret *py.Base, err error) {
var i int
var s string
err = py.Parse(args, &i, &s)
if err != nil {
return
}
fmt.Println("call foo.bar:", i, s)
return py.IncNone(), nil
}func (r *FooModule) Py_bar2(args *py.Tuple) (ret *py.Base, err error) {
var i int
var s []string
err = py.ParseV(args, &i, &s)
if err != nil {
return
}
fmt.Println("call foo.bar2:", i, s)
return py.IncNone(), nil
}// -------------------------------------------------------------------
const pyCode = `
import foo
foo.bar(1, 'Hello')
foo.bar2(1, 'Hello', 'world!')
`func main() {
gomod, err := py.NewGoModule("foo", "", new(FooModule))
if err != nil {
log.Fatal("NewGoModule failed:", err)
}
defer gomod.Decref()code, err := py.Compile(pyCode, "", py.FileInput)
if err != nil {
log.Fatal("Compile failed:", err)
}
defer code.Decref()mod, err := py.ExecCodeModule("test", code.Obj())
if err != nil {
log.Fatal("ExecCodeModule failed:", err)
}
defer mod.Decref()
}// -------------------------------------------------------------------
```