-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathlegacy.go
69 lines (64 loc) · 2.16 KB
/
legacy.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
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package compile
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/stdlib/marshal"
)
// Compile with python3.4 - not used any more but keep for the moment!
// Compile(source, filename, mode, flags, dont_inherit) -> code object
//
// Compile the source string (a Python module, statement or expression)
// into a code object that can be executed by exec() or eval().
// The filename will be used for run-time error messages.
// The mode must be 'exec' to compile a module, 'single' to compile a
// single (interactive) statement, or 'eval' to compile an expression.
// The flags argument, if present, controls which future statements influence
// the compilation of the code.
// The dont_inherit argument, if non-zero, stops the compilation inheriting
// the effects of any future statements in effect in the code calling
// compile; if absent or zero these statements do influence the compilation,
// in addition to any features explicitly specified.
func LegacyCompile(str, filename, mode string, flags int, dont_inherit bool) py.Object {
dont_inherit_str := "False"
if dont_inherit {
dont_inherit_str = "True"
}
// FIXME escaping in filename
code := fmt.Sprintf(`import sys, marshal
str = sys.stdin.buffer.read().decode("utf-8")
code = compile(str, "%s", "%s", %d, %s)
marshalled_code = marshal.dumps(code)
sys.stdout.buffer.write(marshalled_code)
sys.stdout.close()`,
filename,
mode,
flags,
dont_inherit_str,
)
cmd := exec.Command("python3.4", "-c", code)
cmd.Stdin = strings.NewReader(str)
var out bytes.Buffer
cmd.Stdout = &out
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "--- Failed to run python3.4 compile ---\n")
fmt.Fprintf(os.Stderr, "--------------------\n")
_, _ = os.Stderr.Write(stderr.Bytes())
fmt.Fprintf(os.Stderr, "--------------------\n")
panic(err)
}
obj, err := marshal.ReadObject(bytes.NewBuffer(out.Bytes()))
if err != nil {
panic(err)
}
return obj
}