|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | + |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +using Python.Runtime; |
| 9 | + |
| 10 | +namespace Python.EmbeddingTest |
| 11 | +{ |
| 12 | + public class Inheritance |
| 13 | + { |
| 14 | + [OneTimeSetUp] |
| 15 | + public void SetUp() |
| 16 | + { |
| 17 | + PythonEngine.Initialize(); |
| 18 | + var locals = new PyDict(); |
| 19 | + PythonEngine.Exec(InheritanceTestBaseClassWrapper.ClassSourceCode, locals: locals.Handle); |
| 20 | + ExtraBaseTypeProvider.ExtraBase = new PyType(locals[InheritanceTestBaseClassWrapper.ClassName]); |
| 21 | + var baseTypeProviders = PythonEngine.InteropConfiguration.PythonBaseTypeProviders; |
| 22 | + baseTypeProviders.Add(new ExtraBaseTypeProvider()); |
| 23 | + baseTypeProviders.Add(new NoEffectBaseTypeProvider()); |
| 24 | + } |
| 25 | + |
| 26 | + [OneTimeTearDown] |
| 27 | + public void Dispose() |
| 28 | + { |
| 29 | + PythonEngine.Shutdown(); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public void ExtraBase_PassesInstanceCheck() |
| 34 | + { |
| 35 | + var inherited = new Inherited(); |
| 36 | + bool properlyInherited = PyIsInstance(inherited, ExtraBaseTypeProvider.ExtraBase); |
| 37 | + Assert.IsTrue(properlyInherited); |
| 38 | + } |
| 39 | + |
| 40 | + static dynamic PyIsInstance => PythonEngine.Eval("isinstance"); |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void InheritingWithExtraBase_CreatesNewClass() |
| 44 | + { |
| 45 | + PyObject a = ExtraBaseTypeProvider.ExtraBase; |
| 46 | + var inherited = new Inherited(); |
| 47 | + PyObject inheritedClass = inherited.ToPython().GetAttr("__class__"); |
| 48 | + Assert.IsFalse(PythonReferenceComparer.Instance.Equals(a, inheritedClass)); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void InheritedFromInheritedClassIsSelf() |
| 53 | + { |
| 54 | + using var scope = Py.CreateScope(); |
| 55 | + scope.Exec($"from {typeof(Inherited).Namespace} import {nameof(Inherited)}"); |
| 56 | + scope.Exec($"class B({nameof(Inherited)}): pass"); |
| 57 | + PyObject b = scope.Eval("B"); |
| 58 | + PyObject bInstance = b.Invoke(); |
| 59 | + PyObject bInstanceClass = bInstance.GetAttr("__class__"); |
| 60 | + Assert.IsTrue(PythonReferenceComparer.Instance.Equals(b, bInstanceClass)); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void Grandchild_PassesExtraBaseInstanceCheck() |
| 65 | + { |
| 66 | + using var scope = Py.CreateScope(); |
| 67 | + scope.Exec($"from {typeof(Inherited).Namespace} import {nameof(Inherited)}"); |
| 68 | + scope.Exec($"class B({nameof(Inherited)}): pass"); |
| 69 | + PyObject b = scope.Eval("B"); |
| 70 | + PyObject bInst = b.Invoke(); |
| 71 | + bool properlyInherited = PyIsInstance(bInst, ExtraBaseTypeProvider.ExtraBase); |
| 72 | + Assert.IsTrue(properlyInherited); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public void CallInheritedClrMethod_WithExtraPythonBase() |
| 77 | + { |
| 78 | + var instance = new Inherited().ToPython(); |
| 79 | + string result = instance.InvokeMethod(nameof(PythonWrapperBase.WrapperBaseMethod)).As<string>(); |
| 80 | + Assert.AreEqual(result, nameof(PythonWrapperBase.WrapperBaseMethod)); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void CallExtraBaseMethod() |
| 85 | + { |
| 86 | + var instance = new Inherited(); |
| 87 | + using var scope = Py.CreateScope(); |
| 88 | + scope.Set(nameof(instance), instance); |
| 89 | + int actual = instance.ToPython().InvokeMethod("callVirt").As<int>(); |
| 90 | + Assert.AreEqual(expected: Inherited.OverridenVirtValue, actual); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void SetAdHocAttributes_WhenExtraBasePresent() |
| 95 | + { |
| 96 | + var instance = new Inherited(); |
| 97 | + using var scope = Py.CreateScope(); |
| 98 | + scope.Set(nameof(instance), instance); |
| 99 | + scope.Exec($"super({nameof(instance)}.__class__, {nameof(instance)}).set_x_to_42()"); |
| 100 | + int actual = scope.Eval<int>($"{nameof(instance)}.{nameof(Inherited.XProp)}"); |
| 101 | + Assert.AreEqual(expected: Inherited.X, actual); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + class ExtraBaseTypeProvider : IPythonBaseTypeProvider |
| 106 | + { |
| 107 | + internal static PyType ExtraBase; |
| 108 | + public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) |
| 109 | + { |
| 110 | + if (type == typeof(InheritanceTestBaseClassWrapper)) |
| 111 | + { |
| 112 | + return new[] { PyType.Get(type.BaseType), ExtraBase }; |
| 113 | + } |
| 114 | + return existingBases; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + class NoEffectBaseTypeProvider : IPythonBaseTypeProvider |
| 119 | + { |
| 120 | + public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) |
| 121 | + => existingBases; |
| 122 | + } |
| 123 | + |
| 124 | + public class PythonWrapperBase |
| 125 | + { |
| 126 | + public string WrapperBaseMethod() => nameof(WrapperBaseMethod); |
| 127 | + } |
| 128 | + |
| 129 | + public class InheritanceTestBaseClassWrapper : PythonWrapperBase |
| 130 | + { |
| 131 | + public const string ClassName = "InheritanceTestBaseClass"; |
| 132 | + public const string ClassSourceCode = "class " + ClassName + |
| 133 | +@": |
| 134 | + def virt(self): |
| 135 | + return 42 |
| 136 | + def set_x_to_42(self): |
| 137 | + self.XProp = 42 |
| 138 | + def callVirt(self): |
| 139 | + return self.virt() |
| 140 | + def __getattr__(self, name): |
| 141 | + return '__getattr__:' + name |
| 142 | + def __setattr__(self, name, value): |
| 143 | + value[name] = name |
| 144 | +" + ClassName + " = " + ClassName + "\n"; |
| 145 | + } |
| 146 | + |
| 147 | + public class Inherited : InheritanceTestBaseClassWrapper |
| 148 | + { |
| 149 | + public const int OverridenVirtValue = -42; |
| 150 | + public const int X = 42; |
| 151 | + readonly Dictionary<string, object> extras = new Dictionary<string, object>(); |
| 152 | + public int virt() => OverridenVirtValue; |
| 153 | + public int XProp |
| 154 | + { |
| 155 | + get |
| 156 | + { |
| 157 | + using (var scope = Py.CreateScope()) |
| 158 | + { |
| 159 | + scope.Set("this", this); |
| 160 | + try |
| 161 | + { |
| 162 | + return scope.Eval<int>($"super(this.__class__, this).{nameof(XProp)}"); |
| 163 | + } |
| 164 | + catch (PythonException ex) when (ex.Type.Handle == Exceptions.AttributeError) |
| 165 | + { |
| 166 | + if (this.extras.TryGetValue(nameof(this.XProp), out object value)) |
| 167 | + return (int)value; |
| 168 | + throw; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + set => this.extras[nameof(this.XProp)] = value; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments