|
9 | 9 |
|
10 | 10 | namespace Python.Runtime |
11 | 11 | { |
12 | | - [SuppressUnmanagedCodeSecurityAttribute()] |
| 12 | + [SuppressUnmanagedCodeSecurity()] |
13 | 13 | static class NativeMethods |
14 | 14 | { |
15 | 15 | #if MONO_LINUX || MONO_OSX |
16 | | - static public IntPtr LoadLibrary(string fileName) |
| 16 | + private static int RTLD_NOW = 0x2; |
| 17 | + private static int RTLD_SHARED = 0x20; |
| 18 | +#if MONO_OSX |
| 19 | + private static IntPtr RTLD_DEFAULT = new IntPtr(-2); |
| 20 | + private const string NativeDll = "__Internal"; |
| 21 | +#elif MONO_LINUX |
| 22 | + private static IntPtr RTLD_DEFAULT = IntPtr.Zero; |
| 23 | + private const string NativeDll = "libdl.so"; |
| 24 | +#endif |
| 25 | + |
| 26 | + public static IntPtr LoadLibrary(string fileName) |
17 | 27 | { |
18 | 28 | return dlopen(fileName, RTLD_NOW | RTLD_SHARED); |
19 | 29 | } |
20 | 30 |
|
21 | | - static public void FreeLibrary(IntPtr handle) |
| 31 | + public static void FreeLibrary(IntPtr handle) |
22 | 32 | { |
23 | 33 | dlclose(handle); |
24 | 34 | } |
25 | 35 |
|
26 | | - static public IntPtr GetProcAddress(IntPtr dllHandle, string name) |
| 36 | + public static IntPtr GetProcAddress(IntPtr dllHandle, string name) |
27 | 37 | { |
28 | 38 | // look in the exe if dllHandle is NULL |
29 | | - if (IntPtr.Zero == dllHandle) |
| 39 | + if (dllHandle == IntPtr.Zero) |
| 40 | + { |
30 | 41 | dllHandle = RTLD_DEFAULT; |
| 42 | + } |
31 | 43 |
|
32 | 44 | // clear previous errors if any |
33 | 45 | dlerror(); |
34 | | - var res = dlsym(dllHandle, name); |
35 | | - var errPtr = dlerror(); |
| 46 | + IntPtr res = dlsym(dllHandle, name); |
| 47 | + IntPtr errPtr = dlerror(); |
36 | 48 | if (errPtr != IntPtr.Zero) |
37 | 49 | { |
38 | 50 | throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errPtr)); |
39 | 51 | } |
40 | 52 | return res; |
41 | 53 | } |
42 | 54 |
|
43 | | -#if MONO_OSX |
44 | | - static int RTLD_NOW = 0x2; |
45 | | - static int RTLD_SHARED = 0x20; |
46 | | - static IntPtr RTLD_DEFAULT = new IntPtr(-2); |
47 | | - |
48 | | - [DllImport("__Internal")] |
| 55 | + [DllImport(NativeDll)] |
49 | 56 | private static extern IntPtr dlopen(String fileName, int flags); |
50 | 57 |
|
51 | | - [DllImport("__Internal")] |
| 58 | + [DllImport(NativeDll)] |
52 | 59 | private static extern IntPtr dlsym(IntPtr handle, String symbol); |
53 | 60 |
|
54 | | - [DllImport("__Internal")] |
| 61 | + [DllImport(NativeDll)] |
55 | 62 | private static extern int dlclose(IntPtr handle); |
56 | 63 |
|
57 | | - [DllImport("__Internal")] |
| 64 | + [DllImport(NativeDll)] |
58 | 65 | private static extern IntPtr dlerror(); |
59 | | -#elif MONO_LINUX |
60 | | - static int RTLD_NOW = 0x2; |
61 | | - static int RTLD_SHARED = 0x20; |
62 | | - static IntPtr RTLD_DEFAULT = IntPtr.Zero; |
63 | | - |
64 | | - [DllImport("libdl.so")] |
65 | | - private static extern IntPtr dlopen(String fileName, int flags); |
66 | | - |
67 | | - [DllImport("libdl.so")] |
68 | | - private static extern IntPtr dlsym(IntPtr handle, String symbol); |
69 | | - |
70 | | - [DllImport("libdl.so")] |
71 | | - private static extern int dlclose(IntPtr handle); |
72 | | - |
73 | | - [DllImport("libdl.so")] |
74 | | - private static extern IntPtr dlerror(); |
75 | | -#endif |
76 | | - |
77 | 66 | #else // Windows |
| 67 | + private const string NativeDll = "kernel32.dll"; |
78 | 68 |
|
79 | | - [DllImport("kernel32.dll")] |
| 69 | + [DllImport(NativeDll)] |
80 | 70 | public static extern IntPtr LoadLibrary(string dllToLoad); |
81 | 71 |
|
82 | | - [DllImport("kernel32.dll")] |
| 72 | + [DllImport(NativeDll)] |
83 | 73 | public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); |
84 | 74 |
|
85 | | - [DllImport("kernel32.dll")] |
| 75 | + [DllImport(NativeDll)] |
86 | 76 | public static extern bool FreeLibrary(IntPtr hModule); |
87 | 77 | #endif |
88 | 78 | } |
@@ -189,12 +179,12 @@ internal static void Initialize() |
189 | 179 | { |
190 | 180 | is32bit = IntPtr.Size == 4; |
191 | 181 |
|
192 | | - if (0 == Runtime.Py_IsInitialized()) |
| 182 | + if (Runtime.Py_IsInitialized() == 0) |
193 | 183 | { |
194 | 184 | Runtime.Py_Initialize(); |
195 | 185 | } |
196 | 186 |
|
197 | | - if (0 == Runtime.PyEval_ThreadsInitialized()) |
| 187 | + if (Runtime.PyEval_ThreadsInitialized() == 0) |
198 | 188 | { |
199 | 189 | Runtime.PyEval_InitThreads(); |
200 | 190 | } |
|
0 commit comments