Golang bindings to the CPython C-API
Go C
Latest commit 1331402 Dec 20, 2014 @xushiwei xushiwei Update README.md
Permalink
Failed to load latest commit information.
examples/gomodule gomodule examples Mar 11, 2013
pyutil gopy Mar 10, 2013
.gitignore gopy Mar 10, 2013
.travis.yml s Mar 11, 2013
CHANGELOG.md CHANGELOG May 29, 2013
README.md Update README.md Dec 20, 2014
class.go merge from qiniu/py May 30, 2013
code.go merge from qiniu/py May 30, 2013
code_test.go merge from qiniu/py May 30, 2013
dict.go merge from qiniu/py May 30, 2013
error.go merge from qiniu/py May 30, 2013
exc.go gopy Mar 10, 2013
exception.go gopy Mar 10, 2013
float.go float May 29, 2013
goargs.go merge from qiniu/py May 30, 2013
gofunction.c gopy Mar 10, 2013
gofunction.go merge from qiniu/py May 30, 2013
gofunction.h gopy Mar 10, 2013
gomodule.go merge from qiniu/py May 30, 2013
gomodule_test.go merge from qiniu/py May 30, 2013
goregister.go merge from qiniu/py May 30, 2013
goregister_test.go merge from qiniu/py May 30, 2013
int.go merge from qiniu/py May 30, 2013
long.go merge from qiniu/py May 30, 2013
module.go merge from qiniu/py May 30, 2013
module_test.go merge from qiniu/py May 30, 2013
none.go merge from qiniu/py May 30, 2013
number.go gopy Mar 10, 2013
object.go merge from qiniu/py May 30, 2013
object_test.go merge from qiniu/py May 30, 2013
python.go #cgo pkg-config: python-2.7 May 29, 2013
string.go gopy Mar 10, 2013
tuple.go merge from qiniu/py May 30, 2013
type.go gopy Mar 10, 2013

README.md

Golang bindings to the CPython C-API

Build Status Build Status

Qiniu Logo

py is Golang bindings to the CPython C-API.

py project's homepage is: https://github.com/qiniu/py

Document

See http://godoc.org/github.com/qiniu/py

Install

go get github.com/qiniu/py

Example

package main

import (
    "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()
}

// -------------------------------------------------------------------