8000 simplify assembly ResolveHandler, and use official assembly name pars… · pythonnet/pythonnet@9d5f579 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d5f579

Browse files
committed
simplify assembly ResolveHandler, and use official assembly name parsing method
1 parent 3808c3d commit 9d5f579

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/runtime/assemblymanager.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ private static void AssemblyLoadHandler(object ob, AssemblyLoadEventArgs args)
110110
/// </summary>
111111
private static Assembly ResolveHandler(object ob, ResolveEventArgs args)
112112
{
113-
string name = args.Name.ToLower();
114-
foreach (Assembly a in assemblies)
113+
var name = new AssemblyName(args.Name);
114+
foreach (var alreadyLoaded in assemblies)
115115
{
116-
string full = a.FullName.ToLower();
117-
if (full.StartsWith(name))
116+
if (AssemblyName.ReferenceMatchesDefinition(name, alreadyLoaded.GetName()))
118117
{
119-
return a;
118+
return alreadyLoaded;
120119
}
121120
}
122-
return LoadAssemblyPath(args.Name);
121+
return LoadAssemblyPath(name.Name);
123122
}
124123

125124

@@ -154,6 +153,17 @@ internal static void UpdatePath()
154153
}
155154
}
156155

156+
/// <summary>
157+
/// Given an assembly name, try to find this assembly file using the
158+
/// PYTHONPATH. If not found, return null to indicate implicit load
159+
/// using standard load semantics (app base directory then GAC, etc.)
160+
/// </summary>
161+
public static string FindAssembly(AssemblyName name)
162+
{
163+
if (name is null) throw new ArgumentNullException(nameof(name));
164+
165+
return FindAssembly(name.Name);
166+
}
157167

158168
/// <summary>
159169
/// Given an assembly name, try to find this assembly file using the
@@ -162,8 +172,13 @@ internal static void UpdatePath()
162172
/// </summary>
163173
public static string FindAssembly(string name)
164174
{
165-
char sep = Path.DirectorySeparatorChar;
175+
if (name is null) throw new ArgumentNullException(nameof(name));
166176

177+
return FindAssemblyCandidates(name).FirstOrDefault();
178+
}
179+
180+
static IEnumerable<string> FindAssemblyCandidates(string name)
181+
{
167182
foreach (string head in pypath)
168183
{
169184
string path;
@@ -173,22 +188,21 @@ public static string FindAssembly(string name)
173188
}
174189
else
175190
{
176-
path = head + sep + name;
191+
path = Path.Combine(head, name);
177192
}
178193

179194
string temp = path + ".dll";
180195
if (File.Exists(temp))
181196
{
182-
return temp;
197+
yield return temp;
183198
}
184199

185200
temp = path + ".exe";
186201
if (File.Exists(temp))
187202
{
188-
return temp;
203+
yield return temp;
189204
}
190205
}
191-
return null;
192206
}
193207

194208

tests/test_module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ def test_explicit_assembly_load():
232232
from System.Reflection import Assembly
233233
import System, sys
234234

235-
assembly = Assembly.LoadWithPartialName('System.Runtime')
235+
assembly = Assembly.LoadWithPartialName('Microsoft.CSharp')
236236
assert assembly is not None
237237

238-
import System.Runtime
239-
assert 'System.Runtime' in sys.modules
238+
import Microsoft.CSharp
239+
assert 'Microsoft.CSharp' in sys.modules
240240

241241
assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
242242
assert assembly is None

0 commit comments

Comments
 (0)
0