diff --git a/Directory.Build.props b/Directory.Build.props index 14c1434..c30b688 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -6,7 +6,7 @@ true netstandard2.0 en - 9.18.4 + 9.19.3 true diff --git a/Documentation/Release_Notes/ReleaseNotes_3_0.md b/Documentation/Release_Notes/ReleaseNotes_3_0.md index d79dc89..dd07d32 100644 --- a/Documentation/Release_Notes/ReleaseNotes_3_0.md +++ b/Documentation/Release_Notes/ReleaseNotes_3_0.md @@ -4,7 +4,7 @@ Version 3.0 is a breaking release. This means that what was previously supported ## New Features -- Python future compatibility. This version will support Python 3.7 and all newer / future versions. +- Improved Python compatibility. This version will support Python 3.7 - 3.11. - It is no longer necessary to 'build' the python modules before using them. - Every .NET type can be inherited from and every plugin type can be created. - Added support for Mac OS. @@ -18,6 +18,6 @@ Version 3.0 is a breaking release. This means that what was previously supported - Python 2.X is no longer supported. - Python versions less or equal to 3.6 are also not supported. -That means, at the time of writing Python 3.7, 3.8, 3.9, 3.10 and 3.11 are supported, but the new Python Plugin is future compatible, so 3.12 and onwards are expected to be supported as well. +That means, at the time of writing Python 3.7, 3.8, 3.9, 3.10 and 3.11 are supported. - It is no longer possible to build a C# DLL containing a C# API for the Python code. diff --git a/OpenTap.Python.Examples/UserInputExample.py b/OpenTap.Python.Examples/UserInputExample.py index 3d36eb1..c67b4bb 100644 --- a/OpenTap.Python.Examples/UserInputExample.py +++ b/OpenTap.Python.Examples/UserInputExample.py @@ -4,12 +4,18 @@ from System import String, Object, Double import System.Threading import OpenTap -from OpenTap import Display +from OpenTap import Display, Submit from opentap import * +from enum import Enum + + +class OkEnum(Enum): + OK = 1 # Notice, this class inherits from System.Object(see line 4), a .NET class, not the default python object class. class BasicUserInput(Object): Frequency = property(Double, 1.0).add_attribute(Display("Frequency", "The selected frequency.")) + Ok = property(OkEnum, OkEnum.OK).add_attribute(Submit()) def __init__(self): super().__init__() diff --git a/OpenTap.Python.ProjectTemplate/OpenTap.Python.ProjectTemplate.Api/OpenTap.Python.ProjectTemplate.Api.csproj b/OpenTap.Python.ProjectTemplate/OpenTap.Python.ProjectTemplate.Api/OpenTap.Python.ProjectTemplate.Api.csproj index b06dd35..5520271 100644 --- a/OpenTap.Python.ProjectTemplate/OpenTap.Python.ProjectTemplate.Api/OpenTap.Python.ProjectTemplate.Api.csproj +++ b/OpenTap.Python.ProjectTemplate/OpenTap.Python.ProjectTemplate.Api/OpenTap.Python.ProjectTemplate.Api.csproj @@ -9,9 +9,9 @@ - - - + + + diff --git a/OpenTap.Python/DebugServer.cs b/OpenTap.Python/DebugServer.cs new file mode 100644 index 0000000..8f1f06a --- /dev/null +++ b/OpenTap.Python/DebugServer.cs @@ -0,0 +1,60 @@ +using System.Net.Sockets; +using Python.Runtime; + +namespace OpenTap.Python; + +/// A custom debug server emulating what pydebug can do. +class DebugServer +{ + public static DebugServer Instance { get; } = new (); + + public int Port { get; set; } + + TcpListener listener; + TapThread thread; + DebugServerClientHandler handler; + + private DebugServer(){} + + public void Start() + { + listener = new TcpListener(Port); + listener.Start(); + thread = TapThread.Start(AcceptClient); + } + + void AcceptClient() + { + while (TapThread.Current.AbortToken.IsCancellationRequested == false) + { + var cli = listener.AcceptTcpClient(); + if (handler != null) + { + Log.CreateSource("Debug").Error("Only one debugging client can be connected at a time."); + cli.Close(); + continue; + } + + handler = new DebugServerClientHandler(cli); + handler.Disconnected += () => handler = null; + } + } + + public int TraceCallback(PyObject arg1, Runtime.PyFrameObject arg2, Runtime.TraceWhat arg3, PyObject arg4) + { + // the active threads always needs to be updated. + if(arg3 == Runtime.TraceWhat.Call) + DebugServerClientHandler.GetThreadId(TapThread.Current); + handler?.TraceCallback(arg1, arg2, arg3, arg4); + return 0; + } + + public void Stop() + { + listener.Stop(); + thread.Abort(); + thread = null; + listener = null; + handler = null; + } +} \ No newline at end of file diff --git a/OpenTap.Python/DebugServerClientHandler.cs b/OpenTap.Python/DebugServerClientHandler.cs new file mode 100644 index 0000000..1a66ae9 --- /dev/null +++ b/OpenTap.Python/DebugServerClientHandler.cs @@ -0,0 +1,756 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Runtime.CompilerServices; +using System.Text.Json; +using System.Threading; +using Python.Runtime; + +namespace OpenTap.Python; + +/// +/// This class emulated the behavior of the pydebug server, but it is written entirely in C#. +/// +/// This implements the Debug Adapter Protocol also described here: https://microsoft.github.io/debug-adapter-protocol/specification +/// +class DebugServerClientHandler +{ + record BreakStackFrames(StackFrame[] stackFrames, int totalFrames, int threadId); + record StackFrame(int id, string name, int line, int column, object source); + class NextLine + { + public int ThreadId { get; set; } + public int CurrentLine { get; set; } + public string Name { get; set; } + public int BreakLevel { get; set; } + public string Mode { get; set; } + } + + static readonly object successObject = new (); + static int threadCounter = 0; + static readonly ConditionalWeakTable knownThreads = new(); + static readonly List> threads = new(); + + // a running number for marking each response. + int responseSeq; + + readonly ConcurrentQueue<(string, object)> events = new (); + readonly TcpClient client; + readonly StreamReader reader; + readonly StreamWriter writer; + readonly object clientLock = new (); + readonly Dictionary> breakPoints = new (); + + // incremented when execution is waiting for continuing. When it is non-zero, + // it means that we are waiting to continue. + int isWaiting; + object debugState; + + // These values are set when a breakpoint is hit, otherwise they wont + // have valid state. + BreakStackFrames breakStackFrames = null; + ConcurrentQueue processor; + CancellationToken breakCancel; + Dictionary ScopeVariableReferences; + Dictionary<(PyObject, PyObject), int> ScopeVariableReferenceLookup; + + readonly ConcurrentDictionary sourceReference = new(); + + public DebugServerClientHandler(TcpClient client) + { + this.client = client; + var stream = client.GetStream(); + reader = new StreamReader(stream); + writer = new StreamWriter(stream); + TapThread.Start(ProcessRequests); + TapThread.Start(ProcessEvents); + } + + void SendMessage(object obj) + { + var retstr = JsonSerializer.Serialize(obj); + var enc = $"Content-Length: {retstr.Length}\r\n\r\n"; + var content = enc + retstr; + writer.Write(content); + writer.Flush(); + } + + JsonDocument ReadMessage() + { + int contentLength = 0; + while (true) + { + var line = reader.ReadLine(); + if (line == null) break; + var s = line.Split(':').Select(x => x.Trim()).ToArray(); + if (s.Length > 1) + { + if (s[0] == "Content-Length") + { + contentLength = int.Parse(s[1]); + } + continue; + } + + if (line == "") + { + char[] bytes = new char[contentLength]; + reader.Read(bytes, 0, contentLength); + int reqSeq = -1; + string type = null; + + var js = JsonDocument.Parse(bytes); + + if (js.RootElement.TryGetProperty("type", out var reqElem)) + type = reqElem.GetString(); + + if (js.RootElement.TryGetProperty("seq", out var seqElem)) + reqSeq = seqElem.GetInt32(); + + if (type != "request") + throw new Exception("Invalid type of message."); + if (reqSeq == -1) + throw new Exception("Invalid sequence number"); + return js; + } + } + + return null; + } + + void ProcessEvents() + { + while (client.Connected) + { + TapThread.ThrowIfAborted(); + if (events.TryDequeue(out var r)) + { + lock (clientLock) + { + try + { + var evt = WrapEvent(r.Item1, r.Item2); + SendMessage(evt); + } + catch + { + // lost event + } + } + } + else + { + TapThread.Sleep(100); + } + } + } + + void ProcessRequests() + { + try + { + while (client.Connected) + { + TapThread.ThrowIfAborted(); + var msg = ReadMessage(); + if (msg == null) continue; + var command = msg.RootElement.GetProperty("command").GetString(); + var reqseq = msg.RootElement.GetProperty("seq").GetInt32(); + lock (clientLock) + { + msg.RootElement.TryGetProperty("arguments", out var arguments); + try + { + var responseBody = ProcessCommand(command, arguments); + if (responseBody != null) + SendMessage(WrapResponse(command, responseBody, reqseq)); + } + catch + { + // lost event. + } + } + } + } + finally + { + Disconnected?.Invoke(); + } + } + + object WrapEvent(string eventName, object body) + { + if (body == null || body == successObject) + { + return new + { + @event = eventName, + type = "event", + seq = Interlocked.Increment(ref responseSeq) + }; + } + + return new + { + @event = eventName, + type = "event", + seq = Interlocked.Increment(ref responseSeq), + body = body + }; + } + + object WrapResponse(string cmd, object body, int reqseq) + { + if (body == successObject) + { + return new + { + seq = Interlocked.Increment(ref responseSeq), + type = "response", + request_seq = reqseq, + success = true, + command = cmd + }; + } + return new + { + seq = Interlocked.Increment(ref responseSeq), + type = "response", + request_seq = reqseq, + success = true, + command = cmd, + body = body + }; + } + + void PushEvent(string name, object evt) + { + events.Enqueue((name, evt)); + } + + object ProcessCommand(string command, JsonElement args) + { + switch (command) + { + case "initialize": + return InitializeRequest(); + case "attach": + return AttachRequest(args); + case "setBreakpoints": + return SetBreakpointsRequest(args); + case "setFunctionBreakpoints": + return SetFunctionBreakpointsRequest(args); + case "setExceptionBreakpoints": + return SetExceptionBreakpointsRequest(args); + case "configurationDone": + return successObject; + case "threads": + return ThreadsRequest(); + case "stackTrace": + return StackTraceRequest(args); + case "source": + return SourceRequest(args); + case "variables": + return VariablesRequest(args); + case "next": + return NextRequest(args); + case "continue": + return ContinueRequest(args); + case "stepIn": + return StepInRequest(args); + case "stepOut": + return StepOutRequest(args); + case "disconnect": + client.Close(); + return successObject; + case "scopes": + return ScopesRequest(args); + } + return null; + } + + + + object InitializeRequest() + { + PushEvent("initialized", successObject); + var exception_breakpoint_filters = new object[] + { + new { + filter= "raised", + label= "Raised Exceptions", + @default= false, + description= "Break whenever any exception is raised." + } + }; + + return new + { + supportsCompletionsRequest = true, + supportsConditionalBreakpoints = true, + supportsConfigurationDoneRequest = true, + supportsDebuggerProperties = true, + supportsDelayedStackTraceLoading = true, + supportsEvaluateForHovers = true, + supportsExceptionInfoRequest = true, + supportsExceptionOptions = true, + supportsFunctionBreakpoints = true, + supportsHitConditionalBreakpoints = true, + supportsLogPoints = true, + supportsModulesRequest = true, + supportsSetExpression = true, + supportsSetVariable = true, + supportsValueFormattingOptions = true, + supportsTerminateDebuggee = true, + supportsGotoTargetsRequest = true, + supportsClipboardContext = true, + exceptionBreakpointFilters = exception_breakpoint_filters, + supportsStepInTargetsRequest = true, + }; + } + + object AttachRequest(JsonElement args) => successObject; + + object SetBreakpointsRequest(JsonElement args) + { + var idbase = breakPoints.Count; + var source = args.GetProperty("source"); + var name = source.TryGetProperty("name", out var p) ? p.GetString() : ""; + var path = source.GetProperty("path").GetString(); + var lines = args.GetProperty("lines").EnumerateArray().Select(x => x.GetInt32()).ToArray(); + + breakPoints[name] = new HashSet(lines); + + return new + { + breakpoints = lines.Select( (line, id) => new { + verified = true, + id = idbase + id, + line = line, + source = new + { + name = name, + path = path, + } + }).ToArray() + }; + } + + object SetFunctionBreakpointsRequest(JsonElement args) + { + /*var source = args.GetProperty("source"); + var name = source.GetProperty("name").GetString(); + var path = source.GetProperty("path").GetString(); + var lines = source.GetProperty("lines").EnumerateArray().Select(x => x.GetInt32()).ToArray();*/ + return new + { + breakpoints = Array.Empty() + }; + } + + object SetExceptionBreakpointsRequest(JsonElement args) + { + return successObject; + } + + object ThreadsRequest() + { + List threadIds = new List(); + foreach (var trd in threads.ToArray()) + { + if (!trd.TryGetTarget(out var t)) + { + threads.Remove(trd); + } + else + { + threadIds.Add(GetThreadId(t)); + } + } + return new + { + + threads = threadIds + .Select(tid => + new + { + id = tid, + name = $"Thread{tid}" + } + ).ToArray() + }; + } + + object StackTraceRequest(JsonElement args) + { + int threadId = args.GetProperty("threadId").GetInt32(); + //int startFrame = args.GetProperty("startFrame").GetInt32(); + //int levels = args.GetProperty("levels").GetInt32(); + if (breakStackFrames != null && breakStackFrames.threadId == threadId) + return breakStackFrames; + + return new + { + stackFrames = new object[] { }, + totalFrames = 1 + }; + } + + object SourceRequest(JsonElement args) + { + + //:{"sourceReference":1,"source":{"path":"C:\\Keysight\\Development\\python\\bin\\Debug\\Packages\\PythonExamples\\EnumUsage.py","sourceReference":1}}, + var file = args.GetProperty("source").GetProperty("path").GetString(); + var refId = args.GetProperty("source").GetProperty("sourceReference").GetInt32(); + sourceReference[file] = refId; + if (File.Exists(file) == false) return successObject; + return new + { + content = File.ReadAllText(file), + sourceReference = refId + }; + } + + object NextRequest(JsonElement args) + { + if (debugState == null) + { + throw new InvalidOperationException("Unexpected debug state"); + } + + debugState = new NextLine + { + ThreadId = args.GetProperty("threadId").GetInt32(), + CurrentLine = breakStackFrames.stackFrames.FirstOrDefault()?.line ?? -1, + Name = breakStackFrames.stackFrames.FirstOrDefault().name, + BreakLevel = breakStackFrames.totalFrames + }; + + return successObject; + } + object StepOutRequest(JsonElement args) + { + var threadId = args.GetProperty("threadId").GetInt32(); + debugState = new NextLine + { + ThreadId = threadId, + CurrentLine = breakStackFrames.stackFrames.FirstOrDefault()?.line ?? -1, + Name = breakStackFrames.stackFrames.FirstOrDefault().name, + BreakLevel = breakStackFrames.totalFrames, + Mode = "stepOut" + }; + return successObject; + } + + object StepInRequest(JsonElement args) + { + var threadId = args.GetProperty("threadId").GetInt32(); + debugState = new NextLine + { + ThreadId = threadId, + CurrentLine = breakStackFrames.stackFrames.FirstOrDefault()?.line ?? -1, + Name = breakStackFrames.stackFrames.FirstOrDefault().name, + BreakLevel = breakStackFrames.totalFrames, + + Mode = "stepIn" + }; + return successObject; + } + + object ContinueRequest(JsonElement args) + { + if (debugState == null) + { + throw new InvalidOperationException("Unexpected debug state"); + } + + debugState = null; + return successObject; + } + + object ScopesRequest(JsonElement args) + { + var frameId = args.GetProperty("frameId").GetInt32(); + return new + { + scopes = new[] + { + new + { + name = "Locals", + variablesReference = frameId, + expensive = false, + presentationHint = "locals", + source = new {} + }, + new + { + name = "Globals", + variablesReference = 0, + expensive = false, + presentationHint = "globals", + source = new {} + } + } + }; + } + + T Process(Func f) + { + var cancel = breakCancel; + var sem = new SemaphoreSlim(0); + T result = default; + processor.Enqueue(() => + { + result = f(); + sem.Release(); + }); + try + { + sem.Wait(cancel); + } + catch + { + return default; + } + + return result; + } + + object VariablesRequest(JsonElement args) + { + return Process(() => + { + var varRef = args.GetProperty("variablesReference").GetInt32(); + if (ScopeVariableReferences.TryGetValue(varRef, out var v)) + { + bool hasKeys = v.HasAttr("keys"); + + using var items = hasKeys ? v.InvokeMethod("keys") : v.Dir(); + using var iter = new PyIterable(items); + var vars = new List(); + foreach (var i in iter) + { + try + { + bool dispose_i, dispose_value; + var value = hasKeys ? v.GetItem(i) : v.GetAttr(i); + + if (!ScopeVariableReferenceLookup.TryGetValue((value, i), out var i2)) + { + + i2 = ScopeVariableReferenceLookup[(value, i)] = ScopeVariableReferences.Count + 1; + ScopeVariableReferences[i2] = value; + dispose_i = false; + dispose_value = false; + } + else + { + dispose_i = true; + dispose_value = true; + } + + vars.Add(new + { + name = i.ToString(), + value = value.ToString(), + type = "string", + evaluateName = i.ToString(), + variablesReference = i2 + }); + if (dispose_i) + i.Dispose(); + + if (dispose_value) + value.Dispose(); + } + catch + { + continue; + } + } + + return new + { + variables = vars.ToArray() + }; + } + + return new + { + variables = new[] + { + new + { + name = "special variables" + } + } + }; + }); + } + + void WaitForContinueOrNext(PyObject pyFrameObject) + { + if (debugState != null) + throw new InvalidOperationException("Unexpected debug state."); + + var thisState = new + { + reason = "breakpoint", + threadId = GetThreadId(TapThread.Current), + preserveFocusHint = false, + allThreadsStopped = true + }; + PushEvent("stopped", + debugState = thisState + ); + // now wait until next, continue or disconnect. + + List stackFrames = new List(); + Dictionary kv = new (); + + var fi = pyFrameObject; + kv[0] = fi.GetAttr("f_globals"); + while (fi.IsNone() == false) + { + using var code = fi.GetAttr("f_code"); + using var it = fi.GetAttr("f_code").InvokeMethod("co_lines"); + using var line = fi.GetAttr("f_lineno"); + var locals = fi.GetAttr("f_locals"); + using var coName = code.GetAttr("co_name"); + using var coFilename = code.GetAttr("co_filename"); + kv[stackFrames.Count + 1] = locals; + StackFrame item = new(stackFrames.Count + 1, coName.ToString(), line.ToInt32(CultureInfo.InvariantCulture), + 1, new { path = coFilename.ToString(), sourceReference = 1 }); + stackFrames.Add(item); + var fi2 = fi; + fi = fi.GetAttr("f_back"); + fi2.Dispose(); + } + + fi.Dispose(); + + ScopeVariableReferences = kv; + ScopeVariableReferenceLookup = new(); + + breakStackFrames = new BreakStackFrames(stackFrames.ToArray(), stackFrames.Count, thisState.threadId); + var cancel = new CancellationTokenSource(); + breakCancel = cancel.Token; + processor = new ConcurrentQueue(); + Interlocked.Increment(ref isWaiting); + while (debugState == thisState) + { + if (client.Connected == false) + { + debugState = null; + } + while (processor.TryDequeue(out var item)) + { + item(); + } + TapThread.Sleep(10); + } + + + foreach (var elem in ScopeVariableReferences) + { + elem.Value.Dispose(); + } + + foreach (var elem in ScopeVariableReferenceLookup) + { + elem.Key.Item1.Dispose(); + elem.Key.Item2.Dispose(); + } + + ScopeVariableReferences.Clear(); + ScopeVariableReferences = null; + breakStackFrames = null; + ScopeVariableReferenceLookup = null; + cancel.Cancel(); + Interlocked.Decrement(ref isWaiting); + ContinuedEvent(); + } + + void ContinuedEvent() + { + PushEvent("continued", new {threadId = GetThreadId(TapThread.Current), allThreadsContinued = true}); + } + + // The current thread 'id' is currently connected to the current TAP thread. + // but it is not certain that this is a good way of doing it. + public static int GetThreadId(TapThread t0) + { + return (int)knownThreads.GetValue(t0, t => + { + lock (threads) + { + threads.Add(new WeakReference(t)); + } + return Interlocked.Increment(ref threadCounter); + }); + } + + // This trace callback is invoked from the python interpreter through the callback given to PyEval_SetTrace. + public void TraceCallback(PyObject pyObject, Runtime.PyFrameObject pyFrameObject, Runtime.TraceWhat arg3, PyObject arg4) + { + if (debugState is NextLine nl) + { + if (nl.ThreadId != GetThreadId(TapThread.Current)) + return; + if (nl.Mode == "stepIn") + { + // step in is as simple as just stepping to the next line executed. + debugState = null; + return; + } + var p1 = pyFrameObject.AsPyObject(); + int frameCount = 0; + while (p1.IsNone() == false) + { + frameCount += 1; + var p2 = p1; + p1 = p1.GetAttr("f_back"); + p2.Dispose(); + } + + if (frameCount > nl.BreakLevel) return; + if (nl.Mode == "stepOut") + { + if (frameCount > Math.Max(1, nl.BreakLevel - 1)) return; + } + debugState = null; + using var p3 = pyFrameObject.AsPyObject(); + // ok, next location found + WaitForContinueOrNext(p3); + if (frameCount == 0 && arg3 == Runtime.TraceWhat.Return) + { + debugState = null; + } + + return; + } + var i = pyFrameObject.GetLineNumber(); + + if (arg3 == Runtime.TraceWhat.Line) + { + + using var p = pyFrameObject.AsPyObject(); + foreach (var kv in breakPoints) + { + if (kv.Value.Contains(i)) + { + // breakpoint found! Wait for the user to continue somehow. + WaitForContinueOrNext(p); + return; + } + } + } + } + + public event Action Disconnected; +} \ No newline at end of file diff --git a/OpenTap.Python/FakeDebugServer.cs b/OpenTap.Python/FakeDebugServer.cs new file mode 100644 index 0000000..f7fddac --- /dev/null +++ b/OpenTap.Python/FakeDebugServer.cs @@ -0,0 +1,140 @@ +using System.IO; +using System.Linq; +using System.Net.Sockets; + +namespace OpenTap.Python; + +/// This 'fake' debug server sits between pydebug and vs code, recording all the events that takes place. +/// This is used for reverse-engineering and developing the DebugServer. +/// +class FakeDebugServer +{ + public static FakeDebugServer Instance { get; } = new(); + + public int Port { get; set; } + public int Port2 { get; set; } + + static readonly TraceSource log = Log.CreateSource("FakeDebug"); + TcpListener listener; + TapThread thread; + public void Start() + { + log.Debug("Fake debugger on port {0}", Port); + listener = new TcpListener(Port); + listener.Start(); + thread = TapThread.Start(AcceptClient); + } + + void AcceptClient() + { + while (TapThread.Current.AbortToken.IsCancellationRequested == false) + { + var cli = listener.AcceptTcpClient(); + TapThread.Start(() => HandleClient(cli)); + } + } + + public void HandleClient(TcpClient client) + { + var file1 = "session." + Port + ".txt"; + var file2 = "session." + Port2 + ".txt"; + File.Delete(file1); + File.Delete(file2); + using (var fstr1 = File.OpenWrite(file1)) + using (var fstr2 = File.OpenWrite(file2)) + using (var cli2 = new TcpClient()) + { + + cli2.Connect("localhost", Port2); + + var str1 = client.GetStream(); + var str2 = cli2.GetStream(); + TapThread.Start(() => + { + while (client.Connected) + { + TapThread.ThrowIfAborted(); + byte[] buffer = new byte[500]; + str1.ReadTimeout = 500; + try + { + if (str1.DataAvailable == false) + { + TapThread.Sleep(50); + continue; + } + + int read = str1.Read(buffer, 0, buffer.Length); + if (read == -1) + break; + + if (read > -1) + { + fstr1.Write(buffer, 0, read); + fstr1.Flush(); + try + { + log.Info("<<< {0}", + string.Join(" ", System.Text.Encoding.UTF8.GetString(buffer.Take(read).ToArray()))); + } + catch + { + + } + + str2.Write(buffer, 0, read); + } + } + catch (IOException) + { + // continue; + } + } + }); + + while (client.Connected) + { + TapThread.ThrowIfAborted(); + byte[] buffer = new byte[500]; + str2.ReadTimeout = 50; + try + { + if (str2.DataAvailable == false) + { + TapThread.Sleep(50); + continue; + } + int read = str2.Read(buffer, 0, buffer.Length); + if (read == -1) + break; + if (read > -1) + { + try + { + log.Info(">>> {0}", System.Text.Encoding.UTF8.GetString(buffer.Take(read).ToArray())); + } + catch + { + + } + + str1.Write(buffer, 0, read); + fstr2.Write(buffer, 0, read); + fstr2.Flush(); + } + } + catch (IOException) + { + + } + } + + } + } + + public void Stop() + { + listener.Stop(); + } + +} \ No newline at end of file diff --git a/OpenTap.Python/IsExternalInit.cs b/OpenTap.Python/IsExternalInit.cs new file mode 100644 index 0000000..20a539a --- /dev/null +++ b/OpenTap.Python/IsExternalInit.cs @@ -0,0 +1,10 @@ +using System.ComponentModel; + +namespace System.Runtime.CompilerServices +{ + /// + /// This needs to exist for dotnet being happy about records on .netstandard2.0. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + internal static class IsExternalInit { } +} \ No newline at end of file diff --git a/OpenTap.Python/OpenTap.Python.csproj b/OpenTap.Python/OpenTap.Python.csproj index b4a273c..e16e662 100644 --- a/OpenTap.Python/OpenTap.Python.csproj +++ b/OpenTap.Python/OpenTap.Python.csproj @@ -5,7 +5,7 @@ false false AnyCPU - ..\bin\Release + ../bin/Release false true OpenTap.Python @@ -13,13 +13,13 @@ - ..\bin\Debug + ../bin/Debug - + Directory.Build.props - + icon.png PreserveNewest @@ -28,8 +28,6 @@ - - all runtime; build; native; contentfiles; analyzers @@ -60,6 +58,12 @@ ..\Python.Dependencies\Python.Runtime.dll + + $(OutputPath)/Dependencies/System.Memory.4.0.1.1/System.Memory.dll + + + $(OutputPath)/Dependencies/System.Text.Json.4.0.1.0/System.Text.Json.dll + diff --git a/OpenTap.Python/PythonInitializer.cs b/OpenTap.Python/PythonInitializer.cs index be8463d..eaedc1a 100644 --- a/OpenTap.Python/PythonInitializer.cs +++ b/OpenTap.Python/PythonInitializer.cs @@ -6,6 +6,7 @@ using Python.Runtime; using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; @@ -72,7 +73,18 @@ static bool InitInternal() // python is installed with a package. if(pyPath != null && SharedLib.IsWin32 && PythonEngine.PythonHome == "") PythonEngine.PythonHome = pyPath; - + if (PythonSettings.Current.Debug) + { + if (PythonSettings.Current.UseFakeDebugServer) + { + FakeDebugServer.Instance.Port = PythonSettings.Current.DebugPort; + FakeDebugServer.Instance.Port2 = PythonSettings.Current.DebugPort2; + FakeDebugServer.Instance.Start(); + }else{ + DebugServer.Instance.Port = PythonSettings.Current.DebugPort; + DebugServer.Instance.Start(); + } + } PythonEngine.Initialize(false); PythonEngine.BeginAllowThreads(); log.Debug($"Loaded Python Version {PythonEngine.Version} from '{pyPath}'."); @@ -88,18 +100,16 @@ static bool InitInternal() return false; using (Py.GIL()) - { + { PyObject mod = PyModule.FromString("init_mod", loadScript); foreach (var s in PythonSettings.Current.GetSearchList()) mod.InvokeMethod("add_dir", s.ToPython()); - - try - { - Py.Import("opentap"); - } - catch (PythonException e) - { - PrintPythonException(e); + } + if (PythonSettings.Current.Debug) + { + if (!PythonSettings.Current.UseFakeDebugServer) + { + Runtime.TraceCallback += DebugServer.Instance.TraceCallback; } } } diff --git a/OpenTap.Python/PythonSettings.cs b/OpenTap.Python/PythonSettings.cs index 11489b7..63a75d9 100644 --- a/OpenTap.Python/PythonSettings.cs +++ b/OpenTap.Python/PythonSettings.cs @@ -19,7 +19,6 @@ namespace OpenTap.Python [Obfuscation(Exclude = true)] public class PythonSettings : ComponentSettings { - /// /// Makes it possible to configure a custom path to a python installation. /// @@ -73,8 +72,12 @@ public IEnumerable GetSearchPaths() => [Display("Port", "The port which debugpy uses to connect with the developer environment. This needs to match the one defined in the launch configuration.", "Debug", Order: 1)] - public int DebugPort { get; set; } = 5678; - + public int DebugPort { get; set; } = 5679; + + // packages sent to port2 is forwarded to port1. + public int DebugPort2 { get; set; } = 5678; + + public bool UseFakeDebugServer { get; set; } public PythonSettings() { diff --git a/OpenTap.Python/opentap.py b/OpenTap.Python/opentap.py index c922177..5b79324 100644 --- a/OpenTap.Python/opentap.py +++ b/OpenTap.Python/opentap.py @@ -52,15 +52,6 @@ def install_package(file): debugpy_imported = False -try: - # setup debugging this is done using debugpy, but is an optional feature. - if OpenTap.Python.PythonSettings.Current.Debug: - import debugpy - debugpy.configure(subProcess = False) - debugpy.listen(OpenTap.Python.PythonSettings.Current.DebugPort) - debugpy_imported = True -except Exception as e: - print("Could not enable debugging: " + str(e)) attribute = clr.attribute @@ -70,6 +61,9 @@ def debug_this_thread(): else: pass + +attribute = clr.attribute + class Rule(OpenTap.Python.VirtualValidationRule): def __init__(self, property, validFunc, errorFunc): super(Rule, self).__init__(property) @@ -144,6 +138,18 @@ def flush(self): sys.stdout = Logger() sys.stderr = Logger(OpenTap.LogEventType.Error) +try: + # setup debugging this is done using debugpy, but is an optional feature. + if OpenTap.Python.PythonSettings.Current.UseFakeDebugServer: + import debugpy + debugpy.configure(subProcess = False) + print("Debugger on port: ", OpenTap.Python.PythonSettings.Current.DebugPort2) + debugpy.listen(OpenTap.Python.PythonSettings.Current.DebugPort2) + debugpy_imported = True +except Exception as e: + print("Could not enable debugging: " + str(e)) + + def reload_module(module): """Internal: Reloads modules and sub-modules. Similar to imp.reload, but recurses to included sub-modules.""" import imp diff --git a/Python.Dependencies/Python.Runtime.dll b/Python.Dependencies/Python.Runtime.dll index 010cf3c..486e3ad 100644 Binary files a/Python.Dependencies/Python.Runtime.dll and b/Python.Dependencies/Python.Runtime.dll differ