Skip to content

Commit a18babf

Browse files
committed
Execute .py files directly
1 parent 0b3a137 commit a18babf

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

main.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"fmt"
88
_ "github.com/ncw/gpython/builtin"
99
//_ "github.com/ncw/gpython/importlib"
10+
"github.com/ncw/gpython/compile"
1011
"github.com/ncw/gpython/marshal"
1112
"github.com/ncw/gpython/py"
1213
"github.com/ncw/gpython/vm"
14+
"io/ioutil"
1315
"log"
1416
"os"
1517
"strings"
@@ -56,10 +58,21 @@ func main() {
5658
if err != nil {
5759
log.Fatal(err)
5860
}
59-
defer f.Close()
60-
obj, err := marshal.ReadPyc(f)
61-
if err != nil {
62-
log.Fatal(err)
61+
defer f.Close() // FIXME don't leave f open for the whole program!
62+
var obj py.Object
63+
if strings.HasSuffix(prog, ".pyc") {
64+
obj, err = marshal.ReadPyc(f)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
} else if strings.HasSuffix(prog, ".py") {
69+
str, err := ioutil.ReadAll(f)
70+
if err != nil {
71+
log.Fatal(err)
72+
}
73+
obj = compile.Compile(string(str), prog, "exec", 0, true)
74+
} else {
75+
log.Fatalf("Can't execute %q", prog)
6376
}
6477
code := obj.(*py.Code)
6578
module := py.NewModule("__main__", "", nil, nil)

0 commit comments

Comments
 (0)