Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fixes issue where private types are returned
  • Loading branch information
Cronan committed Feb 7, 2018
commit 994d609f9448246cfc3e2790455944d155a73057
3 changes: 2 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
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