Ejemplo n.º 1
0
func stringify(obj Object) string {
	tpyS := C.PyObject_Str(c(obj))
	defer C.decref(tpyS)
	u := C.PyUnicode_AsUTF8String(tpyS)
	defer C.decref(u)
	return C.GoString(C.PyBytes_AsString(u))
}
Ejemplo n.º 2
0
Archivo: code.go Proyecto: MogeiWang/py
func (code *Code) Run(globals, locals *Base) error {
	pyCode := (*C.PyCodeObject)(unsafe.Pointer(code))
	ret := C.PyEval_EvalCode(pyCode, globals.c(), locals.c())
	if ret == nil {
		return exception()
	}
	C.decref(ret)
	return nil
}
Ejemplo n.º 3
0
func (obj *Base) String() string {
	if v, ok := AsString(obj); ok {
		return v.String()
	}
	ret := C.PyObject_Str(obj.c())
	if ret == nil {
		return "<nil>"
	}
	defer C.decref(ret)
	return ((*String)(unsafe.Pointer(ret))).String()
}
Ejemplo n.º 4
0
Archivo: err.go Proyecto: xushiwei/gopy
// Error() returns a string representation of the Python exception represented
// by the Error e.  This is the same as the final line of the Python output from
// an uncaught exception.
func (e *Error) Error() string {
	ts := ""
	en := C.excName(c(e.Kind))
	if en.c == nil {
		tpyS := C.PyObject_Str(c(e.Kind))
		defer C.decref(tpyS)
		ts = C.GoString(C.PyString_AsString(tpyS))
	} else {
		if en.m != nil {
			ts = C.GoString(en.m) + "."
		}
		ts += C.GoString(en.c)
	}

	pyS := C.PyObject_Str(c(e.Value))
	defer C.decref(pyS)
	s := C.GoString(C.PyString_AsString(pyS))

	return fmt.Sprintf("%s: %s", ts, s)
}
Ejemplo n.º 5
0
func (e *Error) release() error {
	if e.Kind != nil {
		e.Kind.Decref()
		e.Value.Decref()
		e.Kind = nil
		e.Value = nil
		if e.tb != nil {
			C.decref(e.tb)
			e.tb = nil
		}
	}
	return nil
}
Ejemplo n.º 6
0
func Import(name string) (*Module, error) {
	s := C.CString(name)
	defer C.free(unsafe.Pointer(s))

	pyName := C.PyString_FromString(s)
	defer C.decref(pyName)

	obj := C.PyImport_Import(pyName)
	if obj == nil {
		return nil, exception()
	}

	return newModule(obj), nil
}
Ejemplo n.º 7
0
func (u *Unicode) String() string {
	if u == nil {
		return "<nil>"
	}
	s := C.PyUnicode_AsUTF8String(c(u))
	if s == nil {
		panic(exception())
	}
	defer C.decref(s)
	ret := C.PyString_AsString(s)
	if ret == nil {
		panic(exception())
	}
	return C.GoString(ret)
}
Ejemplo n.º 8
0
func Raise(err error) {

	var val *C.PyObject
	var exc = C.PyExc_Exception

	e, ok := err.(*Error)
	if ok {
		exc = e.Kind.c()
		val = e.Value.c()
	} else {
		v := NewString(errors.Detail(err))
		val = v.c()
		defer C.decref(val)
	}
	C.PyErr_SetObject(exc, val)
}
Ejemplo n.º 9
0
func (obj *Base) CallMethodObject(name string, args *Tuple) (*Base, error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	f := C.PyObject_GetAttrString(obj.c(), cname)
	if f == nil {
		return nil, AttributeError.Err(name)
	}
	defer C.decref(f)

	if C.PyCallable_Check(f) == 0 {
		return nil, TypeError.Err("attribute of type '%s' is not callable", name)
	}

	ret := C.PyObject_CallObject(f, args.c())
	return obj2ObjErr(ret)
}
Ejemplo n.º 10
0
func (closure *Closure) NewFunction(name string, nin int, doc string) *Base {

	d := &closure.methodDef
	d.ml_name = C.CString(name)
	defer C.free(unsafe.Pointer(d.ml_name))

	if C.setMethod(d, C.int(nin)) != 0 {
		panic("Invalid arguments: nin")
	}
	if doc != "" {
		d.ml_doc = C.CString(doc)
		defer C.free(unsafe.Pointer(d.ml_doc))
	}

	ctx := uintptr(unsafe.Pointer(closure))
	self := C.PyLong_FromLongLong(C.longlong(ctx))
	defer C.decref(self)

	f := C.PyCFunction_NewEx(d, self, nil)
	return (*Base)(unsafe.Pointer(f))
}
Ejemplo n.º 11
0
func (obj *BaseObject) CallMethodObjArgs(name string, args ...Object) (Object, error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	f := C.PyObject_GetAttrString(c(obj), cname)
	if f == nil {
		return nil, AttributeError.Err(name)
	}
	defer C.decref(f)

	if C.PyCallable_Check(f) == 0 {
		return nil, TypeError.Err("attribute of type '%s' is not callable", name)
	}

	t, err := PackTuple(args...)
	if err != nil {
		return nil, err
	}

	ret := C.PyObject_CallObject(f, c(t))
	return obj2ObjErr(ret)
}
Ejemplo n.º 12
0
// Decref decrements obj's reference count, obj may not be nil.
func (obj *Base) Decref() {
	C.decref(obj.c())
}
Ejemplo n.º 13
0
// Decref decrements obj's reference count, obj may not be nil.
func (obj *AbstractObject) Decref() {
	C.decref(c(obj))
}
Ejemplo n.º 14
0
// Decref decrements obj's reference count, obj may be nil.
func Decref(obj Object) {
	if obj != nil {
		C.decref(c(obj))
	}
}