Skip to content

Commit e1f060c

Browse files
committed
Set __file__ when importing a module
1 parent 7f0ea86 commit e1f060c

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

main.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ncw/gpython/compile"
1111
"github.com/ncw/gpython/marshal"
1212
"github.com/ncw/gpython/py"
13+
_ "github.com/ncw/gpython/time"
1314
"github.com/ncw/gpython/vm"
1415
"io/ioutil"
1516
"log"
@@ -54,6 +55,7 @@ func main() {
5455
prog := args[0]
5556
fmt.Printf("Running %q\n", prog)
5657

58+
// FIXME should be using ImportModuleLevelObject() here
5759
f, err := os.Open(prog)
5860
if err != nil {
5961
log.Fatal(err)
@@ -76,6 +78,7 @@ func main() {
7678
}
7779
code := obj.(*py.Code)
7880
module := py.NewModule("__main__", "", nil, nil)
81+
module.Globals["__file__"] = py.String(prog)
7982
res, err := vm.Run(module.Globals, module.Globals, code, nil)
8083
if err != nil {
8184
py.TracebackDump(err)

notes.txt

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ Testing
2727
go build ./... && go build
2828
./gpython hello.pyc
2929

30+
When converting C code, run it through astyle first as a first pass
31+
32+
astyle --style=java --add-brackets < x.c > x.go
33+
34+
3035
Polymorphism
3136
============
3237

py/import.go

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func ImportModuleLevelObject(name string, globals, locals StringDict, fromlist T
117117
if err != nil {
118118
panic(err)
119119
}
120+
module.Globals["__file__"] = String(fullPath)
120121
return module
121122
}
122123
}

0 commit comments

Comments
 (0)