[IronPython] IronPython generators' scope [dlr:249060]

Bill Chiles billchi at microsoft.com
Wed Mar 9 18:37:31 CET 2011


I'm CC'ing the IPy discussion list, but you are seeing a bug in empty scope initialization that was in 2.6.2.  The expected behavior, unless I'm missing something Dino, Jeff, or Michael points out, is what you're getting with 2.7RC2.  I don't recall the time window, but there was a time when Dino and I discussed the default references the ipy engine should apply to scopes.

If you had to run in 2.6.2, it might be that if you did "import clr" followed by clr.AddReference(…) and then followed by "import System", the code might work.  Or, I'm wrong, and it is just the generator's closure didn't have the right context flowed into it.

Bill

From: paolomoretti [mailto:notifications at codeplex.com]
Sent: Wednesday, March 09, 2011 9:21 AM
To: Bill Chiles
Subject: IronPython generators' scope [dlr:249060]


From: paolomoretti

Hi,
I have problems to understand how IronPython determines the scripts' scope, especially when I use generators.

For example, if I run this code (http://pastebin.com/P24BZUJU) with IronPython 2.6.2

ScriptEngine engine = Python.CreateEngine();



string code = @"

import System

print System.Int32(5) # OK



gen = (System.Int32(i) for i in range(1,10))

for x in gen:

    print x

";



ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.AutoDetect);

CompiledCode compiledCode = source.Compile();



try

{

  compiledCode.Execute(engine.CreateScope());

}

catch (System.Exception ex) // IronPython.Runtime.UnboundNameException

{

  Console.WriteLine(ex.ToString());

}



try

{

  compiledCode.Execute();  // OK

}

catch (System.Exception ex)

{

  Console.WriteLine(ex.ToString());

}


In the first trycatch block I get the the following exception:

IronPython.Runtime.UnboundNameException: global name 'System' is not defined
   at IronPython.Compiler.PythonGlobal.GetCachedValue()
   at IronPython.Compiler.PythonGlobal.get_CurrentValue()
   at __generator_$1(Closure , MutableTuple )
   at IronPython.Runtime.PythonGenerator.MoveNextWorker()
   at IronPython.Runtime.PythonGenerator.System.Collections.IEnumerator.MoveNext()
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
   at Microsoft.Scripting.Hosting.CompiledCode.Execute(ScriptScope scope)
   ...

The second trycatch  block works fine instead.

If I run this example with IronPython 2.7 RC2, I don't get any exception.

Is this a bug, or something I am doing wrong?

Regards,

Paolo

Read the full discussion online<http://dlr.codeplex.com/discussions/249060#post577880>.

To add a post to this discussion, reply to this email (dlr at discussions.codeplex.com<mailto:dlr at discussions.codeplex.com?subject=[dlr:249060]>)

To start a new discussion for this project, email dlr at discussions.codeplex.com<mailto:dlr at discussions.codeplex.com>

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings<https://dlr.codeplex.com/subscriptions/thread/project/edit> on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110309/69c22c46/attachment.html>


More information about the Ironpython-users mailing list