1
- // Author: Christian Heimes <christian(at)cheimes(dot)de>
2
-
3
1
#include "pynetclr.h"
4
2
#include "stdlib.h"
5
3
12
10
13
11
14
12
// initialize Mono and PythonNet
15
- PyNet_Args * PyNet_Init (int ext ) {
13
+ PyNet_Args * PyNet_Init (int ext )
14
+ {
16
15
PyNet_Args * pn_args ;
17
16
pn_args = (PyNet_Args * )malloc (sizeof (PyNet_Args ));
18
17
pn_args -> pr_file = PR_ASSEMBLY ;
19
18
pn_args -> error = NULL ;
20
19
pn_args -> shutdown = NULL ;
21
20
pn_args -> module = NULL ;
22
21
23
- if (ext == 0 ) {
22
+ if (ext == 0 )
23
+ {
24
24
pn_args -> init_name = "Python.Runtime:Initialize()" ;
25
- } else {
25
+ }
26
+ else
27
+ {
26
28
pn_args -> init_name = "Python.Runtime:InitExt()" ;
27
29
}
28
30
pn_args -> shutdown_name = "Python.Runtime:Shutdown()" ;
@@ -47,32 +49,38 @@ PyNet_Args* PyNet_Init(int ext) {
47
49
48
50
main_thread_handler (pn_args );
49
51
50
- if (pn_args -> error != NULL ) {
52
+ if (pn_args -> error != NULL )
53
+ {
51
54
PyErr_SetString (PyExc_ImportError , pn_args -> error );
52
55
}
53
56
return pn_args ;
54
57
}
55
58
56
59
// Shuts down PythonNet and cleans up Mono
57
- void PyNet_Finalize (PyNet_Args * pn_args ) {
60
+ void PyNet_Finalize (PyNet_Args * pn_args )
61
+ {
58
62
MonoObject * exception = NULL ;
59
63
60
- if (pn_args -> shutdown ) {
64
+ if (pn_args -> shutdown )
65
+ {
61
66
mono_runtime_invoke (pn_args -> shutdown , NULL , NULL , & exception );
62
- if (exception ) {
67
+ if (exception )
68
+ {
63
69
pn_args -> error = PyNet_ExceptionToString (exception );
64
70
}
65
71
pn_args -> shutdown = NULL ;
66
72
}
67
73
68
- if (pn_args -> domain ) {
74
+ if (pn_args -> domain )
75
+ {
69
76
mono_jit_cleanup (pn_args -> domain );
70
77
pn_args -> domain = NULL ;
71
78
}
72
79
free (pn_args );
73
80
}
74
81
75
- MonoMethod * getMethodFromClass (MonoClass * cls , char * name ) {
82
+ MonoMethod * getMethodFromClass (MonoClass * cls , char * name )
83
+ {
76
84
MonoMethodDesc * mdesc ;
77
85
MonoMethod * method ;
78
86
@@ -83,8 +91,9 @@ MonoMethod *getMethodFromClass(MonoClass *cls, char *name) {
83
91
return method ;
84
92
}
85
93
86
- void main_thread_handler (gpointer user_data ) {
87
- PyNet_Args * pn_args = (PyNet_Args * )user_data ;
94
+ void main_thread_handler (gpointer user_data )
95
+ {
96
+ PyNet_Args * pn_args = (PyNet_Args * )user_data ;
88
97
MonoMethod * init ;
89
98
MonoImage * pr_image ;
90
99
MonoClass * pythonengine ;
@@ -95,15 +104,19 @@ void main_thread_handler (gpointer user_data) {
95
104
// Get the filename of the python shared object and set
96
105
// LD_LIBRARY_PATH so Mono can find it.
97
106
Dl_info dlinfo = {0 };
98
- if (0 != dladdr (& Py_Initialize , & dlinfo )) {
99
- char * fname = alloca (strlen (dlinfo .dli_fname ) + 1 );
107
+ if (0 != dladdr (& Py_Initialize , & dlinfo ))
108
+ {
109
+ char * fname = alloca (strlen (dlinfo .dli_fname ) + 1 );
100
110
strcpy (fname , dlinfo .dli_fname );
101
- char * py_libdir = dirname (fname );
102
- char * ld_library_path = getenv ("LD_LIBRARY_PATH" );
103
- if (NULL == ld_library_path ) {
111
+ char * py_libdir = dirname (fname );
112
+ char * ld_library_path = getenv ("LD_LIBRARY_PATH" );
113
+ if (NULL == ld_library_path )
114
+ {
104
115
setenv ("LD_LIBRARY_PATH" , py_libdir , 1 );
105
- } else {
106
- char * new_ld_library_path = alloca (strlen (py_libdir )
116
+ }
117
+ else
118
+ {
119
+ char * new_ld_library_path = alloca (strlen (py_libdir )
107
120
+ strlen (ld_library_path )
108
121
+ 2 );
109
122
strcpy (new_ld_library_path , py_libdir );
@@ -114,25 +127,26 @@ void main_thread_handler (gpointer user_data) {
114
127
}
115
128
116
129
//get python path system variable
117
- PyObject * syspath = PySys_GetObject ("path" );
118
- char * runtime_full_path = (char * ) malloc (1024 );
119
- const char * slash = "/" ;
130
+ PyObject * syspath = PySys_GetObject ("path" );
131
+ char * runtime_full_path = (char * ) malloc (1024 );
132
+ const char * slash = "/" ;
120
133
int found = 0 ;
121
134
122
135
int ii = 0 ;
123
- for (ii = 0 ; ii < PyList_Size (syspath ); ++ ii ) {
136
+ for (ii = 0 ; ii < PyList_Size (syspath ); ++ ii )
137
+ {
124
138
#if PY_MAJOR_VERSION > 2
125
139
Py_ssize_t wlen ;
126
140
wchar_t * wstr = PyUnicode_AsWideCharString (PyList_GetItem (syspath , ii ), & wlen );
127
- char * pydir = (char * )malloc (wlen + 1 );
141
+ char * pydir = (char * )malloc (wlen + 1 );
128
142
size_t mblen = wcstombs (pydir , wstr , wlen + 1 );
129
143
if (mblen > wlen )
130
144
pydir [wlen ] = '\0' ;
131
145
PyMem_Free (wstr );
132
146
#else
133
- const char * pydir = PyString_AsString (PyList_GetItem (syspath , ii ));
147
+ const char * pydir = PyString_AsString (PyList_GetItem (syspath , ii ));
134
148
#endif
135
- char * curdir = (char * ) malloc (1024 );
149
+ char * curdir = (char * ) malloc (1024 );
136
150
strncpy (curdir , strlen (pydir ) > 0 ? pydir : "." , 1024 );
137
151
strncat (curdir , slash , 1024 );
138
152
@@ -141,12 +155,14 @@ void main_thread_handler (gpointer user_data) {
141
155
#endif
142
156
143
157
//look in this directory for the pn_args->pr_file
144
- DIR * dirp = opendir (curdir );
145
- if (dirp != NULL ) {
146
-
158
+ DIR * dirp = opendir (curdir );
159
+ if (dirp != NULL )
160
+ {
147
161
struct dirent * dp ;
148
- while ((dp = readdir (dirp )) != NULL ) {
149
- if (strcmp (dp -> d_name , pn_args -> pr_file ) == 0 ) {
162
+ while ((dp = readdir (dirp )) != NULL )
163
+ {
164
+ if (strcmp (dp -> d_name , pn_args -> pr_file ) == 0 )
165
+ {
150
166
strcpy (runtime_full_path , curdir );
151
167
strcat (runtime_full_path , pn_args -> pr_file );
152
168
found = 1 ;
@@ -157,21 +173,23 @@ void main_thread_handler (gpointer user_data) {
157
173
}
158
174
free (curdir );
159
175
160
- if (found ) {
176
+ if (found )
177
+ {
161
178
pn_args -> pr_file = runtime_full_path ;
162
179
break ;
163
180
}
164
181
}
165
182
166
- if (!found ) {
183
+ if (!found )
184
+ {
167
185
fprintf (stderr , "Could not find assembly %s. \n" , pn_args -> pr_file );
168
186
return ;
169
187
}
170
188
#endif
171
189
172
-
173
190
pn_args -> pr_assm = mono_domain_assembly_open (pn_args -> domain , pn_args -> pr_file );
174
- if (!pn_args -> pr_assm ) {
191
+ if (!pn_args -> pr_assm )
192
+ {
175
193
pn_args -> error = "Unable to load assembly" ;
176
194
return ;
177
195
}
@@ -180,31 +198,36 @@ void main_thread_handler (gpointer user_data) {
180
198
#endif
181
199
182
200
pr_image = mono_assembly_get_image (pn_args -> pr_assm );
183
- if (!pr_image ) {
201
+ if (!pr_image )
202
+ {
184
203
pn_args -> error = "Unable to get image" ;
185
204
return ;
186
205
}
187
206
188
207
pythonengine = mono_class_from_name (pr_image , "Python.Runtime" , "PythonEngine" );
189
- if (!pythonengine ) {
208
+ if (!pythonengine )
209
+ {
190
210
pn_args -> error = "Unable to load class PythonEngine from Python.Runtime" ;
191
211
return ;
192
212
}
193
213
194
214
init = getMethodFromClass (pythonengine , pn_args -> init_name );
195
- if (!init ) {
215
+ if (!init )
216
+ {
196
217
pn_args -> error = "Unable to fetch Init method from PythonEngine" ;
197
218
return ;
198
219
}
199
220
200
221
pn_args -> shutdown = getMethodFromClass (pythonengine , pn_args -> shutdown_name );
201
- if (!pn_args -> shutdown ) {
222
+ if (!pn_args -> shutdown )
223
+ {
202
224
pn_args -> error = "Unable to fetch shutdown method from PythonEngine" ;
203
225
return ;
204
226
}
205
227
206
228
init_result = mono_runtime_invoke (init , NULL , NULL , & exception );
207
- if (exception ) {
229
+ if (exception )
230
+ {
208
231
pn_args -> error = PyNet_ExceptionToString (exception );
209
232
return ;
210
233
}
@@ -213,17 +236,17 @@ void main_thread_handler (gpointer user_data) {
213
236
if (NULL != init_result )
214
237
pn_args -> module = * (PyObject * * )mono_object_unbox (init_result );
215
238
#endif
216
-
217
239
}
218
240
219
241
// Get string from a Mono exception
220
- char * PyNet_ExceptionToString (MonoObject * e ) {
221
- MonoMethodDesc * mdesc = mono_method_desc_new (":ToString()" , FALSE);
222
- MonoMethod * mmethod = mono_method_desc_search_in_class (mdesc , mono_get_object_class ());
242
+ char * PyNet_ExceptionToString (MonoObject * e )
243
+ {
244
+ MonoMethodDesc * mdesc = mono_method_desc_new (":ToString()" , FALSE);
245
+ MonoMethod * mmethod = mono_method_desc_search_in_class (mdesc , mono_get_object_class ());
223
246
mono_method_desc_free (mdesc );
224
247
225
248
mmethod = mono_object_get_virtual_method (e , mmethod );
226
- MonoString * monoString = (MonoString * ) mono_runtime_invoke (mmethod , e , NULL , NULL );
249
+ MonoString * monoString = (MonoString * ) mono_runtime_invoke (mmethod , e , NULL , NULL );
227
250
mono_runtime_invoke (mmethod , e , NULL , NULL );
228
251
return mono_string_to_utf8 (monoString );
229
252
}
0 commit comments