gopy generates a CPython extension module from a go package.
Go to file
Sebastien Binet 9a166327fe bind: handle all types as opaque pointers
Change-Id: I06a021ced190401afd10cda9057238f4399cd60f
2015-09-02 17:47:52 +02:00
_examples Starting to add the ability recognize maps in GO. (Updates #22) 2015-08-31 15:25:37 -07:00
bind bind: handle all types as opaque pointers 2015-09-02 17:47:52 +02:00
.travis.yml travis: try new infrastructure 2015-08-11 21:24:54 +02:00
LICENSE gopy-gen: first import 2015-01-30 18:01:15 +01:00
README.md doc: add 'contribute' blurb 2015-08-31 10:39:02 +02:00
cmd_bind.go gopy: run go-get on package to wrap to handle deps 2015-09-02 17:13:33 +02:00
cmd_gen.go gopy: proper handling of lang option 2015-08-03 14:26:34 +02:00
doc.go all: gopy-gen -> gopy 2015-07-24 16:16:31 +02:00
gen.go bind: discover pkgconfig package name 2015-08-31 10:23:57 +02:00
gopy.py gopy: dont reload C-extension module 2015-08-12 11:44:27 +02:00
main.go all: gopy-gen -> gopy 2015-07-24 16:16:31 +02:00
main_test.go bind: handling of private fields in struct.tp_init 2015-08-31 18:40:47 +02:00
python.go gopy: proper handling of lang option 2015-08-03 14:26:34 +02:00

README.md

gopy

GoDoc Build Status

gopy generates (and compiles) a CPython extension module from a go package.

Installation

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

Documentation

Documentation is available on godoc: https://godoc.org/github.com/go-python/gopy

or directly from the command-line prompt:

$ gopy help
gopy - 

Commands:

    bind        generate and compile (C)Python language bindings for Go
    gen         generate (C)Python language bindings for Go

Use "gopy help <command>" for more information about a command.


$ gopy help gen
Usage: gopy gen <go-package-name>

gen generates (C)Python language bindings for a Go package.

ex:
 $ gopy gen [options] <go-package-name>
 $ gopy gen github.com/go-python/gopy/_examples/hi

Options:
  -lang="python": target language for bindings
  -output="": output directory for bindings


$ gopy help bind
Usage: gopy bind <go-package-name>

bind generates and compiles (C)Python language bindings for a Go package.

ex:
 $ gopy bind [options] <go-package-name>
 $ gopy bind github.com/go-python/gopy/_examples/hi

Options:
  -output="": output directory for bindings

Examples

From the python shell

gopy comes with a little python module allowing to wrap and compile go packages directly from the python interactive shell:

>>> import gopy
>>> hi = gopy.load("github.com/go-python/gopy/_examples/hi")
gopy> inferring package name...
gopy> loading 'github.com/go-python/gopy/_examples/hi'...
gopy> importing 'github.com/go-python/gopy/_examples/hi'

>>> print hi
<module 'github.com/go-python/gopy/_examples/hi' from '/some/path/.../hi.so'>

>>> print hi.__doc__
package hi exposes a few Go functions to be wrapped and used from Python.

From the command line

$ gopy bind -output=out github.com/go-python/gopy/_examples/hi
$ ls out
hi.so

$ cd out
$ python2
>>> import hi
>>> dir(hi)
['Add', 'Concat', 'Hello', 'Hi', 'NewPerson', 'Person', '__doc__', '__file__', '__name__', '__package__']

>>> hi.Hello("you")
hello you from go

You can also run:

go test -v -run=TestBind
=== RUN   TestBind
processing "Add"...
processing "Concat"...
processing "Hello"...
processing "Hi"...
processing "NewPerson"...
processing "Person"...
processing "Add"...
processing "Concat"...
processing "Hello"...
processing "Hi"...
processing "NewPerson"...
processing "Person"...
github.com/go-python/gopy/_examples/hi
_/home/binet/dev/go/root/tmp/gopy-431003574
--- hi.Hi()...
hi from go
--- hi.Hello('you')...
hello you from go
--- hi.Add(1, 41)...
42
--- hi.Concat('4', '2')...
42
--- doc(hi.Person):
Person is a simple struct

--- p = hi.Person()...
<hi.Person object at 0x7fc46cc330f0>
['Age', 'Name', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
--- p.Name: None
--- p.Age: None
--- doc(p):
Person is a simple struct

--- PASS: TestBind (2.13s)
PASS
ok  	github.com/go-python/gopy	2.135s

Limitations

  • wrap go structs into python classes [DONE]
  • better pythonization: turn go errors into python exceptions [DONE]
  • wrap arrays and slices into types implementing tp_as_sequence [DONE]
  • only python-2 supported for now

Contribute

gopy is part of the go-python organization and licensed under BSD-3. When you want to contribute a patch or some code to gopy, please send a pull request against the gopy issue tracker AND a pull request against (go-python/license)[https://github.com/go-python/license] adding yourself to the AUTHORS and CONTRIBUTORS files.