-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.go
71 lines (56 loc) · 1.39 KB
/
run.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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"
import "C"
import "unsafe"
type StartToken int
const (
EvalInput StartToken = iota
FileInput
SingleInput
)
func RunString(code string, start StartToken, globals, locals Object) (Object, error) {
codestr := C.CString(code)
defer C.free(unsafe.Pointer(codestr))
var token C.int
switch start {
case EvalInput:
token = C.Py_eval_input
case FileInput:
token = C.Py_file_input
case SingleInput:
token = C.Py_single_input
}
obj := C.PyRun_StringFlags(codestr, token, c(globals), c(locals), nil)
if obj == nil {
return nil, exception()
}
return newObject(obj), nil
}
func RunFile(filename string, start StartToken, globals, locals Object) (Object, error) {
name := C.CString(filename)
defer C.free(unsafe.Pointer(name))
mode := C.CString("r")
defer C.free(unsafe.Pointer(mode))
var token C.int
switch start {
case EvalInput:
token = C.Py_eval_input
case FileInput:
token = C.Py_file_input
case SingleInput:
token = C.Py_single_input
}
file, err := C.fopen(name, mode)
if file == nil {
return nil, err
}
defer C.fclose(file)
obj := C.PyRun_FileExFlags(file, name, token, c(globals), c(locals), 0, nil)
if obj == nil {
return nil, exception()
}
return newObject(obj), nil
}