Skip to content

Addresses issue #261 with providing more detail information missing d… #298

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 3 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
32 changes: 32 additions & 0 deletions src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,38 @@ public static bool LoadImplicit(string name, bool warn = true)
return loaded;
}

//===================================================================
// Return the Assembly for a given name
//===================================================================
internal static Assembly GetAssembly(string name)
{
string[] names = name.Split('.');
string s = "";
HashSet<Assembly> assembliesSet = null;
for (int i = 0; i < names.Length; i++)
{
s = (i == 0) ? names[0] : s + "." + names[i];
if (assembliesSet == null)
{
assembliesSet = new HashSet<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
}
Assembly a = FindLoadedAssembly(s);
if (a == null)
{
a = LoadAssemblyPath(s);
}
if (a == null)
{
a = LoadAssembly(s);
}
if (a != null && assembliesSet.Contains(a))
{
return a;
}
}
return null;
}


//===================================================================
// Scans an assembly for exported namespaces, adding them to the
Expand Down
30 changes: 29 additions & 1 deletion src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,35 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
{
// May be called when a module being imported imports a module.
// In particular, I've seen decimal import copy import org.python.core
return Runtime.PyObject_Call(py_import, args, kw);
var res = Runtime.PyObject_Call(py_import, args, kw);

// Check if there is an ImportError Exception if so
// determine which dependencies are missing
if(Exceptions.ExceptionMatches(Exceptions.ImportError))
{
var target_assembly = AssemblyManager.GetAssembly(realname);
if (target_assembly != null)
{
System.Collections.Generic.List<string> missing_dependencies =
new System.Collections.Generic.List<string>();
foreach (var assembly in target_assembly.GetReferencedAssemblies())
{
var depedentAssembly = AssemblyManager.LoadAssembly(assembly.Name);
if (depedentAssembly == null)
missing_dependencies.Add(assembly.Name);
}

if (missing_dependencies.Count > 0)
{
// We found missing dependencies
string error = String.Format("No module named {0} missing dependencies {1}",
realname, String.Join(", ", missing_dependencies));
Exceptions.SetError(Exceptions.ImportError, error);
}
}
}
return res;

}
}

Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions src/tests/test_suite/_missing_import_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
import clr
path_to_dll = os.path.join(os.path.dirname(__file__), "TestDependencyAssembly.dll")
clr.AddReference(path_to_dll)
from TestDependencyAssembly import TestDependency
6 changes: 6 additions & 0 deletions src/tests/test_suite/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def testRealtiveMissingImport(self):
from . import _missing_import
except ImportError:
pass

def testMissingImportDepencency(self):
missing_dependency_name = "MissingDependencyAssembly"
with self.assertRaisesRegexp(ImportError, missing_dependency_name):
from . import _missing_import_dependency



def test_suite():
Expand Down