Skip to content

Commit 79ede22

Browse files
authored
Fix to pythonas (#19)
Fix ToPythonAs<> so that it always returns an explicit interface wrapper. ToPythonAs opts into the converter's behavior that this fork doesn't use by default, but it is still valid behavior when ToPythonAs is used
2 parents 77e44a5 + f2df778 commit 79ede22

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ coverage.xml
3636
*.vcxproj.filters
3737
*.userprefs
3838
*.DotSettings.user
39+
.runsettings
3940

4041
# Build results
4142
[Bb]in/

README.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ Ansys fork of `pythonnet <https://github.com/pythonnet/pythonnet>`_.
33
We will try to keep this up-to-date with pythonnet and upstream changes that might benefit the pythonnet community
44

55
Changes relative to pythonnet:
6-
- Revert of `#1240 <https://github.com/pythonnet/pythonnet/pull/1240>`_.
7-
- Enum REPR `#2239 <https://github.com/pythonnet/pythonnet/pull/2239>` is included in this release of version 3.0.2, but is unreleased in pythonnet
6+
7+
* Revert of `#1240 <https://github.com/pythonnet/pythonnet/pull/1240>`_.
8+
* Enum REPR `#2239 <https://github.com/pythonnet/pythonnet/pull/2239>` is included in this release of version 3.0.2, but is unreleased in pythonnet
9+
* Opt-into explicit interface wrapping, `#19 <https://github.com/ansys/ansys-pythonnet/pull/19>`. This opts into the behavior that became the default in #1240 if ToPythonAs<T> is explicitly used

src/runtime/Converter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ private static Func<object, bool> GetIsTransparentProxy()
117117

118118
internal static NewReference ToPythonDetectType(object? value)
119119
=> value is null ? new NewReference(Runtime.PyNone) : ToPython(value, value.GetType());
120-
internal static NewReference ToPython(object? value, Type type)
120+
121+
122+
internal static NewReference ToPython(object? value, Type type, bool wrapInterface = false)
121123
{
122124
if (value is PyObject pyObj)
123125
{
@@ -144,6 +146,12 @@ internal static NewReference ToPython(object? value, Type type)
144146
return CLRObject.GetReference(value, type);
145147
}
146148

149+
if (wrapInterface && type.IsInterface)
150+
{
151+
var ifaceObj = (InterfaceObject)ClassManager.GetClassImpl(type);
152+
return ifaceObj.TryWrapObject(value);
153+
}
154+
147155
// it the type is a python subclass of a managed type then return the
148156
// underlying python object rather than construct a new wrapper object.
149157
if (value is IPythonDerivedType pyderived)
@@ -979,7 +987,7 @@ public static PyObject ToPython(this object? o)
979987
public static PyObject ToPythonAs<T>(this T? o)
980988
{
981989
if (o is null) return Runtime.None;
982-
return Converter.ToPython(o, typeof(T)).MoveToPyObject();
990+
return Converter.ToPython(o, typeof(T), true).MoveToPyObject();
983991
}
984992
}
985993
}

0 commit comments

Comments
 (0)