Guest User

Untitled

a guest
Mar 9th, 2011
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using IronPython.Hosting;
  3. using Microsoft.Scripting;
  4. using Microsoft.Scripting.Hosting;
  5.  
  6. namespace IronPythonTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ScriptEngine engine = Python.CreateEngine();
  13.  
  14.             string code = @"
  15. import System
  16. print System.Int32(5) #OK
  17.  
  18. gen = (System.Int32(i) for i in range(1,10))
  19. for x in gen:
  20.    print x
  21.            ";
  22.  
  23.             ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.AutoDetect);
  24.             CompiledCode compiledCode = source.Compile();
  25.  
  26.             try
  27.             {
  28.                 compiledCode.Execute(engine.CreateScope()); // IronPython.Runtime.UnboundNameException
  29.             }
  30.             catch (System.Exception ex)
  31.             {
  32.                 Console.WriteLine(ex.ToString());
  33.             }
  34.  
  35.             try
  36.             {
  37.                 compiledCode.Execute(); //OK
  38.             }
  39.             catch (System.Exception ex)
  40.             {
  41.                 Console.WriteLine(ex.ToString());
  42.             }
  43.  
  44.             Console.ReadLine();
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment