Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb76ff3

Browse files
committed
Domain reload work: port previous commit to Windows
Nothing major: - python platform.machine() returns x86_64 on mac, and AMD64 on windows, for the same platform. Parse them properly. - Microsoft's runtime C# compiler wants language version 2.0, so no $"{foo}" syntax in our debug printing. - Microsoft has decided you can't catch SEGV even in a unit test, so disable that test on windows. Most of my time was spent trying to convince VS to see the unit tests.
1 parent 9ae91ba commit bb76ff3

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/embed_tests/TestDomainReload.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,34 @@ public static void DomainReloadAndGC()
6565
//
6666
// What matters in the python code is gc.collect and clr.AddReference.
6767
//
68+
// Note that the language version is 2.0, so no $"foo{bar}" syntax.
69+
//
6870
const string TestCode = @"
6971
using Python.Runtime;
7072
using System;
7173
class PythonRunner {
7274
public static void RunPython() {
7375
AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
7476
string name = AppDomain.CurrentDomain.FriendlyName;
75-
Console.WriteLine($""[{name} in .NET] In PythonRunner.RunPython"");
77+
Console.WriteLine(string.Format(""[{0} in .NET] In PythonRunner.RunPython"", name));
7678
using (Py.GIL()) {
7779
try {
78-
var pyScript = ""import clr\n""
79-
+ $""print('[{name} in python] imported clr')\n""
80+
var pyScript = string.Format(""import clr\n""
81+
+ ""print('[{0} in python] imported clr')\n""
8082
+ ""clr.AddReference('System')\n""
81-
+ $""print('[{name} in python] allocated a clr object')\n""
83+
+ ""print('[{0} in python] allocated a clr object')\n""
8284
+ ""import gc\n""
8385
+ ""gc.collect()\n""
84-
+ $""print('[{name} in python] collected garbage')\n"";
86+
+ ""print('[{0} in python] collected garbage')\n"",
87+
name);
8588
PythonEngine.Exec(pyScript);
8689
} catch(Exception e) {
87-
Console.WriteLine($""[{name} in .NET] Caught exception: {e}"");
90+
Console.WriteLine(string.Format(""[{0} in .NET] Caught exception: {1}"", name, e));
8891
}
8992
}
9093
}
9194
static void OnDomainUnload(object sender, EventArgs e) {
92-
System.Console.WriteLine(string.Format($""[{AppDomain.CurrentDomain.FriendlyName} in .NET] unloading""));
95+
System.Console.WriteLine(string.Format(""[{0} in .NET] unloading"", AppDomain.CurrentDomain.FriendlyName));
9396
}
9497
}";
9598

@@ -139,7 +142,7 @@ class Proxy : MarshalByRefObject
139142

140143
public void InitAssembly(string assemblyPath)
141144
{
142-
theAssembly = Assembly.LoadFile(assemblyPath);
145+
theAssembly = Assembly.LoadFile(System.IO.Path.GetFullPath(assemblyPath));
143146
}
144147

145148
public void RunPython()

src/embed_tests/TestTypeManager.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ public static void TestMemoryMapping()
3131
Assert.That(() => { Marshal.WriteInt64(page, 17); }, Throws.Nothing);
3232
Assert.That(Marshal.ReadInt64(page), Is.EqualTo(17));
3333

34-
// Mark it read-execute, now we can't write anymore (I'm not testing we can execute).
35-
// We should be getting AccessViolationException, but Mono translates
36-
// SIGSEGV to NullReferenceException instead, so just check for some exception.
34+
// Mark it read-execute, now we can't write anymore.
35+
// We can't actually test access protectoin under Windows,
36+
// because AccessViolationException is assumed to mean we're in a
37+
// corrupted state:
38+
// https://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception
3739
mapper.SetReadExec(page, len);
3840
Assert.That(Marshal.ReadInt64(page), Is.EqualTo(17));
39-
Assert.That(() => { Marshal.WriteInt64(page, 18); }, Throws.Exception);
40-
Assert.That(Marshal.ReadInt64(page), Is.EqualTo(17));
41+
if (Runtime.Runtime.OperatingSystem != Runtime.Runtime.OperatingSystemType.Windows)
42+
{
43+
// Mono throws NRE instead of AccessViolationException for some reason.
44+
Assert.That(() => { Marshal.WriteInt64(page, 73); }, Throws.TypeOf<System.NullReferenceException>());
45+
Assert.That(Marshal.ReadInt64(page), Is.EqualTo(17));
46+
}
4147

4248
Runtime.Runtime.Shutdown();
4349
}

src/runtime/runtime.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,17 @@ public enum MachineType
231231
Other
232232
};
233233

234+
/// <summary>
235+
/// Map lower-case version of the python machine name to the processor
236+
/// type. There are aliases, e.g. x86_64 and amd64 are two names for
237+
/// the same thing. Make sure to lower-case the search string, because
238+
/// capitalization can differ.
239+
/// </summary>
234240
static readonly Dictionary<string, MachineType> MachineTypeMapping = new Dictionary<string, MachineType>()
235241
{
236242
{ "i386", MachineType.i386 },
237243
{ "x86_64", MachineType.x86_64 },
244+
{ "amd64", MachineType.x86_64 },
238245
};
239246

240247
/// <summary>
@@ -444,7 +451,7 @@ private static void InitializePlatformData()
444451
OperatingSystem = OSType;
445452

446453
MachineType MType;
447-
if (!MachineTypeMapping.TryGetValue(MachineName, out MType))
454+
if (!MachineTypeMapping.TryGetValue(MachineName.ToLower(), out MType))
448455
{
449456
MType = MachineType.Other;
450457
}

src/runtime/typemanager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Reflection;
@@ -507,7 +507,7 @@ public static NativeCode Active
507507
case Runtime.MachineType.x86_64:
508508
return X86_64;
509509
default:
510-
throw new NotImplementedException($"No support for ${Runtime.MachineName}");
510+
throw new NotImplementedException($"No support for {Runtime.MachineName}");
511511
}
512512
}
513513
}

0 commit comments

Comments
 (0)