1- // Author: Christian Heimes <christian(at)cheimes(dot)de>
2-
31#include "pynetclr.h"
42#include "stdlib.h"
53
1210
1311
1412// initialize Mono and PythonNet
15- PyNet_Args * PyNet_Init (int ext ) {
13+ PyNet_Args * PyNet_Init (int ext )
14+ {
1615 PyNet_Args * pn_args ;
1716 pn_args = (PyNet_Args * )malloc (sizeof (PyNet_Args ));
1817 pn_args -> pr_file = PR_ASSEMBLY ;
1918 pn_args -> error = NULL ;
2019 pn_args -> shutdown = NULL ;
2120 pn_args -> module = NULL ;
2221
23- if (ext == 0 ) {
22+ if (ext == 0 )
23+ {
2424 pn_args -> init_name = "Python.Runtime:Initialize()" ;
25- } else {
25+ }
26+ else
27+ {
2628 pn_args -> init_name = "Python.Runtime:InitExt()" ;
2729 }
2830 pn_args -> shutdown_name = "Python.Runtime:Shutdown()" ;
@@ -47,32 +49,38 @@ PyNet_Args* PyNet_Init(int ext) {
4749
4850 main_thread_handler (pn_args );
4951
50- if (pn_args -> error != NULL ) {
52+ if (pn_args -> error != NULL )
53+ {
5154 PyErr_SetString (PyExc_ImportError , pn_args -> error );
5255 }
5356 return pn_args ;
5457}
5558
5659// Shuts down PythonNet and cleans up Mono
57- void PyNet_Finalize (PyNet_Args * pn_args ) {
60+ void PyNet_Finalize (PyNet_Args * pn_args )
61+ {
5862 MonoObject * exception = NULL ;
5963
60- if (pn_args -> shutdown ) {
64+ if (pn_args -> shutdown )
65+ {
6166 mono_runtime_invoke (pn_args -> shutdown , NULL , NULL , & exception );
62- if (exception ) {
67+ if (exception )
68+ {
6369 pn_args -> error = PyNet_ExceptionToString (exception );
6470 }
6571 pn_args -> shutdown = NULL ;
6672 }
6773
68- if (pn_args -> domain ) {
74+ if (pn_args -> domain )
75+ {
6976 mono_jit_cleanup (pn_args -> domain );
7077 pn_args -> domain = NULL ;
7178 }
7279 free (pn_args );
7380}
7481
75- MonoMethod * getMethodFromClass (MonoClass * cls , char * name ) {
82+ MonoMethod * getMethodFromClass (MonoClass * cls , char * name )
83+ {
7684 MonoMethodDesc * mdesc ;
7785 MonoMethod * method ;
7886
@@ -83,8 +91,9 @@ MonoMethod *getMethodFromClass(MonoClass *cls, char *name) {
8391 return method ;
8492}
8593
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 ;
8897 MonoMethod * init ;
8998 MonoImage * pr_image ;
9099 MonoClass * pythonengine ;
@@ -95,15 +104,19 @@ void main_thread_handler (gpointer user_data) {
95104 // Get the filename of the python shared object and set
96105 // LD_LIBRARY_PATH so Mono can find it.
97106 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 );
100110 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+ {
104115 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 )
107120 + strlen (ld_library_path )
108121 + 2 );
109122 strcpy (new_ld_library_path , py_libdir );
@@ -114,25 +127,26 @@ void main_thread_handler (gpointer user_data) {
114127 }
115128
116129 //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 = "/" ;
120133 int found = 0 ;
121134
122135 int ii = 0 ;
123- for (ii = 0 ; ii < PyList_Size (syspath ); ++ ii ) {
136+ for (ii = 0 ; ii < PyList_Size (syspath ); ++ ii )
137+ {
124138#if PY_MAJOR_VERSION > 2
125139 Py_ssize_t wlen ;
126140 wchar_t * wstr = PyUnicode_AsWideCharString (PyList_GetItem (syspath , ii ), & wlen );
127- char * pydir = (char * )malloc (wlen + 1 );
141+ char * pydir = (char * )malloc (wlen + 1 );
128142 size_t mblen = wcstombs (pydir , wstr , wlen + 1 );
129143 if (mblen > wlen )
130144 pydir [wlen ] = '\0' ;
131145 PyMem_Free (wstr );
132146#else
133- const char * pydir = PyString_AsString (PyList_GetItem (syspath , ii ));
147+ const char * pydir = PyString_AsString (PyList_GetItem (syspath , ii ));
134148#endif
135- char * curdir = (char * ) malloc (1024 );
149+ char * curdir = (char * ) malloc (1024 );
136150 strncpy (curdir , strlen (pydir ) > 0 ? pydir : "." , 1024 );
137151 strncat (curdir , slash , 1024 );
138152
@@ -141,12 +155,14 @@ void main_thread_handler (gpointer user_data) {
141155#endif
142156
143157 //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+ {
147161 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+ {
150166 strcpy (runtime_full_path , curdir );
151167 strcat (runtime_full_path , pn_args -> pr_file );
152168 found = 1 ;
@@ -157,21 +173,23 @@ void main_thread_handler (gpointer user_data) {
157173 }
158174 free (curdir );
159175
160- if (found ) {
176+ if (found )
177+ {
161178 pn_args -> pr_file = runtime_full_path ;
162179 break ;
163180 }
164181 }
165182
166- if (!found ) {
183+ if (!found )
184+ {
167185 fprintf (stderr , "Could not find assembly %s. \n" , pn_args -> pr_file );
168186 return ;
169187 }
170188#endif
171189
172-
173190 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+ {
175193 pn_args -> error = "Unable to load assembly" ;
176194 return ;
177195 }
@@ -180,31 +198,36 @@ void main_thread_handler (gpointer user_data) {
180198#endif
181199
182200 pr_image = mono_assembly_get_image (pn_args -> pr_assm );
183- if (!pr_image ) {
201+ if (!pr_image )
202+ {
184203 pn_args -> error = "Unable to get image" ;
185204 return ;
186205 }
187206
188207 pythonengine = mono_class_from_name (pr_image , "Python.Runtime" , "PythonEngine" );
189- if (!pythonengine ) {
208+ if (!pythonengine )
209+ {
190210 pn_args -> error = "Unable to load class PythonEngine from Python.Runtime" ;
191211 return ;
192212 }
193213
194214 init = getMethodFromClass (pythonengine , pn_args -> init_name );
195- if (!init ) {
215+ if (!init )
216+ {
196217 pn_args -> error = "Unable to fetch Init method from PythonEngine" ;
197218 return ;
198219 }
199220
200221 pn_args -> shutdown = getMethodFromClass (pythonengine , pn_args -> shutdown_name );
201- if (!pn_args -> shutdown ) {
222+ if (!pn_args -> shutdown )
223+ {
202224 pn_args -> error = "Unable to fetch shutdown method from PythonEngine" ;
203225 return ;
204226 }
205227
206228 init_result = mono_runtime_invoke (init , NULL , NULL , & exception );
207- if (exception ) {
229+ if (exception )
230+ {
208231 pn_args -> error = PyNet_ExceptionToString (exception );
209232 return ;
210233 }
@@ -213,17 +236,17 @@ void main_thread_handler (gpointer user_data) {
213236 if (NULL != init_result )
214237 pn_args -> module = * (PyObject * * )mono_object_unbox (init_result );
215238#endif
216-
217239}
218240
219241// 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 ());
223246 mono_method_desc_free (mdesc );
224247
225248 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 );
227250 mono_runtime_invoke (mmethod , e , NULL , NULL );
228251 return mono_string_to_utf8 (monoString );
229252}
0 commit comments