-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (99 loc) · 2.21 KB
/
main.go
File metadata and controls
115 lines (99 loc) · 2.21 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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.
// Gpython binary
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/repl"
"github.com/go-python/gpython/repl/cli"
_ "github.com/go-python/gpython/stdlib"
)
var (
cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to file")
)
// syntaxError prints the syntax
func syntaxError() {
fmt.Fprintf(os.Stderr, `GPython
A python implementation in Go
Full options:
`)
flag.PrintDefaults()
}
func main() {
flag.Usage = syntaxError
flag.Parse()
xmain(flag.Args())
}
func xmain(args []string) {
opts := py.DefaultContextOpts()
opts.SysArgs = args
ctx := py.NewContext(opts)
defer ctx.Close()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
var err error
// IF no args, enter REPL mode
if len(args) == 0 {
fmt.Printf("Python 3.4.0 (%s, %s)\n", commit, date)
fmt.Printf("[Gpython %s]\n", version)
fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf("- go version: %s\n", runtime.Version())
replCtx := repl.New(ctx)
err = cli.RunREPL(replCtx)
} else {
_, err = py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
}
if err != nil {
if py.IsException(py.SystemExit, err) {
handleSystemExit(err.(py.ExceptionInfo).Value.(*py.Exception))
}
py.TracebackDump(err)
os.Exit(1)
}
}
func handleSystemExit(exc *py.Exception) {
args := exc.Args.(py.Tuple)
if len(args) == 0 {
os.Exit(0)
} else if len(args) == 1 {
if code, ok := args[0].(py.Int); ok {
c, err := code.GoInt()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(c)
}
msg, err := py.ReprAsString(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
} else {
msg, err := py.ReprAsString(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
}
}