← Winnitude! Search  ·  Reader Mode (1223 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 23 LINKS

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

Repository navigation

py: fix RunSrc to allow multiple top-level statements (#257) - #258


Open
ncw wants to merge 1 commit into main from fix-257-RunSrc
Open

py: fix RunSrc to allow multiple top-level statements (#257) #258 ncw wants to merge 1 commit into main from fix-257-RunSrc

Conversation

ncw commented Jul 3, 2026



Copy link Copy Markdown
Collaborator

Problem

py.RunSrc fails with SyntaxError: 'invalid syntax' when the source contains more than one top-level statement -- for example, two class definitions:

class A:
    pass

class B:
    pass

The equivalent py.RunFile path works fine.

Reported in #257 .

Root cause

RunSrc compiled the source in SingleMode:

code, err := Compile(pySrc+"\n", pySrcDesc, SingleMode, 0, true)

SingleMode maps to Python's single/interactive grammar, which accepts only a single top-level statement. RunFile goes through ResolveAndCompile, which uses ExecMode (the file/module grammar that accepts a whole module of statements) -- hence the discrepancy.

This has been present since RunSrc was introduced in 727b7c4 .

Fix

Compile in ExecMode instead, matching RunFile and the semantics of Python's exec() for a source buffer.

Test

Adds TestRunSrcMultipleStatements in pytest (which imports both py and stdlib, avoiding the import cycle in the py package). Verified it fails against the old SingleMode code and passes with the fix. Full suite green.

Fixes #257

py: fix RunSrc to compile in ExecMode so multi-statement source works 7f571f8
RunSrc compiled source in SingleMode, whose interactive grammar only
accepts a single top-level statement. Source with more than one
statement (e.g. two class definitions) failed with SyntaxError, while
the equivalent RunFile path (which uses ExecMode) worked fine.

Compile in ExecMode instead, matching RunFile and the semantics of
Python's exec() for a source buffer, and add a regression test.

Fixes #257

drew-512 commented Nov 10, 2026



Copy link Copy Markdown
Contributor

Tested against main (444ae5e ): the two-class and two-function cases fail there and pass here, and CI is green.

One gap: the "assignments and expression" case passes on main too. In SingleMode only the first statement is compiled and no error is reported, so x = 1\ny = 2\nz = x + y silently runs just x = 1 (y and z are never defined). Checking that the last name exists would make each case a regression test, e.g. a want field per case:

m, err := py.RunSrc(ctx, test.src, "run", nil)
if err != nil {
	t.Fatalf("RunSrc failed: %v", err)
}
if _, ok := m.Globals[test.want]; !ok { // "B", "b", "z"
	t.Fatalf("%s is not defined: not every statement ran", test.want)
}

Also maybe worth a line in the doc comment: in ExecMode an expression statement no longer echoes its value (RunSrc(ctx, "1+2", ...) printed 3 on main), which matches running a file.

drew-512 mentioned this pull request Nov 10, 2026 py.RunFile / RunCode doesn't handle a nil module pointer #249 Open Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

2 participants