Documentation ¶
Index ¶
- Variables
- func AddPythonPath(path string)
- func CheckSignals() bool
- func ClearError()
- func FetchErr() error
- func Finalize()
- func Initialize() error
- func NewPyLong(v int64) *C.PyObject
- func NewPyNone() *C.PyObject
- func PyArgsParseKeywords(args PyArgs, kwargs PyKwargs, kwlist []string) (string, string, string, []string)
- func SetPath(path string)
- type Dict
- type GIL
- type MethFlags
- type Module
- type PyArgs
- type PyGILState
- type PyKwargs
- type PyObject
- func (ob *PyObject) Call(args ...*PyObject) *PyObject
- func (ob *PyObject) CallableArgCount() uint32
- func (ob *PyObject) DecRef()
- func (ob *PyObject) GetAttrString(name string) (*PyObject, error)
- func (ob *PyObject) HasAttr(name string) bool
- func (ob *PyObject) IncRef()
- func (ob *PyObject) Int() int
- func (ob *PyObject) IsCallable() bool
- func (ob *PyObject) IsNull() bool
- func (ob *PyObject) SetAttrString(name string, value *C.PyObject) error
- func (ob *PyObject) String() string
- func (ob *PyObject) StringSlice() []string
- func (ob *PyObject) Time() time.Time
- func (ob *PyObject) Type() string
- func (ob *PyObject) Uint32() uint32
- func (ob *PyObject) Uint64() uint64
- type PyRawObject
- type Tuple
Constants ¶
This section is empty.
Variables ¶
var DefaultMethFlags = MethVarArgs | MethKeyWords
DefaultMethFlags represents the default method flags
var ErrGILInit = errors.New("unable to initialize the GIL")
ErrGILInit indicates that the global interpreter lock failed to initialize
var ErrPyInit = errors.New("couldn't initialize the Python interpreter")
ErrPyInit signals that an error ocurred while initializing the Python interpreter
Functions ¶
func AddPythonPath ¶
func AddPythonPath(path string)
AddPythonPath adds a new path to the PYTHONPATH environment variable.
func FetchErr ¶
func FetchErr() error
FetchErr retrieves the error indicator into three variables whose addresses are passed. If the error indicator is not set, set all three variables to NULL. If it is set, it will be cleared and you own a reference to each object retrieved. The value and traceback object may be NULL even when the type object is not.
func Finalize ¶
func Finalize()
Finalize undos all initializations made by Initialize() and subsequent use of Python/C API functions, and destroys all sub-interpreters that were created and not yet destroyed since the last call to Initialize(). Ideally, this frees all memory allocated by the Python interpreter.
func Initialize ¶
func Initialize() error
Initialize initializes the Python interpreter and its global interpreter lock (GIL).
func PyArgsParseKeywords ¶
func PyArgsParseKeywords(args PyArgs, kwargs PyKwargs, kwlist []string) (string, string, string, []string)
PyArgsParseKeywords parses tuple and keywords arguments.
func SetPath ¶
func SetPath(path string)
SetPath sets the default module search path. If this function is called before Py_Initialize(), then Py_GetPath() won’t attempt to compute a default search path but uses the one provided instead. This is useful if Python is embedded by an application that has full knowledge of the location of all modules.
Types ¶
type Dict ¶
type Dict struct {
*PyObject
}
Dict represents the abstraction over the Python dictionary object.
func NewDictFromObject ¶
NewDictFromObject constructs a new dictionary object from the existing dictionary.
func (*Dict) Get ¶
Get returns the object from dictionary with the provided key. Returns a null object if the key key is not present, but without setting an exception.
type GIL ¶
type GIL struct {
// contains filtered or unexported fields
}
GIL is responsible for interacting with the Python GIL. Goroutines are executed on multiple threads and the scheduler might decide to pause the goroutine on one thread and resume it later in a different thread. This would cause catastrophic effects if the Python interpreter finds out the GIL was acquired in one thread but released in a different one. We have to provide extra safety to avoid runtime crashes at the cost of sacrificing some performance since we'll always stick the goroutine to be scheduled on the same thread.
func (*GIL) Lock ¶
func (g *GIL) Lock()
Lock acquires the GIL lock by ensuring the current thread is ready to call Python C API.
func (*GIL) RestoreThread ¶
func (g *GIL) RestoreThread()
RestoreThread acquire the global interpreter lock (if it has been created and thread support is enabled) and set the thread state to tstate, which must not be NULL. If the lock has been created, the current thread must not have acquired it, otherwise deadlock ensues.
func (*GIL) SaveThread ¶
func (g *GIL) SaveThread()
SaveThread releases the global interpreter lock (if it has been created and thread support is enabled) and reset the thread state to NULL, returning the previous thread state (which is not NULL).
type MethFlags ¶
type MethFlags int
MethFlags is the type alias for the method flags
const ( // MethVarArgs indicates that the method or function accepts positional arguments MethVarArgs MethFlags = C.METH_VARARGS // MethKeyWords indicates that the method or function accepts keyword arguments MethKeyWords MethFlags = C.METH_KEYWORDS // MethNoArgs indicates that the method or function accepts no arguments MethNoArgs MethFlags = C.METH_NOARGS )
type Module ¶
type Module struct { *PyObject // contains filtered or unexported fields }
Module encapsulates the Python module.
func NewModule ¶
NewModule imports a module leaving the globals and locals arguments set to NULL and level set to 0. When the name argument contains a dot (when it specifies a submodule of a package), the fromlist argument is set to the list ['*'] so that the return value is the named module rather than the top-level package containing it as would otherwise be the case. (Unfortunately, this has an additional side effect when name in fact specifies a subpackage instead of a submodule: the submodules specified in the package’s __all__ variable are loaded.) Return a new reference to the imported module, or NULL with an exception set on failure. A failing import of a module doesn't leave the module in sys.modules.
func (*Module) RegisterFn ¶
RegisterFn anchors the function to this module. The callable Python object is built from the method definition that specifies the function name, the args expected by the function and the pointer to the Go callback.
type PyArgs ¶
type PyArgs uintptr
PyArgs represents the alias for the Python positional arguments.
type PyGILState ¶
type PyGILState C.PyGILState_STATE
PyGILState is the type alias for the native Python GIL state
type PyKwargs ¶
type PyKwargs uintptr
PyKwargs represents the alias for the Python keyword arguments.
type PyObject ¶
type PyObject struct {
// contains filtered or unexported fields
}
PyObject is the main abstraction for manipulating the native CPython objects.
func NewPyObjectFromValue ¶
func NewPyObjectFromValue(value interface{}) *PyObject
NewPyObjectFromValue builds a new Python object based on the underlying interface type.
func PyUnicodeFromString ¶
PyUnicodeFromString creates the Python Unicode object from the Go string.
func (*PyObject) Call ¶
Call calls a callable Python object with arguments given by the tuple args. If no arguments are needed, then args may be NULL. Returns the result of the call on success, or a null reference on failure.
func (*PyObject) CallableArgCount ¶
CallableArgCount returns the number of arguments declared in the callable Python object.
func (*PyObject) DecRef ¶
func (ob *PyObject) DecRef()
DecRef decrements the reference count for object o. If the object is NULL, nothing happens. If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL) is invoked.
func (*PyObject) GetAttrString ¶
GetAttrString retrieves an attribute named from object the object. Returns an error if the attribute can't be fetched.
func (*PyObject) IncRef ¶
func (ob *PyObject) IncRef()
IncRef increment the reference count for object o. The object may be NULL, in which case this method has no effect.
func (*PyObject) IsCallable ¶
IsCallable determines if the object is callable.
func (*PyObject) SetAttrString ¶
SetAttrString set the value of the attribute provided for this object to the specified value.
func (*PyObject) String ¶
String encodes an Unicode object and returns the result as a Python bytes object converted to the Go string.
func (*PyObject) StringSlice ¶
StringSlice returns this object as a string slice.
type PyRawObject ¶
PyRawObject is the type alias for the raw Python object pointer.