FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

py: fix RunSrc to allow multiple top-level statements (#257) by ncw · Pull Request #258 · go-python/gpython · GitHub

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

Filter by extension

Filter by extension .go  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion py/run.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL