Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ def build_extension(self, ext):
defines.extend(["DEBUG", "TRACE"])

if sys.platform != "win32" and DEVTOOLS == "Mono":
defines.append("MONO_LINUX")
if sys.platform == "darwin":
defines.append("MONO_OSX")
else:
defines.append("MONO_LINUX")

# Check if --enable-shared was set when Python was built
enable_shared = get_config_var("Py_ENABLE_SHARED")
Expand Down
4 changes: 4 additions & 0 deletions src/monoclr/clrmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ static PyObject *_initclr() {
if (pn_args->error) {
return NULL;
}

if (NULL != pn_args->module)
return pn_args->module;

return m;
}

Expand Down
1 change: 1 addition & 0 deletions src/monoclr/pynetclr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef struct {
char *error;
char *init_name;
char *shutdown_name;
PyObject* module;
} PyNet_Args;

PyNet_Args* PyNet_Init(int);
Expand Down
10 changes: 9 additions & 1 deletion src/monoclr/pynetinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ PyNet_Args* PyNet_Init(int ext) {
pn_args->pr_file = PR_ASSEMBLY;
pn_args->error = NULL;
pn_args->shutdown = NULL;
pn_args->module = NULL;

if (ext == 0) {
pn_args->init_name = "Python.Runtime:Initialize()";
Expand Down Expand Up @@ -96,6 +97,7 @@ void main_thread_handler (gpointer user_data) {
MonoImage *pr_image;
MonoClass *pythonengine;
MonoObject *exception = NULL;
MonoObject *init_result;

#ifndef _WIN32
// Get the filename of the python shared object and set
Expand Down Expand Up @@ -209,11 +211,17 @@ void main_thread_handler (gpointer user_data) {
return;
}

mono_runtime_invoke(init, NULL, NULL, &exception);
init_result = mono_runtime_invoke(init, NULL, NULL, &exception);
if (exception) {
pn_args->error = PyNet_ExceptionToString(exception);
return;
}

#if PY_MAJOR_VERSION >= 3
if (NULL != init_result)
pn_args->module = *(PyObject**)mono_object_unbox(init_result);
#endif

}

// Get string from a Mono exception
Expand Down
49 changes: 41 additions & 8 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Python.Runtime {

static class NativeMethods
{
#if MONO_LINUX
#if (MONO_LINUX || MONO_OSX)
static public IntPtr LoadLibrary(string fileName) {
return dlopen(fileName, RTLD_NOW | RTLD_SHARED);
}
Expand All @@ -35,6 +35,10 @@ static public void FreeLibrary(IntPtr handle) {
}

static public IntPtr GetProcAddress(IntPtr dllHandle, string name) {
// look in the exe if dllHandle is NULL
if (IntPtr.Zero == dllHandle)
dllHandle = RTLD_DEFAULT;

// clear previous errors if any
dlerror();
var res = dlsym(dllHandle, name);
Expand All @@ -45,8 +49,26 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) {
return res;
}

const int RTLD_NOW = 2;
const int RTLD_SHARED = 20;
#if (MONO_OSX)
static int RTLD_NOW = 0x2;
static int RTLD_SHARED = 0x20;
static IntPtr RTLD_DEFAULT = new IntPtr(-2);

[DllImport("__Internal")]
private static extern IntPtr dlopen(String fileName, int flags);

[DllImport("__Internal")]
private static extern IntPtr dlsym(IntPtr handle, String symbol);

[DllImport("__Internal")]
private static extern int dlclose(IntPtr handle);

[DllImport("__Internal")]
private static extern IntPtr dlerror();
#else
static int RTLD_NOW = 0x2;
static int RTLD_SHARED = 0x20;
static IntPtr RTLD_DEFAULT = IntPtr.Zero;

[DllImport("libdl.so")]
private static extern IntPtr dlopen(String fileName, int flags);
Expand All @@ -59,6 +81,8 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) {

[DllImport("libdl.so")]
private static extern IntPtr dlerror();
#endif

#else
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
Expand Down Expand Up @@ -139,7 +163,7 @@ public class Runtime {
#if (PYTHON27)
internal const string dllBase = "python27";
#endif
#if (MONO_LINUX)
#if (MONO_LINUX || MONO_OSX)
#if (PYTHON32)
internal const string dllBase = "python3.2";
#endif
Expand Down Expand Up @@ -295,10 +319,15 @@ internal static void Initialize() {
Error = new IntPtr(-1);

#if (PYTHON32 || PYTHON33 || PYTHON34)
IntPtr dll = NativeMethods.LoadLibrary(Runtime.dll);
IntPtr dll = IntPtr.Zero;
if ("__Internal" != Runtime.dll) {
NativeMethods.LoadLibrary(Runtime.dll);
}
_PyObject_NextNotImplemented = NativeMethods.GetProcAddress(dll, "_PyObject_NextNotImplemented");
#if !MONO_LINUX
NativeMethods.FreeLibrary(dll);
#if !(MONO_LINUX || MONO_OSX)
if (IntPtr.Zero != dll) {
NativeMethods.FreeLibrary(dll);
}
#endif
#endif

Expand Down Expand Up @@ -1751,7 +1780,11 @@ internal unsafe static string GetManagedString(IntPtr op)
if (type == Runtime.PyUnicodeType)
{
IntPtr p = Runtime.PyUnicode_AsUnicode(op);
return UnixMarshal.PtrToString(p, Encoding.UTF32);
int length = Runtime.PyUnicode_GetSize(op);
int size = length * 4;
byte[] buffer = new byte[size];
Marshal.Copy(p, buffer, 0, size);
return Encoding.UTF32.GetString(buffer, 0, size);
}

return null;
Expand Down