-
Notifications
You must be signed in to change notification settings - Fork 62
/
org_cellprofiler_javabridge_CPython.c
474 lines (437 loc) · 12.5 KB
/
org_cellprofiler_javabridge_CPython.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
python-javabridge is licensed under the BSD license. See the
accompanying file LICENSE for details.
Copyright (c) 2003-2009 Massachusetts Institute of Technology
Copyright (c) 2009-2013 Broad Institute
All rights reserved.
*/
#include <jni.h>
#include <stdio.h>
#include <Python.h>
#ifdef __linux__
#include <stdlib.h>
#include <dlfcn.h>
#endif
#include "org_cellprofiler_javabridge_CPython.h"
int initialized = 0;
static void check_init(void);
#ifdef __linux__
/*
* On Linux, it appears that Python's symbols cannot be found by other
* native libraries if we let the JVM load libpython... so we have to load it
* explicitly, with the correct flag (RTLD_GLOBAL).
*/
static char *get_property(JavaVM *vm, const char *key)
{
JNIEnv *pEnv;
jclass system;
jmethodID get;
jstring string;
const char *characters;
char *result;
if ((*vm)->GetEnv(vm, (void **)&pEnv, JNI_VERSION_1_2) != JNI_OK) {
fprintf(stderr, "Could not obtain JNI environment\n");
return NULL;
}
if (!(system = (*pEnv)->FindClass(pEnv, "java/lang/System"))) {
fprintf(stderr, "Could not access System class\n");
return NULL;
}
if (!(get = (*pEnv)->GetStaticMethodID(pEnv, system, "getProperty",
"(Ljava/lang/String;)Ljava/lang/String;"))) {
fprintf(stderr, "Could not find getProperty method\n");
return NULL;
}
if (!(string = (jstring)(*pEnv)->CallStaticObjectMethod(pEnv, system,
get, (*pEnv)->NewStringUTF(pEnv, key))))
return NULL;
characters = (*pEnv)->GetStringUTFChars(pEnv, string, NULL);
result = strdup(characters);
(*pEnv)->ReleaseStringUTFChars(pEnv, string, characters);
(*vm)->DetachCurrentThread(vm);
return result;
}
#endif
JavaVM *pVM;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
#ifdef __linux__
char buf[1024];
char *python_location = get_property(vm, "python.location");
const char *command = "python -c \"import sysconfig; from os.path import join; print join(sysconfig.get_config_var('LIBDIR'), sysconfig.get_config_var('multiarchsubdir')[1:], sysconfig.get_config_var('LDLIBRARY'))\"";
if (!python_location) {
size_t len=1024;
FILE *stream = popen(command, "r");
python_location = buf;
getline(&python_location, &len, stream);
python_location[strlen(python_location)-1] = 0;
pclose(stream);
}
if (!dlopen(python_location, RTLD_LAZY | RTLD_GLOBAL))
fprintf(stderr, "Warning: Error loading %s\n", python_location);
#endif
pVM = vm;
return JNI_VERSION_1_2;
}
/*
* Run some code here to encapsulate a pointer to the VM and call
* import javabridge
* javabridge.jvm_enter(capsule)
*
*/
static int set_vm(void)
{
PyObject *pPyVM;
PyObject *pJavabridge;
PyObject *pJVMEnter;
PyObject *pArgs;
PyObject *pResult;
pPyVM = PyCapsule_New((void *)pVM, NULL, NULL);
if (PyErr_Occurred()) {
fprintf(stderr, "Unable to encapsulate VM for Python.\n");
return -1;
}
pJavabridge = PyImport_ImportModule("javabridge");
if (PyErr_Occurred()) {
fprintf(stderr, "Failed to import javabridge.\n");
Py_DECREF(pPyVM);
return -1;
}
pJVMEnter = PyObject_GetAttrString(pJavabridge, "jvm_enter");
if (PyErr_Occurred()) {
fprintf(stderr, "Failed to find function, javabridge.jvm_enter\n");
Py_DECREF(pJavabridge);
Py_DECREF(pPyVM);
return -1;
}
pArgs = PyTuple_Pack(1, pPyVM);
if (! pArgs) {
fprintf(stderr, "Failed to create the arguments for jvm_enter\n");
Py_DECREF(pJVMEnter);
Py_DECREF(pJavabridge);
Py_DECREF(pPyVM);
return -1;
}
pResult = PyObject_CallObject(pJVMEnter, pArgs);
if (! pResult) {
fprintf(stderr, "Caught exception in jvm_enter.\n");
Py_DECREF(pArgs);
Py_DECREF(pJVMEnter);
Py_DECREF(pJavabridge);
Py_DECREF(pPyVM);
return -1;
}
Py_DECREF(pResult);
Py_DECREF(pArgs);
Py_DECREF(pJVMEnter);
Py_DECREF(pJavabridge);
Py_DECREF(pPyVM);
return 0;
}
#ifdef _WIN32
/*
* If MSVC90.dll is on the path, we will perish horribly in Windows
* with an R6034 exception on loading it from some of the numerous
* pyd files that include it.
*/
const char *pCleanPath = "import os;os.environ['path']=';'.join([path for path in os.environ['path'].split(';') if 'msvcr90.dll' not in map((lambda x:x.lower()), os.listdir(path))])";
#endif
static void check_init(void) {
if ((initialized == 0) && ! Py_IsInitialized()) {
Py_Initialize();
#ifdef _WIN32
PyRun_SimpleString(pCleanPath);
#endif
set_vm();
initialized = 1;
}
}
/*
throwError
Throw an error that indicates a problem beyond what would be caused
by the evaluation or execution of Python code
pEnv - JNI environment
message - message to report
*/
static void throwError(JNIEnv *pEnv, char *message) {
jclass exClass;
char *className = "java/lang/Error";
exClass = (*pEnv)->FindClass(pEnv, className);
if (exClass == NULL) {
return;
}
(*pEnv)->ThrowNew(pEnv, exClass, message);
PyErr_Clear();
}
/*
throwWrappedError
Throw an exception that reflects the current Python exception into
Java.
*/
static void throwWrappedError(JNIEnv *pEnv, int linenumber)
{
char buffer[1000];
/* TODO: implement*/
PyErr_Print();
snprintf(buffer, sizeof(buffer),
"Python exception at %s:%d", __FILE__, linenumber);
throwError(pEnv, buffer);
}
/*
attach_env
Attach the supplied environment to the javabridge thread-local context.
pEnv - the environment passed via the JNI call
Prerequisites: The GIL must be taken.
Returns: 0 if successful, negative value on failure indicating that a
Java exception has been thrown.
*/
static int attach_env(JNIEnv *pEnv){
PyObject *pPyEnv;
PyObject *pJavabridge;
PyObject *pJNIEnter;
PyObject *pArgs;
PyObject *pResult;
/*
Equivalent to:
import javabridge
javabridge.jni_enter(env)
*/
pPyEnv = PyCapsule_New((void *)pEnv, NULL, NULL);
if (PyErr_Occurred()) {
throwWrappedError(pEnv, __LINE__);
return -1;
}
pJavabridge = PyImport_ImportModule("javabridge");
if (PyErr_Occurred()) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pPyEnv);
return -1;
}
pJNIEnter = PyObject_GetAttrString(pJavabridge, "jni_enter");
if (PyErr_Occurred()) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pJavabridge);
Py_DECREF(pPyEnv);
return -1;
}
pArgs = PyTuple_Pack(1, pPyEnv);
if (! pArgs) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pJNIEnter);
Py_DECREF(pJavabridge);
Py_DECREF(pPyEnv);
return -1;
}
pResult = PyObject_CallObject(pJNIEnter, pArgs);
if (! pResult) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pArgs);
Py_DECREF(pJNIEnter);
Py_DECREF(pJavabridge);
Py_DECREF(pPyEnv);
return -1;
}
Py_DECREF(pResult);
Py_DECREF(pArgs);
Py_DECREF(pJNIEnter);
Py_DECREF(pJavabridge);
Py_DECREF(pPyEnv);
return 0;
}
/*
detach_env
Detach an environment previously attached using attach_env
*/
static int detach_env(JNIEnv *pEnv) {
PyObject *pJavabridge;
PyObject *pJNIExit;
PyObject *pArgs;
PyObject *pResult;
pJavabridge = PyImport_ImportModule("javabridge");
if (! pJavabridge) {
throwWrappedError(pEnv, __LINE__);
return -1;
}
pJNIExit = PyObject_GetAttrString(pJavabridge, "jni_exit");
if (! pJNIExit) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pJavabridge);
return -1;
}
pArgs = PyTuple_New(0);
if (! pArgs) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pJNIExit);
Py_DECREF(pJavabridge);
}
pResult = PyObject_CallObject(pJNIExit, pArgs);
if (! pResult) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pArgs);
Py_DECREF(pJNIExit);
Py_DECREF(pJavabridge);
return -1;
}
Py_DECREF(pResult);
Py_DECREF(pArgs);
Py_DECREF(pJNIExit);
Py_DECREF(pJavabridge);
return 0;
}
static PyObject *wrapJObject(JNIEnv *pEnv, jobject j) {
PyObject *pJavabridge;
PyObject *pGetEnv;
PyObject *pTheEnv;
PyObject *pCapsule;
PyObject *pResult;
if (! j) {
Py_RETURN_NONE;
}
pJavabridge = PyImport_ImportModule("javabridge");
if (! pJavabridge) {
throwWrappedError(pEnv, __LINE__);
return NULL;
}
pGetEnv = PyObject_GetAttrString(pJavabridge, "get_env");
if (! pGetEnv) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pJavabridge);
return NULL;
}
pTheEnv = PyObject_CallObject(pGetEnv, NULL);
if (! pTheEnv) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pGetEnv);
Py_DECREF(pJavabridge);
return NULL;
}
pCapsule = PyCapsule_New((void *)j, NULL, NULL);
if (! pCapsule) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pTheEnv);
Py_DECREF(pGetEnv);
Py_DECREF(pJavabridge);
return NULL;
}
pResult = PyObject_CallMethod(pTheEnv, "make_jb_object", "O", pCapsule);
if (! pResult) {
throwWrappedError(pEnv, __LINE__);
}
Py_DECREF(pCapsule);
Py_DECREF(pTheEnv);
Py_DECREF(pGetEnv);
Py_DECREF(pJavabridge);
return pResult;
}
static PyObject *mapToDictionary(JNIEnv *pEnv, jobject map) {
PyObject *pMap;
PyObject *pJavabridge;
PyObject *pFn;
PyObject *pArgs;
PyObject *pResult;
if (! map) {
pResult = PyDict_New();
if (! pResult) {
throwWrappedError(pEnv, __LINE__);
}
return pResult;
}
pMap = wrapJObject(pEnv, map);
if (! pMap) {
return NULL;
}
pJavabridge = PyImport_ImportModule("javabridge.jutil");
if (! pJavabridge) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pMap);
return NULL;
}
pFn = PyObject_GetAttrString(pJavabridge, "make_run_dictionary");
Py_DECREF(pJavabridge);
if (! pFn) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pMap);
return NULL;
}
pArgs = PyTuple_Pack(1, pMap);
Py_DECREF(pMap);
if (! pArgs) {
throwWrappedError(pEnv, __LINE__);
Py_DECREF(pFn);
return NULL;
}
pResult = PyObject_CallObject(pFn, pArgs);
Py_DECREF(pFn);
Py_DECREF(pArgs);
if (! pResult){
throwWrappedError(pEnv, __LINE__);
return NULL;
}
return pResult;
}
/*
* Add globals from __main__ to a dictionary of them.
*/
static int add_globals(JNIEnv *pEnv, PyObject *pGlobals) {
PyObject *pMain;
PyObject *pMainDict;
int result;
pMain = PyImport_AddModule("__main__");
if (! pMain) {
throwWrappedError(pEnv, __LINE__);
return -1;
}
pMainDict = PyModule_GetDict(pMain);
result = PyDict_Merge(pGlobals, pMainDict, 0);
if (result) {
throwWrappedError(pEnv, __LINE__);
}
return result;
}
JNIEXPORT void JNICALL Java_org_cellprofiler_javabridge_CPython_exec
(JNIEnv *pEnv, jobject thiss, jstring script, jobject locals, jobject globals) {
PyGILState_STATE state;
PyObject *pLocals;
PyObject *pGlobals;
const char *pScript;
PyObject *pResult;
if (! pEnv) {
throwError(pEnv, "JNIEnv was null.");
return;
}
if (! script) {
throwError(pEnv, "Script was null.");
return;
}
check_init();
state = PyGILState_Ensure();
if (attach_env(pEnv) == 0) {
pLocals = mapToDictionary(pEnv, locals);
if (pLocals) {
if ((locals != NULL) &&
((*pEnv)->IsSameObject(pEnv, locals, globals))) {
pGlobals = pLocals;
Py_INCREF(pGlobals);
} else {
pGlobals = mapToDictionary(pEnv, globals);
}
if (pGlobals) {
if (! add_globals(pEnv, pGlobals)) {
pScript = (*pEnv)->GetStringUTFChars(pEnv, script, NULL);
pResult = PyRun_String(
pScript, Py_file_input, pGlobals, pLocals);
(*pEnv)->ReleaseStringUTFChars(pEnv, script, NULL);
if (pResult) {
Py_DECREF(pResult);
} else {
throwWrappedError(pEnv, __LINE__);
}
}
Py_DECREF(pGlobals);
}
Py_DECREF(pLocals);
}
}
detach_env(pEnv);
PyGILState_Release(state);
}