IronPython and Dynamic Languages on .NET by Mahesh Prakriya

3,944 views

Published on

IronPython is the codename for a new implementation of the Python programming language on the .NET Framework. IronPython is fast—in fact, up to 1.8 times faster than Python-2.4 on the standard pystone benchmark. It supports an interactive interpreter with fully dynamic compilation as well as static compilation to produce pre-compiled executables. It's well integrated with the rest of the framework and makes all .NET libraries easily available to Python programmers. This session shows how IronPython brings the power of .NET to Python and the power of Python to .NET. At OSCON 2004, the first public release of IronPython was announced. This session demonstrates the latest IronPython version in a range of situations from using GUI frameworks to driving Microsoft Office applications to working with a variety of external libraries. We also discuss other scripting languages on .NET.

Published in: Technology
  • Be the first to comment

IronPython and Dynamic Languages on .NET by Mahesh Prakriya

  1. 1. IronPython and Dynamic Languages on .NET Mahesh Prakriya [email_address] Principal PM Lead, DLR
  2. 2. Dynamic Languages on .NET <ul><li>Popular </li></ul><ul><li>Powerful </li></ul><ul><li>Simple </li></ul><ul><li>Intuitive </li></ul><ul><li>Interactive </li></ul><ul><li>Inspiring </li></ul><ul><li>Fun </li></ul>
  3. 3. DLR Dynamic Languages VBx JScript IronPython IronRuby 3rd Party langs CLR Frameworks VS Integration Tools WinForms, WPF Applications ASP.net, Silverlight Robotics, XNA
  4. 4. IronPython – four pillars <ul><li>True Python Implementation </li></ul><ul><li>Seamless integration with .NET; built on DLR </li></ul><ul><li>Open source under Microsoft Public License </li></ul><ul><li>Fastest implementation of Python </li></ul>
  5. 5. IronPython Demos: Compatability Winforms/Tablet Silverlight WebServices Mahesh Prakriya [email_address]
  6. 6. Standard Pystone Benchmark IronPython 0.1 Python 2.3 IronPython 1.0 IronPython 2.0 alpha1 Python 2.5 Python 2.1
  7. 7. Rough Roadmap
  8. 8. FePy and IPCE <ul><li>Sourceforge project led by Seo Sanghyeon </li></ul><ul><li>“ FePy project aims to provide enhancements and add-ons for IronPython” </li></ul><ul><li>IronPython Community Edition (IPCE) </li></ul><ul><ul><ul><li>Files under Src/ are licensed under Shared Source License for IronPython. </li></ul></ul></ul><ul><ul><ul><li>Files under Lib/ are licensed under Python Software Foundation License Version 2, except the following: </li></ul></ul></ul><ul><ul><ul><li>ctypes, hashlib, md5, pyexpat, select, sha, site, socket, ssl, unicodedata, wsgi, zlib modules are part of FePy library </li></ul></ul></ul><ul><ul><ul><li>MySQLdb, dbapi, odbc, psycopg, sqlclient, sqlite, sqlite3 modules are part of FePy DB-API library </li></ul></ul></ul><ul><ul><ul><li>FePy library and FePy DB-API library are licensed under MIT License. </li></ul></ul></ul><ul><ul><ul><li>Files under Lib/elementtree are licensed under ElementTree License. </li></ul></ul></ul><ul><ul><ul><li>Files under Lib/Crypto are licensed under Python Cryptography Toolkit License. </li></ul></ul></ul><ul><ul><ul><li>Files under Lib/paramiko are licensed under GNU Lesser General Public License. Click the link for the exact terms </li></ul></ul></ul>
  9. 9. Visual Studio support for IronPython <ul><li>VS support for IPy through VS SDK </li></ul><ul><ul><li>Supports both VS 2005 and VS 2008 </li></ul></ul><ul><li>Key features </li></ul><ul><ul><li>Editor – color coding, </li></ul></ul><ul><ul><li>Project system, </li></ul></ul><ul><ul><li>Debugger, </li></ul></ul><ul><ul><li>Rich client (WPF, Winforms) designers emit IronPython </li></ul></ul><ul><ul><ul><li>WPF designer support only in VS2008 </li></ul></ul></ul><ul><li>http://blogs.msdn.com/aaronmar/ </li></ul>
  10. 10. Copyright Microsoft Corporation
  11. 11. Copyright Microsoft Corporation
  12. 12. Copyright Microsoft Corporation
  13. 13. <ul><li>A framework for building 2d and 3d games </li></ul><ul><li>Targets Windows and XBox 360 </li></ul><ul><ul><ul><li>Free for Windows, $100/year to run on 360 </li></ul></ul></ul><ul><ul><ul><li>Encouraging hobbyist game builder community </li></ul></ul></ul><ul><ul><ul><li>Security and type-safety is vital for hobbyist games </li></ul></ul></ul><ul><ul><ul><ul><ul><li>Let’s you comfortably run other’s games without virus risk </li></ul></ul></ul></ul></ul><ul><li>Focus is on C# as development language </li></ul><ul><ul><ul><li>This is a dramatic step away from C++ </li></ul></ul></ul><ul><li>What if I don’t want to use C#? </li></ul>
  14. 14. Microsoft Robotics Studio
  15. 15. IronPython - customer examples <ul><li>3 rd party languages running on DLR, some examples </li></ul><ul><ul><li>IronLisp, Nua, ColdFusion, SmallTalk, … </li></ul></ul><ul><li>MySpace </li></ul><ul><ul><li>Currently using IronPython on their web server farm of ~2500 </li></ul></ul><ul><li>Nvidia </li></ul><ul><ul><li>FXComposer 2 (IDE for shader authoring) </li></ul></ul><ul><li>Resolver Systems </li></ul><ul><ul><li>Startup based out of London doing dynamic spreadsheets </li></ul></ul><ul><ul><li>120K LOC (IronPython) </li></ul></ul><ul><li>Many Others: </li></ul><ul><ul><li>Manifold 8.0, Multiverse.net, … </li></ul></ul><ul><ul><li>Miguel ships Ipy (and will ship DLR & IronRuby) on Mono </li></ul></ul><ul><ul><li>FePy on Source Forge </li></ul></ul>
  16. 16. DLR Dynamic Languages VBx JScript IronPython IronRuby 3rd Party langs CLR Frameworks VS Integration Tools WinForms, WPF Applications ASP.net, Silverlight Robotics, XNA
  17. 17. Resources: www.codeplex.com/ironpython Mahesh Prakriya [email_address] IronPython Samples, Videos etc. blogs.msdn.com/ironpython Silverlight silverlight.net and codeplex.com/dynamicsilverlight ASP.NET asp.net/downloads/futures Web Services blogs.msdn.com/dmitryr
  18. 18. Questions? Image By Little Bitty Tam http://www.flickr.com/photos/tammra/
  19. 19. Dynamic Language Runtime <ul><li>Platform for building dynamic languages </li></ul><ul><li>Built on top of Microsoft .NET Framework </li></ul><ul><ul><li>Garbage collector </li></ul></ul><ul><ul><li>Just-in-time compiler (JIT) </li></ul></ul><ul><ul><li>Rich libraries </li></ul></ul><ul><ul><li>Tools </li></ul></ul><ul><li>http://www.codeplex.com/IronPython </li></ul>
  20. 20. Compiler Overview Frontend return Syntax Tree Return def add2(a) { return 2 + a; } Scan 2 + a ; Parse Token stream Add Named(a) Const(2)
  21. 21. Compiler Overview Traditional Backend Syntax Tree Return Add Named(a) Const(2) Generate IL ldc.i4.2 // load 2 box [mscorlib]System.Int32 ldarg.0 // load “a” call object ToyHelpers::Add(object, object) ret IL public static object Add (object x, object y) { ... } Runtime Library
  22. 22. Compiler Overview DLR Backend Syntax Tree Return Add Named(a) Const(2) Generate DLR Tree public static object Add (object x, object y) { ... } Runtime Library DLR Tree Return MethodCall ToyHelpers.Add BoundExpression ConstantExpression 2 ConvertTo Object Variable a: Object
  23. 23. Why DLR? <ul><li>Focus on your language </li></ul><ul><ul><li>Scanner </li></ul></ul><ul><ul><li>Parser </li></ul></ul><ul><ul><li>Runtime semantics </li></ul></ul><ul><li>DLR </li></ul><ul><ul><li>Code generation </li></ul></ul><ul><ul><li>Dynamic operations </li></ul></ul><ul><ul><li>Extension methods for .NET Type customization </li></ul></ul><ul><ul><li>Common hosting for all DLR languages </li></ul></ul>
  24. 24. DLR Trees <ul><li>DLR representation of programs </li></ul><ul><ul><li>Similar to LINQ expression trees </li></ul></ul><ul><li>Expressions: Constant , Unary, Binary, Method call, Property value, Field value, Assignment, … </li></ul><ul><li>Statements: If, While, Try, Return, Switch, Throw, … </li></ul><ul><li>Dynamic behavior support: ActionExpression </li></ul><ul><li>Factory methods (Ast.…) </li></ul>
  25. 25. Targeting the DLR <ul><li>Implement scanner and parser </li></ul><ul><li>Translate your AST to the DLR Tree </li></ul><ul><li>Implement your custom types </li></ul><ul><li>Implement customization to .NET types </li></ul><ul><ul><li>Via extension methods </li></ul></ul><ul><li>Tune performance </li></ul><ul><ul><li>Runtime library </li></ul></ul><ul><ul><li>Dynamic types </li></ul></ul>
  26. 26. Generating DLR Trees <ul><li>def add2(a) { </li></ul><ul><li>return 2 + a; </li></ul><ul><li>} </li></ul>Return MethodCall ToyHelpers.Add BoundExpression ConstantExpression 2 ConvertTo Object CodeBlock “ add2” Parameter a: Object public static object Add (object x, object y) { ... } Runtime Library
  27. 27. Generating DLR Trees <ul><li>CodeBlock cb = Ast.CodeBlock(&quot;add2&quot;); </li></ul><ul><li>Variable a = cb.CreateParameter( </li></ul><ul><li>SymbolTable.StringToId( &quot;a&quot; ), </li></ul><ul><li>typeof(object) </li></ul><ul><li>); </li></ul><ul><li>cb.Body = Ast.Return( </li></ul><ul><li>Ast.Call( </li></ul><ul><li>typeof(ToyHelpers).GetMethod(&quot;Add&quot;), </li></ul><ul><li>Ast.Convert(Ast.Constant(2), typeof(object)), </li></ul><ul><li>Ast.Read(a) </li></ul><ul><li>) </li></ul><ul><li>); </li></ul>
  28. 28. Demo DLR Trees Image By Ryan Forsythe: http://flickr.com/photos/fweez/
  29. 29. Operators Method Call <ul><li>ToyHelpers.Add(a, b) </li></ul>BoundExpression Variable a MethodCall ToyHelpers.Add BoundExpression Variable b
  30. 30. Dynamic Actions <ul><li>a + b </li></ul>
  31. 31. Operators Action Expression BoundExpression Variable a Action Add BoundExpression Variable b
  32. 32. Dynamic Actions Call Site <ul><li>static DynamicSite a_plus_b_site = </li></ul><ul><li>new DynamicSite(Add); </li></ul><ul><li>// a + b </li></ul><ul><li>a_plus_b_site.Invoke(a, b) </li></ul><ul><li>// DynamicSite.Invoke </li></ul><ul><li>object Invoke(object a0, object a1) { </li></ul><ul><li>this._handler(this, a0, a1); </li></ul><ul><li>} </li></ul>
  33. 33. Dynamic Actions Target Delegate <ul><li>object Handler(DynamicSite s, object a0, object a1) </li></ul><ul><li>{ </li></ul><ul><li>// HELP !!! </li></ul><ul><li>return s.UpdateMe(a0, a1); </li></ul><ul><li>} </li></ul>
  34. 34. Dynamic Actions Updated Target Delegate - Int <ul><li>object Handler(DynamicSite s, object a0, object a1) </li></ul><ul><li>{ </li></ul><ul><li>if (a0 is int && a1 is int) { </li></ul><ul><li>return (int)a0 + (int)a1; </li></ul><ul><li>} </li></ul><ul><li>// HELP !!! </li></ul><ul><li>return s.UpdateMe(a0, a1); </li></ul><ul><li>} </li></ul>
  35. 35. <ul><li>object Handler(DynamicSite s, object a0, object a1) </li></ul><ul><li>{ </li></ul><ul><li>if (a0 is int && a1 is int) { </li></ul><ul><li>return (int)a0 + (int)a1; </li></ul><ul><li>} </li></ul><ul><li>if (a0 is double && a1 is double) { </li></ul><ul><li>return (double)a0 + (double)a1; </li></ul><ul><li>} </li></ul><ul><li>// HELP !!! </li></ul><ul><li>return s.UpdateMe(a0, a1); </li></ul><ul><li>} </li></ul>Dynamic Actions Updated Target Delegate - Double Rule
  36. 36. Rules <ul><li>Rule = Test + Target </li></ul><ul><li>Test </li></ul><ul><ul><li>Condition examining the arguments </li></ul></ul><ul><li>Target </li></ul><ul><ul><li>An operation to perform if the test succeeds </li></ul></ul><ul><li>Who makes the rules ??? </li></ul><ul><ul><li>The Language </li></ul></ul><ul><ul><li>The DLR </li></ul></ul>
  37. 37. Rules Language Action Binder <ul><li>DLR requests: </li></ul><ul><ul><li>“Tell me how to perform this operation with these arguments!” </li></ul></ul><ul><li>KEY: “Tell me how!” NOT: “Do it!” </li></ul><ul><li>Language responds: </li></ul><ul><ul><li>“Here is the Tree” </li></ul></ul><ul><ul><li>“I don’t know” (DLR tries its own built-in behaviors) </li></ul></ul>
  38. 38. Rules Adding Strings - Test <ul><li>(a0 is string) && (a1 is string) </li></ul>BoundExpression Parameter a0 string TypeIs BinaryExpression AndAlso BoundExpression Parameter a1 string TypeIs
  39. 39. Rules Adding Strings - Target <ul><li>String.Concat((string)a0, (string)a1) </li></ul>BoundExpression Parameter a0 string Convert MethodCall String.Concat(string, string) BoundExpression Parameter a1 string Convert
  40. 40. Demo Actions Image by Kim S http://flickr.com/photos/dearbarbie
  41. 41. Customizing .NET Types Extension Methods <ul><li>&quot;Kathryn&quot;.Greet() </li></ul>
  42. 42. Customizing .NET Types Extension Methods – C# <ul><li>static class StringExtensions { </li></ul><ul><li>public static string Greet( this string self) { </li></ul><ul><li>return &quot;Hello &quot; + self + &quot;!!!&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Program { </li></ul><ul><li>static void Main(string[] args) { </li></ul><ul><li>&quot;Kathryn&quot;.Greet(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  43. 43. Customizing .NET Types Extension Methods – DLR <ul><li>[assembly:ExtensionType( typeof(string), typeof(StringExtensions))] </li></ul><ul><li>static class StringExtensions { </li></ul><ul><li>public static string Greet(string self) { </li></ul><ul><li>return &quot;Hello &quot; + self + &quot;!!!&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>>>> &quot;Kathryn&quot;.Greet() </li></ul>
  44. 44. Customizing .NET Types Extension Methods – DLR <ul><li>Similar to C# extension methods </li></ul><ul><li>Support for extension properties and fields </li></ul><ul><li>To use: </li></ul><ul><li>[assembly:ExtensionTypeAttribute( … )] </li></ul><ul><li>RuntimeHelpers.RegisterAssembly( typeof(StringExtensions).Assembly </li></ul><ul><li>); </li></ul>

×