@@ -12,22 +12,13 @@ import (
1212 "runtime"
1313 "runtime/pprof"
1414
15- _ "github.com/go-python/gpython/builtin "
15+ "github.com/go-python/gpython/repl "
1616 "github.com/go-python/gpython/repl/cli"
1717
18- //_ "github.com/go-python/gpython/importlib"
19- "io/ioutil"
2018 "log"
2119 "os"
22- "strings"
2320
24- "github.com/go-python/gpython/compile"
25- "github.com/go-python/gpython/marshal"
26- _ "github.com/go-python/gpython/math"
2721 "github.com/go-python/gpython/py"
28- pysys "github.com/go-python/gpython/sys"
29- _ "github.com/go-python/gpython/time"
30- "github.com/go-python/gpython/vm"
3122)
3223
3324// Globals
@@ -48,33 +39,14 @@ Full options:
4839 flag .PrintDefaults ()
4940}
5041
51- // Exit with the message
52- func fatal (message string , args ... interface {}) {
53- if ! strings .HasSuffix (message , "\n " ) {
54- message += "\n "
55- }
56- syntaxError ()
57- fmt .Fprintf (os .Stderr , message , args ... )
58- os .Exit (1 )
59- }
60-
6142func main () {
6243 flag .Usage = syntaxError
6344 flag .Parse ()
6445 args := flag .Args ()
65- py .MustGetModule ("sys" ).Globals ["argv" ] = pysys .MakeArgv (args )
66- if len (args ) == 0 {
67-
68- fmt .Printf ("Python 3.4.0 (%s, %s)\n " , commit , date )
69- fmt .Printf ("[Gpython %s]\n " , version )
70- fmt .Printf ("- os/arch: %s/%s\n " , runtime .GOOS , runtime .GOARCH )
71- fmt .Printf ("- go version: %s\n " , runtime .Version ())
7246
73- cli .RunREPL ()
74- return
75- }
76- prog := args [0 ]
77- // fmt.Printf("Running %q\n", prog)
47+ opts := py .DefaultContextOpts ()
48+ opts .SysArgs = flag .Args ()
49+ ctx := py .NewContext (opts )
7850
7951 if * cpuprofile != "" {
8052 f , err := os .Create (* cpuprofile )
@@ -88,41 +60,23 @@ func main() {
8860 defer pprof .StopCPUProfile ()
8961 }
9062
91- // FIXME should be using ImportModuleLevelObject() here
92- f , err := os .Open (prog )
93- if err != nil {
94- log .Fatalf ("Failed to open %q: %v" , prog , err )
95- }
96- var obj py.Object
97- if strings .HasSuffix (prog , ".pyc" ) {
98- obj , err = marshal .ReadPyc (f )
99- if err != nil {
100- log .Fatalf ("Failed to marshal %q: %v" , prog , err )
101- }
102- } else if strings .HasSuffix (prog , ".py" ) {
103- str , err := ioutil .ReadAll (f )
104- if err != nil {
105- log .Fatalf ("Failed to read %q: %v" , prog , err )
106- }
107- obj , err = compile .Compile (string (str ), prog , "exec" , 0 , true )
63+ // IF no args, enter REPL mode
64+ if len (args ) == 0 {
65+
66+ fmt .Printf ("Python 3.4.0 (%s, %s)\n " , commit , date )
67+ fmt .Printf ("[Gpython %s]\n " , version )
68+ fmt .Printf ("- os/arch: %s/%s\n " , runtime .GOOS , runtime .GOARCH )
69+ fmt .Printf ("- go version: %s\n " , runtime .Version ())
70+
71+ replCtx := repl .New (ctx )
72+ cli .RunREPL (replCtx )
73+
74+ } else {
75+ _ , err := py .RunFile (ctx , args [0 ], py.CompileOpts {}, nil )
10876 if err != nil {
109- log .Fatalf ("Can't compile %q: %v" , prog , err )
77+ py .TracebackDump (err )
78+ log .Fatal (err )
11079 }
111- } else {
112- log .Fatalf ("Can't execute %q" , prog )
113- }
114- if err = f .Close (); err != nil {
115- log .Fatalf ("Failed to close %q: %v" , prog , err )
116- }
117- code := obj .(* py.Code )
118- module := py .NewModule ("__main__" , "" , nil , nil )
119- module .Globals ["__file__" ] = py .String (prog )
120- res , err := vm .Run (module .Globals , module .Globals , code , nil )
121- if err != nil {
122- py .TracebackDump (err )
123- log .Fatal (err )
12480 }
125- // fmt.Printf("Return = %v\n", res)
126- _ = res
12781
12882}
0 commit comments