From 65ecd5ad19472fb4a5198bf43027ad76096aa38f Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Wed, 23 Oct 2024 09:54:05 -0500 Subject: [PATCH 1/3] add runsettings to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cc7e4a65..3b988c9e 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ coverage.xml *.vcxproj.filters *.userprefs *.DotSettings.user +.runsettings # Build results [Bb]in/ From 603f33246a60650be9576951a64f62de9f9e5250 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Wed, 23 Oct 2024 09:55:00 -0500 Subject: [PATCH 2/3] Add optional argument to wrap interface in ToPython This is used by ToPythonAs in this fork. In the upstream pythonnet, it is effectively always true --- src/runtime/Converter.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/runtime/Converter.cs b/src/runtime/Converter.cs index 5971ff6a..6b939c62 100644 --- a/src/runtime/Converter.cs +++ b/src/runtime/Converter.cs @@ -117,7 +117,9 @@ private static Func GetIsTransparentProxy() internal static NewReference ToPythonDetectType(object? value) => value is null ? new NewReference(Runtime.PyNone) : ToPython(value, value.GetType()); - internal static NewReference ToPython(object? value, Type type) + + + internal static NewReference ToPython(object? value, Type type, bool wrapInterface = false) { if (value is PyObject pyObj) { @@ -144,6 +146,12 @@ internal static NewReference ToPython(object? value, Type type) return CLRObject.GetReference(value, type); } + if (wrapInterface && type.IsInterface) + { + var ifaceObj = (InterfaceObject)ClassManager.GetClassImpl(type); + return ifaceObj.TryWrapObject(value); + } + // it the type is a python subclass of a managed type then return the // underlying python object rather than construct a new wrapper object. if (value is IPythonDerivedType pyderived) @@ -979,7 +987,7 @@ public static PyObject ToPython(this object? o) public static PyObject ToPythonAs(this T? o) { if (o is null) return Runtime.None; - return Converter.ToPython(o, typeof(T)).MoveToPyObject(); + return Converter.ToPython(o, typeof(T), true).MoveToPyObject(); } } } From f2df77865591bcb99f4229556b33d1f54cfdbdbb Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Wed, 23 Oct 2024 09:58:48 -0500 Subject: [PATCH 3/3] update readme --- README.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 00d4c722..4b78594c 100644 --- a/README.rst +++ b/README.rst @@ -3,5 +3,7 @@ Ansys fork of `pythonnet `_. We will try to keep this up-to-date with pythonnet and upstream changes that might benefit the pythonnet community Changes relative to pythonnet: -- Revert of `#1240 `_. -- Enum REPR `#2239 ` is included in this release of version 3.0.2, but is unreleased in pythonnet \ No newline at end of file + +* Revert of `#1240 `_. +* Enum REPR `#2239 ` is included in this release of version 3.0.2, but is unreleased in pythonnet +* Opt-into explicit interface wrapping, `#19 `. This opts into the behavior that became the default in #1240 if ToPythonAs is explicitly used \ No newline at end of file