From 464ce0844d59cc249341948eaf1476b4734c97e7 Mon Sep 17 00:00:00 2001 From: "Ville M. Vainio" Date: Wed, 22 Mar 2017 17:58:40 +0200 Subject: [PATCH 1/5] Support clr.GetClrType() - as in IronPython Implements https://github.com/pythonnet/pythonnet/issues/432 --- src/runtime/moduleobject.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/runtime/moduleobject.cs b/src/runtime/moduleobject.cs index 258d77bac..8cba4d4b1 100644 --- a/src/runtime/moduleobject.cs +++ b/src/runtime/moduleobject.cs @@ -403,6 +403,18 @@ public static Assembly AddReference(string name) return assembly; } + [ModuleFunction] + [ForbidPythonThreads] + public static Type GetClrType(object type) + { + var converted = type as Type; + if (converted == null) + { + throw new ArgumentException("Cannot convert object to Type"); + } + return converted; + } + [ModuleFunction] [ForbidPythonThreads] public static string FindAssembly(string name) From 443e3077d5c11674ddb8755d9e9e67e2fc7c6432 Mon Sep 17 00:00:00 2001 From: "Ville M. Vainio" Date: Wed, 22 Mar 2017 19:02:39 +0200 Subject: [PATCH 2/5] Tests for clr.GetClrType() --- src/tests/test_module.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tests/test_module.py b/src/tests/test_module.py index 2255ea411..a3c3f4723 100644 --- a/src/tests/test_module.py +++ b/src/tests/test_module.py @@ -352,6 +352,16 @@ def test_clr_add_reference(): with pytest.raises(FileNotFoundException): AddReference("somethingtotallysilly") +def test_clr_get_clr_type(): + """Test clr.GetClrType().""" + from clr import GetClrType + from System import String + from System import IComparable + + assert GetClrType(String).FullName == "System.String" + comparable = GetClrType(IComparable) + assert comparable.FullName == "System.IComparable" + assert comparable.IsInterface def test_assembly_load_thread_safety(): from Python.Test import ModuleTest From 0eb4ca074311975a11d89f0d704d507fcfcb9b17 Mon Sep 17 00:00:00 2001 From: "Ville M. Vainio" Date: Wed, 22 Mar 2017 19:56:59 +0200 Subject: [PATCH 3/5] clr.GetClrType test: ensure bad type raises ArgumentException --- src/tests/test_module.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tests/test_module.py b/src/tests/test_module.py index a3c3f4723..b16c9adcf 100644 --- a/src/tests/test_module.py +++ b/src/tests/test_module.py @@ -357,12 +357,16 @@ def test_clr_get_clr_type(): from clr import GetClrType from System import String from System import IComparable + from System import ArgumentException assert GetClrType(String).FullName == "System.String" comparable = GetClrType(IComparable) assert comparable.FullName == "System.IComparable" assert comparable.IsInterface + with pytest.raises(ArgumentException): + GetClrType("thiswillfail") + def test_assembly_load_thread_safety(): from Python.Test import ModuleTest # spin up .NET thread which loads assemblies and triggers AppDomain.AssemblyLoad event From 8feacd8355f3387f6657acb80ccd77f49ed2f80b Mon Sep 17 00:00:00 2001 From: "Ville M. Vainio" Date: Wed, 22 Mar 2017 22:35:43 +0200 Subject: [PATCH 4/5] clr.GetClrType - added xml doc comment, updated AUTHORS.md and CHANGELOG.md --- AUTHORS.md | 1 + CHANGELOG.md | 2 +- src/runtime/moduleobject.cs | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/AUTHORS.md b/AUTHORS.md index 09358586e..24ac8f956 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -31,6 +31,7 @@ - Sam Winstanley ([@swinstanley](https://github.com/swinstanley)) - Sean Freitag ([@cowboygneox](https://github.com/cowboygneox)) - Serge Weinstock ([@sweinst](https://github.com/sweinst)) +- Ville M. Vainio ([@vivainio](https://github.com/vivainio)) - Virgil Dupras ([@hsoft](https://github.com/hsoft)) - Wenguang Yang ([@yagweb](https://github.com/yagweb)) - Xavier Dupré ([@sdpython](https://github.com/sdpython)) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9a3d7a2e..80b3bbf35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. ## [unreleased][] ### Added - +- Added clr.GetClrType (#432)(#433) - Added `Foo` feature ### Changed diff --git a/src/runtime/moduleobject.cs b/src/runtime/moduleobject.cs index 8cba4d4b1..d17ecfa92 100644 --- a/src/runtime/moduleobject.cs +++ b/src/runtime/moduleobject.cs @@ -403,6 +403,14 @@ public static Assembly AddReference(string name) return assembly; } + /// + /// Get a Type instance for a class object. + /// clr.GetClrType(IComparable) gives you the Type for IComparable, + /// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C# + /// or clr.GetClrType(IComparable) in IronPython + /// + /// + /// The Type object [ModuleFunction] [ForbidPythonThreads] public static Type GetClrType(object type) From 68f3fabd1d5f9246c8f0a9a2231aa791559c1eac Mon Sep 17 00:00:00 2001 From: "Ville M. Vainio" Date: Thu, 23 Mar 2017 17:55:58 +0200 Subject: [PATCH 5/5] Simplified implementation of clr.GetClrType (taken from IronPython) --- src/runtime/moduleobject.cs | 19 ++++++++----------- src/tests/test_module.py | 14 ++++++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/runtime/moduleobject.cs b/src/runtime/moduleobject.cs index d17ecfa92..e683026f9 100644 --- a/src/runtime/moduleobject.cs +++ b/src/runtime/moduleobject.cs @@ -403,24 +403,21 @@ public static Assembly AddReference(string name) return assembly; } - /// + /// /// Get a Type instance for a class object. /// clr.GetClrType(IComparable) gives you the Type for IComparable, /// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C# - /// or clr.GetClrType(IComparable) in IronPython - /// - /// + /// or clr.GetClrType(IComparable) in IronPython. + /// + /// + /// /// The Type object + [ModuleFunction] [ForbidPythonThreads] - public static Type GetClrType(object type) + public static Type GetClrType(Type type) { - var converted = type as Type; - if (converted == null) - { - throw new ArgumentException("Cannot convert object to Type"); - } - return converted; + return type; } [ModuleFunction] diff --git a/src/tests/test_module.py b/src/tests/test_module.py index b16c9adcf..e02aa6e01 100644 --- a/src/tests/test_module.py +++ b/src/tests/test_module.py @@ -355,16 +355,22 @@ def test_clr_add_reference(): def test_clr_get_clr_type(): """Test clr.GetClrType().""" from clr import GetClrType - from System import String + import System from System import IComparable from System import ArgumentException - - assert GetClrType(String).FullName == "System.String" + assert GetClrType(System.String).FullName == "System.String" comparable = GetClrType(IComparable) assert comparable.FullName == "System.IComparable" assert comparable.IsInterface + assert GetClrType(int).FullName == "System.Int32" + assert GetClrType(str).FullName == "System.String" + assert GetClrType(float).FullName == "System.Double" + dblarr = System.Array[System.Double] + assert GetClrType(dblarr).FullName == "System.Double[]" - with pytest.raises(ArgumentException): + with pytest.raises(TypeError): + GetClrType(1) + with pytest.raises(TypeError): GetClrType("thiswillfail") def test_assembly_load_thread_safety():