Skip to content

Replace custom platform handling by RuntimeInformation #1314

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

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 0 additions & 19 deletions src/embed_tests/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@ public void SetUp()
}
}

/// <summary>
/// Test the cache of the information from the platform module.
///
/// Test fails on platforms we haven't implemented yet.
/// </summary>
[Test]
public static void PlatformCache()
{
Runtime.Runtime.Initialize();

Assert.That(NativeCodePageHelper.Machine, Is.Not.EqualTo(MachineType.Other));
Assert.That(!string.IsNullOrEmpty(NativeCodePageHelper.MachineName));

Assert.That(NativeCodePageHelper.OperatingSystem, Is.Not.EqualTo(OperatingSystemType.Other));
Assert.That(!string.IsNullOrEmpty(NativeCodePageHelper.OperatingSystemName));

Runtime.Runtime.Shutdown();
}

[Test]
public static void Py_IsInitializedValue()
{
Expand Down
29 changes: 19 additions & 10 deletions src/runtime/platform/LibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ interface ILibraryLoader

static class LibraryLoader
{
public static ILibraryLoader Get(OperatingSystemType os)
static ILibraryLoader _instance = null;

public static ILibraryLoader Instance
{
switch (os)
get
{
case OperatingSystemType.Windows:
return new WindowsLoader();
case OperatingSystemType.Darwin:
return new DarwinLoader();
case OperatingSystemType.Linux:
return new LinuxLoader();
default:
throw new PlatformNotSupportedException($"This operating system ({os}) is not supported");
if (_instance == null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
_instance = new WindowsLoader();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_instance = new LinuxLoader();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
_instance = new DarwinLoader();
else
throw new PlatformNotSupportedException(
"This operating system is not supported"
);
}

return _instance;
}
}
}
Expand Down
99 changes: 17 additions & 82 deletions src/runtime/platform/NativeCodePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@ namespace Python.Runtime.Platform
{
class NativeCodePageHelper
{
/// <summary>
/// Gets the operating system as reported by python's platform.system().
/// </summary>
public static OperatingSystemType OperatingSystem { get; private set; }

/// <summary>
/// Gets the operating system as reported by python's platform.system().
/// </summary>
[Obsolete]
public static string OperatingSystemName => PythonEngine.Platform;

/// <summary>
/// Gets the machine architecture as reported by python's platform.machine().
/// </summary>
public static MachineType Machine { get; private set; }/* set in Initialize using python's platform.machine */

/// <summary>
/// Gets the machine architecture as reported by python's platform.machine().
/// </summary>
[Obsolete]
public static string MachineName { get; private set; }

/// <summary>
/// Initialized by InitializeNativeCodePage.
///
Expand All @@ -45,33 +23,6 @@ class NativeCodePageHelper
internal static IntPtr NativeCodePage = IntPtr.Zero;


static readonly Dictionary<string, OperatingSystemType> OperatingSystemTypeMapping = new Dictionary<string, OperatingSystemType>()
{
{ "Windows", OperatingSystemType.Windows },
{ "Darwin", OperatingSystemType.Darwin },
{ "Linux", OperatingSystemType.Linux },
};

/// <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,
["i686"] = MachineType.i386,
["x86"] = MachineType.i386,
["x86_64"] = MachineType.x86_64,
["amd64"] = MachineType.x86_64,
["x64"] = MachineType.x86_64,
["em64t"] = MachineType.x86_64,
["armv7l"] = MachineType.armv7l,
["armv8"] = MachineType.armv8,
["aarch64"] = MachineType.aarch64,
};

/// <summary>
/// Structure to describe native code.
///
Expand Down Expand Up @@ -108,11 +59,11 @@ public static NativeCode Active
{
get
{
switch (Machine)
switch (RuntimeInformation.ProcessArchitecture)
{
case MachineType.i386:
case Architecture.X86:
return I386;
case MachineType.x86_64:
case Architecture.X64:
return X86_64;
default:
return null;
Expand Down Expand Up @@ -205,14 +156,14 @@ int MAP_ANONYMOUS
{
get
{
switch (OperatingSystem)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
case OperatingSystemType.Darwin:
return 0x1000;
case OperatingSystemType.Linux:
return 0x20;
default:
throw new NotImplementedException($"mmap is not supported on {OperatingSystemName}");
return 0x20;
}
else
{
// OSX, FreeBSD
return 0x1000;
}
}
}
Expand All @@ -236,32 +187,16 @@ public void SetReadExec(IntPtr mappedMemory, int numBytes)
}
}

/// <summary>
/// Initializes the data about platforms.
///
/// This must be the last step when initializing the runtime:
/// GetManagedString needs to have the cached values for types.
/// But it must run before initializing anything outside the runtime
/// because those rely on the platform data.
/// </summary>
public static void InitializePlatformData()
{
MachineName = SystemInfo.GetArchitecture();
Machine = SystemInfo.GetMachineType();
OperatingSystem = SystemInfo.GetSystemType();
}

internal static IMemoryMapper CreateMemoryMapper()
{
switch (OperatingSystem)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return new WindowsMemoryMapper();
}
else
{
case OperatingSystemType.Darwin:
case OperatingSystemType.Linux:
return new UnixMemoryMapper();
case OperatingSystemType.Windows:
return new WindowsMemoryMapper();
default:
throw new NotImplementedException($"No support for {OperatingSystemName}");
// Linux, OSX, FreeBSD
return new UnixMemoryMapper();
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
ClassDerivedObject.Reset();
TypeManager.Initialize();

// Initialize data about the platform we're running on. We need
// this for the type manager and potentially other details. Must
// happen after caching the python types, above.
NativeCodePageHelper.InitializePlatformData();

// Initialize modules that depend on the runtime class.
AssemblyManager.Initialize();
if (mode == ShutdownMode.Reload && RuntimeData.HasStashData())
Expand Down Expand Up @@ -2153,7 +2148,7 @@ internal static void Py_CLEAR(ref IntPtr ob)

internal static void SetNoSiteFlag()
{
var loader = LibraryLoader.Get(NativeCodePageHelper.OperatingSystem);
var loader = LibraryLoader.Instance;
IntPtr dllLocal = IntPtr.Zero;
if (_PythonDll != "__Internal")
{
Expand Down