<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
        {font-family:Verdana;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Bill<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-left:.5in"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> paolomoretti [mailto:notifications@codeplex.com]
<br>
<b>Sent:</b> Wednesday, March 09, 2011 9:21 AM<br>
<b>To:</b> Bill Chiles<br>
<b>Subject:</b> IronPython generators' scope [dlr:249060]<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">From: paolomoretti<o:p></o:p></span></p>
<div style="margin-bottom:24.0pt" id="ThreadNotificationPostBody">
<p style="mso-margin-top-alt:5.0pt;margin-right:0in;margin-bottom:12.0pt;margin-left:.5in">
<span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">Hi,<br>
I have problems to understand how IronPython determines the scripts' scope, especially when I use generators.<br>
<br>
For example, if I run this code (http://pastebin.com/P24BZUJU) with IronPython 2.6.2<o:p></o:p></span></p>
<div>
<pre style="margin-left:.5in;background:white"><span style="color:black">ScriptEngine engine = Python.CreateEngine();<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"><o:p> </o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:blue">string</span><span style="color:black"> code = </span><span style="color:#A31515">@"<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515">import System<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515">print System.Int32(5) # OK<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515"><o:p> </o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515">gen = (System.Int32(i) for i in range(1,10))<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515">for x in gen:<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515"> print x<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:#A31515">"</span><span style="color:black">;<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"><o:p> </o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.AutoDetect);<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">CompiledCode compiledCode = source.Compile();<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"><o:p> </o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:blue">try</span><span style="color:black"><o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">{<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"> compiledCode.Execute(engine.CreateScope());<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">}<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:blue">catch</span><span style="color:black"> (System.Exception ex) </span><span style="color:green">// IronPython.Runtime.UnboundNameException</span><span style="color:black"><o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">{<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"> Console.WriteLine(ex.ToString());<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">}<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"><o:p> </o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:blue">try</span><span style="color:black"><o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">{<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"> compiledCode.Execute(); </span><span style="color:green">// OK</span><span style="color:black"><o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">}<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:blue">catch</span><span style="color:black"> (System.Exception ex)<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">{<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black"> Console.WriteLine(ex.ToString());<o:p></o:p></span></pre>
<pre style="margin-left:.5in;background:white"><span style="color:black">}<o:p></o:p></span></pre>
</div>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif""><br>
<br>
In the first trycatch block I get the the following exception:<br>
<br>
<em><span style="font-family:"Verdana","sans-serif"">IronPython.Runtime.UnboundNameException: global name 'System' is not defined</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Compiler.PythonGlobal.GetCachedValue()</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Compiler.PythonGlobal.get_CurrentValue()</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at __generator_$1(Closure , MutableTuple )</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Runtime.PythonGenerator.MoveNextWorker()</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Runtime.PythonGenerator.System.Collections.IEnumerator.MoveNext()</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Compiler.PythonScriptCode.Run(Scope scope)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> at Microsoft.Scripting.Hosting.CompiledCode.Execute(ScriptScope scope)</span></em><br>
<em><span style="font-family:"Verdana","sans-serif""> ...</span></em><br>
<br>
The second trycatch block works fine instead.<br>
<br>
If I run this example with IronPython 2.7 RC2, I don't get any exception.<br>
<br>
Is this a bug, or something I am doing wrong?<br>
<br>
Regards,<br>
<br>
Paolo <o:p></o:p></span></p>
</div>
<div style="border:none;border-top:solid #CCCCCC 1.0pt;padding:0in 0in 0in 0in" id="ThreadNotificationFooter">
<p style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif";color:gray">Read the
<a href="http://dlr.codeplex.com/discussions/249060#post577880">full discussion online</a>.<o:p></o:p></span></p>
<p style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif";color:gray">To add a post to this discussion, reply to this email (<a href="mailto:dlr@discussions.codeplex.com?subject=[dlr:249060]">dlr@discussions.codeplex.com</a>)<o:p></o:p></span></p>
<p style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif";color:gray">To start a new discussion for this project, email
<a href="mailto:dlr@discussions.codeplex.com">dlr@discussions.codeplex.com</a><o:p></o:p></span></p>
<p style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif";color:gray">You are receiving this email because you subscribed to this discussion on CodePlex. You can
<a href="https://dlr.codeplex.com/subscriptions/thread/project/edit">unsubscribe or change your settings</a> on codePlex.com.<o:p></o:p></span></p>
<p style="margin-left:.5in"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif";color:gray">Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com<o:p></o:p></span></p>
</div>
</div>
</body>
</html>