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

Skip to content

Commit c23958e

Browse files
filmorlostmsu
authored andcommitted
Replace custom platform handling by RuntimeInformation
1 parent 9934cc8 commit c23958e

File tree

4 files changed

+37
-117
lines changed

4 files changed

+37
-117
lines changed

src/embed_tests/TestRuntime.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,6 @@ public void SetUp()
1818
}
1919
}
2020

21-
/// <summary>
22-
/// Test the cache of the information from the platform module.
23-
///
24-
/// Test fails on platforms we haven't implemented yet.
25-
/// </summary>
26-
[Test]
27-
public static void PlatformCache()
28-
{
29-
Runtime.Runtime.Initialize();
30-
31-
Assert.That(NativeCodePageHelper.Machine, Is.Not.EqualTo(MachineType.Other));
32-
Assert.That(!string.IsNullOrEmpty(NativeCodePageHelper.MachineName));
33-
34-
Assert.That(NativeCodePageHelper.OperatingSystem, Is.Not.EqualTo(OperatingSystemType.Other));
35-
Assert.That(!string.IsNullOrEmpty(NativeCodePageHelper.OperatingSystemName));
36-
37-
Runtime.Runtime.Shutdown();
38-
}
39-
4021
[Test]
4122
public static void Py_IsInitializedValue()
4223
{

src/runtime/platform/LibraryLoader.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@ interface ILibraryLoader
1515

1616
static class LibraryLoader
1717
{
18-
public static ILibraryLoader Get(OperatingSystemType os)
18+
static ILibraryLoader _instance = null;
19+
20+
public static ILibraryLoader Instance
1921
{
20-
switch (os)
22+
get
2123
{
22-
case OperatingSystemType.Windows:
23-
return new WindowsLoader();
24-
case OperatingSystemType.Darwin:
25-
return new DarwinLoader();
26-
case OperatingSystemType.Linux:
27-
return new LinuxLoader();
28-
default:
29-
throw new PlatformNotSupportedException($"This operating system ({os}) is not supported");
24+
if (_instance == null)
25+
{
26+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
27+
_instance = new WindowsLoader();
28+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
29+
_instance = new LinuxLoader();
30+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
31+
_instance = new DarwinLoader();
32+
else
33+
throw new PlatformNotSupportedException(
34+
"This operating system is not supported"
35+
);
36+
}
37+
38+
return _instance;
3039
}
3140
}
3241
}

src/runtime/platform/NativeCodePage.cs

Lines changed: 17 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,6 @@ namespace Python.Runtime.Platform
66
{
77
class NativeCodePageHelper
88
{
9-
/// <summary>
10-
/// Gets the operating system as reported by python's platform.system().
11-
/// </summary>
12-
public static OperatingSystemType OperatingSystem { get; private set; }
13-
14-
/// <summary>
15-
/// Gets the operating system as reported by python's platform.system().
16-
/// </summary>
17-
[Obsolete]
18-
public static string OperatingSystemName => PythonEngine.Platform;
19-
20-
/// <summary>
21-
/// Gets the machine architecture as reported by python's platform.machine().
22-
/// </summary>
23-
public static MachineType Machine { get; private set; }/* set in Initialize using python's platform.machine */
24-
25-
/// <summary>
26-
/// Gets the machine architecture as reported by python's platform.machine().
27-
/// </summary>
28-
[Obsolete]
29-
public static string MachineName { get; private set; }
30-
319
/// <summary>
3210
/// Initialized by InitializeNativeCodePage.
3311
///
@@ -45,33 +23,6 @@ class NativeCodePageHelper
4523
internal static IntPtr NativeCodePage = IntPtr.Zero;
4624

4725

48-
static readonly Dictionary<string, OperatingSystemType> OperatingSystemTypeMapping = new Dictionary<string, OperatingSystemType>()
49-
{
50-
{ "Windows", OperatingSystemType.Windows },
51-
{ "Darwin", OperatingSystemType.Darwin },
52-
{ "Linux", OperatingSystemType.Linux },
53-
};
54-
55-
/// <summary>
56-
/// Map lower-case version of the python machine name to the processor
57-
/// type. There are aliases, e.g. x86_64 and amd64 are two names for
58-
/// the same thing. Make sure to lower-case the search string, because
59-
/// capitalization can differ.
60-
/// </summary>
61-
static readonly Dictionary<string, MachineType> MachineTypeMapping = new Dictionary<string, MachineType>()
62-
{
63-
["i386"] = MachineType.i386,
64-
["i686"] = MachineType.i386,
65-
["x86"] = MachineType.i386,
66-
["x86_64"] = MachineType.x86_64,
67-
["amd64"] = MachineType.x86_64,
68-
["x64"] = MachineType.x86_64,
69-
["em64t"] = MachineType.x86_64,
70-
["armv7l"] = MachineType.armv7l,
71-
["armv8"] = MachineType.armv8,
72-
["aarch64"] = MachineType.aarch64,
73-
};
74-
7526
/// <summary>
7627
/// Structure to describe native code.
7728
///
@@ -108,11 +59,11 @@ public static NativeCode Active
10859
{
10960
get
11061
{
111-
switch (Machine)
62+
switch (RuntimeInformation.ProcessArchitecture)
11263
{
113-
case MachineType.i386:
64+
case Architecture.X86:
11465
return I386;
115-
case MachineType.x86_64:
66+
case Architecture.X64:
11667
return X86_64;
11768
default:
11869
return null;
@@ -205,14 +156,14 @@ int MAP_ANONYMOUS
205156
{
206157
get
207158
{
208-
switch (OperatingSystem)
159+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
209160
{
210-
case OperatingSystemType.Darwin:
211-
return 0x1000;
212-
case OperatingSystemType.Linux:
213-
return 0x20;
214-
default:
215-
throw new NotImplementedException($"mmap is not supported on {OperatingSystemName}");
161+
return 0x20;
162+
}
163+
else
164+
{
165+
// OSX, FreeBSD
166+
return 0x1000;
216167
}
217168
}
218169
}
@@ -236,32 +187,16 @@ public void SetReadExec(IntPtr mappedMemory, int numBytes)
236187
}
237188
}
238189

239-
/// <summary>
240-
/// Initializes the data about platforms.
241-
///
242-
/// This must be the last step when initializing the runtime:
243-
/// GetManagedString needs to have the cached values for types.
244-
/// But it must run before initializing anything outside the runtime
245-
/// because those rely on the platform data.
246-
/// </summary>
247-
public static void InitializePlatformData()
248-
{
249-
MachineName = SystemInfo.GetArchitecture();
250-
Machine = SystemInfo.GetMachineType();
251-
OperatingSystem = SystemInfo.GetSystemType();
252-
}
253-
254190
internal static IMemoryMapper CreateMemoryMapper()
255191
{
256-
switch (OperatingSystem)
192+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
193+
{
194+
return new WindowsMemoryMapper();
195+
}
196+
else
257197
{
258-
case OperatingSystemType.Darwin:
259-
case OperatingSystemType.Linux:
260-
return new UnixMemoryMapper();
261-
case OperatingSystemType.Windows:
262-
return new WindowsMemoryMapper();
263-
default:
264-
throw new NotImplementedException($"No support for {OperatingSystemName}");
198+
// Linux, OSX, FreeBSD
199+
return new UnixMemoryMapper();
265200
}
266201
}
267202

src/runtime/runtime.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
158158
ClassDerivedObject.Reset();
159159
TypeManager.Initialize();
160160

161-
// Initialize data about the platform we're running on. We need
162-
// this for the type manager and potentially other details. Must
163-
// happen after caching the python types, above.
164-
NativeCodePageHelper.InitializePlatformData();
165-
166161
// Initialize modules that depend on the runtime class.
167162
AssemblyManager.Initialize();
168163
if (mode == ShutdownMode.Reload && RuntimeData.HasStashData())
@@ -2153,7 +2148,7 @@ internal static void Py_CLEAR(ref IntPtr ob)
21532148

21542149
internal static void SetNoSiteFlag()
21552150
{
2156-
var loader = LibraryLoader.Get(NativeCodePageHelper.OperatingSystem);
2151+
var loader = LibraryLoader.Instance;
21572152
IntPtr dllLocal = IntPtr.Zero;
21582153
if (_PythonDll != "__Internal")
21592154
{

0 commit comments

Comments
 (0)