8000 Fix numpy array and README example (#427) · pythonnet/pythonnet@4df6105 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4df6105

Browse files
authored
Fix numpy array and README example (#427)
* Fix numpy array and README example Generic Lists were falling through and being classified as `typecode.Object` To solve this, adding a specific processing branch for `Generic Lists` only to avoid breaking the changes from 471673a Closes #249 * Update syntax
1 parent d40a2dc commit 4df6105

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,14 @@ static void Main(string[] args)
6363

6464
double c = np.cos(5) + sin(5);
6565
Console.WriteLine(c);
66-
/* this block is temporarily disabled due to regression #249
66+
6767
dynamic a = np.array(new List<float> { 1, 2, 3 });
6868
Console.WriteLine(a.dtype);
6969

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

7373
Console.WriteLine(a * b);
74-
*/
7574
Console.ReadKey();
7675
}
7776
}

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="pyinitialize.cs" />
8686
<Compile Include="pyrunstring.cs" />
8787
<Compile Include="TestCustomMarshal.cs" />
88+
<Compile Include="TestExample.cs" />
8889
<Compile Include="TestPyAnsiString.cs" />
8990
<Compile Include="TestPyFloat.cs" />
9091
<Compile Include="TestPyInt.cs" />

src/embed_tests/TestExample.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
using Python.Runtime;
5+
6+
namespace Python.EmbeddingTest
7+
{
8+
public class TestExample
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+
[Test]
23+
public void TestReadme()
24+
{
25+
dynamic np;
26+
try
27+
{
28+
np = Py.Import("numpy");
29+
}
30+
catch (PythonException)
31+
{
32+
Assert.Inconclusive("Numpy or dependency not installed");
33+
return;
34+
}
35+
36+
Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString());
37+
38+
dynamic sin = np.sin;
39+
StringAssert.StartsWith("-0.95892", sin(5).ToString());
40+
41+
double c = np.cos(5) + sin(5);
42+
Assert.AreEqual(-0.675262, c, 0.01);
43+
44+
dynamic a = np.array(new List<float> { 1, 2, 3 });
45+
Assert.AreEqual("float64", a.dtype.ToString());
46+
47+
dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
48+
Assert.AreEqual("int32", b.dtype.ToString());
49+
50+
Assert.AreEqual("[ 6. 10. 12.]", (a * b).ToString());
51+
}
52+
}
53+
}

src/runtime/converter.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using System.Globalization;
45
using System.Reflection;
56
using System.Runtime.InteropServices;
@@ -133,6 +134,22 @@ internal static IntPtr ToPython(object value, Type type)
133134
return result;
134135
}
135136

137+
if (value is IList && value.GetType().IsGenericType)
138+
{
139+
using (var resultlist = new PyList())
140+
{
141+
foreach (object o in (IEnumerable)value)
142+
{
143+
using (var p = new PyObject(ToPython(o, o?.GetType())))
144+
{
145+
resultlist.Append(p);
146+
}
147+
}
148+
Runtime.XIncref(resultlist.Handle);
149+
return resultlist.Handle;
150+
}
151+
}
152+
136153
// it the type is a python subclass of a managed type then return the
137154
// underlying python object rather than construct a new wrapper object.
138155
var pyderived = value as IPythonDerivedType;

0 commit comments

Comments
 (0)
0