This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclrmod.c
More file actions
215 lines (178 loc) · 5.45 KB
/
clrmod.c
File metadata and controls
215 lines (178 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// #define Py_LIMITED_API 0x03050000
#include <Python.h>
#include "stdlib.h"
#define MONO_VERSION "v4.0.30319.1"
#define MONO_DOMAIN "Python"
#include <mono/jit/jit.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/mono-config.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#ifndef _WIN32
#include "dirent.h"
#include "dlfcn.h"
#include "libgen.h"
#include "alloca.h"
#endif
typedef struct
{
MonoDomain *domain;
MonoAssembly *pr_assm;
MonoMethod *shutdown;
const char *pr_file;
char *error;
char *init_name;
char *shutdown_name;
PyObject *module;
} PyNet_Args;
PyNet_Args *PyNet_Init(void);
static PyNet_Args *pn_args;
PyMODINIT_FUNC
PyInit_clr(void)
{
pn_args = PyNet_Init();
if (pn_args->error)
{
return NULL;
}
return pn_args->module;
}
void PyNet_Finalize(PyNet_Args *);
void main_thread_handler(PyNet_Args *user_data);
// initialize Mono and PythonNet
PyNet_Args *PyNet_Init()
{
PyObject *pn_module;
PyObject *pn_path;
PyNet_Args *pn_args;
pn_args = (PyNet_Args *)malloc(sizeof(PyNet_Args));
pn_module = PyImport_ImportModule("pythonnet");
if (pn_module == NULL)
{
pn_args->error = "Failed to import pythonnet";
return pn_args;
}
pn_path = PyObject_CallMethod(pn_module, "get_assembly_path", NULL);
if (pn_path == NULL)
{
Py_DecRef(pn_module);
pn_args->error = "Failed to get assembly path";
return pn_args;
}
pn_args->pr_file = PyUnicode_AsUTF8(pn_path);
pn_args->error = NULL;
pn_args->shutdown = NULL;
pn_args->module = NULL;
#ifdef __linux__
// Force preload libmono-2.0 as global. Without this, on some systems
// symbols from libmono are not found by libmononative (which implements
// some of the System.* namespaces). Since the only happened on Linux so
// far, we hardcode the library name, load the symbols into the global
// namespace and leak the handle.
dlopen("libmono-2.0.so", RTLD_LAZY | RTLD_GLOBAL);
#endif
pn_args->init_name = "Python.Runtime:InitExt()";
pn_args->shutdown_name = "Python.Runtime:Shutdown()";
pn_args->domain = mono_jit_init_version(MONO_DOMAIN, MONO_VERSION);
// XXX: Skip setting config for now, should be derived from pr_file
// mono_domain_set_config(pn_args->domain, ".", "Python.Runtime.dll.config");
/*
* Load the default Mono configuration file, this is needed
* if you are planning on using the dllmaps defined on the
* system configuration
*/
mono_config_parse(NULL);
main_thread_handler(pn_args);
if (pn_args->error != NULL)
{
PyErr_SetString(PyExc_ImportError, pn_args->error);
}
return pn_args;
}
char *PyNet_ExceptionToString(MonoObject *e);
// Shuts down PythonNet and cleans up Mono
void PyNet_Finalize(PyNet_Args *pn_args)
{
MonoObject *exception = NULL;
if (pn_args->shutdown)
{
mono_runtime_invoke(pn_args->shutdown, NULL, NULL, &exception);
if (exception)
{
pn_args->error = PyNet_ExceptionToString(exception);
}
pn_args->shutdown = NULL;
}
if (pn_args->domain)
{
mono_jit_cleanup(pn_args->domain);
pn_args->domain = NULL;
}
free(pn_args);
}
MonoMethod *getMethodFromClass(MonoClass *cls, char *name)
{
MonoMethodDesc *mdesc;
MonoMethod *method;
mdesc = mono_method_desc_new(name, 1);
method = mono_method_desc_search_in_class(mdesc, cls);
mono_method_desc_free(mdesc);
return method;
}
void main_thread_handler(PyNet_Args *user_data)
{
PyNet_Args *pn_args = user_data;
MonoMethod *init;
MonoImage *pr_image;
MonoClass *pythonengine;
MonoObject *exception = NULL;
MonoObject *init_result;
pn_args->pr_assm = mono_domain_assembly_open(pn_args->domain, pn_args->pr_file);
if (!pn_args->pr_assm)
{
pn_args->error = "Unable to load assembly";
return;
}
pr_image = mono_assembly_get_image(pn_args->pr_assm);
if (!pr_image)
{
pn_args->error = "Unable to get image";
return;
}
pythonengine = mono_class_from_name(pr_image, "Python.Runtime", "PythonEngine");
if (!pythonengine)
{
pn_args->error = "Unable to load class PythonEngine from Python.Runtime";
return;
}
init = getMethodFromClass(pythonengine, pn_args->init_name);
if (!init)
{
pn_args->error = "Unable to fetch Init method from PythonEngine";
return;
}
pn_args->shutdown = getMethodFromClass(pythonengine, pn_args->shutdown_name);
if (!pn_args->shutdown)
{
pn_args->error = "Unable to fetch shutdown method from PythonEngine";
return;
}
init_result = mono_runtime_invoke(init, NULL, NULL, &exception);
if (exception)
{
pn_args->error = PyNet_ExceptionToString(exception);
return;
}
pn_args->module = *(PyObject**)mono_object_unbox(init_result);
}
// Get string from a Mono exception
char *PyNet_ExceptionToString(MonoObject *e)
{
MonoMethodDesc *mdesc = mono_method_desc_new(":ToString()", 0 /*FALSE*/);
MonoMethod *mmethod = mono_method_desc_search_in_class(mdesc, mono_get_object_class());
mono_method_desc_free(mdesc);
mmethod = mono_object_get_virtual_method(e, mmethod);
MonoString *monoString = (MonoString*) mono_runtime_invoke(mmethod, e, NULL, NULL);
mono_runtime_invoke(mmethod, e, NULL, NULL);
return mono_string_to_utf8(monoString);
}