Skip to content

Commit

Permalink
Merge pull request #5 from qiniu/develop
Browse files Browse the repository at this point in the history
Release v1.0.01
  • Loading branch information
xushiwei committed Mar 11, 2013
2 parents 22f26e6 + 0be54b5 commit a43d94c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: go
before_install:
- sudo apt-get install -qq python2.7-dev

11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#CHANGELOG

## v1.0.01

2013-03-10

Issue [#5](https://github.com/qiniu/py/pull/5):

- 增加 gomodule 样例
- Travis-CI 支持

87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,89 @@
py
==

Golang bindings to the CPython C-API
Golang bindings to the CPython C-API

# Summary

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

```
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()
}
// -------------------------------------------------------------------
```

67 changes: 67 additions & 0 deletions examples/gomodule/gomodule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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()
}

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

2 changes: 1 addition & 1 deletion python.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package py

/*
#cgo CFLAGS: -Werror -I/usr/local/include/python2.7
#cgo CFLAGS: -Werror -I/usr/local/include/python2.7 -I/usr/include/python2.7
#cgo LDFLAGS: -lpython2.7
#include <Python.h>
Expand Down

0 comments on commit a43d94c

Please sign in to comment.