@@ -60,6 +60,7 @@ func xmain(args []string) {
60
60
defer pprof .StopCPUProfile ()
61
61
}
62
62
63
+ var err error
63
64
// IF no args, enter REPL mode
64
65
if len (args ) == 0 {
65
66
@@ -69,13 +70,46 @@ func xmain(args []string) {
69
70
fmt .Printf ("- go version: %s\n " , runtime .Version ())
70
71
71
72
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
+ }
73
85
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 )
74
106
} else {
75
- _ , err := py .RunFile ( ctx , args [ 0 ], py. CompileOpts {}, nil )
107
+ msg , err := py .ReprAsString ( args )
76
108
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 )
79
112
}
113
+ os .Exit (1 )
80
114
}
81
115
}
0 commit comments