From 6b2e75a53414da0b98f9501b0610fb4b21549f39 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Fri, 11 Dec 2020 07:51:27 +0100 Subject: [PATCH] Replace custom platform handling by RuntimeInformation --- src/embed_tests/TestRuntime.cs | 19 ----- src/runtime/platform/LibraryLoader.cs | 29 +++++--- src/runtime/platform/NativeCodePage.cs | 99 +++++--------------------- src/runtime/runtime.cs | 7 +- 4 files changed, 37 insertions(+), 117 deletions(-) diff --git a/src/embed_tests/TestRuntime.cs b/src/embed_tests/TestRuntime.cs index 38878205c..cde5dd6fa 100644 --- a/src/embed_tests/TestRuntime.cs +++ b/src/embed_tests/TestRuntime.cs @@ -18,25 +18,6 @@ public void SetUp() } } - /// - /// Test the cache of the information from the platform module. - /// - /// Test fails on platforms we haven't implemented yet. - /// - [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() { diff --git a/src/runtime/platform/LibraryLoader.cs b/src/runtime/platform/LibraryLoader.cs index a6d88cd19..193dff274 100644 --- a/src/runtime/platform/LibraryLoader.cs +++ b/src/runtime/platform/LibraryLoader.cs @@ -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; } } } diff --git a/src/runtime/platform/NativeCodePage.cs b/src/runtime/platform/NativeCodePage.cs index ab2ee3bcf..b6fe89425 100644 --- a/src/runtime/platform/NativeCodePage.cs +++ b/src/runtime/platform/NativeCodePage.cs @@ -6,28 +6,6 @@ namespace Python.Runtime.Platform { class NativeCodePageHelper { - /// - /// Gets the operating system as reported by python's platform.system(). - /// - public static OperatingSystemType OperatingSystem { get; private set; } - - /// - /// Gets the operating system as reported by python's platform.system(). - /// - [Obsolete] - public static string OperatingSystemName => PythonEngine.Platform; - - /// - /// Gets the machine architecture as reported by python's platform.machine(). - /// - public static MachineType Machine { get; private set; }/* set in Initialize using python's platform.machine */ - - /// - /// Gets the machine architecture as reported by python's platform.machine(). - /// - [Obsolete] - public static string MachineName { get; private set; } - /// /// Initialized by InitializeNativeCodePage. /// @@ -45,33 +23,6 @@ class NativeCodePageHelper internal static IntPtr NativeCodePage = IntPtr.Zero; - static readonly Dictionary OperatingSystemTypeMapping = new Dictionary() - { - { "Windows", OperatingSystemType.Windows }, - { "Darwin", OperatingSystemType.Darwin }, - { "Linux", OperatingSystemType.Linux }, - }; - - /// - /// 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. - /// - static readonly Dictionary MachineTypeMapping = new Dictionary() - { - ["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, - }; - /// /// Structure to describe native code. /// @@ -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; @@ -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; } } } @@ -236,32 +187,16 @@ public void SetReadExec(IntPtr mappedMemory, int numBytes) } } - /// - /// 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. - /// - 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(); } } diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 43c63f346..d9301acdc 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -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()) @@ -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") { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy