|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using NUnit.Framework; |
| 4 | +using Python.Runtime; |
| 5 | + |
| 6 | +namespace Python.EmbeddingTest |
| 7 | +{ |
| 8 | + public class TestInstanceWrapping |
| 9 | + { |
| 10 | + [OneTimeSetUp] |
| 11 | + public void SetUp() |
| 12 | + { |
| 13 | + PythonEngine.Initialize(); |
| 14 | + } |
| 15 | + |
| 16 | + [OneTimeTearDown] |
| 17 | + public void Dispose() |
| 18 | + { |
| 19 | + PythonEngine.Shutdown(); |
| 20 | + } |
| 21 | + |
| 22 | + // regression test for https://github.com/pythonnet/pythonnet/issues/811 |
| 23 | + [Test] |
| 24 | + public void OverloadResolution_UnknownToObject() |
| 25 | + { |
| 26 | + var overloaded = new Overloaded(); |
| 27 | + using (Py.GIL()) |
| 28 | + { |
| 29 | + var o = overloaded.ToPython(); |
| 30 | + |
| 31 | + dynamic callWithSelf = PythonEngine.Eval("lambda o: o.ObjOrClass(KeyError())"); |
| 32 | + callWithSelf(o); |
| 33 | + Assert.AreEqual(Overloaded.Object, overloaded.Value); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + class Base {} |
| 38 | + class Derived: Base { } |
| 39 | + |
| 40 | + class Overloaded: Derived |
| 41 | + { |
| 42 | + public int Value { get; set; } |
| 43 | + public void IntOrStr(int arg) => this.Value = arg; |
| 44 | + public void IntOrStr(string arg) => |
| 45 | + this.Value = int.Parse(arg, NumberStyles.Integer, CultureInfo.InvariantCulture); |
| 46 | + |
| 47 | + public const int Object = 1; |
| 48 | + public const int ConcreteClass = 2; |
| 49 | + public void ObjOrClass(object _) => this.Value = Object; |
| 50 | + public void ObjOrClass(Overloaded _) => this.Value = ConcreteClass; |
| 51 | + |
| 52 | + public const int Base = ConcreteClass + 1; |
| 53 | + public const int Derived = Base + 1; |
| 54 | + public void BaseOrDerived(Base _) => this.Value = Base; |
| 55 | + public void BaseOrDerived(Derived _) => this.Value = Derived; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments