8000 Make .NET objects that have `__call__` method callable from Python by lostmsu · Pull Request #1589 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Make .NET objects that have __call__ method callable from Python #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ See [Mixins/collections.py](src/runtime/Mixins/collections.py).
- .NET arrays implement Python buffer protocol
- Python.NET will correctly resolve .NET methods, that accept `PyList`, `PyInt`,
and other `PyObject` derived types when called from Python.
- .NET classes, that have `__call__` method are callable from Python
- `PyIterable` type, that wraps any iterable object in Python


Expand Down
87 changes: 87 additions & 0 deletions src/embed_tests/CallableObject.cs
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
public class CallableObject
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
using var locals = new PyDict();
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals.Handle);
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider());
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}
[Test]
public void CallMethodMakesObjectCallable()
{
var doubler = new DerivedDoubler();
dynamic applyObjectTo21 = PythonEngine.Eval("lambda o: o(21)");
Assert.AreEqual(doubler.__call__(21), (int)applyObjectTo21(doubler.ToPython()));
}
[Test]
public void CallMethodCanBeInheritedFromPython()
{
var callViaInheritance = new CallViaInheritance();
dynamic applyObjectTo14 = PythonEngine.Eval("lambda o: o(14)");
Assert.AreEqual(callViaInheritance.Call(14), (int)applyObjectTo14(callViaInheritance.ToPython()));
}

[Test]
public void CanOverwriteCall()
{
var callViaInheritance = new CallViaInheritance();
using var scope = Py.CreateScope();
scope.Set("o", callViaInheritance);
scope.Exec("orig_call = o.Call");
scope.Exec("o.Call = lambda a: orig_call(a*7)");
int result = scope.Eval<int>("o.Call(5)");
Assert.AreEqual(105, result);
}

class Doubler
{
public int __call__(int arg) => 2 * arg;
}

class DerivedDoubler : Doubler { }

class CallViaInheritance
{
public const string BaseClassName = "Forwarder";
public static readonly string BaseClassSource = $@"
class MyCallableBase:
def __call__(self, val):
return self.Call(val)

class {BaseClassName}(MyCallableBase): pass
";
public int Call(int arg) => 3 * arg;
}

class CustomBaseTypeProvider : IPythonBaseTypeProvider
{
internal static PyType BaseClass;

public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
{
Assert.Greater(BaseClass.Refcount, 0);
return type != typeof(CallViaInheritance)
? existingBases
: new[] { BaseClass };
}
}
}
}
42 changes: 42 additions & 0 deletions src/runtime/classbase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Python.Runtime
Expand Down Expand Up @@ -557,5 +560,44 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)

return 0;
}

static IntPtr tp_call_impl(IntPtr ob, IntPtr args, IntPtr kw)
{
IntPtr tp = Runtime.PyObject_TYPE(ob);
var self = (ClassBase)GetManagedObject(tp);

if (!self.type.Valid)
{
return Exceptions.RaiseTypeError(self.type.DeletedMessage);
}

Type type = self.type.Value;

var calls = GetCallImplementations(type).ToList();
Debug.Assert(calls.Count > 0);
var callBinder = new MethodBinder();
foreach (MethodInfo call in calls)
{
callBinder.AddMethod(call);
}
return callBinder.Invoke(ob, args, kw);
}

static IEnumerable<MethodInfo> GetCallImplementations(Type type)
=> type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.Name == "__call__");

static readonly Interop.TernaryFunc tp_call_delegate = tp_call_impl;

public virtual void InitializeSlots(SlotsHolder slotsHolder)
{
if (!this.type.Valid) return;

if (GetCallImplementations(this.type.Value).Any()
&& !slotsHolder.IsHolding(TypeOffset.tp_call))
{
TypeManager.InitializeSlot(ObjectReference, TypeOffset.tp_call, tp_call_delegate, slotsHolder);
}
}
}
}
3 changes: 3 additions & 0 deletions src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(R
Runtime.PyType_Modified(pair.Value.TypeReference);
var context = contexts[pair.Value.pyHandle];
pair.Value.Load(context);
var slotsHolder = TypeManager.GetSlotsHolder(pyType);
pair.Value.InitializeSlots(slotsHolder);
Runtime.PyType_Modified(pair.Value.TypeReference);
loadedObjs.Add(pair.Value, context);
}

Expand Down
9 changes: 7 additions & 2 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,13 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
return ThunkInfo.Empty;
}
Delegate d = Delegate.CreateDelegate(dt, method);
var info = new ThunkInfo(d);
allocatedThunks[info.Address] = d;
return GetThunk(d);
}

internal static ThunkInfo GetThunk(Delegate @delegate)
{
var info = new ThunkInfo(@delegate);
allocatedThunks[info.Address] = @delegate;
return info;
}

Expand Down
14 changes: 14 additions & 0 deletions src/runtime/pytype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ internal static BorrowedReference GetBase(BorrowedReference type)
return new BorrowedReference(basePtr);
}

internal static BorrowedReference GetBases(BorrowedReference type)
{
Debug.Assert(IsType(type));
IntPtr basesPtr = Marshal.ReadIntPtr(type.DangerousGetAddress(), TypeOffset.tp_bases);
return new BorrowedReference(basesPtr);
}

internal static BorrowedReference GetMRO(BorrowedReference type)
{
Debug.Assert(IsType(type));
IntPtr basesPtr = Marshal.ReadIntPtr(type.DangerousGetAddress(), TypeOffset.tp_mro);
return new BorrowedReference(basesPtr);
}

private static IntPtr EnsureIsType(in StolenReference reference)
{
IntPtr address = reference.DangerousGetAddressOrNull();
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ static void InitializeClass(PyType pyType, ClassBase impl, Type clrType)
impl.tpHandle = type;
impl.pyHandle = type;

impl.InitializeSlots(slotsHolder);

Runtime.PyType_Modified(pyType.Reference);

//DebugUtil.DumpType(type);
}

Expand Down Expand Up @@ -787,6 +791,12 @@ static void InitializeSlot(IntPtr type, int slotOffset, MethodInfo method, Slots
InitializeSlot(type, slotOffset, thunk, slotsHolder);
}

internal static void InitializeSlot(BorrowedReference type, int slotOffset, Delegate impl, SlotsHolder slotsHolder)
{
var thunk = Interop.GetThunk(impl);
InitializeSlot(type.DangerousGetAddress(), slotOffset, thunk, slotsHolder);
}

static void InitializeSlot(IntPtr type, int slotOffset, ThunkInfo thunk, SlotsHolder slotsHolder)
{
Marshal.WriteIntPtr(type, slotOffset, thunk.Address);
Expand Down Expand Up @@ -848,6 +858,9 @@ private static SlotsHolder CreateSolotsHolder(IntPtr type)
_slotsHolders.Add(type, holder);
return holder;
}

internal static SlotsHolder GetSlotsHolder(PyType type)
=> _slotsHolders[type.Handle];
}


Expand All @@ -873,6 +886,8 @@ public SlotsHolder(IntPtr type)
_type = type;
}

public bool IsHolding(int offset) => _slots.ContainsKey(offset);

public void Set(int offset, ThunkInfo thunk)
{
_slots[offset] = thunk;
Expand Down
0