Skip to content

Commit e84a1ef

Browse files
authored
Add to 2 to 3 upgrade guide (#1598)
* Add note about None * Add note about redirecting IO
1 parent 66d0292 commit e84a1ef

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

Documentation/upgrading-from-ipy2.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

5-
## Binary Compatibility
5+
## Binary compatibility
66

77
The IronPython 3 binaries are not compatible with the IronPython 2 binaries. Modules compiled with `clr.CompileModules` using IronPython 2 are not compatible and will need to be recompiled using IronPython 3.
88

@@ -15,6 +15,22 @@ if sys.implementation.name == "ironpython":
1515
print("IronPython!")
1616
```
1717

18+
## `None` is a keyword
19+
20+
`None` is a keyword in Python 3 and trying to access a member called `None` will raise a `SyntaxError`. Since this name is frequently used in .NET code (e.g. in enums), code trying to use it is going to throw. You can use alternate syntax in order to access the .NET member, for example `getattr(x, "None")` or an accessor for enums `MyEnum["None"]`.
21+
22+
```python
23+
# IronPython 2
24+
System.StringSplitOptions.None
25+
```
26+
27+
```python
28+
# IronPython 3
29+
System.StringSplitOptions["None"]
30+
```
31+
32+
Similarly, `True` and `False` are also keywords in Python 3.
33+
1834
## `int` Type
1935

2036
One of the major backward incompatible changes in Python 3 is [PEP 237 – Unifying Long Integers and Integers][PEP 0237]: Essentially, `long` renamed to `int`. That is, there is only one built-in integral type, named `int`; but it behaves mostly like the old `long` type. From the pure Python perspective this means that `int` should be used wherever previously `long` was used. More consideration has to be applied in interop cases with .NET.
@@ -80,7 +96,7 @@ False
8096
'0x7fffffff'
8197
```
8298

83-
The creation of either `Int32` or `BigInteger` instances happens automatically by the `int` constructor. If for interop purposes it is important to create a `BigInteger` (despite the value fitting in 32 bits), use method `ToBigInteger`. It converts `Int32` values to `BigInteger` and leaves `BigInteger` values unaffected.
99+
The creation of either `Int32` or `BigInteger` instances happens automatically by the `int` constructor. If for interop purposes it is important to create a `BigInteger` (despite the value fitting in 32 bits), use method `ToBigInteger`. It converts `Int32` values to `BigInteger` and leaves `BigInteger` values unaffected.
84100

85101
```pycon
86102
>>> bi = i.ToBigInteger()
@@ -188,5 +204,29 @@ i64 = System.Int64(j)
188204

189205
IronPython's `range` is a generator that produces a sequence of `int` values. The values are instances of `Int32` or `BigInteger`, depending on the actual integer value they represent. When `range` is used in a LINQ context, it exposes interface `IEnumerable<Int32>` and all values generated are of type `Int32`. This limits the possible value to the range `Int32.MinValue` to `Int32.MaxValue`.
190206

191-
192207
[PEP 0237]: https://python.org/dev/peps/pep-0237
208+
209+
210+
## Redirecting output
211+
212+
With IronPython 2, standard output was written to the runtime's `SharedIO.OutputWriter` (which was `Console.Out` by default). This is no longer the case with IronPython 3 where the standard output is a binary stream. The output is now written to runtime's `SharedIO.OutputStream`. Similarly, standard input and error are now using `SharedIO.InputStream` and `SharedIO.ErrorStream` respectively.
213+
214+
Because of this, using a `TextWriter` to capture output will no longer work. As a workaround, in order to use a `TextWriter` as the main method of redirection, one could wrap the writer inside a stream (for example, see [TextStream][TextStream]).
215+
216+
IronPython 2
217+
```c#
218+
var engine = Python.CreateEngine();
219+
var textWriter = new MyTextWriter();
220+
// no longer works!
221+
engine.Runtime.IO.RedirectToConsole();
222+
Console.SetOut(textWriter);
223+
```
224+
225+
IronPython 3
226+
```c#
227+
var engine = Python.CreateEngine();
228+
var textWriter = new MyTextWriter();
229+
engine.Runtime.IO.SetOutput(new TextStream(textWriter), textWriter);
230+
```
231+
232+
[TextStream]: https://github.com/IronLanguages/main/blob/master/Runtime/Microsoft.Dynamic/Utils/TextStream.cs

0 commit comments

Comments
 (0)