From 8cc310206d10fdf9ba06f63b6be86ad0fd833f9c Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 16 Apr 2020 21:37:55 -0500 Subject: [PATCH 1/4] allow conversion of object --- src/runtime/pyobject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/pyobject.cs b/src/runtime/pyobject.cs index 37d53eeec..e246db687 100644 --- a/src/runtime/pyobject.cs +++ b/src/runtime/pyobject.cs @@ -156,7 +156,7 @@ public object AsManagedObject(Type t) /// public T As() { - if (typeof(T) == typeof(PyObject) || typeof(T) == typeof(object)) + if (typeof(T) == typeof(PyObject)) { return (T)(this as object); } From f2ea4956a9af30c558cccf04336c702e966e6149 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 16 Apr 2020 21:49:09 -0500 Subject: [PATCH 2/4] add test case --- src/embed_tests/TestConverter.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/embed_tests/TestConverter.cs b/src/embed_tests/TestConverter.cs index 078f4c0f8..aa7f60b99 100644 --- a/src/embed_tests/TestConverter.cs +++ b/src/embed_tests/TestConverter.cs @@ -67,5 +67,15 @@ public void RawPyObjectProxy() var proxiedHandle = pyObjectProxy.GetAttr("Handle").As(); Assert.AreEqual(pyObject.Handle, proxiedHandle); } + + [Test] + public void ConvertToObject() + { + object pyInt = new PyInt(12).As(); + Assert.AreSame(pyInt.GetType(), typeof(int)); + + object pyString = new PyString("hello").As(); + Assert.AreSame(pyString.GetType(), typeof(string)); + } } } From 007e5ddf00065f29e77f2e36162848dce5fc00c6 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 16 Apr 2020 21:50:27 -0500 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cc571ad4..dd00804ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881]) - Added support for kwarg parameters when calling .NET methods from Python - Changed method for finding MSBuild using vswhere +- Allow conversion to C# object in PyObject.As ### Fixed From 7951d499b2c01aa0e6cf85ee31f7705d220c690d Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Wed, 22 Apr 2020 10:14:36 -0500 Subject: [PATCH 4/4] use new function instead --- CHANGELOG.md | 2 +- src/embed_tests/TestConverter.cs | 4 ++-- src/runtime/pyobject.cs | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd00804ff..0760f0993 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881]) - Added support for kwarg parameters when calling .NET methods from Python - Changed method for finding MSBuild using vswhere -- Allow conversion to C# object in PyObject.As +- Allow conversion to C# object in PyObject.As2 ### Fixed diff --git a/src/embed_tests/TestConverter.cs b/src/embed_tests/TestConverter.cs index aa7f60b99..caec9aa7f 100644 --- a/src/embed_tests/TestConverter.cs +++ b/src/embed_tests/TestConverter.cs @@ -71,10 +71,10 @@ public void RawPyObjectProxy() [Test] public void ConvertToObject() { - object pyInt = new PyInt(12).As(); + object pyInt = new PyInt(12).As2(); Assert.AreSame(pyInt.GetType(), typeof(int)); - object pyString = new PyString("hello").As(); + object pyString = new PyString("hello").As2(); Assert.AreSame(pyString.GetType(), typeof(string)); } } diff --git a/src/runtime/pyobject.cs b/src/runtime/pyobject.cs index e246db687..e47463157 100644 --- a/src/runtime/pyobject.cs +++ b/src/runtime/pyobject.cs @@ -155,6 +155,20 @@ public object AsManagedObject(Type t) /// value of the Python object. /// public T As() + { + if (typeof(T) == typeof(PyObject) || typeof(T) == typeof(object)) + { + return (T)(this as object); + } + object result; + if (!Converter.ToManaged(obj, typeof(T), out result, false)) + { + throw new InvalidCastException("cannot convert object to target type"); + } + return (T)result; + } + + public T As2() { if (typeof(T) == typeof(PyObject)) {