| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Back | FazBrowse Home | New Git URL |
Problem
py.RunSrc fails with SyntaxError: 'invalid syntax' when the source contains more than one top-level statement — for example, two class definitions:
The equivalent py.RunFile path works fine.
Reported in #257.
Root cause
RunSrc compiled the source in SingleMode:
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