|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +using Python.Runtime; |
| 7 | + |
| 8 | +namespace Python.EmbeddingTest |
| 9 | +{ |
| 10 | + public class CallableObject |
| 11 | + { |
| 12 | + [OneTimeSetUp] |
| 13 | + public void SetUp() |
| 14 | + { |
| 15 | + PythonEngine.Initialize(); |
| 16 | + using var locals = new PyDict(); |
| 17 | + PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals.Handle); |
| 18 | + CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]); |
| 19 | + PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider()); |
| 20 | + } |
| 21 | + |
| 22 | + [OneTimeTearDown] |
| 23 | + public void Dispose() |
| 24 | + { |
| 25 | + PythonEngine.Shutdown(); |
| 26 | + } |
| 27 | + [Test] |
| 28 | + public void CallMethodMakesObjectCallable() |
| 29 | + { |
| 30 | + var doubler = new DerivedDoubler(); |
| 31 | + dynamic applyObjectTo21 = PythonEngine.Eval("lambda o: o(21)"); |
| 32 | + Assert.AreEqual(doubler.__call__(21), (int)applyObjectTo21(doubler.ToPython())); |
| 33 | + } |
| 34 | + [Test] |
| 35 | + public void CallMethodCanBeInheritedFromPython() |
| 36 | + { |
| 37 | + var callViaInheritance = new CallViaInheritance(); |
| 38 | + dynamic applyObjectTo14 = PythonEngine.Eval("lambda o: o(14)"); |
| 39 | + Assert.AreEqual(callViaInheritance.Call(14), (int)applyObjectTo14(callViaInheritance.ToPython())); |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void CanOverwriteCall() |
| 44 | + { |
| 45 | + var callViaInheritance = new CallViaInheritance(); |
| 46 | + using var scope = Py.CreateScope(); |
| 47 | + scope.Set("o", callViaInheritance); |
| 48 | + scope.Exec("orig_call = o.Call"); |
| 49 | + scope.Exec("o.Call = lambda a: orig_call(a*7)"); |
| 50 | + int result = scope.Eval<int>("o.Call(5)"); |
| 51 | + Assert.AreEqual(105, result); |
| 52 | + } |
| 53 | + |
| 54 | + class Doubler |
| 55 | + { |
| 56 | + public int __call__(int arg) => 2 * arg; |
| 57 | + } |
| 58 | + |
| 59 | + class DerivedDoubler : Doubler { } |
| 60 | + |
| 61 | + class CallViaInheritance |
| 62 | + { |
| 63 | + public const string BaseClassName = "Forwarder"; |
| 64 | + public static readonly string BaseClassSource = $@" |
| 65 | +class MyCallableBase: |
| 66 | + def __call__(self, val): |
| 67 | + return self.Call(val) |
| 68 | +
|
| 69 | +class {BaseClassName}(MyCallableBase): pass |
| 70 | +"; |
| 71 | + public int Call(int arg) => 3 * arg; |
| 72 | + } |
| 73 | + |
| 74 | + class CustomBaseTypeProvider : IPythonBaseTypeProvider |
| 75 | + { |
| 76 | + internal static PyType BaseClass; |
| 77 | + |
| 78 | + public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) |
| 79 | + { |
| 80 | + Assert.Greater(BaseClass.Refcount, 0); |
| 81 | + return type != typeof(CallViaInheritance) |
| 82 | + ? existingBases |
| 83 | + : new[] { BaseClass }; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments