Skip to content

Commit 88aa3c4

Browse files
committed
Fixes for OSX.
1 parent 497b638 commit 88aa3c4

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ def build_extension(self, ext):
127127
defines.extend(["DEBUG", "TRACE"])
128128

129129
if sys.platform != "win32" and DEVTOOLS == "Mono":
130-
defines.append("MONO_LINUX")
130+
if sys.platform == "darwin":
131+
defines.append("MONO_OSX")
132+
else:
133+
defines.append("MONO_LINUX")
131134

132135
# Check if --enable-shared was set when Python was built
133136
enable_shared = get_config_var("Py_ENABLE_SHARED")

src/monoclr/clrmod.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ static PyObject *_initclr() {
5757
if (pn_args->error) {
5858
return NULL;
5959
}
60+
61+
if (NULL != pn_args->module)
62+
return pn_args->module;
63+
6064
return m;
6165
}
6266

src/monoclr/pynetclr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef struct {
3232
char *error;
3333
char *init_name;
3434
char *shutdown_name;
35+
PyObject* module;
3536
} PyNet_Args;
3637

3738
PyNet_Args* PyNet_Init(int);

src/monoclr/pynetinit.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ PyNet_Args* PyNet_Init(int ext) {
2727
pn_args->pr_file = PR_ASSEMBLY;
2828
pn_args->error = NULL;
2929
pn_args->shutdown = NULL;
30+
pn_args->module = NULL;
3031

3132
if (ext == 0) {
3233
pn_args->init_name = "Python.Runtime:Initialize()";
@@ -96,6 +97,7 @@ void main_thread_handler (gpointer user_data) {
9697
MonoImage *pr_image;
9798
MonoClass *pythonengine;
9899
MonoObject *exception = NULL;
100+
MonoObject *init_result;
99101

100102
#ifndef _WIN32
101103
// Get the filename of the python shared object and set
@@ -209,11 +211,17 @@ void main_thread_handler (gpointer user_data) {
209211
return;
210212
}
211213

212-
mono_runtime_invoke(init, NULL, NULL, &exception);
214+
init_result = mono_runtime_invoke(init, NULL, NULL, &exception);
213215
if (exception) {
214216
pn_args->error = PyNet_ExceptionToString(exception);
215217
return;
216218
}
219+
220+
#if PY_MAJOR_VERSION >= 3
221+
if (NULL != init_result)
222+
pn_args->module = *(PyObject**)mono_object_unbox(init_result);
223+
#endif
224+
217225
}
218226

219227
// Get string from a Mono exception

src/runtime/runtime.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)