From 994d609f9448246cfc3e2790455944d155a73057 Mon Sep 17 00:00:00 2001 From: "Cronyn, Ivan (London)" Date: Wed, 7 Feb 2018 17:01:37 +0000 Subject: [PATCH] Fixes issue where private types are returned --- AUTHORS.md | 3 ++- CHANGELOG.md | 1 + src/runtime/assemblymanager.cs | 18 +++++++++++++++++- src/tests/test_import.py | 11 +++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 78bb25f9e..66e025039 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -23,7 +23,8 @@ - David Lechner ([@dlech](https://github.com/dlech)) - Dmitriy Se ([@dmitriyse](https://github.com/dmitriyse)) - He-chien Tsai ([@t3476](https://github.com/t3476)) -- Jeff Reback ([@jreback](https://github.com/jreback)) +-   Ivan Cronyn ([@cronan](https://github.com/cronan)) +-   Jeff Reback ([@jreback](https://github.com/jreback)) - Joe Frayne ([@jfrayne](https://github.com/jfrayne)) - John Burnett ([@johnburnett](https://github.com/johnburnett)) - Luke Stratman ([@lstratman](https://github.com/lstratman)) diff --git a/CHANGELOG.md b/CHANGELOG.md index 380c6f6d3..612ab61e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - Fixed 'clrmethod' for python 2 (#492) - Fixed double calling of constructor when deriving from .NET class (#495) - Fixed `clr.GetClrType` when iterating over `System` members (#607) +- Fixed bug where private types are returned when public types exist (#622) ## [2.3.0][] - 2017-03-11 diff --git a/src/runtime/assemblymanager.cs b/src/runtime/assemblymanager.cs index 06a4449a2..8e24d7921 100644 --- a/src/runtime/assemblymanager.cs +++ b/src/runtime/assemblymanager.cs @@ -6,6 +6,7 @@ using System.IO; using System.Reflection; using System.Threading; +using System.Linq; namespace Python.Runtime { @@ -448,17 +449,32 @@ public static List GetNames(string nsname) /// Returns the System.Type object for a given qualified name, /// looking in the currently loaded assemblies for the named /// type. Returns null if the named type cannot be found. + /// If more than one is found, returns the first public type /// public static Type LookupType(string qname) { + List foundTypes = new List(); + foreach (Assembly assembly in assemblies) { Type type = assembly.GetType(qname); if (type != null) { - return type; + foundTypes.Add(type); } } + + Func criteria = (t) => t.IsPublic; + + if(foundTypes.Any(criteria)) + { + return foundTypes.First(criteria); + } + else if (foundTypes.Any()) + { + return foundTypes.First(); + } + return null; } diff --git a/src/tests/test_import.py b/src/tests/test_import.py index 42cafc4df..90daa40f0 100644 --- a/src/tests/test_import.py +++ b/src/tests/test_import.py @@ -11,3 +11,14 @@ def test_relative_missing_import(): Relative import in the site-packages folder""" with pytest.raises(ImportError): from . import _missing_import + + +def test_private_and_public_types_import(): + """Tests that importing a type where private and public + versions exist imports the public version. In .Net Core + 2.0 there are public and private versions of the same + types in different assemblies. For example, there is a + private version of System.Environment in mscorlib.dll, + and a public version in System.Runtime.Extensions.dll.""" + from System import Environment + assert len(Environment.MachineName) > 0