8000 fixup! formatted PySysIO · yagweb/pythonnetLab@428ccbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 428ccbe

Browse files
committed
fixup! formatted PySysIO
1 parent 8926aae commit 428ccbe

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

Test/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Program
1010
static int Main(string[] args)
1111
{
1212
new TestSysIO().ToConsoleOut();
13+
new TestSysIO().ToNullTextWriter();
1314
new TestSysIO().ToTextWriter();
1415
//TestMatplotlib.Plot();
1516
//TestMatplotlib.PlotInWindow();

Test/TestSysIO.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Python.Runtime;
2+
using System;
23
using System.IO;
34

45
namespace Test
@@ -7,25 +8,42 @@ class TestSysIO
78
{
89
public void ToConsoleOut()
910
{
10-
// redirect to Console.Out
11+
// redirect to Console.Out, I usually put this at the entrance of a program.
1112
PySysIO.ToConsoleOut();
13+
1214
//test
1315
using (Py.GIL())
1416
{
15-
PythonEngine.RunSimpleString("print('hello pythonnet!')");
17+
PythonEngine.RunSimpleString("print('hello ConsoleOut!')");
18+
}
19+
}
20+
21+
public void ToNullTextWriter()
22+
{
23+
// redirect to a TextWriter
24+
PySysIO.ToTextWriter();
25+
26+
//test
27+
using (Py.GIL())
28+
{
29+
PythonEngine.RunSimpleString("print('hello NullTextWriter!')");
1630
}
1731
}
1832

1933
public void ToTextWriter()
2034
{
2135
// redirect to a TextWriter
22-
var writer = new StringWriter();
23-
PySysIO.ToTextWriter(writer);
36+
PySysIO.ToTextWriter(new StringWriter());
37+
38+
//test
2439
using (Py.GIL())
2540
{
26-
PythonEngine.RunSimpleString("print('hello pythonnet!')");
41+
PythonEngine.RunSimpleString("print('hello TextWriter!')");
2742
}
43+
44+
var writer = PySysIO.TextWriter as StringWriter;
2845
var content = writer.GetStringBuilder().ToString();
46+
Console.WriteLine(content);
2947
}
3048
}
3149
}

pynetLab/PySysIO.cs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ def close(self):
2929

3030
private static SysIOWriter SysIOStream = new SysIOWriter();
3131

32+
public static TextWriter TextWriter
33+
{
34+
get
35+
{
36+
return SysIOStream.TextWriter;
37+
}
38+
}
39+
3240
public static TextWriter ToTextWriter(TextWriter writer = null)
3341
{
3442
using (Py.GIL())
3543
{
36-
if(writer != null)
37-
{
38-
SysIOStream.TextWriter = writer;
39-
}
44+
SysIOStream.TextWriter = writer;
4045
dynamic sys = Py.Import("sys");
4146
sys.stdout = sys.stderr = SysIOStream;
4247
}
@@ -54,62 +59,48 @@ public static void Flush()
5459
/// </summary>
5560
public class SysIOWriter
5661
{
62+
private TextWriter _TextWriter;
5763
public TextWriter TextWriter
5864
{
59-
get;
60-
internal set;
65+
get
66+
{
67+
return _TextWriter == null ? Console.Out : _TextWriter;
68+
}
69+
set
70+
{
71+
_TextWriter = value;
72+
}
6173
}
6274

6375
public SysIOWriter(TextWriter writer = null)
6476
{
65-
TextWriter = writer;
77+
this._TextWriter = writer;
6678
}
6779

6880
public void write(String str)
6981
{
70-
str = str.Replace("\n", Environment.NewLine);
71-
if (TextWriter != null)
72-
{
73-
TextWriter.Write(str);
74-
}
75-
else
76-
{
77-
Console.Out.Write(str);
78-
}
82+
//str = str.Replace("\n", Environment.NewLine);
83+
this.TextWriter.Write(str);
7984
}
8085

8186
public void writelines(String[] str)
8287
{
8388
foreach (String line in str)
8489
{
85-
if (TextWriter != null)
86-
{
87-
TextWriter.Write(str);
88-
}
89-
else
90-
{
91-
Console.Out.Write(str);
92-
}
90+
this.write(line);
9391
}
9492
}
9593

9694
public void flush()
9795
{
98-
if (TextWriter != null)
99-
{
100-
TextWriter.Flush();
101-
}
102-
else
103-
{
104-
Console.Out.Flush();
105-
}
96+
this.TextWriter.Flush();
10697
}
10798

10899
public void close()
109100
{
110-
if (TextWriter != null)
101+
if (this._TextWriter != null)
111102
{
112-
TextWriter.Close();
103+
this._TextWriter.Close();
113104
}
114105
}
115106
}

0 commit comments

Comments
 (0)
0