Skip to content

LookupType returns the first type found #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Reflection;
using System.Threading;
using System.Linq;

namespace Python.Runtime
{
Expand Down Expand Up @@ -448,17 +449,32 @@ public static List<string> 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
/// </summary>
public static Type LookupType(string qname)
{
List<Type> foundTypes = new List<Type>();

foreach (Assembly assembly in assemblies)
{
Type type = assembly.GetType(qname);
if (type != null)
{
return type;
foundTypes.Add(type);
}
}

Func<Type, bool> criteria = (t) => t.IsPublic;

if(foundTypes.Any(criteria))
{
return foundTypes.First(criteria);
}
else if (foundTypes.Any())
{
return foundTypes.First();
}

return null;
}

Expand Down
11 changes: 11 additions & 0 deletions src/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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