Skip to content

Commit 0964304

Browse files
committed
py: first import
0 parents  commit 0964304

File tree

11 files changed

+1016
-0
lines changed

11 files changed

+1016
-0
lines changed

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright ©2015 The go-python Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are met:
5+
* Redistributions of source code must retain the above copyright
6+
notice, this list of conditions and the following disclaimer.
7+
* Redistributions in binary form must reproduce the above copyright
8+
notice, this list of conditions and the following disclaimer in the
9+
documentation and/or other materials provided with the distribution.
10+
* Neither the name of the gonum project nor the names of its authors and
11+
contributors may be used to endorse or promote products derived from this
12+
software without specific prior written permission.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
py
2+
==
3+
4+
`py` is a high-level API wrapping the low-level `CPython` C-API, for `go`.
5+
6+
## Installation
7+
8+
```sh
9+
$ go get github.com/go-python/py
10+
```
11+
12+
13+
## Documentation
14+
15+
Documentation is available on [godoc](https://godoc.org):
16+
17+
[github.com/go-python/py](https://godoc.org/github.com/go-python/py)
18+
19+
20+
## Examples
21+
22+
```go
23+
package main
24+
25+
import (
26+
"fmt"
27+
28+
"github.com/go-python/py"
29+
)
30+
31+
func init() {
32+
err := py.Initialize()
33+
if err != nil {
34+
panic(err)
35+
}
36+
}
37+
38+
func main() {
39+
gostr := "foo"
40+
pystr := py.NewString(gostr)
41+
fmt.Printf("hello [%v]\n", pystr)
42+
}
43+
```
44+
45+
```sh
46+
$ go run ./main.go
47+
hello [foo]
48+
```

_examples/py-hello/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/go-python/py"
11+
)
12+
13+
func init() {
14+
err := py.Initialize()
15+
if err != nil {
16+
panic(err)
17+
}
18+
}
19+
20+
func main() {
21+
gostr := "foo"
22+
pystr := py.NewString(gostr)
23+
fmt.Printf("hello [%v]\n", pystr)
24+
}

bind/bind.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2015 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package bind
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"go/token"
11+
"io"
12+
"os"
13+
14+
"golang.org/x/tools/go/types"
15+
)
16+
17+
// ErrorList is a list of errors
18+
type ErrorList []error
19+
20+
func (list ErrorList) Error() string {
21+
buf := new(bytes.Buffer)
22+
for i, err := range list {
23+
if i > 0 {
24+
buf.WriteRune('\n')
25+
}
26+
io.WriteString(buf, err.Error())
27+
}
28+
return buf.String()
29+
}
30+
31+
// GenCPython generates a (C)Python package from a Go package
32+
func GenCPython(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
33+
buf := new(bytes.Buffer)
34+
gen := &cpyGen{
35+
printer: &printer{buf: buf, indentEach: []byte("\t")},
36+
fset: fset,
37+
pkg: pkg,
38+
}
39+
err := gen.gen()
40+
if err != nil {
41+
return err
42+
}
43+
44+
_, err = io.Copy(w, gen.buf)
45+
46+
return err
47+
}
48+
49+
// GenGo generates a cgo package from a Go package
50+
func GenGo(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
51+
buf := new(bytes.Buffer)
52+
gen := &goGen{
53+
printer: &printer{buf: buf, indentEach: []byte("\t")},
54+
fset: fset,
55+
pkg: pkg,
56+
}
57+
err := gen.gen()
58+
if err != nil {
59+
return err
60+
}
61+
62+
_, err = io.Copy(w, gen.buf)
63+
64+
return err
65+
}
66+
67+
const (
68+
doDebug = true
69+
)
70+
71+
func debugf(format string, args ...interface{}) (int, error) {
72+
if doDebug {
73+
return fmt.Fprintf(os.Stderr, format, args...)
74+
}
75+
return 0, nil
76+
}

0 commit comments

Comments
 (0)