@@ -25,7 +25,7 @@ namespace Python.Runtime {
2525
2626 static class NativeMethods
2727 {
28- #if MONO_LINUX
28+ #if ( MONO_LINUX || MONO_OSX )
2929 static public IntPtr LoadLibrary ( string fileName ) {
3030 return dlopen ( fileName , RTLD_NOW | RTLD_SHARED ) ;
3131 }
@@ -35,6 +35,10 @@ static public void FreeLibrary(IntPtr handle) {
3535 }
3636
3737 static public IntPtr GetProcAddress ( IntPtr dllHandle , string name ) {
38+ // look in the exe if dllHandle is NULL
39+ if ( IntPtr . Zero == dllHandle )
40+ dllHandle = RTLD_DEFAULT ;
41+
3842 // clear previous errors if any
3943 dlerror ( ) ;
4044 var res = dlsym ( dllHandle , name ) ;
@@ -45,8 +49,26 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) {
4549 return res ;
4650 }
4751
48- const int RTLD_NOW = 2 ;
49- const int RTLD_SHARED = 20 ;
52+ #if ( MONO_OSX )
53+ static int RTLD_NOW = 0x2 ;
54+ static int RTLD_SHARED = 0x20 ;
55+ static IntPtr RTLD_DEFAULT = new IntPtr ( - 2 ) ;
56+
57+ [ DllImport ( "__Internal" ) ]
58+ private static extern IntPtr dlopen ( String fileName , int flags ) ;
59+
60+ [ DllImport ( "__Internal" ) ]
61+ private static extern IntPtr dlsym ( IntPtr handle , String symbol ) ;
62+
63+ [ DllImport ( "__Internal" ) ]
64+ private static extern int dlclose ( IntPtr handle ) ;
65+
66+ [ DllImport ( "__Internal" ) ]
67+ private static extern IntPtr dlerror ( ) ;
68+ #else
69+ static int RTLD_NOW = 0x2 ;
70+ static int RTLD_SHARED = 0x20 ;
71+ static IntPtr RTLD_DEFAULT = IntPtr . Zero ;
5072
5173 [ DllImport ( "libdl.so" ) ]
5274 private static extern IntPtr dlopen ( String fileName , int flags ) ;
@@ -59,6 +81,8 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) {
5981
6082 [ DllImport ( "libdl.so" ) ]
6183 private static extern IntPtr dlerror ( ) ;
84+ #endif
85+
6286#else
6387 [ DllImport ( "kernel32.dll" ) ]
6488 public static extern IntPtr LoadLibrary ( string dllToLoad ) ;
@@ -139,7 +163,7 @@ public class Runtime {
139163#if ( PYTHON27 )
140164 internal const string dllBase = "python27" ;
141165#endif
142- #if ( MONO_LINUX )
166+ #if ( MONO_LINUX || MONO_OSX )
143167#if ( PYTHON32 )
144168 internal const string dllBase = "python3.2" ;
145169#endif
@@ -295,10 +319,15 @@ internal static void Initialize() {
295319 Error = new IntPtr ( - 1 ) ;
296320
297321#if ( PYTHON32 || PYTHON33 || PYTHON34 )
298- IntPtr dll = NativeMethods . LoadLibrary ( Runtime . dll ) ;
322+ IntPtr dll = IntPtr . Zero ;
323+ if ( "__Internal" != Runtime . dll ) {
324+ NativeMethods . LoadLibrary ( Runtime . dll ) ;
325+ }
299326 _PyObject_NextNotImplemented = NativeMethods . GetProcAddress ( dll , "_PyObject_NextNotImplemented" ) ;
300- #if ! MONO_LINUX
301- NativeMethods . FreeLibrary ( dll ) ;
327+ #if ! ( MONO_LINUX || MONO_OSX )
328+ if ( IntPtr . Zero != dll ) {
329+ NativeMethods . FreeLibrary ( dll ) ;
330+ }
302331#endif
303332#endif
304333
@@ -1751,7 +1780,11 @@ internal unsafe static string GetManagedString(IntPtr op)
17511780 if ( type = = Runtime . PyUnicodeType )
17521781 {
17531782 IntPtr p = Runtime. PyUnicode_AsUnicode( op) ;
1754- return UnixMarshal. PtrToString( p , Encoding . UTF32 ) ;
1783+ int length = Runtime. PyUnicode_GetSize( op ) ;
1784+ int size = length * 4 ;
1785+ byte [ ] buffer = new byte [ size ] ;
1786+ Marshal . Copy ( p , buffer , 0 , size ) ;
1787+ return Encoding . UTF32 . GetString ( buffer , 0 , size ) ;
17551788 }
17561789
17571790 return null ;
0 commit comments