GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — py: fix RunSrc to allow multiple top-level statements (#257) by ncw · Pull Request #258 · go-python/gpython

Conversation

@ncw

@ncw 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ncw
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

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@drew-512 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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Multiple class definitions in the same file raise error "invalid syntax"

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ncw @drew-512
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.