// static inline int complexCheck(PyObject *o) { return PyComplex_Check(o); } import "C" import ( "fmt" "unsafe" ) type Complex struct { AbstractObject NumberProtocol o C.PyComplexObject } // ComplexType is the Type object that represents the Complex type. var ComplexType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyComplex_Type))) func complexCheck(obj Object) bool { return C.complexCheck(c(obj)) != 0 } func newComplex(obj *C.PyObject) *Complex { return (*Complex)(unsafe.Pointer(obj)) } func NewComplex(v complex128) (*Complex, error) { ret := C.PyComplex_FromDoubles(C.double(real(v)), C.double(imag(v))) if ret == nil { return nil, exception() } return newComplex(ret), nil
// static inline int floatCheck(PyObject *o) { return PyFloat_Check(o); } import "C" import ( "fmt" "unsafe" ) type Float struct { AbstractObject NumberProtocol o C.PyFloatObject } // FloatType is the Type object that represents the Float type. var FloatType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyFloat_Type))) func floatCheck(obj Object) bool { return C.floatCheck(c(obj)) != 0 } func newFloat(obj *C.PyObject) *Float { return (*Float)(unsafe.Pointer(obj)) } func NewFloat(v float64) (*Float, error) { ret := C.PyFloat_FromDouble(C.double(v)) if ret == nil { return nil, exception() } return newFloat(ret), nil
import ( "fmt" "unsafe" ) // *Dict represents a Python dictionary. In addition to satisfying the Object // interface, Dict pointers also have a number of methods defined - representing // the PyDict_XXX functions from the Python C API. type Dict struct { AbstractObject o C.PyDictObject } // DictType is the Type object that represents the Dict type. var DictType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyDict_Type))) func dictCheck(obj Object) bool { if obj == nil { return false } return C.dictCheck(c(obj)) != 0 } func newDict(obj *C.PyObject) *Dict { return (*Dict)(unsafe.Pointer(obj)) } // NewDict creates a new empty dictionary. // // Return value: New Reference.
func newObject(obj *C.PyObject) Object { if obj == nil { return nil } o := unsafe.Pointer(obj) if o == unsafe.Pointer(None) { return None } pyType := (*C.PyTypeObject)(obj.ob_type) class, ok := getType(pyType) if ok { ret, ok := obj2Class(class, obj) if ok { return ret } } for pyType.tp_base != nil { pyType = (*C.PyTypeObject)(unsafe.Pointer(pyType.tp_base)) class, ok := getType(pyType) if ok { ret, ok := obj2Class(class, obj) if ok { return ret } } } switch C.getBasePyType(obj) { case &C.PyList_Type: return (*List)(o) case &C.PyTuple_Type: return (*Tuple)(o) case &C.PyDict_Type: return (*Dict)(o) case &C.PyString_Type: return (*String)(o) case &C.PyBool_Type: return newBool(obj) case &C.PyInt_Type: return (*Int)(o) case &C.PyLong_Type: return (*Long)(o) case &C.PyFloat_Type: return (*Float)(o) case &C.PyModule_Type: return (*Module)(o) case &C.PyType_Type: return (*Type)(o) case &C.PyCode_Type: return (*Code)(o) case &C.PyCFunction_Type: return (*CFunction)(o) case &C.PyComplex_Type: return (*Complex)(o) case &C.PyFrozenSet_Type: return (*FrozenSet)(o) case &C.PySet_Type: return (*Set)(o) case &C.PyFunction_Type: return (*Function)(o) } if C.exceptionCheck(obj) != 0 { return newException(obj) } return newBaseObject(obj) }
package py // #include "utils.h" // static inline int unicodeCheck(PyObject *o) { return PyUnicode_Check(o); } import "C" import "unsafe" type Unicode struct { AbstractObject o C.PyUnicodeObject } // UnicodeType is the Type object that represents the Unicode type. var UnicodeType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyUnicode_Type))) func unicodeCheck(obj Object) bool { return C.unicodeCheck(c(obj)) != 0 } func newUnicode(obj *C.PyObject) *Unicode { return (*Unicode)(unsafe.Pointer(obj)) } func NewUnicode(s string) (*Unicode, error) { cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) ret := C.PyUnicode_FromStringAndSize(cs, C.Py_ssize_t(len(s))) if ret == nil { return nil, exception()
import ( "fmt" "unsafe" ) // *Bool is the representation of the Python bool type. There are only two // possible values for a Bool, True and False. Every True value refers to the // same instance, and every False value refers to the same value. type Bool struct { AbstractObject o C.PyObject } // BoolType is the Type object that represents the Bool type. var BoolType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyBool_Type))) // True is the true value of the Bool type. It is a singleton value, all true // values refer to the same instance. var True = (*Bool)(C.pyTrue()) // False is the false value of the Bool type. It is a singleton value, all // false values refer to the same instance. var False = (*Bool)(C.pyFalse()) func boolCheck(obj Object) bool { return C.boolCheck(c(obj)) != 0 } func newBool(obj *C.PyObject) *Bool { if obj == c(True) {
// return ((PyTypeObject *)t)->tp_alloc((PyTypeObject *)t, n); // } // static inline int typeInit(PyObject *t, PyObject *o, PyObject *a, PyObject *k) { // return ((PyTypeObject *)t)->tp_init(o, a, k); // } import "C" import "unsafe" type Type struct { AbstractObject o C.PyTypeObject } // TypeType is the Type object that represents the Type type. var TypeType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyType_Type))) func typeCheck(obj Object) bool { if obj == nil { return false } return C.typeCheck(c(obj)) != 0 } func newType(obj *C.PyObject) *Type { return (*Type)(unsafe.Pointer(obj)) } func (t *Type) Alloc(n int64) (Object, error) { ret := C.typeAlloc(c(t), C.Py_ssize_t(n)) return obj2ObjErr(ret)
// #include "utils.h" // static inline int functionCheck(PyObject *o) { return PyFunction_Check(o); } import "C" import "unsafe" // *Function represents a Python function. In Python this is a function created // using the "def" statement. type Function struct { AbstractObject o C.PyFunctionObject } // FunctionType is the Type object that represents the Function type. var FunctionType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyFunction_Type))) func newFunction(obj *C.PyObject) *Function { return (*Function)(unsafe.Pointer(obj)) } func functionCheck(obj Object) bool { if obj == nil { return false } return C.functionCheck(c(obj)) != 0 } // NewFunction returns a new Function object that is associated with the given // "code" and "globals". "globals" must be a dictionary, and it should hold the // global variables for the function.
// static inline int moduleCheck(PyObject *o) { return PyModule_Check(o); } // static inline int moduleCheckE(PyObject *o) { return PyModule_CheckExact(o); } // static inline void decref(PyObject *obj) { Py_DECREF(obj); } import "C" import ( "unsafe" ) type Module struct { AbstractObject o C.PyObject } // ModuleType is the Type object that represents the Module type. var ModuleType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyModule_Type))) func moduleCheck(obj Object) bool { if obj == nil { return false } return C.moduleCheck(c(obj)) != 0 } func newModule(obj *C.PyObject) *Module { return (*Module)(unsafe.Pointer(obj)) } func Import(name string) (*Module, error) { s := C.CString(name) defer C.free(unsafe.Pointer(s))
import "unsafe" // *Set represents a Python set. In addition to satisfying the Object // interface, Set pointers also have a number of methods defined - representing // the PySet_XXX functions from the Python C API. type Set struct { AbstractObject o C.PySetObject } type FrozenSet struct { Set } // SetType is the Type object that represents the Set type. var SetType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPySet_Type))) // FrozenSetType is the Type object that represents the FrozenSet type. var FrozenSetType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyFrozenSet_Type))) func setCheck(obj Object) bool { return C.setCheck(c(obj)) != 0 } func frozenSetCheck(obj Object) bool { return C.frozenSetCheck(c(obj)) != 0 } func newSet(obj *C.PyObject) *Set { return (*Set)(unsafe.Pointer(obj)) }
import "unsafe" // *BaseObject is the concrete representation of the Python "Object *". It is // used less than in the C API, as the Object interface is mostly used when the // type is not fixed. Any Object "o" can be turned into a *BaseObject using the // Base() method (i.e. o.Base() returns a *BaseObject that refers to the same // underlying Python object as "o"). This allows the Python functions that // accept any type of object to be defined as methods on *BaseObject. type BaseObject struct { AbstractObject o C.PyObject } // BaseType is the Type object that represents the BaseObject type. var BaseType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyBaseObject_Type))) func newBaseObject(obj *C.PyObject) *BaseObject { return (*BaseObject)(unsafe.Pointer(obj)) } // HasAttr returns true if "obj" has the attribute "name". This is equivalent // to the Python "hasattr(obj, name)". func (obj *BaseObject) HasAttr(name Object) bool { ret := C.PyObject_HasAttr(c(obj), c(name)) if ret == 1 { return true } return false }
// static inline long longCheck(PyObject *o) { return PyLong_Check(o); } import "C" import ( "fmt" "unsafe" ) type Long struct { AbstractObject NumberProtocol o C.PyLongObject } // LongType is the Type object that represents the Long type. var LongType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyLong_Type))) func longCheck(obj Object) bool { if obj == nil { return false } return C.longCheck(c(obj)) != 0 } func newLong(obj *C.PyObject) *Long { return (*Long)(unsafe.Pointer(obj)) } func NewLong(i int64) *Long { return newLong(C.PyLong_FromLongLong(C.PY_LONG_LONG(i))) }