Skip to content
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ static void Main(string[] args)

double c = np.cos(5) + sin(5);
Console.WriteLine(c);
/* this block is temporarily disabled due to regression #249

dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);

dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Console.WriteLine(b.dtype);

Console.WriteLine(a * b);
*/
Console.ReadKey();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="pyinitialize.cs" />
<Compile Include="pyrunstring.cs" />
<Compile Include="TestCustomMarshal.cs" />
<Compile Include="TestExample.cs" />
<Compile Include="TestPyAnsiString.cs" />
<Compile Include="TestPyFloat.cs" />
<Compile Include="TestPyInt.cs" />
Expand Down
53 changes: 53 additions & 0 deletions src/embed_tests/TestExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Python.Runtime;

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

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

[Test]
public void TestReadme()
{
dynamic np;
try
{
np = Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return;
}

Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString());

dynamic sin = np.sin;
StringAssert.StartsWith("-0.95892", sin(5).ToString());

double c = np.cos(5) + sin(5);
Assert.AreEqual(-0.675262, c, 0.01);

dynamic a = np.array(new List<float> { 1, 2, 3 });
Assert.AreEqual("float64", a.dtype.ToString());

dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Assert.AreEqual("int32", b.dtype.ToString());

Assert.AreEqual("[ 6. 10. 12.]", (a * b).ToString());
}
}
}
11 changes: 11 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -133,6 +134,16 @@ internal static IntPtr ToPython(object value, Type type)
return result;
}

if (value is IList && value.GetType().IsGenericType)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be completely consistent with the converter code the IEnumerable should be replaced with IList like you did this pull request.

The commit message mentioned that strings should not be converted to PyList.

471673a

But IEnumerable is not quite right when converting to PyList anyway. IList is the closest interface like you selected.
Other example which are IEnumerable, but not IList besides strings are HashSet, maybe even HashTable.

var resultlist = new PyList();
foreach (object o in (IEnumerable)value)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vmuriart can you make this consistent with this syntax in the same file?

using (var p = new PyObject(ToPython(o, o?.GetType())))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

resultlist.Append(new PyObject(ToPython(o, o.GetType())));
}
return resultlist.Handle;
}

// it the type is a python subclass of a managed type then return the
// underlying python object rather than construct a new wrapper object.
var pyderived = value as IPythonDerivedType;
Expand Down