Thank you so much for your insight. I'm not really a Python guy. Your remark about a generator being a new function scope is probably right on spot. My square function works pretty much everywhere else. Although I still don't understand exactly how I'd fix
My interpreter works that way:
At constructor time, I create a new PythonEngine and a new PythonScope.
Each time a line is entered I use engine.CreateScriptSourceFromString (with SourceCode.InteractiveCode) to create a ScriptSource and check whether the interpreter should allow for continuation on a new line.
Then when I think the code is complete, I use source.Execute(scope) to try to execute the code. (scope being always the same ScriptScope at each call: the one created in the constructor).
The error saying "global name 'square'" and your remark about generator having a new scope makes me wonder if the square function is defined in my own "scope" object rather than globally and if the generator scope doesn't have access
to it. But how should I fix that?
I like being able to define different scopes when executing various pieces of Python script, and I like the possibility to "throw away" a scope and start in a clean environment again. Ideally the fix for my problem should let me do that.