GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonnet/src/embed_tests/CallableObject.cs at master · pythonnet/pythonnet Expand file tree/ Copy pathCallableObject.csMore file actions 88 lines (75 loc) · 2.76 KB / Copy pathCallableObject.csFile metadata and controls88 lines (75 loc) · 2.76 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 using System; using System.Collections.Generic; using NUnit.Framework; using Python.Runtime; namespace Python.EmbeddingTest { public class CallableObject { IPythonBaseTypeProvider BaseTypeProvider; [OneTimeSetUp] public void SetUp() { using var locals = new PyDict(); PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals); BaseTypeProvider = new CustomBaseTypeProvider(new PyType(locals[CallViaInheritance.BaseClassName])); PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(BaseTypeProvider); } [OneTimeTearDown] public void Dispose() { PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Remove(BaseTypeProvider); } [Test] public void CallMethodMakesObjectCallable() { var doubler = new DerivedDoubler(); dynamic applyObjectTo21 = PythonEngine.Eval("lambda o: o(21)"); Assert.That((int)applyObjectTo21(doubler.ToPython()), Is.EqualTo(doubler.__call__(21))); } [Test] public void CallMethodCanBeInheritedFromPython() { var callViaInheritance = new CallViaInheritance(); dynamic applyObjectTo14 = PythonEngine.Eval("lambda o: o(14)"); Assert.That((int)applyObjectTo14(callViaInheritance.ToPython()), Is.EqualTo(callViaInheritance.Call(14))); } [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.That(result, Is.EqualTo(105)); } 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(PyType BaseClass) : IPythonBaseTypeProvider { public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) { Assert.That(BaseClass.Refcount, Is.GreaterThan(0)); return type != typeof(CallViaInheritance) ? existingBases : [BaseClass]; } } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |