Skip to content

Commit 71b65b7

Browse files
committed
py: import - make work better in REPL and look for __init__.py
1 parent 4ccf870 commit 71b65b7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

py/import.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,27 @@ func ImportModuleLevelObject(name string, globals, locals StringDict, fromlist T
9191
if mpath == "" {
9292
mpathObj, ok := globals["__file__"]
9393
if !ok {
94-
panic(ExceptionNewf(SystemError, "Couldn't find __file__ in globals"))
94+
var err error
95+
mpath, err = os.Getwd()
96+
if err != nil {
97+
panic(err)
98+
}
99+
} else {
100+
mpath = path.Dir(string(mpathObj.(String)))
95101
}
96-
mpath = path.Dir(string(mpathObj.(String)))
97102
}
98103
fullPath := path.Join(mpath, pathParts)
99104
// FIXME Read pyc/pyo too
100-
fullPath, err := filepath.Abs(fullPath + ".py")
105+
fullPath, err := filepath.Abs(fullPath)
101106
if err != nil {
102107
continue
103108
}
109+
if fi, err := os.Stat(fullPath); err == nil && fi.IsDir() {
110+
// FIXME this is a massive simplification!
111+
fullPath = path.Join(fullPath, "__init__.py")
112+
} else {
113+
fullPath += ".py"
114+
}
104115
// Check if file exists
105116
if _, err := os.Stat(fullPath); err == nil {
106117
str, err := ioutil.ReadFile(fullPath)

0 commit comments

Comments
 (0)