-
Notifications
You must be signed in to change notification settings - Fork 9
/
code.go
46 lines (37 loc) · 1.05 KB
/
code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
// static inline int codeCheck(PyObject *o) { return PyCode_Check(o); }
import "C"
import "unsafe"
type Code struct {
AbstractObject
o C.PyCodeObject
}
// CodeType is the Type object that represents the Code type.
var CodeType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyCode_Type)))
func newCode(obj *C.PyObject) *Code {
return (*Code)(unsafe.Pointer(obj))
}
func CompileFile(name string) (*Code, error) {
fn := C.CString(name)
defer C.free(unsafe.Pointer(fn))
ret := C.compileFile(fn)
if ret == nil {
return nil, exception()
}
return newCode(ret), nil
}
func codeCheck(obj Object) bool {
if obj == nil {
return false
}
return C.codeCheck(c(obj)) != 0
}
func (code *Code) Eval(globals, locals Object) (Object, error) {
pyCode := (*C.PyObject)(unsafe.Pointer(code))
ret := C.PyEval_EvalCode(pyCode, c(globals), c(locals))
return obj2ObjErr(ret)
}