Skip to content

Retry: Enable C# parameters of type object accept any argument, passed from Python #889

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 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added a regression test for #881
  • Loading branch information
lostmsu committed Feb 27, 2020
commit 7daa47f9a3582eb59fecadda190dd814f4250a24
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="TestDomainReload.cs" />
<Compile Include="TestExample.cs" />
<Compile Include="TestFinalizer.cs" />
<Compile Include="TestInstanceWrapping.cs" />
<Compile Include="TestPyAnsiString.cs" />
<Compile Include="TestPyFloat.cs" />
<Compile Include="TestPyInt.cs" />
Expand Down
58 changes: 58 additions & 0 deletions src/embed_tests/TestInstanceWrapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Globalization;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestInstanceWrapping
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

// regression test for https://github.com/pythonnet/pythonnet/issues/811
[Test]
public void OverloadResolution_UnknownToObject()
{
var overloaded = new Overloaded();
using (Py.GIL())
{
var o = overloaded.ToPython();

dynamic callWithSelf = PythonEngine.Eval("lambda o: o.ObjOrClass(KeyError())");
callWithSelf(o);
Assert.AreEqual(Overloaded.Object, overloaded.Value);
}
}

class Base {}
class Derived: Base { }

class Overloaded: Derived
{
public int Value { get; set; }
public void IntOrStr(int arg) => this.Value = arg;
public void IntOrStr(string arg) =>
this.Value = int.Parse(arg, NumberStyles.Integer, CultureInfo.InvariantCulture);

public const int Object = 1;
public const int ConcreteClass = 2;
public void ObjOrClass(object _) => this.Value = Object;
public void ObjOrClass(Overloaded _) => this.Value = ConcreteClass;

public const int Base = ConcreteClass + 1;
public const int Derived = Base + 1;
public void BaseOrDerived(Base _) => this.Value = Base;
public void BaseOrDerived(Derived _) => this.Value = Derived;
}
}
}