Skip to content

Commit 6838ee1

Browse files
committed
fixed name collision for generated delegate dispatchers
1 parent b5c222c commit 6838ee1

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/runtime/Util/CodeGenerator.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
35
using System.Reflection;
46
using System.Reflection.Emit;
57
using System.Threading;
@@ -17,13 +19,15 @@ internal class CodeGenerator
1719
private readonly AssemblyBuilder aBuilder;
1820
private readonly ModuleBuilder mBuilder;
1921

22+
const string NamePrefix = "__Python_Runtime_Generated_";
23+
2024
internal CodeGenerator()
2125
{
22-
var aname = new AssemblyName { Name = "__CodeGenerator_Assembly" };
26+
var aname = new AssemblyName { Name = GetUniqueAssemblyName(NamePrefix + "Assembly") };
2327
var aa = AssemblyBuilderAccess.Run;
2428

2529
aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa);
26-
mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module");
30+
mBuilder = aBuilder.DefineDynamicModule(NamePrefix + "Module");
2731
}
2832

2933
/// <summary>
@@ -77,5 +81,20 @@ internal static void GenerateMarshalByRefsBack(ILGenerator il, IReadOnlyList<Typ
7781
}
7882
}
7983
}
84+
85+
static string GetUniqueAssemblyName(string name)
86+
{
87+
var taken = new HashSet<string>(AppDomain.CurrentDomain
88+
.GetAssemblies()
89+
.Select(a => a.GetName().Name));
90+
for (int i = 0; i < int.MaxValue; i++)
91+
{
92+
string candidate = name + i.ToString(CultureInfo.InvariantCulture);
93+
if (!taken.Contains(candidate))
94+
return candidate;
95+
}
96+
97+
throw new NotSupportedException("Too many assemblies");
98+
}
8099
}
81100
}

0 commit comments

Comments
 (0)