Skip to content

Fixed FileLoadException when trying clr.AddReference('/full/path.dll') #1573

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

Merged
merged 1 commit into from
Sep 28, 2021
Merged
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
14 changes: 12 additions & 2 deletions src/embed_tests/pyimport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;

using NUnit.Framework;
using Python.Runtime;

Expand Down Expand Up @@ -83,10 +85,18 @@ public void TestCastGlobalVar()
public void BadAssembly()
{
string path;
if (Python.Runtime.Runtime.IsWindows)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path = @"C:\Windows\System32\kernel32.dll";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
path = "/usr/lib/libc.dylib";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
path = "/usr/lib/locale/locale-archive";
}
else
{
Assert.Pass("TODO: add bad assembly location for other platforms");
Expand All @@ -98,7 +108,7 @@ import clr
clr.AddReference('{path}')
";

Assert.Throws<FileLoadException>(() => PythonEngine.Exec(code));
Assert.Throws<BadImageFormatException>(() => PythonEngine.Exec(code));
}
}
}
25 changes: 15 additions & 10 deletions src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ private static Assembly ResolveHandler(object ob, ResolveEventArgs args)
return LoadAssemblyPath(name.Name);
}

internal static AssemblyName? TryParseAssemblyName(string name)
{
try
{
return new AssemblyName(name);
}
catch (FileLoadException)
{
return null;
}
}


/// <summary>
/// We __really__ want to avoid using Python objects or APIs when
Expand Down Expand Up @@ -208,18 +220,11 @@ static IEnumerable<string> FindAssemblyCandidates(string name)

/// <summary>
/// Loads an assembly from the application directory or the GAC
/// given a simple assembly name. Returns the assembly if loaded.
/// given its name. Returns the assembly if loaded.
/// </summary>
public static Assembly LoadAssembly(string name)
public static Assembly LoadAssembly(AssemblyName name)
{
try
{
return Assembly.Load(name);
}
catch (FileNotFoundException)
{
return null;
}
return Assembly.Load(name);
}


Expand Down
4 changes: 2 additions & 2 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ public static Assembly AddReference(string name)
{
assembly = AssemblyManager.LoadAssemblyPath(name);
}
if (assembly == null)
if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName)
{
assembly = AssemblyManager.LoadAssembly(name);
assembly = AssemblyManager.LoadAssembly(parsedName);
}
if (assembly == null)
{
Expand Down