Skip to content
This repository was archived by the owner on Jun 4, 2019. It is now read-only.

Commit 43139d2

Browse files
committed
Added fixes to get Python.NET working under Mono 1.9 on Mac OS X.
-added workaround to clrmod.c for mono error: "Symbol not found: _environ" (a fix has been added to Mono SVN, according to Geoff Norton at Novell.) -changed ordering of mono_config_parse(NULL) call in pynetinit.c, to handle latest changes to mono embedding interface (a fix has been added to Mono SVN, according to Geoff Norton.) -fixed TypeManager static constructor to properly handle .NET exceptions (thanks to Geoff Norton.) -added to pynetinit.c to make clr.so search the site-packages folder of the currently running python to find the Python.Runtime.dll, instead of just the cwd (thanks to Peter Shinners and Wei Qiao at Blizzard Entertainment.) -updated one of the Python.Runtime.dll.config to map to .dylib instead of .so (thanks to Wei Qiao.) This should be looked at further, as there are a few of these files floating around... -changed the Makefile to be self-contained and create the clr.so (thanks to Wei Qiao.) This should be looked at further, as running setup.py currently builds the clr.so, but gives an error afterwards. -updated README.txt a bit more.
1 parent bab1983 commit 43139d2

6 files changed

Lines changed: 72 additions & 15 deletions

File tree

pythonnet/doc/Python.Runtime.dll.config

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ For more information read:
99
-->
1010

1111
<configuration>
12-
<dllmap dll="python23" target="libpython2.3.so" os="!windows" />
13-
<dllmap dll="python24" target="libpython2.4.so" os="!windows" />
14-
<dllmap dll="python25" target="libpython2.5.so" os="!windows" />
15-
<dllmap dll="python26" target="libpython2.6.so" os="!windows" />
16-
<dllmap dll="python23.dll" target="libpython2.3.so" os="!windows" />
17-
<dllmap dll="python24.dll" target="libpython2.4.so" os="!windows" />
18-
<dllmap dll="python25.dll" target="libpython2.5.so" os="!windows" />
19-
<dllmap dll="python26.dll" target="libpython2.6.so" os="!windows" />
12+
<dllmap dll="python23" target="/usr/lib/libpython2.3.dylib" os="!windows" />
13+
<dllmap dll="python24" target="/usr/lib/libpython2.4.dylib" os="!windows" />
14+
<dllmap dll="python25" target="/usr/lib/libpython2.5.dylib" os="!windows" />
15+
<dllmap dll="python26" target="/usr/lib/libpython2.6.dylib" os="!windows" />
16+
<dllmap dll="python23.dll" target="/usr/lib/libpython2.3.dylib" os="!windows" />
17+
<dllmap dll="python24.dll" target="/usr/lib/libpython2.4.dylib" os="!windows" />
18+
<dllmap dll="python25.dll" target="/usr/lib/libpython2.5.dylib" os="!windows" />
19+
<dllmap dll="python26.dll" target="/usr/lib/libpython2.6.dylib" os="!windows" />
2020
</configuration>
2121

pythonnet/src/monoclr/Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ BASENAME = $(shell $(PYTHON) -c "import sys; print 'python%i.%i' % sys.version_i
55
GCC = gcc
66

77
PY_LIBS = $(shell $(PYTHON) -c "from distutils.sysconfig import get_config_vars; \
8-
print get_config_vars('BLDLIBRARY')[0]")
8+
print get_config_vars('BLDLIBRARY')[0]") -lpython
99
PY_CFLAGS = -I$(shell $(PYTHON) -c "from distutils.sysconfig import get_config_vars; \
1010
print get_config_vars('CFLAGS')[0] + ' -I' + get_config_vars('CONFINCLUDEPY')[0]")
1111

@@ -15,10 +15,10 @@ MONO_CFLAGS = $(shell pkg-config --cflags mono)
1515
LIBS = $(MONO_LIBS) $(PY_LIBS)
1616
CFLAGS = $(MONO_CFLAGS) $(PY_CFLAGS)
1717

18-
all: clr$(BASENAME) $(BASENAME)
18+
all: clr$(BASENAME) $(BASENAME) clr.so
1919

20-
clr$(BASENAME): clrpython.o pynetinit.o
21-
$(GCC) $(LIBS) clrpython.o pynetinit.o -o clr$(BASENAME)
20+
clrmod.o: pynetclr.h clrmod.c
21+
$(GCC) $(CFLAGS) -c clrmod.c -o clrmod.o
2222

2323
clrpython.o: pynetclr.h clrpython.c
2424
$(GCC) $(CFLAGS) -c clrpython.c -o clrpython.o
@@ -29,6 +29,12 @@ pynetinit.o: pynetclr.h pynetinit.c
2929
$(BASENAME): python.c
3030
$(GCC) $(PY_CFLAGS) $(PY_LIBS) python.c -o $(BASENAME)
3131

32+
clr.so: clrmod.o pynetinit.o
33+
$(GCC) $(LIBS) -dynamiclib pynetinit.o clrmod.o -o clr.so
34+
35+
clr$(BASENAME): clrpython.o pynetinit.o
36+
$(GCC) $(LIBS) clrpython.o pynetinit.o -o clr$(BASENAME)
37+
3238
clean:
3339
rm -f *.o
3440
rm -f *.so

pythonnet/src/monoclr/README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
This has been updated to work with MonoFramework 1.9.
2+
13
1. On MacOS X, create the following directory structure:
24

35
/PythonNET/src/monoclr
@@ -11,3 +13,5 @@
1113
$ python setup.py build
1214

1315
5. This creates the clr.so
16+
17+
6. Copy the clr.so, the Python.Runtime.dll, and the Python.Runtime.dll.config to the site-packages folder.

pythonnet/src/monoclr/clrmod.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ PyDoc_STRVAR(clr_module_doc,
2323
);
2424

2525
static PyNet_Args *pn_args;
26+
char** environ = NULL;
2627

2728
PyMODINIT_FUNC
2829
initclr(void)

pythonnet/src/monoclr/pynetinit.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Author: Christian Heimes <christian(at)cheimes(dot)de>
1111

1212
#include "pynetclr.h"
13+
#include "dirent.h"
1314

1415
// initialize Mono and PythonNet
1516
PyNet_Args* PyNet_Init(int ext) {
@@ -18,22 +19,23 @@ PyNet_Args* PyNet_Init(int ext) {
1819
pn_args->pr_file = PR_ASSEMBLY;
1920
pn_args->error = NULL;
2021
pn_args->shutdown = NULL;
22+
2123
if (ext == 0) {
2224
pn_args->init_name = "Python.Runtime:Initialize()";
2325
} else {
2426
pn_args->init_name = "Python.Runtime:InitExt()";
2527
}
2628
pn_args->shutdown_name = "Python.Runtime:Shutdown()";
2729

30+
pn_args->domain = mono_jit_init_version(MONO_DOMAIN, MONO_VERSION);
31+
2832
/*
2933
* Load the default Mono configuration file, this is needed
3034
* if you are planning on using the dllmaps defined on the
3135
* system configuration
3236
*/
3337
mono_config_parse(NULL);
3438

35-
pn_args->domain = mono_jit_init_version(MONO_DOMAIN, MONO_VERSION);
36-
3739
/* I can't use this call to run the main_thread_handler. The function
3840
* runs it in another thread but *this* thread holds the Python
3941
* import lock -> DEAD LOCK.
@@ -87,11 +89,55 @@ void main_thread_handler (gpointer user_data) {
8789
MonoClass *pythonengine;
8890
MonoObject *exception = NULL;
8991

92+
//get python path system variable
93+
PyObject* syspath = PySys_GetObject("path");
94+
char* runtime_full_path = (char*) malloc(1024);
95+
const char* slash = "/";
96+
int found = 0;
97+
98+
int ii = 0;
99+
for (ii = 0; ii < PyList_Size(syspath); ++ii) {
100+
const char* pydir = PyString_AsString(PyList_GetItem(syspath, ii));
101+
char* curdir = (char*) malloc(1024);
102+
if (strlen(pydir) == 0) pydir = ".";
103+
104+
strcpy(curdir, pydir);
105+
strcat(curdir, slash);
106+
107+
//look in this directory for the pn_args->pr_file
108+
DIR* dirp = opendir(curdir);
109+
if (dirp != NULL) {
110+
111+
struct dirent *dp;
112+
while ((dp = readdir(dirp)) != NULL) {
113+
if (strcmp(dp->d_name, pn_args->pr_file) == 0) {
114+
strcpy(runtime_full_path, curdir);
115+
strcat(runtime_full_path, pn_args->pr_file);
116+
found = 1;
117+
break;
118+
}
119+
}
120+
closedir(dirp);
121+
}
122+
free(curdir);
123+
124+
if (found) {
125+
pn_args->pr_file = runtime_full_path;
126+
break;
127+
}
128+
}
129+
130+
if (!found) {
131+
fprintf(stderr, "Could not find assembly %s. \n", pn_args->pr_file);
132+
return;
133+
}
134+
90135
pn_args->pr_assm = mono_domain_assembly_open(pn_args->domain, pn_args->pr_file);
91136
if (!pn_args->pr_assm) {
92137
pn_args->error = "Unable to load assembly";
93138
return;
94139
}
140+
free(runtime_full_path);
95141

96142
pr_image = mono_assembly_get_image(pn_args->pr_assm);
97143
if (!pr_image) {

pythonnet/src/runtime/typemanager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class TypeManager {
3030

3131
static TypeManager() {
3232
tbFlags = BindingFlags.Public | BindingFlags.Static;
33-
obSize = 4 * IntPtr.Size;
33+
obSize = 5 * IntPtr.Size;
3434
cache = new Dictionary<Type, IntPtr>(128);
3535
}
3636

0 commit comments

Comments
 (0)