| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,20 +1,18 @@ | |||
| 1 | - // Author: Christian Heimes <christian(at)cheimes(dot)de> | ||
| 2 | - | ||
| 3 | 1 | #include "pynetclr.h" | |
| 4 | 2 | ||
| 5 | 3 | /* List of functions defined in the module */ | |
| 6 | 4 | static PyMethodDef clr_methods[] = { | |
| 7 | - {NULL, NULL, 0, NULL} /* Sentinel */ | ||
| 5 | + {NULL, NULL, 0, NULL} /* Sentinel */ | ||
| 8 | 6 | }; | |
| 9 | 7 | ||
| 10 | 8 | PyDoc_STRVAR(clr_module_doc, | |
| 11 | - "clr facade module to initialize the CLR. It's later " | ||
| 12 | - "replaced by the real clr module. This module has a facade " | ||
| 13 | - "attribute to make it distinguishable from the real clr module." | ||
| 9 | + "clr facade module to initialize the CLR. It's later " | ||
| 10 | + "replaced by the real clr module. This module has a facade " | ||
| 11 | + "attribute to make it distinguishable from the real clr module." | ||
| 14 | 12 | ); | |
| 15 | 13 | ||
| 16 | 14 | static PyNet_Args *pn_args; | |
| 17 | - char** environ = NULL; | ||
| 15 | + char **environ = NULL; | ||
| 18 | 16 | ||
| 19 | 17 | #if PY_MAJOR_VERSION >= 3 | |
| 20 | 18 | static struct PyModuleDef clrdef = { | |
@@ -30,7 +28,8 @@ static struct PyModuleDef clrdef = { | |||
| 30 | 28 | }; | |
| 31 | 29 | #endif | |
| 32 | 30 | ||
| 33 | - static PyObject *_initclr() { | ||
| 31 | + static PyObject *_initclr() | ||
| 32 | + { | ||
| 34 | 33 | PyObject *m; | |
| 35 | 34 | ||
| 36 | 35 | /* Create the module and add the functions */ | |
@@ -45,7 +44,8 @@ static PyObject *_initclr() { | |||
| 45 | 44 | Py_INCREF(Py_True); | |
| 46 | 45 | ||
| 47 | 46 | pn_args = PyNet_Init(1); | |
| 48 | - if (pn_args->error) { | ||
| 47 | + if (pn_args->error) | ||
| 48 | + { | ||
| 49 | 49 | return NULL; | |
| 50 | 50 | } | |
| 51 | 51 | ||
@@ -57,12 +57,14 @@ static PyObject *_initclr() { | |||
| 57 | 57 | ||
| 58 | 58 | #if PY_MAJOR_VERSION >= 3 | |
| 59 | 59 | PyMODINIT_FUNC | |
| 60 | - PyInit_clr(void) { | ||
| 60 | + PyInit_clr(void) | ||
| 61 | + { | ||
| 61 | 62 | return _initclr(); | |
| 62 | 63 | } | |
| 63 | 64 | #else | |
| 64 | 65 | PyMODINIT_FUNC | |
| 65 | - initclr(void) { | ||
| 66 | + initclr(void) | ||
| 67 | + { | ||
| 66 | 68 | _initclr(); | |
| 67 | 69 | } | |
| 68 | 70 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,3 @@ | |||
| 1 | - // Author: Christian Heimes <christian(at)cheimes(dot)de> | ||
| 2 | - | ||
| 3 | 1 | #ifndef PYNET_CLR_H | |
| 4 | 2 | #define PYNET_CLR_H | |
| 5 | 3 | ||
@@ -15,20 +13,21 @@ | |||
| 15 | 13 | #define MONO_DOMAIN "Python.Runtime" | |
| 16 | 14 | #define PR_ASSEMBLY "Python.Runtime.dll" | |
| 17 | 15 | ||
| 18 | - typedef struct { | ||
| 16 | + typedef struct | ||
| 17 | + { | ||
| 19 | 18 | MonoDomain *domain; | |
| 20 | 19 | MonoAssembly *pr_assm; | |
| 21 | 20 | MonoMethod *shutdown; | |
| 22 | 21 | char *pr_file; | |
| 23 | 22 | char *error; | |
| 24 | 23 | char *init_name; | |
| 25 | 24 | char *shutdown_name; | |
| 26 | - PyObject* module; | ||
| 25 | + PyObject *module; | ||
| 27 | 26 | } PyNet_Args; | |
| 28 | 27 | ||
| 29 | - PyNet_Args* PyNet_Init(int); | ||
| 30 | - void PyNet_Finalize(PyNet_Args*); | ||
| 28 | + PyNet_Args *PyNet_Init(int); | ||
| 29 | + void PyNet_Finalize(PyNet_Args *); | ||
| 31 | 30 | void main_thread_handler(gpointer user_data); | |
| 32 | - char* PyNet_ExceptionToString(MonoObject *); | ||
| 31 | + char *PyNet_ExceptionToString(MonoObject *); | ||
| 33 | 32 | ||
| 34 | 33 | #endif // PYNET_CLR_H | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,3 @@ | |||
| 1 | - // Author: Christian Heimes <christian(at)cheimes(dot)de> | ||
| 2 | - | ||
| 3 | 1 | #include "pynetclr.h" | |
| 4 | 2 | #include "stdlib.h" | |
| 5 | 3 | ||
@@ -12,17 +10,21 @@ | |||
| 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 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments