goserpent

command module
v0.0.0-...-56dbff9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: BSD-3-Clause Imports: 18 Imported by: 0

README

goserpent

goserpent generates CPython wrappers from Go functions. The wrapped Go functions can then be directly called from Python.

This project can be seen as a simplified version of gopy. goserpent can only export Go function, and the Go code can directly interact with PyObject structures. gopy exports Go functions and strutures, and is much more mature than goserpent. Use goserpent if you need to export Go functions directly interacting with PyObject structures. Otherwise gopy is a better choice.

This project has been successfully tested with Go 1.21 and Python 3.10.

The textfile.go shows a few example of exported Go functions.

Installation

$ go install github.com/fabgeyer/goserpent@latest

Usage

To export a Go function to Python, add go:pyexport in the function's comment:

// go:pyexport
func ExampleFunction(arg int) int {
	println(arg)
	return arg * arg
}

Functions directly returning a *C.PyObject are automatically exported and the go:pyexport is optional.

Generate the CPython wrappers for the Go functions using:

$ goserpent <filename.go>

Then compile the CPython module:

$ go build -buildmode=c-shared -o gomodule.so

The Go functions can be called from Python using it's snakecase name:

from gomodule import example_function
example_function()

goserpent also supports functions returning an error. If the function returns an error, a Python runtime error is thrown when called from Python.

// go:pyexport
func ExampleFunctionWithError(arg int) (int, error) {
	if arg == 0 {
		// Will throw a runtime error when called from Python
		return 0, errors.New("Invalid argument")
	}
	println(arg)
	return arg * arg, nil
}

Limitations

  • goserpent only supports a small subset of native Go types for the function's argument and return types.
  • goserpent does not export Go structures to Python.
  • goserpent does not directly generate and compile a Python package.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL