Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ var defaultPaths = []py.Object{

func resolveRunPath(runPath string, opts py.CompileOpts, pathObjs []py.Object, tryPath func(pyPath string) (bool, error)) error {
runPath = strings.TrimSuffix(runPath, "/")
if filepath.IsAbs(runPath) {
// Absolute paths are independent of the configured search directories.
pathObjs = []py.Object{py.String("")}
}

var (
err error
Expand Down
34 changes: 34 additions & 0 deletions stdlib/stdlib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2026 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package stdlib

import (
"os"
"path/filepath"
"testing"

"github.com/go-python/gpython/py"
)

func TestRunFileAbsolutePath(t *testing.T) {
filename := filepath.Join(t.TempDir(), "script.py")
if err := os.WriteFile(filename, []byte("value = 42\n"), 0600); err != nil {
t.Fatal(err)
}
for _, useSysPaths := range []bool{false, true} {
ctx := NewContext(py.DefaultContextOpts())
defer ctx.Close()
// Absolute paths must work even without any search directories.
ctx.Store().MustGetModule("sys").Globals["path"] = py.NewList()
module, err := py.RunFile(ctx, filename, py.CompileOpts{UseSysPaths: useSysPaths}, nil)
if err != nil {
t.Errorf("UseSysPaths=%v: %v", useSysPaths, err)
continue
}
if module.Globals["value"] != py.Int(42) {
t.Errorf("UseSysPaths=%v: script was not executed", useSysPaths)
}
}
}