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 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
Addresses issue #261 with providing more detail information missing d…
…ependencies with ImportError Exceptions
  • Loading branch information
Daniel Fernandez authored and Daniel Fernandez committed Dec 1, 2016
commit 6984ee28e77f7c4f172f225fa1877cbb5a49d435
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