naive go bindings to the CPython C-API
Go C Other
Permalink
Failed to load latest commit information.
cmd/go-python reorganize code to make "go get" and friends happier Feb 8, 2013
tests tests/modify-values: first import Aug 5, 2016
.travis.yml travis: add gopath/bin to PATH Feb 4, 2014
LICENSE add python2 license + separate the python file into separate ones Nov 18, 2010
Makefile make: install via 'go get' Nov 26, 2014
README.md doc: add drone.io badge May 16, 2014
capi.go all: introduce go-python.{h,c} Sep 1, 2015
cgoflags.go all: introduce go-python.{h,c} Sep 1, 2015
dict.go fix PyDict_Next, C function actually returns bool Sep 1, 2017
exceptions.go fixes to make tests pass on windows Jan 26, 2017
exceptions_posix.go restore build on posix Jan 26, 2017
go-python.c object: implement PyObject_Call{Function,Method} Sep 1, 2015
go-python.go object: implement PyObject_Call{Function,Method} Sep 1, 2015
go-python.h object: implement PyObject_Call{Function,Method} Sep 1, 2015
heap.go all: introduce go-python.{h,c} Sep 1, 2015
none.go all: introduce go-python.{h,c} Sep 1, 2015
numeric.go numeric: use float64 for PyFloat Aug 9, 2016
object.go fix PyDict_Next, C function actually returns bool Sep 1, 2017
object_posix.go fixes to make tests pass on windows Jan 26, 2017
object_windows.go fixes to make tests pass on windows Jan 26, 2017
otherobjects.go other: add ability to iterate an iterable Jan 16, 2017
python.go added some threading related functions Apr 1, 2016
python_test.go tests/modify-values: first import Aug 5, 2016
sequence.go all: introduce go-python.{h,c} Sep 1, 2015
type.go all: introduce go-python.{h,c} Sep 1, 2015
utilities.go all: introduce go-python.{h,c} Sep 1, 2015
veryhigh.go veryhigh: add defer to fclose in PyRun_SimpleFile May 19, 2016

README.md

go-python

Build Status

Naive go bindings towards the C-API of CPython-2.

this package provides a go package named "python" under which most of the PyXYZ functions and macros of the public C-API of CPython have been exposed.

theoretically, you should be able to just look at:

http://docs.python.org/c-api/index.html

and know what to type in your go program.

this package also provides an executable "go-python" which just loads "python" and then call python.Py_Main(os.Args). the rational being that under such an executable, go based extensions for C-Python would be easier to implement (as this usually means calling into go from C through some rather convoluted functions hops)

Install

With Go 1 and the go tool, cgo packages can't pass anymore additional CGO_CFLAGS from external programs (except pkg-config) to the "fake" #cgo preprocessor directive.

go-python now uses pkg-config to get the correct location of headers and libraries. Unfortunately, the naming convention for the pkg-config package is not standardised across distributions and OSes, so you may have to edit the cgoflags.go file accordingly.

 $ go get github.com/sbinet/go-python

If go get + pkg-config failed:

 $ cd go-python
 $ edit cgoflags.go
 $ make VERBOSE=1

Note: you'll need the proper header and python development environment. On Debian, you'll need to install the python-all-dev package

Documentation

Is available on godoc:

http://godoc.org/github.com/sbinet/go-python

Example:

package main

import "fmt"
import "github.com/sbinet/go-python"

func init() {
   err := python.Initialize()
   if err != nil {
          panic(err.Error())
   } 
}

func main() {
 	 gostr := "foo" 
	 pystr := python.PyString_FromString(gostr)
	 str := python.PyString_AsString(pystr)
	 fmt.Println("hello [", str, "]")
}
$ go run ./main.go
hello [ foo ]

TODO:

  • fix handling of integers (I did a poor job at making sure everything was ok)

  • add CPython unit-tests

  • do not expose C.FILE pointer and replace it with os.File in "go-python" API

  • provide an easy way to extend go-python with go based extensions

  • think about the need (or not) to translate CPython exceptions into go panic/recover mechanism

  • use SWIG to automatically wrap the whole CPython api ?