Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4df6105

Browse files
authored
Fix numpy array and README example (#427)
* Fix numpy array and README exampleGeneric Lists were falling through and being classified as `typecode.Object`To solve this, adding a specific processing branch for `Generic Lists` onlyto avoid breaking the changes from471673aCloses#249* Update syntax
1 parentd40a2dc commit4df6105

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
doublec=np.cos(5)+sin(5);
6565
Console.WriteLine(c);
66-
/* this block is temporarily disabled due to regression #249
66+
6767
dynamica=np.array(newList<float> {1,2,3 });
6868
Console.WriteLine(a.dtype);
6969

7070
dynamicb=np.array(newList<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
<CompileInclude="pyinitialize.cs" />
8686
<CompileInclude="pyrunstring.cs" />
8787
<CompileInclude="TestCustomMarshal.cs" />
88+
<CompileInclude="TestExample.cs" />
8889
<CompileInclude="TestPyAnsiString.cs" />
8990
<CompileInclude="TestPyFloat.cs" />
9091
<CompileInclude="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+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingNUnit.Framework;
4+
usingPython.Runtime;
5+
6+
namespacePython.EmbeddingTest
7+
{
8+
publicclassTestExample
9+
{
10+
[OneTimeSetUp]
11+
publicvoidSetUp()
12+
{
13+
PythonEngine.Initialize();
14+
}
15+
16+
[OneTimeTearDown]
17+
publicvoidDispose()
18+
{
19+
PythonEngine.Shutdown();
20+
}
21+
22+
[Test]
23+
publicvoidTestReadme()
24+
{
25+
dynamicnp;
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+
dynamicsin=np.sin;
39+
StringAssert.StartsWith("-0.95892",sin(5).ToString());
40+
41+
doublec=np.cos(5)+sin(5);
42+
Assert.AreEqual(-0.675262,c,0.01);
43+
44+
dynamica=np.array(newList<float>{1,2,3});
45+
Assert.AreEqual("float64",a.dtype.ToString());
46+
47+
dynamicb=np.array(newList<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
usingSystem;
22
usingSystem.Collections;
3+
usingSystem.Collections.Generic;
34
usingSystem.Globalization;
45
usingSystem.Reflection;
56
usingSystem.Runtime.InteropServices;
@@ -133,6 +134,22 @@ internal static IntPtr ToPython(object value, Type type)
133134
returnresult;
134135
}
135136

137+
if(valueisIList&&value.GetType().IsGenericType)
138+
{
139+
using(varresultlist=newPyList())
140+
{
141+
foreach(objectoin(IEnumerable)value)
142+
{
143+
using(varp=newPyObject(ToPython(o,o?.GetType())))
144+
{
145+
resultlist.Append(p);
146+
}
147+
}
148+
Runtime.XIncref(resultlist.Handle);
149+
returnresultlist.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
varpyderived=valueasIPythonDerivedType;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp