7
7
8
8
namespace Python . Runtime
9
9
{
10
- [ SuppressUnmanagedCodeSecurity ]
11
- internal static class NativeMethods
12
- {
13
- #if MONO_LINUX || MONO_OSX
14
- #if NETSTANDARD
15
- private static int RTLD_NOW = 0x2 ;
16
- #if MONO_LINUX
17
- private static int RTLD_GLOBAL = 0x100 ;
18
- private static IntPtr RTLD_DEFAULT = IntPtr . Zero ;
19
- private const string NativeDll = "libdl.so" ;
20
- public static IntPtr LoadLibrary ( string fileName )
21
- {
22
- return dlopen ( $ "lib{ fileName } .so", RTLD_NOW | RTLD_GLOBAL ) ;
23
- }
24
- #elif MONO_OSX
25
- private static int RTLD_GLOBAL = 0x8 ;
26
- private const string NativeDll = "/usr/lib/libSystem.dylib" ;
27
- private static IntPtr RTLD_DEFAULT = new IntPtr ( - 2 ) ;
28
-
29
- public static IntPtr LoadLibrary ( string fileName )
30
- {
31
- return dlopen ( $ "lib{ fileName } .dylib", RTLD_NOW | RTLD_GLOBAL ) ;
32
- }
33
- #endif
34
- #else
35
- private static int RTLD_NOW = 0x2 ;
36
- private static int RTLD_SHARED = 0x20 ;
37
- #if MONO_OSX
38
- private static IntPtr RTLD_DEFAULT = new IntPtr ( - 2 ) ;
39
- private const string NativeDll = "__Internal" ;
40
- #elif MONO_LINUX
41
- private static IntPtr RTLD_DEFAULT = IntPtr . Zero ;
42
- private const string NativeDll = "libdl.so" ;
43
- #endif
44
-
45
- public static IntPtr LoadLibrary ( string fileName )
46
- {
47
- return dlopen ( fileName , RTLD_NOW | RTLD_SHARED ) ;
48
- }
49
- #endif
50
-
51
-
52
- public static void FreeLibrary ( IntPtr handle )
53
- {
54
- dlclose ( handle ) ;
55
- }
56
-
57
- public static IntPtr GetProcAddress ( IntPtr dllHandle , string name )
58
- {
59
- // look in the exe if dllHandle is NULL
60
- if ( dllHandle == IntPtr . Zero )
61
- {
62
- dllHandle = RTLD_DEFAULT ;
63
- }
64
-
65
- // clear previous errors if any
66
- dlerror ( ) ;
67
- IntPtr res = dlsym ( dllHandle , name ) ;
68
- IntPtr errPtr = dlerror ( ) ;
69
- if ( errPtr != IntPtr . Zero )
70
- {
71
- throw new Exception ( "dlsym: " + Marshal . PtrToStringAnsi ( errPtr ) ) ;
72
- }
73
- return res ;
74
- }
75
-
76
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
77
- public static extern IntPtr dlopen ( String fileName , int flags ) ;
78
-
79
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
80
- private static extern IntPtr dlsym ( IntPtr handle , String symbol ) ;
81
-
82
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl ) ]
83
- private static extern int dlclose ( IntPtr handle ) ;
84
-
85
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl ) ]
86
- private static extern IntPtr dlerror ( ) ;
87
- #else // Windows
88
- private const string NativeDll = "kernel32.dll" ;
89
-
90
- [ DllImport ( NativeDll ) ]
91
- public static extern IntPtr LoadLibrary ( string dllToLoad ) ;
92
-
93
- [ DllImport ( NativeDll ) ]
94
- public static extern IntPtr GetProcAddress ( IntPtr hModule , string procedureName ) ;
95
-
96
- [ DllImport ( NativeDll ) ]
97
- public static extern bool FreeLibrary ( IntPtr hModule ) ;
98
- #endif
99
- }
10
+ using Python . Runtime . Platform ;
100
11
101
12
/// <summary>
102
13
/// Encapsulates the low-level Python C API. Note that it is
@@ -197,17 +108,6 @@ public class Runtime
197
108
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
198
109
internal static bool IsWindows = Environment . OSVersion . Platform == PlatformID . Win32NT ;
199
110
200
- /// <summary>
201
- /// Operating system type as reported by Python.
202
- /// </summary>
203
- public enum OperatingSystemType
204
- {
205
- Windows ,
206
- Darwin ,
207
- Linux ,
208
- Other
209
- }
210
-
211
111
static readonly Dictionary < string , OperatingSystemType > OperatingSystemTypeMapping = new Dictionary < string , OperatingSystemType > ( )
212
112
{
213
113
{ "Windows" , OperatingSystemType . Windows } ,
@@ -225,14 +125,6 @@ public enum OperatingSystemType
225
125
/// </summary>
226
126
public static string OperatingSystemName { get ; private set ; }
227
127
228
- public enum MachineType
229
- {
230
- i386 ,
231
- x86_64 ,
232
- armv7l ,
233
- armv8 ,
234
- Other
235
- } ;
236
128
237
129
/// <summary>
238
130
/// Map lower-case version of the python machine name to the processor
@@ -397,24 +289,24 @@ internal static void Initialize(bool initSigs = false)
397
289
398
290
Error = new IntPtr ( - 1 ) ;
399
291
292
+ // Initialize data about the platform we're running on. We need
293
+ // this for the type manager and potentially other details. Must
294
+ // happen after caching the python types, above.
295
+ InitializePlatformData ( ) ;
296
+
400
297
IntPtr dllLocal = IntPtr . Zero ;
298
+ var loader = LibraryLoader . Get ( OperatingSystem ) ;
401
299
402
300
if ( _PythonDll != "__Internal" )
403
301
{
404
- dllLocal = NativeMethods . LoadLibrary ( _PythonDll ) ;
302
+ dllLocal = loader . Load ( _PythonDll ) ;
405
303
}
406
- _PyObject_NextNotImplemented = NativeMethods . GetProcAddress ( dllLocal , "_PyObject_NextNotImplemented" ) ;
304
+ _PyObject_NextNotImplemented = loader . GetFunction ( dllLocal , "_PyObject_NextNotImplemented" ) ;
407
305
408
- #if ! ( MONO_LINUX || MONO_OSX )
409
306
if ( dllLocal != IntPtr . Zero )
410
307
{
411
- NativeMethods . FreeLibrary ( dllLocal ) ;
308
+ loader . Free ( dllLocal ) ;
412
309
}
413
- #endif
414
- // Initialize data about the platform we're running on. We need
415
- // this for the type manager and potentially other details. Must
416
- // happen after caching the python types, above.
417
- InitializePlatformData ( ) ;
418
310
419
311
// Initialize modules that depend on the runtime class.
420
312
AssemblyManager . Initialize ( ) ;
0 commit comments