Skip to content

Commit ed6da83

Browse files
committed
feat: separate builtins from site-packages
1 parent bb35fec commit ed6da83

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ src/lang/testdata
7575

7676
tools
7777
abcoder
78+
79+
/*.txt
80+
/*.json

lang/python/spec.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ func (c *PythonSpec) NameSpace(path string) (string, string, error) {
8383
// XXX: hardcoded python version
8484
condaPrefix := "/home/zhenyang/anaconda3/envs/abcoder/lib/python3.11"
8585
if strings.HasPrefix(path, condaPrefix) {
86+
if strings.HasPrefix(path, condaPrefix+"/site-packages") {
87+
// external module
88+
relPath, err := filepath.Rel(condaPrefix+"/site-packages", path)
89+
if err != nil {
90+
return "", "", err
91+
}
92+
relPath = strings.TrimSuffix(relPath, ".py")
93+
pkgPath := strings.ReplaceAll(relPath, string(os.PathSeparator), ".")
94+
modPath := strings.Split(pkgPath, ".")
95+
if len(modPath) >= 1 {
96+
modName := modPath[0]
97+
return modName, pkgPath, nil
98+
}
99+
panic(fmt.Sprintf("Malformed Namespace %s, pkgPath %s", path, pkgPath))
100+
}
101+
// builtin module
86102
modName := "builtins"
87103
relPath, err := filepath.Rel(condaPrefix, path)
88104
if err != nil {
@@ -93,7 +109,7 @@ func (c *PythonSpec) NameSpace(path string) (string, string, error) {
93109
return modName, pkgPath, nil
94110
}
95111

96-
panic(fmt.Sprintf("Namespace %s", path))
112+
panic(fmt.Sprintf("Unhandled Namespace %s", path))
97113
}
98114

99115
func (c *PythonSpec) ShouldSkip(path string) bool {

0 commit comments

Comments
 (0)