cpython

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMethFlags = MethVarArgs | MethKeyWords

DefaultMethFlags represents the default method flags

View Source
var ErrGILInit = errors.New("unable to initialize the GIL")

ErrGILInit indicates that the global interpreter lock failed to initialize

View Source
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 CheckSignals

func CheckSignals() bool

CheckSignals checks the signal queue.

func ClearError

func ClearError()

ClearError clears the error indicator.

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 NewPyLong

func NewPyLong(v int64) *C.PyObject

func NewPyNone

func NewPyNone() *C.PyObject

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 NewDict

func NewDict() *Dict

NewDict constructs a new empty dictionary object.

func NewDictFromObject

func NewDictFromObject(o *PyObject) *Dict

NewDictFromObject constructs a new dictionary object from the existing dictionary.

func (*Dict) Get

func (d *Dict) Get(k *PyObject) *PyObject

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.

func (*Dict) Insert

func (d *Dict) Insert(k, v *PyObject)

Insert inserts a value into the dictionary indexed with a key. Key must be hashable, otherwise TypeError is raised.

func (*Dict) Object

func (d *Dict) Object() *PyObject

Object returns the underlying Python object reference.

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 NewGIL

func NewGIL() *GIL

NewGIL creates a new instance of the GIL manager.

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) Locked

func (g *GIL) Locked() bool

Locked indicates if the lock on the GIL was acquired.

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).

func (*GIL) Unlock

func (g *GIL) Unlock()

Unlock releases the lock on the GIL.

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

func NewModule(name string) (*Module, error)

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

func (m *Module) RegisterFn(name string, fn interface{}, flags MethFlags) error

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.

func (PyArgs) GetInt

func (args PyArgs) GetInt(n uint8) int

GetInt returns the nth positional argument as an integer.

func (PyArgs) GetSlice

func (args PyArgs) GetSlice(n uint8) ([]interface{}, error)

GetSlice returns the nth argument as a slice of generic values.

func (PyArgs) GetString

func (args PyArgs) GetString(n uint8) string

GetString returns the nth positional argument as a string.

func (PyArgs) GetStringSlice

func (args PyArgs) GetStringSlice(n uint8) ([]string, error)

GetStringSlice returns the nth argument as a slice of string values.

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

func PyUnicodeFromString(s string) *PyObject

PyUnicodeFromString creates the Python Unicode object from the Go string.

func (*PyObject) Call

func (ob *PyObject) Call(args ...*PyObject) *PyObject

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

func (ob *PyObject) CallableArgCount() uint32

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

func (ob *PyObject) GetAttrString(name string) (*PyObject, error)

GetAttrString retrieves an attribute named from object the object. Returns an error if the attribute can't be fetched.

func (*PyObject) HasAttr

func (ob *PyObject) HasAttr(name string) bool

HasAttr determines if the Python object has the specified attribute.

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) Int

func (ob *PyObject) Int() int

Int returns an integer from the raw Python object.

func (*PyObject) IsCallable

func (ob *PyObject) IsCallable() bool

IsCallable determines if the object is callable.

func (*PyObject) IsNull

func (ob *PyObject) IsNull() bool

IsNull determines whether this object's instance is null.

func (*PyObject) SetAttrString

func (ob *PyObject) SetAttrString(name string, value *C.PyObject) error

SetAttrString set the value of the attribute provided for this object to the specified value.

func (*PyObject) String

func (ob *PyObject) String() string

String encodes an Unicode object and returns the result as a Python bytes object converted to the Go string.

func (*PyObject) StringSlice

func (ob *PyObject) StringSlice() []string

StringSlice returns this object as a string slice.

func (*PyObject) Time

func (ob *PyObject) Time() time.Time

Time returns the time from the raw Python object.

func (*PyObject) Type

func (ob *PyObject) Type() string

Type returns the Python type representation.

func (*PyObject) Uint32

func (ob *PyObject) Uint32() uint32

Uint32 returns an uint32 integer from the raw Python object.

func (*PyObject) Uint64

func (ob *PyObject) Uint64() uint64

Uint64 returns an uint64 integer from the raw Python object.

type PyRawObject

type PyRawObject *C.PyObject

PyRawObject is the type alias for the raw Python object pointer.

type Tuple

type Tuple struct {
	*PyObject
}

Tuple represents the Python tuple sequence object.

func NewTuple

func NewTuple(size int) *Tuple

NewTuple constructs a new tuple object of the provided size.

func (*Tuple) Set

func (t *Tuple) Set(pos int, ob *PyObject)

Set inserts a reference to object at specified position of the tuple.

Jump to

Keyboard shortcuts

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