Skip to content

Commit

Permalink
Script: Ironpython update. from 2.7 to 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Jul 21, 2023
1 parent 142d367 commit 95b77f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
16 changes: 16 additions & 0 deletions ExtLibs/Utilities/StringRedirectWriter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
using System;
using System.Text;
using System.IO;
using System.Linq;

namespace MissionPlanner.Utilities
{
public delegate void StringWrittenEvent(object sender, string writtenString);

public class StringRedirectStream : MemoryStream
{
public StringRedirectStream(StringRedirectWriter outputWriter)
{
OutputWriter = outputWriter;
}

public StringRedirectWriter OutputWriter { get; set; }

public override void Write(byte[] buffer, int offset, int count)
{
OutputWriter.Write(buffer.Select(a=>(char)a).ToArray(), offset, count);
}
}

/// <summary>
/// This class only pretends to be a string writer
/// when in fact it raises an event whenever a string is written
Expand Down
4 changes: 2 additions & 2 deletions MissionPlanner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="GeoJSON.Net" version="1.1.64" targetFramework="net462" />
<PackageReference Include="IronPython" version="2.7.8.1" targetFramework="net462" />
<PackageReference Include="IronPython.StdLib" version="2.7.8.1" targetFramework="net462" />
<PackageReference Include="IronPython" version="3.4.1" targetFramework="net462" />
<PackageReference Include="IronPython.StdLib" version="3.4.1" targetFramework="net462" />
<PackageReference Include="JetBrains.Profiler.SelfApi">
<Version>2.0.2</Version>
</PackageReference>
Expand Down
8 changes: 4 additions & 4 deletions Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public Script(bool redirectOutput = false)
scope.SetVariable("mavutil", this);
scope.SetVariable("Joystick", MainV2.joystick);

engine.CreateScriptSourceFromString("print 'hello world from python'").Execute(scope);
engine.CreateScriptSourceFromString("print cs.roll").Execute(scope);
engine.CreateScriptSourceFromString("print('hello world from python')").Execute(scope);
engine.CreateScriptSourceFromString("print(cs.roll)").Execute(scope);

if (redirectOutput)
{
//Redirect output through this writer
//this writer will not actually write to the memorystreams
OutputWriter = new Utilities.StringRedirectWriter();
engine.Runtime.IO.SetErrorOutput(new MemoryStream(), OutputWriter);
engine.Runtime.IO.SetOutput(new MemoryStream(), OutputWriter);
engine.Runtime.IO.SetErrorOutput(new StringRedirectStream(OutputWriter), OutputWriter);
engine.Runtime.IO.SetOutput(new StringRedirectStream(OutputWriter), OutputWriter);
}
else
OutputWriter = null;
Expand Down

0 comments on commit 95b77f7

Please sign in to comment.