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
2 changes: 1 addition & 1 deletion py/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func RunSrc(ctx Context, pySrc string, pySrcDesc string, inModule interface{}) (
if pySrcDesc == "" {
pySrcDesc = "<run>"
}
code, err := Compile(pySrc+"\n", pySrcDesc, SingleMode, 0, true)
code, err := Compile(pySrc+"\n", pySrcDesc, ExecMode, 0, true)
if err != nil {
return nil, err
}
Expand Down
64 changes: 64 additions & 0 deletions pytest/runsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 pytest

import (
"testing"

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

_ "github.com/go-python/gpython/stdlib"
)

// Regression test for https://github.com/go-python/gpython/issues/257
//
// RunSrc used to compile in SingleMode which only accepts a single
// interactive statement, so source containing more than one top-level
// statement (e.g. two class definitions) failed with a SyntaxError.
func TestRunSrcMultipleStatements(t *testing.T) {
for _, test := range []struct {
name string
src string
}{
{
name: "two classes",
src: `
class A:
pass


class B:
pass
`,
},
{
name: "two functions",
src: `
def a():
return 1

def b():
return 2
`,
},
{
name: "assignments and expression",
src: `
x = 1
y = 2
z = x + y
`,
},
} {
t.Run(test.name, func(t *testing.T) {
ctx := py.NewContext(py.DefaultContextOpts())
defer ctx.Close()
_, err := py.RunSrc(ctx, test.src, "run", nil)
if err != nil {
t.Fatalf("RunSrc failed: %v", err)
}
})
}
}
Loading