Categorygithub.com/voutilad/gogopython
modulepackage
0.17.0
Repository: https://github.com/voutilad/gogopython.git
Documentation: pkg.go.dev

# README

gogopython

Build and Test

A redpanda & a python sipping tea together as friends.

Python, but in Go.

CGO_ENABLED=0 go build

Heads up: this currently requires Python 3.12. No if's, and's or but's.

Using

gogopython merely exposes the Python C-Api, so you'll need to use it just like you would without Go. For an example of spinning up a sub-interpreter, see the example program in cmd/main.go.

Library Detection

The biggest pain is finding the Python dynamic library. On some Linux systems, it must be installed separately (Debian-based distros for sure).

sudo apt install libpython3.12

gogopython will try to find the library using distutils via the given Python binary. This may require installing setuptools via pip.

Quick command line test

Simply run the example program via go run example/example.go or, if python3 is not in your path, provide it as a command line argument. For example, using a virtual environment might look like:

# Create and activate virtual environment.
python3 -m venv venv
. venv/bin/activate

# Install setuptools. This is used for library discovery.
pip install setuptools

# We no longer need the virtual environment enabled.
deactivate

# Run the test app.
go run example/example.go ./venv/bin/python3

Note: if on Linux, make sure you have setuptools installed.

Known Issues

  • Requires Python 3.12 as it uses sub-interpreters. Sorry, not sorry.

  • Linux requires a shim using the ffi Go module that uses purego to leverage libffi, so on Linux libffi must be available. This is all because some super old Python C API functions decide to return a struct on the stack and purego only supports that on macOS currently.

  • The Python API is super thread local storage oriented. Using it with Go is a small nightmare. Gratuitous use of runtime.LockOSThread() is required. gogopython does not enforce this behavior.

  • Not all of the C API is wrapped and is being wrapped incrementally as needed.

# Packages

No description provided by the author
No description provided by the author

# Functions

BaseType identifies the Python base type from a Python *PyObject.
FindPythonHomeAndPaths uses the provided Python executable to discovery the Python Home and path settings.
LoadLibrary attempts to load and wrap the appropriate dynamic library for embedding Python, given a particular Python binary.
NewFunction creates a new Python function object, with the given name, that calls the provided Go func.
UnicodeToString converts a Python Unicode object (i.e.
WCharToString copies out a Python *wchar_t to a Go string.

# Constants

No description provided by the author
No description provided by the author
Python bytes (not bytearray).
On Python 3.12, defaults to SharedGil.
Python dictionary.
Python float.
Python function.
Python generator.
No description provided by the author
Python list.
Python long.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Python module.
The Python "None" type.
No optimization, __debug__ is True.
NullInterpreterState is a NULL version of a Python InterpreterState.
NullPyCodeObjectPtr represents a NULL pointer to a Python code object.
NullPyObjectPtr represents a NULL pointer to a Python PyObject.
NullPyTypeObjectPtr represents a NULL pointer to a Python PyTypeObject.
NullThreadState is a NULL version of a Python ThreadState.
On Python 3.12 and newer, creates a unique GIL.
PyCFunctionDefault has the C function signature: PyObject *PyCFunction(PyObject *self, PyObject *args).
PyCFunctionFast has the C function signature: PyObject *fn(PyObject *self, PyObject *const *args, Py_ssize_t nargs).
PyCFunctionWithKeywords has the C function signature: PyObject *fn(PyObject *self, PyObject *args, PyObject *kwargs).
Used for expressions(?).
Used for modules (many statements).
No idea what this is!.
No description provided by the author
No description provided by the author
No description provided by the author
Default with debug hooks.
Use defaults allocators.
Use malloc(3).
Use malloc(3) with debug hooks.
Don't change allocator (use defaults).
Use Python's pymalloc.
Use Python's pymalloc with debug hooks.
Used for single statements.
__debug__ is False, no asserts.
__debug__ is False, no asserts, no docstrings.
No description provided by the author
Python set.
On Python 3.12 and newer, uses unified GIL.
No description provided by the author
Python Unicode string.
Python tuple.
We have no idea what the type is...
Uses whatever the interpreter was built with.
No description provided by the author

# Variables

Py_CompileString is a simplified form of Py_CompileStringFlags using default compiler flags.
Py_CompileStringExFlags compiles the Python script in str and returns the compiled Python code object.
Py_CompileStringFlags is a simplified form of Py_CompileStringExFlags with optimizations set to UseInterpreterLevel.
Py_DecodeLocale converts a Go string into a Python *wchar_t, optionally storing some error information in the provided index (if non-nil).
No description provided by the author
Py_EncodeLocale converts a Python *wchar_t to a C char*, optionally storing some error information in the provided index (if non-nil).
Py_EndInterpreter tears down a sub-interpreter using the provided thread state.
Py_FinalizeEx tears down the global Python interpreter state.
No description provided by the author
Our problem children.
Our problem children.
Our problem children.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PyConfig_Clear clears set values in a given PyConfig_3_12.
PyConfig_InitIsolatedPythonConfig initializes the provided Python interpreter config using "isolated" defaults.
PyConfig_InitPythonConfig initializes the provided Python interpreter config using defaults.
Our problem children.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PyGILState_Check reports whether the caller has the GIL.
PyGILState_Ensure takes a reference to the GIL.
PyGILState_Release releases a reference to the GIL.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PyPreConfig_InitIsolatedConfig pre-initializes the provided Python interpreter config using "isolated" defaults.
PyRun_SimpleString evaluates the given Python script in the current interpreter, returning an exit code based on if there was a Python exception raised.
PyRun_String evaluates a given Python script in the current interpreter using the given StartToken mode and globals/locals dicts.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
PyConfig_3_12 configures a Python 3.12 interpreter.
PyInterpreterConfig defines settings for a sub-interpreter.
PyMethodDef describes a C function made callable from Python.
No description provided by the author
PyStatus is returned by some Python C API calls.

# Type aliases

No description provided by the author
No description provided by the author
MethodFlags bits indicate how the method call is constructed.
No description provided by the author
PyCFunction points to a C function implementation.
PyCodeObjectPtr is a pointer to an underlying Python code object.
PyGILState is used sort as a cookie in the ensure/release function calls.
PyInterpreterStatePtr is a pointer to a Python InterpreterState.
No description provided by the author
PyObjectPtr is a pointer to an underlying PyObject instance.
No description provided by the author
PythonLibraryPtr is a pointer to a Python dynamic library state.
PyThreadStatePtr is a pointer to a Python ThreadState.
PyTypeObjectPtr is a pointer to an underlying PyTypeObject instance.
StartToken (confusingly named) is used to dictate to the Python interpreter and compiler how to evaluate a Python script.
Type describes the native Python type of PyObject.