From 7165801d2d9b6b2cd30a6632411703c630a2ac54 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Mon, 6 Mar 2017 21:19:08 -0700 Subject: [PATCH 1/2] 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 471673a1fa4691b3becb4d9fee6941308535bb04 Closes #249 --- README.md | 3 +- src/embed_tests/Python.EmbeddingTest.csproj | 1 + src/embed_tests/TestExample.cs | 53 +++++++++++++++++++++ src/runtime/converter.cs | 11 +++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/embed_tests/TestExample.cs diff --git a/README.md b/README.md index 2794298a6..da43abacd 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ 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 { 1, 2, 3 }); Console.WriteLine(a.dtype); @@ -71,7 +71,6 @@ static void Main(string[] args) Console.WriteLine(b.dtype); Console.WriteLine(a * b); - */ Console.ReadKey(); } } diff --git a/src/embed_tests/Python.EmbeddingTest.csproj b/src/embed_tests/Python.EmbeddingTest.csproj index 9c417cd27..3ec0d4b57 100644 --- a/src/embed_tests/Python.EmbeddingTest.csproj +++ b/src/embed_tests/Python.EmbeddingTest.csproj @@ -85,6 +85,7 @@ + diff --git a/src/embed_tests/TestExample.cs b/src/embed_tests/TestExample.cs new file mode 100644 index 000000000..0cf795f5d --- /dev/null +++ b/src/embed_tests/TestExample.cs @@ -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 { 1, 2, 3 }); + Assert.AreEqual("float64", a.dtype.ToString()); + + dynamic b = np.array(new List { 6, 5, 4 }, Py.kw("dtype", np.int32)); + Assert.AreEqual("int32", b.dtype.ToString()); + + Assert.AreEqual("[ 6. 10. 12.]", (a * b).ToString()); + } + } +} diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index b66150a29..0174d28a3 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Runtime.InteropServices; @@ -133,6 +134,16 @@ internal static IntPtr ToPython(object value, Type type) return result; } + if (value is IList && value.GetType().IsGenericType) + { + var resultlist = new PyList(); + foreach (object o in (IEnumerable)value) + { + 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; From 6af5f885c8c4184420de1684655244eb0efd083b Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Sat, 11 Mar 2017 21:51:58 -0700 Subject: [PATCH 2/2] Update syntax --- src/runtime/converter.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index 0174d28a3..e68767da9 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -136,12 +136,18 @@ internal static IntPtr ToPython(object value, Type type) if (value is IList && value.GetType().IsGenericType) { - var resultlist = new PyList(); - foreach (object o in (IEnumerable)value) + using (var resultlist = new PyList()) { - resultlist.Append(new PyObject(ToPython(o, o.GetType()))); + foreach (object o in (IEnumerable)value) + { + using (var p = new PyObject(ToPython(o, o?.GetType()))) + { + resultlist.Append(p); + } + } + Runtime.XIncref(resultlist.Handle); + return resultlist.Handle; } - return resultlist.Handle; } // it the type is a python subclass of a managed type then return the