Skip to content

Commit edada03

Browse files
committed
Refactor nativemethods
1 parent de3d2ef commit edada03

1 file changed

Lines changed: 29 additions & 39 deletions

File tree

src/runtime/runtime.cs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,70 @@
99

1010
namespace Python.Runtime
1111
{
12-
[SuppressUnmanagedCodeSecurityAttribute()]
12+
[SuppressUnmanagedCodeSecurity()]
1313
static class NativeMethods
1414
{
1515
#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)
1727
{
1828
return dlopen(fileName, RTLD_NOW | RTLD_SHARED);
1929
}
2030

21-
static public void FreeLibrary(IntPtr handle)
31+
public static void FreeLibrary(IntPtr handle)
2232
{
2333
dlclose(handle);
2434
}
2535

26-
static public IntPtr GetProcAddress(IntPtr dllHandle, string name)
36+
public static IntPtr GetProcAddress(IntPtr dllHandle, string name)
2737
{
2838
// look in the exe if dllHandle is NULL
29-
if (IntPtr.Zero == dllHandle)
39+
if (dllHandle == IntPtr.Zero)
40+
{
3041
dllHandle = RTLD_DEFAULT;
42+
}
3143

3244
// clear previous errors if any
3345
dlerror();
34-
var res = dlsym(dllHandle, name);
35-
var errPtr = dlerror();
46+
IntPtr res = dlsym(dllHandle, name);
47+
IntPtr errPtr = dlerror();
3648
if (errPtr != IntPtr.Zero)
3749
{
3850
throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errPtr));
3951
}
4052
return res;
4153
}
4254

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)]
4956
private static extern IntPtr dlopen(String fileName, int flags);
5057

51-
[DllImport("__Internal")]
58+
[DllImport(NativeDll)]
5259
private static extern IntPtr dlsym(IntPtr handle, String symbol);
5360

54-
[DllImport("__Internal")]
61+
[DllImport(NativeDll)]
5562
private static extern int dlclose(IntPtr handle);
5663

57-
[DllImport("__Internal")]
64+
[DllImport(NativeDll)]
5865
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-
7766
#else // Windows
67+
private const string NativeDll = "kernel32.dll";
7868

79-
[DllImport("kernel32.dll")]
69+
[DllImport(NativeDll)]
8070
public static extern IntPtr LoadLibrary(string dllToLoad);
8171

82-
[DllImport("kernel32.dll")]
72+
[DllImport(NativeDll)]
8373
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
8474

85-
[DllImport("kernel32.dll")]
75+
[DllImport(NativeDll)]
8676
public static extern bool FreeLibrary(IntPtr hModule);
8777
#endif
8878
}
@@ -189,12 +179,12 @@ internal static void Initialize()
189179
{
190180
is32bit = IntPtr.Size == 4;
191181

192-
if (0 == Runtime.Py_IsInitialized())
182+
if (Runtime.Py_IsInitialized() == 0)
193183
{
194184
Runtime.Py_Initialize();
195185
}
196186

197-
if (0 == Runtime.PyEval_ThreadsInitialized())
187+
if (Runtime.PyEval_ThreadsInitialized() == 0)
198188
{
199189
Runtime.PyEval_InitThreads();
200190
}

0 commit comments

Comments
 (0)