Skip to content

Fixes to integrate pythonnet into Unity #745

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
410ac15
UNI-63112: unit test for the domain reload crash
Sep 14, 2018
c07fff0
UNI-62864: shutdown on domain reload.
Sep 14, 2018
d016b24
Drive-by improve a hashtable to hashset.
Sep 26, 2018
9ae91ba
UNI-63112: implement platform-aware native code for tp_traverse et al
Sep 26, 2018
bb76ff3
Domain reload work: port previous commit to Windows
benoithudson Sep 28, 2018
0c5b78f
Fixed a bogus comment.
benoithudson Sep 28, 2018
9ffb705
Make prettier
benoithudson Sep 28, 2018
2e03eb8
Added author and changelog information.
Sep 28, 2018
156f554
Fix for linux mmap requiring MAP_PRIVATE
benoithudson Oct 2, 2018
84f5087
Doc and typo fixes.
benoithudson Oct 2, 2018
b9f6c2c
Merge branch 'uni-63112-hotreload-crash-test' of https://github.com/U…
benoithudson Oct 2, 2018
e585bdc
Merge pull request #7 from Unity-Technologies/uni-63112-hotreload-cra…
benoithudson Oct 2, 2018
9ab7b13
Disable app domain test case on .NET Standard
Oct 3, 2018
13f8b53
Fix for CI: define NETSTANDARD in src/embed_tests
Oct 3, 2018
c0b52fa
Fix compile error on OSX that came from an upstream merge.
Oct 3, 2018
4096a95
Fix in dotnet code path: find mmap in libc rather than __Internal
Oct 3, 2018
9b74cce
Fix TypeManager test for running on .NET Core under linux/OSX
Oct 3, 2018
5e15b2c
Fix for python3 tests crashing: it's about test order
Oct 4, 2018
211155e
WIP - debug: turn off "quiet" so that I get an error message
Oct 11, 2018
14bc2e2
Use msbuild v14 for linux/darwin.
Oct 11, 2018
8a80fd5
Upgrade setuptools on appveyor
Oct 11, 2018
dd77fa5
Flush the console on every WriteLine so messages are in order.
Oct 11, 2018
f947e3b
Grasping at straws: try using conda to set up environment
Oct 11, 2018
1fda82b
Revert "Grasping at straws: try using conda to set up environment"
Oct 11, 2018
ec6f6c5
Give up on python 3.4 for appveyor.
Oct 11, 2018
4bac40e
Travis: don't print from domain-reload test
Oct 11, 2018
386a034
Fixed appveyor "allow_failures" syntax.
Oct 11, 2018
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
benoithudson committed Sep 28, 2018
commit bb76ff30fbbb96704d24bc7fe7611702efca72c6
19 changes: 11 additions & 8 deletions src/embed_tests/TestDomainReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,34 @@ public static void DomainReloadAndGC()
//
// What matters in the python code is gc.collect and clr.AddReference.
//
// Note that the language version is 2.0, so no $"foo{bar}" syntax.
//
const string TestCode = @"
using Python.Runtime;
using System;
class PythonRunner {
public static void RunPython() {
AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
string name = AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine($""[{name} in .NET] In PythonRunner.RunPython"");
Console.WriteLine(string.Format(""[{0} in .NET] In PythonRunner.RunPython"", name));
using (Py.GIL()) {
try {
var pyScript = ""import clr\n""
+ $""print('[{name} in python] imported clr')\n""
var pyScript = string.Format(""import clr\n""
+ ""print('[{0} in python] imported clr')\n""
+ ""clr.AddReference('System')\n""
+ $""print('[{name} in python] allocated a clr object')\n""
+ ""print('[{0} in python] allocated a clr object')\n""
+ ""import gc\n""
+ ""gc.collect()\n""
+ $""print('[{name} in python] collected garbage')\n"";
+ ""print('[{0} in python] collected garbage')\n"",
name);
PythonEngine.Exec(pyScript);
} catch(Exception e) {
Console.WriteLine($""[{name} in .NET] Caught exception: {e}"");
Console.WriteLine(string.Format(""[{0} in .NET] Caught exception: {1}"", name, e));
}
}
}
static void OnDomainUnload(object sender, EventArgs e) {
System.Console.WriteLine(string.Format($""[{AppDomain.CurrentDomain.FriendlyName} in .NET] unloading""));
System.Console.WriteLine(string.Format(""[{0} in .NET] unloading"", AppDomain.CurrentDomain.FriendlyName));
}
}";

Expand Down Expand Up @@ -139,7 +142,7 @@ class Proxy : MarshalByRefObject

public void InitAssembly(string assemblyPath)
{
theAssembly = Assembly.LoadFile(assemblyPath);
theAssembly = Assembly.LoadFile(System.IO.Path.GetFullPath(assemblyPath));
}

public void RunPython()
Expand Down
16 changes: 11 additions & 5 deletions src/embed_tests/TestTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ public static void TestMemoryMapping()
Assert.That(() => { Marshal.WriteInt64(page, 17); }, Throws.Nothing);
Assert.That(Marshal.ReadInt64(page), Is.EqualTo(17));

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

Runtime.Runtime.Shutdown();
}
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,17 @@ public enum MachineType
Other
};

/// <summary>
/// Map lower-case version of the python machine name to the processor
/// type. There are aliases, e.g. x86_64 and amd64 are two names for
/// the same thing. Make sure to lower-case the search string, because
/// capitalization can differ.
/// </summary>
static readonly Dictionary<string, MachineType> MachineTypeMapping = new Dictionary<string, MachineType>()
{
{ "i386", MachineType.i386 },
{ "x86_64", MachineType.x86_64 },
{ "amd64", MachineType.x86_64 },
};

/// <summary>
Expand Down Expand Up @@ -444,7 +451,7 @@ private static void InitializePlatformData()
OperatingSystem = OSType;

MachineType MType;
if (!MachineTypeMapping.TryGetValue(MachineName, out MType))
if (!MachineTypeMapping.TryGetValue(MachineName.ToLower(), out MType))
{
MType = MachineType.Other;
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
Expand Down Expand Up @@ -507,7 +507,7 @@ public static NativeCode Active
case Runtime.MachineType.x86_64:
return X86_64;
default:
throw new NotImplementedException($"No support for ${Runtime.MachineName}");
throw new NotImplementedException($"No support for {Runtime.MachineName}");
}
}
}
Expand Down