GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||
GeezerVison is showing github.com โ Upgrading from IronPython2 Upgrading from IronPython2
Pavel Koneski edited this page
IronPython 3.4 uses Python 3.4 syntax and standard libraries and so your Python code will need to be updated accordingly. There are numerous tools and guides available on the web to help porting from Python 2 to 3. The IronPython 3 binaries are not compatible with the IronPython 2 binaries. Modules compiled with In an effort to improve compatibility, if sys.implementation.name == "ironpython": print("IronPython!")
# IronPython 2 System.StringSplitOptions.None # IronPython 3 System.StringSplitOptions["None"] Similarly, With IronPython 2, standard output was written to the runtime's Because of this, using a // IronPython 2 var engine = Python.CreateEngine(); var textWriter = new MyTextWriter(); // no longer works in IronPython 3! engine.Runtime.IO.RedirectToConsole(); Console.SetOut(textWriter); // IronPython 3 var engine = Python.CreateEngine(); var textWriter = new MyTextWriter(); engine.Runtime.IO.SetOutput(new TextStream(textWriter), textWriter); Another way of achieving the redirection behavior similar to IronPython 2 is to set engine option // IronPython 3 var engine = Python.CreateEngine(new Dictionary<string, object> { { "ConsoleSupportLevel", Microsoft.Scripting.Runtime.SharedIO.SupportLevel.Basic }, }); var textWriter = new MyTextWriter(); // works again! engine.Runtime.IO.RedirectToConsole(); Console.SetOut(textWriter); This method is particularly useful when embedding the IronPython 3 engine in a host that sets console's writers/reader before the engine is created and the host's code is not accessible to the user (for instance, scripting in LINQPad). // IronPython 3 in LINQPad var engine = Python.CreateEngine(new Dictionary<string, object> { { "ConsoleSupportLevel", Microsoft.Scripting.Runtime.SharedIO.SupportLevel.Basic }, }); engine.Execute("print('abc')"); // shows output in the "Results" pane dynamic ans = engine.Execute("input()"); // pauses the script and asks for input at the bottom of the "Results" pane; terminate your input with Ctrl+Z, Enter One of the major backward incompatible changes in Python 3 is PEP 237 โ Unifying Long Integers and Integers: Essentially, The Python >>> import clr >>> clr.AddReference("System.Numerics") >>> import System >>> int is System.Numerics.BigInteger True >>> int is System.Int32 False >>> clr.GetClrType(int).Name 'BigInteger' This means that in interop cases, when the Example: # IronPython 2 System.Collections.Generic.List[int] # IronPython 3 System.Collections.Generic.List[System.Int32] Overview of
As for instances of i = 1 # instance of Int32 j = 1 << 31 # instance of BigInteger k = j - 1 # still BigInteger, as one of the arguments makes the result type BigInteger This means that the type of >>> import System >>> type(i) <class 'int'> >>> isinstance(i, System.Int32) True >>> type(j) <class 'int'> >>> isinstance(j, System.Int32) False >>> hex(i.MaxValue) '0x7fffffff' The creation of either >>> bi = i.ToBigInteger() >>> isinstance(j, System.Int32) False In the opposite direction, if it is essential to create >>> # k is a BigInteger that fits in 32 bits >>> isinstance(k, System.Int32) False >>> hex(k) '0x7fffffff' >>> ki = int(k) # converts k to Int32 >>> isinstance(ki, System.Int32) True >>> ki = System.Int32(k) # also converts k to Int32 >>> isinstance(ki, System.Int32) True >>> # j is a BigInteger that does not fit in 32 bits >>> isinstance(j, System.Int32) False >>> hex(j) '0x80000000' >>> j = int(j) # no type change, j stays BigInteger >>> isinstance(j, System.Int32) False >>> j = System.Int32(j) # conversion fails Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: Arithmetic operation resulted in an overflow. Such explicit conversions are in most cases unnecessary since the runtime recognizes >>> import System >>> int_list = System.Collections.Generic.List[int]() >>> int_list.Add(1) # Int32 instance converted to BigInteger >>> int32_list = System.Collections.Generic.List[System.Int32]() >>> int32_list.Add((1).ToBigInteger()) # BigInteger instance converted to Int32 >>> int_list[0] == int32_list[0] True When an In IronPython 2, >>> # IronPython 2 >>> i = 1 # instance of Int32 (int) >>> j = 1 << 64 # instance of BigInteger (long) >>> import System >>> j.GetWords() Array[UInt32]((0, 0, 1)) >>> i.GetWords() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'GetWords' >>> long.GetWords(i) Array[UInt32]((1)) >>> # IronPython 3 >>> i = 1 # instance of Int32 (int) >>> j = 1 << 64 # instance of BigInteger (int) >>> import clr >>> clr.AddReference("Microsoft.Dynamic") >>> import Microsoft.Scripting.Utils.MathUtils >>> clr.ImportExtensions(Microsoft.Scripting.Utils.MathUtils) >>> j.GetWords() Array[UInt32]((0, 0, 1)) >>> i.GetWords() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'GetWords' >>> Microsoft.Scripting.Utils.MathUtils.GetWords(i) Array[UInt32]((1)) Another set of Python-hidden methods on # IronPython 2 j = long(1) i64 = j.ToInt64() # IronPython 3 import System j = (1).ToBigInteger() i64 = System.Int64(j) IronPython's Still looking for more? Browse the Discussions tab, where you can ask questions to the IronPython community. ๐ | |||||||||
| You are visitor 01402503 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |