@@ -60,6 +60,7 @@ func xmain(args []string) {
6060 defer pprof .StopCPUProfile ()
6161 }
6262
63+ var err error
6364 // IF no args, enter REPL mode
6465 if len (args ) == 0 {
6566
@@ -69,13 +70,46 @@ func xmain(args []string) {
6970 fmt .Printf ("- go version: %s\n " , runtime .Version ())
7071
7172 replCtx := repl .New (ctx )
72- cli .RunREPL (replCtx )
73+ err = cli .RunREPL (replCtx )
74+ } else {
75+ _ , err = py .RunFile (ctx , args [0 ], py.CompileOpts {}, nil )
76+ }
77+ if err != nil {
78+ if py .IsException (py .SystemExit , err ) {
79+ handleSystemExit (err .(py.ExceptionInfo ).Value .(* py.Exception ))
80+ }
81+ py .TracebackDump (err )
82+ os .Exit (1 )
83+ }
84+ }
7385
86+ func handleSystemExit (exc * py.Exception ) {
87+ args := exc .Args .(py.Tuple )
88+ if len (args ) == 0 {
89+ os .Exit (0 )
90+ } else if len (args ) == 1 {
91+ if code , ok := args [0 ].(py.Int ); ok {
92+ c , err := code .GoInt ()
93+ if err != nil {
94+ fmt .Fprintln (os .Stderr , err )
95+ os .Exit (1 )
96+ }
97+ os .Exit (c )
98+ }
99+ msg , err := py .ReprAsString (args [0 ])
100+ if err != nil {
101+ fmt .Fprintln (os .Stderr , err )
102+ } else {
103+ fmt .Fprintln (os .Stderr , msg )
104+ }
105+ os .Exit (1 )
74106 } else {
75- _ , err := py .RunFile ( ctx , args [ 0 ], py. CompileOpts {}, nil )
107+ msg , err := py .ReprAsString ( args )
76108 if err != nil {
77- py .TracebackDump (err )
78- os .Exit (1 )
109+ fmt .Fprintln (os .Stderr , err )
110+ } else {
111+ fmt .Fprintln (os .Stderr , msg )
79112 }
113+ os .Exit (1 )
80114 }
81115}
0 commit comments