Skip to content

Commit fc989d5

Browse files
committed
fixed FileLoadException when trying to do clr.AddReference('/full/lib/path.dll')
Before trying to load an assembly by its full path we were trying to call `Assembly.Load` on it. `Assembly.Load` interprets its argument as a valid `AssemblyName`. However full paths are not valid assembly names, so that call would throw `FileLoadException`, which we did not handle. reported in 9d5f579#commitcomment-57061082 Related: #1514
1 parent 497c22a commit fc989d5

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/embed_tests/pyimport.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.IO;
3+
using System.Runtime.InteropServices;
4+
35
using NUnit.Framework;
46
using Python.Runtime;
57

@@ -83,10 +85,18 @@ public void TestCastGlobalVar()
8385
public void BadAssembly()
8486
{
8587
string path;
86-
if (Python.Runtime.Runtime.IsWindows)
88+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
8789
{
8890
path = @"C:\Windows\System32\kernel32.dll";
8991
}
92+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
93+
{
94+
path = "/usr/lib/libc.dylib";
95+
}
96+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
97+
{
98+
path = "/usr/lib/locale/locale-archive";
99+
}
90100
else
91101
{
92102
Assert.Pass("TODO: add bad assembly location for other platforms");
@@ -98,7 +108,7 @@ import clr
98108
clr.AddReference('{path}')
99109
";
100110

101-
Assert.Throws<FileLoadException>(() => PythonEngine.Exec(code));
111+
Assert.Throws<BadImageFormatException>(() => PythonEngine.Exec(code));
102112
}
103113
}
104114
}

src/runtime/assemblymanager.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ private static Assembly ResolveHandler(object ob, ResolveEventArgs args)
121121
return LoadAssemblyPath(name.Name);
122122
}
123123

124+
internal static AssemblyName? TryParseAssemblyName(string name)
125+
{
126+
try
127+
{
128+
return new AssemblyName(name);
129+
}
130+
catch (FileLoadException)
131+
{
132+
return null;
133+
}
134+
}
135+
124136

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

209221
/// <summary>
210222
/// Loads an assembly from the application directory or the GAC
211-
/// given a simple assembly name. Returns the assembly if loaded.
223+
/// given its name. Returns the assembly if loaded.
212224
/// </summary>
213-
public static Assembly LoadAssembly(string name)
225+
public static Assembly LoadAssembly(AssemblyName name)
214226
{
215-
try
216-
{
217-
return Assembly.Load(name);
218-
}
219-
catch (FileNotFoundException)
220-
{
221-
return null;
222-
}
227+
return Assembly.Load(name);
223228
}
224229

225230

src/runtime/moduleobject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ public static Assembly AddReference(string name)
516516
{
517517
assembly = AssemblyManager.LoadAssemblyPath(name);
518518
}
519-
if (assembly == null)
519+
if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName)
520520
{
521-
assembly = AssemblyManager.LoadAssembly(name);
521+
assembly = AssemblyManager.LoadAssembly(parsedName);
522522
}
523523
if (assembly == null)
524524
{

0 commit comments

Comments
 (0)