X Tutup
Skip to content

Commit 6450362

Browse files
author
dse
committed
Fix for importing numpy and other python modules with native parts.
1 parent 4c13e92 commit 6450362

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/runtime/runtime.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Runtime.InteropServices;
33
using System.Security;
44
using System.Text;
@@ -9,6 +9,27 @@ namespace Python.Runtime
99
internal static class NativeMethods
1010
{
1111
#if MONO_LINUX || MONO_OSX
12+
#if NETCOREAPP
13+
private static int RTLD_NOW = 0x2;
14+
#if MONO_LINUX
15+
private static int RTLD_GLOBAL = 0x100;
16+
private static IntPtr RTLD_DEFAULT = IntPtr.Zero;
17+
private const string NativeDll = "libdl.so";
18+
public static IntPtr LoadLibrary(string fileName)
19+
{
20+
return dlopen($"lib{fileName}.so", RTLD_NOW | RTLD_GLOBAL);
21+
}
22+
#elif MONO_OSX
23+
private static int RTLD_GLOBAL = 0x8;
24+
private const string NativeDll = "/usr/lib/libSystem.dylib"
25+
private static IntPtr RTLD_DEFAULT = new IntPtr(-2);
26+
27+
public static IntPtr LoadLibrary(string fileName)
28+
{
29+
return dlopen($"lib{fileName}.dylib", RTLD_NOW | RTLD_GLOBAL);
30+
}
31+
#endif
32+
#else
1233
private static int RTLD_NOW = 0x2;
1334
private static int RTLD_SHARED = 0x20;
1435
#if MONO_OSX
@@ -23,6 +44,8 @@ public static IntPtr LoadLibrary(string fileName)
2344
{
2445
return dlopen(fileName, RTLD_NOW | RTLD_SHARED);
2546
}
47+
#endif
48+
2649

2750
public static void FreeLibrary(IntPtr handle)
2851
{
@@ -48,16 +71,16 @@ public static IntPtr GetProcAddress(IntPtr dllHandle, string name)
4871
return res;
4972
}
5073

51-
[DllImport(NativeDll)]
52-
private static extern IntPtr dlopen(String fileName, int flags);
74+
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
75+
public static extern IntPtr dlopen(String fileName, int flags);
5376

54-
[DllImport(NativeDll)]
77+
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
5578
private static extern IntPtr dlsym(IntPtr handle, String symbol);
5679

57-
[DllImport(NativeDll)]
80+
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
5881
private static extern int dlclose(IntPtr handle);
5982

60-
[DllImport(NativeDll)]
83+
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
6184
private static extern IntPtr dlerror();
6285
#else // Windows
6386
private const string NativeDll = "kernel32.dll";
@@ -158,7 +181,7 @@ public class Runtime
158181

159182
public static readonly string PythonDLL = _PythonDll;
160183

161-
#if PYTHON_WITHOUT_ENABLE_SHARED
184+
#if PYTHON_WITHOUT_ENABLE_SHARED && !NETCOREAPP
162185
internal const string _PythonDll = "__Internal";
163186
#else
164187
internal const string _PythonDll = dllBase + dllWithPyDebug + dllWithPyMalloc;
@@ -298,19 +321,20 @@ internal static void Initialize()
298321
Error = new IntPtr(-1);
299322

300323
IntPtr dllLocal = IntPtr.Zero;
301-
#if !NETCOREAPP
324+
302325
if (_PythonDll != "__Internal")
303326
{
304327
dllLocal = NativeMethods.LoadLibrary(_PythonDll);
305328
}
306329
_PyObject_NextNotImplemented = NativeMethods.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented");
330+
307331
#if !(MONO_LINUX || MONO_OSX)
308332
if (dllLocal != IntPtr.Zero)
309333
{
310334
NativeMethods.FreeLibrary(dllLocal);
311335
}
312336
#endif
313-
#endif
337+
314338
// Initialize modules that depend on the runtime class.
315339
AssemblyManager.Initialize();
316340
PyCLRMetaType = MetaType.Initialize();

0 commit comments

Comments
 (0)
X Tutup