Skip to content

Commit 5753d38

Browse files
committed
Drop InitializePlatformData
1 parent 60d15cd commit 5753d38

File tree

4 files changed

+36
-142
lines changed

4 files changed

+36
-142
lines changed

Python.Runtime/platform/InternalLoadContext.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ class InternalLoadContext : AssemblyLoadContext
1010

1111
protected override IntPtr LoadUnmanagedDll(string name)
1212
{
13-
if (name == "__Internal")
14-
{
15-
var loader = LibraryLoader.Get(OperatingSystemType.Linux);
16-
return loader.Load(null);
17-
}
18-
19-
return IntPtr.Zero;
13+
var filtered = name == "__Internal" ? null : name;
14+
return LibraryLoader.Instance.Load(filtered);
2015
}
2116

2217
public static AssemblyLoadContext Instance { get; } = new InternalLoadContext();

Python.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.OSX))
29+
_instance = new DarwinLoader();
30+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
31+
_instance = new LinuxLoader();
32+
else
33+
throw new PlatformNotSupportedException(
34+
$"This operating system is not supported"
35+
);
36+
}
37+
38+
return _instance;
3039
}
3140
}
3241
}

Python.Runtime/runtime.cs

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,7 @@ public static partial class Runtime
2626

2727
internal static bool Is32Bit = IntPtr.Size == 4;
2828

29-
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
30-
internal static bool IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
31-
32-
static readonly Dictionary<string, OperatingSystemType> OperatingSystemTypeMapping = new Dictionary<string, OperatingSystemType>()
33-
{
34-
{ "Windows", OperatingSystemType.Windows },
35-
{ "Darwin", OperatingSystemType.Darwin },
36-
{ "Linux", OperatingSystemType.Linux },
37-
};
38-
39-
/// <summary>
40-
/// Gets the operating system as reported by python's platform.system().
41-
/// </summary>
42-
public static OperatingSystemType OperatingSystem { get; private set; }
43-
44-
/// <summary>
45-
/// Gets the operating system as reported by python's platform.system().
46-
/// </summary>
47-
public static string OperatingSystemName { get; private set; }
48-
49-
50-
/// <summary>
51-
/// Map lower-case version of the python machine name to the processor
52-
/// type. There are aliases, e.g. x86_64 and amd64 are two names for
53-
/// the same thing. Make sure to lower-case the search string, because
54-
/// capitalization can differ.
55-
/// </summary>
56-
static readonly Dictionary<string, MachineType> MachineTypeMapping = new Dictionary<string, MachineType>()
57-
{
58-
["i386"] = MachineType.i386,
59-
["i686"] = MachineType.i386,
60-
["x86"] = MachineType.i386,
61-
["x86_64"] = MachineType.x86_64,
62-
["amd64"] = MachineType.x86_64,
63-
["x64"] = MachineType.x86_64,
64-
["em64t"] = MachineType.x86_64,
65-
["armv7l"] = MachineType.armv7l,
66-
["armv8"] = MachineType.armv8,
67-
["aarch64"] = MachineType.aarch64,
68-
};
69-
70-
/// <summary>
71-
/// Gets the machine architecture as reported by python's platform.machine().
72-
/// </summary>
73-
public static MachineType Machine { get; private set; }/* set in Initialize using python's platform.machine */
74-
75-
/// <summary>
76-
/// Gets the machine architecture as reported by python's platform.machine().
77-
/// </summary>
78-
public static string MachineName { get; private set; }
29+
internal static bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
7930

8031
#if PYTHON2
8132
internal static bool IsPython2 = true;
@@ -221,13 +172,8 @@ internal static void Initialize(bool initSigs = false)
221172

222173
Error = new IntPtr(-1);
223174

224-
// Initialize data about the platform we're running on. We need
225-
// this for the type manager and potentially other details. Must
226-
// happen after caching the python types, above.
227-
InitializePlatformData();
228-
229175
IntPtr dllLocal = IntPtr.Zero;
230-
var loader = LibraryLoader.Get(OperatingSystem);
176+
var loader = LibraryLoader.Instance;
231177

232178
_PyObject_NextNotImplemented = loader.GetFunction(dllLocal, "_PyObject_NextNotImplemented");
233179
PyModuleType = loader.GetFunction(dllLocal, "PyModule_Type");
@@ -248,53 +194,6 @@ internal static void Initialize(bool initSigs = false)
248194
AssemblyManager.UpdatePath();
249195
}
250196

251-
/// <summary>
252-
/// Initializes the data about platforms.
253-
///
254-
/// This must be the last step when initializing the runtime:
255-
/// GetManagedString needs to have the cached values for types.
256-
/// But it must run before initializing anything outside the runtime
257-
/// because those rely on the platform data.
258-
/// </summary>
259-
private static void InitializePlatformData()
260-
{
261-
IntPtr op;
262-
IntPtr fn;
263-
IntPtr platformModule = PyImport_ImportModule("platform");
264-
IntPtr emptyTuple = PyTuple_New(0);
265-
266-
fn = PyObject_GetAttrString(platformModule, "system");
267-
op = PyObject_Call(fn, emptyTuple, IntPtr.Zero);
268-
OperatingSystemName = GetManagedString(op);
269-
XDecref(op);
270-
XDecref(fn);
271-
272-
fn = PyObject_GetAttrString(platformModule, "machine");
273-
op = PyObject_Call(fn, emptyTuple, IntPtr.Zero);
274-
MachineName = GetManagedString(op);
275-
XDecref(op);
276-
XDecref(fn);
277-
278-
XDecref(emptyTuple);
279-
XDecref(platformModule);
280-
281-
// Now convert the strings into enum values so we can do switch
282-
// statements rather than constant parsing.
283-
OperatingSystemType OSType;
284-
if (!OperatingSystemTypeMapping.TryGetValue(OperatingSystemName, out OSType))
285-
{
286-
OSType = OperatingSystemType.Other;
287-
}
288-
OperatingSystem = OSType;
289-
290-
MachineType MType;
291-
if (!MachineTypeMapping.TryGetValue(MachineName.ToLower(), out MType))
292-
{
293-
MType = MachineType.Other;
294-
}
295-
Machine = MType;
296-
}
297-
298197
internal static void Shutdown()
299198
{
300199
AssemblyManager.Shutdown();

Python.Runtime/typemanager.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,11 @@ public static NativeCode Active
503503
{
504504
get
505505
{
506-
switch (Runtime.Machine)
506+
switch (RuntimeInformation.ProcessArchitecture)
507507
{
508-
case MachineType.i386:
508+
case Architecture.X86:
509509
return I386;
510-
case MachineType.x86_64:
510+
case Architecture.X64:
511511
return X86_64;
512512
default:
513513
return null;
@@ -600,15 +600,12 @@ int MAP_ANONYMOUS
600600
{
601601
get
602602
{
603-
switch (Runtime.OperatingSystem)
604-
{
605-
case OperatingSystemType.Darwin:
606-
return 0x1000;
607-
case OperatingSystemType.Linux:
608-
return 0x20;
609-
default:
610-
throw new NotImplementedException($"mmap is not supported on {Runtime.OperatingSystemName}");
611-
}
603+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
604+
return 0x1000;
605+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
606+
return 0x20;
607+
else
608+
throw new NotImplementedException($"mmap is not supported on this operating system");
612609
}
613610
}
614611

@@ -633,16 +630,10 @@ public void SetReadExec(IntPtr mappedMemory, int numBytes)
633630

634631
internal static IMemoryMapper CreateMemoryMapper()
635632
{
636-
switch (Runtime.OperatingSystem)
637-
{
638-
case OperatingSystemType.Darwin:
639-
case OperatingSystemType.Linux:
640-
return new UnixMemoryMapper();
641-
case OperatingSystemType.Windows:
642-
return new WindowsMemoryMapper();
643-
default:
644-
throw new NotImplementedException($"No support for {Runtime.OperatingSystemName}");
645-
}
633+
if (Runtime.IsWindows)
634+
return new WindowsMemoryMapper();
635+
else
636+
return new UnixMemoryMapper();
646637
}
647638

648639
/// <summary>

0 commit comments

Comments
 (0)