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.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..b384787
--- /dev/null
+++ b/OpenTap.Python/DebugServer.cs
@@ -0,0 +1,57 @@
+using System;
+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)
+ {
+ throw new Exception("Only one debug client can be attached at a time");
+ }
+ handler = new DebugServerClientHandler(cli);
+ }
+ }
+
+ 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..dd422d6
--- /dev/null
+++ b/OpenTap.Python/DebugServerClientHandler.cs
@@ -0,0 +1,708 @@
+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);
+ 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; }
+ }
+
+ 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()
+ {
+ while (client.Connected)
+ {
+ TapThread.ThrowIfAborted();
+ var msg = ReadMessage();
+ 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.
+ }
+ }
+ }
+ }
+
+ 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 "disconnect":
+ 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)
+ {
+ return successObject;
+ }
+
+
+
+ //{"command":"setBreakpoints","arguments":{"source":{"name":"EnumUsage.py","path":"c:\\Keysight\\Development\\python\\bin\\Debug\\Packages\\PythonExamples\\EnumUsage.py"},"lines":[62],"breakpoints":[{"line":62}],"sourceModified":false},"type":"request","seq":3}Content-Length: 265
+ //{"seq": 6, "type": "response", "request_seq": 3, "success": true, "command": "setBreakpoints", "body": {"breakpoints": [{"verified": true, "id": 0, "source": {"name": "EnumUsage.py", "path": "c:\\Keysight\\Development\\python\\bin\\Debug\\Packages\\PythonExamples\\EnumUsage.py"}, "line": 62}]}}Content-Length: 301
+
+ object SetBreakpointsRequest(JsonElement args)
+ {
+ var idbase = breakPoints.Count;
+ var source = args.GetProperty("source");
+ var name = source.GetProperty("name").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
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/PythonPluginProvider.cs b/OpenTap.Python/PythonPluginProvider.cs
index fa828fd..28325cf 100644
--- a/OpenTap.Python/PythonPluginProvider.cs
+++ b/OpenTap.Python/PythonPluginProvider.cs
@@ -118,7 +118,7 @@ public void Search()
var mod = pyType.GetAttr("__module__").As();
if (!sources.TryGetValue(mod, out var asm))
{
- asm = sources[mod] = new PythonTypeDataSource(mod, mod);
+ asm = sources[mod] = new PythonTypeDataSource(mod, py);
}
var td = TypeData.FromType(type);
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 73636c7..486e3ad 100644
Binary files a/Python.Dependencies/Python.Runtime.dll and b/Python.Dependencies/Python.Runtime.dll differ