-
Notifications
You must be signed in to change notification settings - Fork 63
/
_javabridge.pyx
2020 lines (1737 loc) · 76.9 KB
/
_javabridge.pyx
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# cython: language_level=3
"""_javabridge.pyx - low-level interface to the JVM
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.
"""
import numpy as np
import sys
import threading
cimport numpy as np
cimport cython
cimport _javabridge_osspecific
cimport cpython
if sys.version_info >= (3, 0, 0):
# unchir -> chr in Python 3
unichr = chr
cdef extern from "Python.h":
ctypedef int Py_intptr_t
ctypedef unsigned long Py_ssize_t
unicode PyUnicode_DecodeUTF16(char *s, Py_ssize_t size, char *errors, int *byteorder)
bint PyCapsule_CheckExact(object o)
void *PyCapsule_GetPointer(object o,char *name)
cdef extern from "stdlib.h":
ctypedef unsigned long size_t
void free(void *ptr)
void *malloc(size_t size)
cdef extern from "string.h":
void *memset(void *, int, int)
void *memcpy(void *, void *, int)
cdef extern from "numpy/arrayobject.h":
ctypedef class numpy.ndarray [object PyArrayObject]:
cdef char *data
cdef Py_intptr_t *dimensions
cdef Py_intptr_t *strides
cdef void import_array()
cdef int PyArray_ITEMSIZE(np.ndarray)
import_array()
cdef extern from "jni.h":
enum:
JNI_VERSION_1_4
JNI_COMMIT
JNI_ABORT
ctypedef struct _jobject
ctypedef struct _jmethodID
ctypedef struct _jfieldID
ctypedef long jint
ctypedef unsigned char jboolean
ctypedef unsigned char jbyte
ctypedef unsigned short jchar
ctypedef short jshort
ctypedef long long jlong
ctypedef float jfloat
ctypedef double jdouble
ctypedef jint jsize
ctypedef _jobject *jobject
ctypedef jobject jclass
ctypedef jobject jthrowable
ctypedef jobject jstring
ctypedef jobject jarray
ctypedef jarray jbooleanArray
ctypedef jarray jbyteArray
ctypedef jarray jcharArray
ctypedef jarray jshortArray
ctypedef jarray jintArray
ctypedef jarray jlongArray
ctypedef jarray jfloatArray
ctypedef jarray jdoubleArray
ctypedef jarray jobjectArray
ctypedef union jvalue:
jboolean z
jbyte b
jchar c
jshort s
jint i
jlong j
jfloat f
jdouble d
jobject l
ctypedef jvalue jvalue
ctypedef _jmethodID *jmethodID
ctypedef _jfieldID *jfieldID
ctypedef struct JNIInvokeInterface_
ctypedef JNIInvokeInterface_ *JavaVM
ctypedef struct JNIInvokeInterface_:
jint (*DestroyJavaVM)(JavaVM *vm) nogil
jint (*AttachCurrentThread)(JavaVM *vm, void **penv, void *args) nogil
jint (*DetachCurrentThread)(JavaVM *vm) nogil
jint (*GetEnv)(JavaVM *vm, void **penv, jint version) nogil
jint (*AttachCurrentThreadAsDaemon)(JavaVM *vm, void *penv, void *args) nogil
struct JavaVMOption:
char *optionString
void *extraInfo
ctypedef JavaVMOption JavaVMOption
struct JavaVMInitArgs:
jint version
jint nOptions
JavaVMOption *options
jboolean ignoreUnrecognized
ctypedef JavaVMInitArgs JavaVMInitArgs
struct JNIEnv_
struct JNINativeInterface_
ctypedef JNINativeInterface_ *JNIEnv
struct JNINativeInterface_:
jint (* GetVersion)(JNIEnv *env) nogil
jclass (* FindClass)(JNIEnv *env, char *name) nogil
jclass (* GetObjectClass)(JNIEnv *env, jobject obj) nogil
jboolean (* IsInstanceOf)(JNIEnv *env, jobject obj, jclass klass) nogil
jobject (* NewGlobalRef)(JNIEnv *env, jobject lobj) nogil
void (* DeleteGlobalRef)(JNIEnv *env, jobject gref) nogil
void (* DeleteLocalRef)(JNIEnv *env, jobject obj) nogil
#
# Exception handling
#
jobject (* ExceptionOccurred)(JNIEnv *env) nogil
void (* ExceptionDescribe)(JNIEnv *env) nogil
void (* ExceptionClear)(JNIEnv *env) nogil
#
# Method IDs
#
jmethodID (*GetMethodID)(JNIEnv *env, jclass clazz, char *name, char *sig) nogil
jmethodID (*GetStaticMethodID)(JNIEnv *env, jclass clazz, char *name, char *sig) nogil
jmethodID (*FromReflectedMethod)(JNIEnv *env, jobject method) nogil
jmethodID (*FromReflectedField)(JNIEnv *env, jobject field) nogil
#
# New object
#
jobject (* NewObjectA)(JNIEnv *env, jclass clazz, jmethodID id, jvalue *args) nogil
#
# Methods for object calls
#
jboolean (* CallBooleanMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jbyte (* CallByteMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jchar (* CallCharMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jshort (* CallShortMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jint (* CallIntMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jlong (* CallLongMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jfloat (* CallFloatMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jdouble (* CallDoubleMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
void (* CallVoidMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
jobject (* CallObjectMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args) nogil
#
# Methods for static class calls
#
jboolean (* CallStaticBooleanMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jbyte (* CallStaticByteMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jchar (* CallStaticCharMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jshort (* CallStaticShortMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jint (* CallStaticIntMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jlong (* CallStaticLongMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jfloat (* CallStaticFloatMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jdouble (* CallStaticDoubleMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
void (* CallStaticVoidMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
jobject (* CallStaticObjectMethodA)(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args) nogil
#
# Methods for fields
#
jfieldID (* GetFieldID)(JNIEnv *env, jclass clazz, char *name, char *sig) nogil
jobject (* GetObjectField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jboolean (* GetBooleanField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jbyte (* GetByteField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jchar (* GetCharField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jshort (* GetShortField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jint (* GetIntField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jlong (* GetLongField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jfloat (*GetFloatField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
jdouble (*GetDoubleField)(JNIEnv *env, jobject obj, jfieldID fieldID) nogil
void (* SetObjectField)(JNIEnv *env, jobject obj, jfieldID fieldID, jobject val) nogil
void (* SetBooleanField)(JNIEnv *env, jobject obj, jfieldID fieldID, jboolean val) nogil
void (* SetByteField)(JNIEnv *env, jobject obj, jfieldID fieldID, jbyte val) nogil
void (* SetCharField)(JNIEnv *env, jobject obj, jfieldID fieldID, jchar val) nogil
void (*SetShortField)(JNIEnv *env, jobject obj, jfieldID fieldID, jshort val) nogil
void (*SetIntField)(JNIEnv *env, jobject obj, jfieldID fieldID, jint val) nogil
void (*SetLongField)(JNIEnv *env, jobject obj, jfieldID fieldID, jlong val) nogil
void (*SetFloatField)(JNIEnv *env, jobject obj, jfieldID fieldID, jfloat val) nogil
void (*SetDoubleField)(JNIEnv *env, jobject obj, jfieldID fieldID, jdouble val) nogil
jfieldID (*GetStaticFieldID)(JNIEnv *env, jclass clazz, char *name, char *sig) nogil
jobject (* GetStaticObjectField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jboolean (* GetStaticBooleanField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jbyte (* GetStaticByteField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jchar (* GetStaticCharField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jshort (* GetStaticShortField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jint (* GetStaticIntField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jlong (* GetStaticLongField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jfloat (*GetStaticFloatField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
jdouble (* GetStaticDoubleField)(JNIEnv *env, jclass clazz, jfieldID fieldID) nogil
void (*SetStaticObjectField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jobject value) nogil
void (*SetStaticBooleanField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jboolean value) nogil
void (*SetStaticByteField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jbyte value) nogil
void (*SetStaticCharField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jchar value) nogil
void (*SetStaticShortField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jshort value) nogil
void (*SetStaticIntField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jint value) nogil
void (*SetStaticLongField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jlong value) nogil
void (*SetStaticFloatField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jfloat value) nogil
void (*SetStaticDoubleField)(JNIEnv *env, jclass clazz, jfieldID fieldID, jdouble value) nogil
#
# Methods for handling strings
#
jobject (* NewStringUTF)(JNIEnv *env, char *utf) nogil
jobject (* NewString)(JNIEnv *env, jchar *unicode, jsize len) nogil
char *(* GetStringUTFChars)(JNIEnv *env, jobject str, jboolean *is_copy) nogil
void (* ReleaseStringUTFChars)(JNIEnv *env, jobject str, char *chars) nogil
jsize (* GetStringLength)(JNIEnv *env, jobject str) nogil
jchar *(* GetStringChars)(JNIEnv *env, jobject str, jboolean *isCopy) nogil
void (* ReleaseStringChars)(JNIEnv *env, jobject str, jchar *chars) nogil
#
# Methods for making arrays (which I am not distinguishing from jobjects here) nogil
#
jsize (* GetArrayLength)(JNIEnv *env, jobject array) nogil
jobject (* NewObjectArray)(JNIEnv *env, jsize len, jclass clazz, jobject init) nogil
jobject (* GetObjectArrayElement)(JNIEnv *env, jobject array, jsize index) nogil
void (* SetObjectArrayElement)(JNIEnv *env, jobject array, jsize index, jobject val) nogil
jobject (* NewBooleanArray)(JNIEnv *env, jsize len) nogil
jobject (* NewByteArray)(JNIEnv *env, jsize len) nogil
jobject (* NewCharArray)(JNIEnv *env, jsize len) nogil
jobject (* NewShortArray)(JNIEnv *env, jsize len) nogil
jobject (* NewIntArray)(JNIEnv *env, jsize len) nogil
jobject (*NewLongArray)(JNIEnv *env, jsize len) nogil
jobject (* NewFloatArray)(JNIEnv *env, jsize len) nogil
jobject (* NewDoubleArray)(JNIEnv *env, jsize len) nogil
jboolean * (* GetBooleanArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jbyte * (* GetByteArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jchar * (*GetCharArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jshort * (*GetShortArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jint * (* GetIntArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jlong * (* GetLongArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jfloat * (* GetFloatArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
jdouble * (* GetDoubleArrayElements)(JNIEnv *env, jobject array, jboolean *isCopy) nogil
void (*ReleaseBooleanArrayElements)(JNIEnv *env, jobject array, jboolean *elems, jint mode) nogil
void (*ReleaseByteArrayElements)(JNIEnv *env, jobject array, jbyte *elems, jint mode) nogil
void (*ReleaseCharArrayElements)(JNIEnv *env, jobject array, jchar *elems, jint mode) nogil
void (*ReleaseShortArrayElements)(JNIEnv *env, jobject array, jshort *elems, jint mode) nogil
void (*ReleaseIntArrayElements)(JNIEnv *env, jobject array, jint *elems, jint mode) nogil
void (*ReleaseLongArrayElements)(JNIEnv *env, jobject array, jlong *elems, jint mode) nogil
void (*ReleaseFloatArrayElements)(JNIEnv *env, jobject array, jfloat *elems, jint mode) nogil
void (*ReleaseDoubleArrayElements)(JNIEnv *env, jobject array, jdouble *elems, jint mode) nogil
void (* GetBooleanArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize l, jboolean *buf) nogil
void (* GetByteArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jbyte *buf) nogil
void (*GetCharArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jchar *buf) nogil
void (* GetShortArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jshort *buf) nogil
void (* GetIntArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jint *buf) nogil
void (* GetLongArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jlong *buf) nogil
void (* GetFloatArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jfloat *buf) nogil
void (* GetDoubleArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jdouble *buf) nogil
void (*SetBooleanArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize l, jboolean *buf) nogil
void (*SetByteArrayRegion)(JNIEnv *env, jobject array, jsize start,
jsize len, jbyte *buf) nogil
void (*SetCharArrayRegion)(JNIEnv *env, jobject array, jsize start, jsize len,
char *buf) nogil
void (*SetShortArrayRegion)(JNIEnv *env, jobject array, jsize start, jsize len,
jshort *buf) nogil
void (*SetIntArrayRegion)(JNIEnv *env, jobject array, jsize start, jsize len,
jint *buf) nogil
void (*SetLongArrayRegion)(JNIEnv *env, jobject array, jsize start, jsize len,
jlong *buf) nogil
void (*SetFloatArrayRegion)(JNIEnv *env, jobject array, jsize start, jsize len,
jfloat *buf) nogil
void (*SetDoubleArrayRegion)(JNIEnv *env, jobject array, jsize start, jsize len,
jdouble *buf) nogil
cdef extern from "mac_javabridge_utils.h":
int MacStartVM(JavaVM **, JavaVMInitArgs *pVMArgs, char *class_name,
char *path_to_libjvm, char *path_to_libjli) nogil
void MacStopVM() nogil
void MacRunLoopInit() nogil
void MacRunLoopRun() nogil
void MacRunLoopStop() nogil
void MacRunLoopReset() nogil
int MacIsMainThread() nogil
void MacRunLoopRunInMode(double) nogil
# NOTE: its required to have a 'from *' after the 'extern' declaration
# here, in order to avoid problems with cython on Cywin and MSYS
# Windows environments. The 'from *' will make cython think the
# declaration is from some header, so it avoids the __imp_ prefix
# on the symbols. Otherwise linking will fail. For more details see
# https://trac.sagemath.org/ticket/19868
cdef extern from *:
void StopVM(JavaVM *vm) nogil
int CreateJavaVM(JavaVM **pvm, void **pEnv, void *args) nogil
def mac_run_loop_init():
MacRunLoopInit()
def mac_reset_run_loop():
'''Reset the run loop's internal state so that it's ready to run
'''
with nogil:
MacRunLoopReset()
def mac_enter_run_loop():
'''Enter the run loop and stay there until mac_stop_run_loop is called
This enters the main run loop in the main thread and stays in the
run loop until some other thread calls MacStopRunLoop.
'''
with nogil:
MacRunLoopRun()
def mac_poll_run_loop(timeout):
MacRunLoopRunInMode(timeout)
def mac_stop_run_loop():
'''Signal the run loop to stop
Wait for the main thread to enter the run loop, if necessary, then
signal the run loop to stop.
'''
with nogil:
MacRunLoopStop()
def mac_is_main_thread():
'''Return True if the current thread runs the main OS/X run loop
'''
return MacIsMainThread() != 0
#####################################################
#
# Threading
#
# Java environments are thread-specific and the Java
# VM is global. This section helps maintain each thread's
# Java environment as a thread-local variable shared with
# the Cython code and maintains the VM singleton.
#
# In addition, there's a wakeup event that's used to
# communicate with the thread that's in charge of garbage-
# collection objects deleted on a thread without an environment.
#
#######################################################
__vm = None
__thread_local_env = threading.local()
__dead_objects = []
__wake_event = threading.Event()
def wait_for_wake_event():
'''Wait for dead objects to be enqueued or other event on monitor thread'''
__wake_event.wait()
__wake_event.clear()
def set_wake_event():
'''Wake up the monitor thread'''
__wake_event.set()
def get_vm():
global __vm
if __vm is None:
__vm = JB_VM()
return __vm
def get_thread_local(key, default=None):
if not hasattr(__thread_local_env, key):
setattr(__thread_local_env, key, default)
return getattr(__thread_local_env, key)
def set_thread_local(key, value):
setattr(__thread_local_env, key, value)
def get_env():
'''Get the environment for this thread'''
return get_thread_local("env")
def jb_attach():
'''Attach to this thread's environment'''
assert __vm is not None
assert get_env() is None
assert __vm.is_active()
set_thread_local("env", __vm.attach_as_daemon())
return get_env()
def jb_detach():
'''Detach from this thread's environment'''
assert __vm is not None
assert get_env() is not None
set_thread_local("env", None)
__vm.detach()
def jni_enter(env):
'''Enter Python from Java
:param env: pointer to JNIEnv wrapped in a PyCapsule
Set this thread's environment to the one passed in through
a JNI native call.
'''
env_stack = get_thread_local("envstack", None)
if env_stack is None:
env_stack = []
set_thread_local("envstack", env_stack)
old_env = get_env()
if old_env is not None:
env_stack.append(old_env)
new_env = JB_Env()
new_env.set_env(env)
set_thread_local("env", new_env)
def jni_exit():
'''Exit the JNI from Python'''
env_stack = get_thread_local("envstack")
if len(env_stack) == 0:
set_thread_local("env", None)
else:
set_thread_local("env", env_stack.pop())
def jvm_enter(vm):
'''Initialize the JVM on entry into Python
:param vm: pointer to JavaVM wrapped in a PyCapsule
Set the global Java VM.
'''
get_vm().set_vm(vm)
def reap():
'''Reap all of the garbage-collected Java objects on the dead_objects list'''
if len(__dead_objects) > 0:
env = get_env()
assert env is not None
try:
while True:
to_die = __dead_objects.pop()
env.dealloc_jobject(to_die)
except IndexError:
pass
cdef class JB_Object:
'''Represents a Java object.'''
cdef:
jobject o
gc_collect
def __cinit__(self):
self.o = NULL
self.gc_collect = False
def __repr__(self):
return "<Java object at 0x%x>"%<int>(self.o)
def __dealloc__(self):
cdef:
JB_Object alternate
if not self.gc_collect:
return
env = get_env()
if env is None:
alternate = JB_Object()
alternate.o = self.o
__dead_objects.append(alternate)
set_wake_event()
else:
env.dealloc_jobject(self)
def addr(self):
'''Return the address of the Java object as a string'''
return str(<int>(self.o))
cdef class JB_Class:
'''A Java class'''
cdef:
jclass c
def __cinit__(self):
self.c = NULL
def __repr__(self):
return "<Java class at 0x%x>"%<int>(self.c)
def as_class_object(self):
result = JB_Object()
result.o = self.c
return result
cdef class __JB_MethodID:
'''A method ID as returned by get_method_id'''
cdef:
jmethodID id
sig
is_static
def __cinit__(self):
self.id = NULL
self.sig = ''
self.is_static = False
def __repr__(self):
return "<Java method with sig=%s at 0x%x>"%(self.sig,<int>(self.id))
cdef class __JB_FieldID:
'''A field ID as returned by get_field_id'''
cdef:
jfieldID id
sig
is_static
def __cinit__(self):
self.id = NULL
self.sig = ''
self.is_static = False
def __repr__(self):
return "<Java field with sig=%s at 0x%x>"%(self.sig, <int>(self.id))
cdef fill_values(orig_sig, args, jvalue **pvalues):
cdef:
jvalue *values
int i
JB_Object jbobject
JB_Class jbclass
Py_UNICODE *usz
sig = orig_sig
values = <jvalue *>malloc(sizeof(jvalue)*len(args))
pvalues[0] = values
for i,arg in enumerate(args):
if len(sig) == 0:
free(<void *>values)
return ValueError("# of arguments (%d) in call did not match signature (%s)"%
(len(args), orig_sig))
if sig[0] == 'Z': #boolean
values[i].z = 1 if arg else 0
sig = sig[1:]
elif sig[0] == 'B': #byte
values[i].b = int(arg)
sig = sig[1:]
elif sig[0] == 'C': #char
values[i].c = ord(arg[0])
sig = sig[1:]
elif sig[0] == 'S': #short
values[i].s = int(arg)
sig = sig[1:]
elif sig[0] == 'I': #int
values[i].i = int(arg)
sig = sig[1:]
elif sig[0] == 'J': #long
values[i].j = int(arg)
sig = sig[1:]
elif sig[0] == 'F': #float
values[i].f = float(arg)
sig = sig[1:]
elif sig[0] == 'D': #double
values[i].d = float(arg)
sig = sig[1:]
elif sig[0] == 'L' or sig[0] == '[': #object
if isinstance(arg, JB_Object):
jbobject = arg
values[i].l = jbobject.o
elif isinstance(arg, JB_Class):
jbclass = arg
values[i].l = jbclass.c
elif arg is None:
values[i].l = NULL
else:
free(<void *>values)
return ValueError("%s is not a Java object"%str(arg))
if sig[0] == '[':
if len(sig) == 1:
raise ValueError("Bad signature: %s"%orig_sig)
non_bracket_ind = 1
try:
while sig[non_bracket_ind] == '[':
non_bracket_ind += 1
except IndexError:
raise ValueError("Bad signature: %s"%orig_sig)
if sig[non_bracket_ind] != 'L':
# An array of primitive type:
sig = sig[(non_bracket_ind+1):]
continue
sig = sig[sig.find(';')+1:]
else:
return ValueError("Unhandled signature: %s"%orig_sig)
if len(sig) > 0:
return ValueError("Too few arguments (%d) for signature (%s)"%
(len(args), orig_sig))
cdef class JB_VM:
'''Represents the Java virtual machine'''
cdef JavaVM *vm
def set_vm(self, capsule):
'''Set the pointer to the JavaVM
This is here to handle the case where Java is the boss and Python
is being started from Java, e.g. from
org.cellprofiler.javabridge.CPython.
:param capsule: an encapsulated pointer to the JavaVM
'''
if not PyCapsule_CheckExact(capsule):
raise ValueError(
"set_vm called with something other than a wrapped environment")
self.vm = <JavaVM *>PyCapsule_GetPointer(capsule, NULL)
if not self.vm:
raise ValueError(
"set_vm called with non-environment capsule")
def is_active(self):
'''Return True if JVM has been started, but not killed'''
return self.vm != NULL
def create(self, options):
'''Create the Java VM'''
cdef:
JavaVMInitArgs args
JNIEnv *env
JB_Env jenv
args.version = JNI_VERSION_1_4
args.nOptions = len(options)
args.options = <JavaVMOption *>malloc(sizeof(JavaVMOption)*args.nOptions)
if args.options == NULL:
raise MemoryError("Failed to allocate JavaVMInitArgs")
options = [str(option) for option in options]
optionutf8=[] # list for temporarily storing utf-8 copies of strings
for i, option in enumerate(options):
optionutf8.append(option.encode('utf-8'))
args.options[i].optionString = optionutf8[-1]
result = CreateJavaVM(&self.vm, <void **>&env, &args)
free(args.options)
if result != 0:
raise RuntimeError("Failed to create Java VM. Return code = %d"%result)
jenv = JB_Env()
jenv.env = env
set_thread_local("env", jenv)
return jenv
def create_mac(self, options, class_name, path_to_libjvm, path_to_libjli):
'''Create the Java VM on OS/X in a different thread
On the Mac, (assuming this works), you need to start a PThread
and do so in a very particular manner if you ever want to run UI
code in Java and Python. This creates that thread and it then runs
a runnable.
org.cellprofiler.runnablequeue.RunnableQueue is a class that uses
a queue to ferry runnables to this main thread. You can use that
and then call RunnableQueue's static methods to run things on the
main thread.
You should run this on its own thread since it will not return until
after the JVM exits.
options - the option strings
class_name - the name of the Runnable to run on the Java main thread
path_to_libjvm - path to libjvm.dylib
path_to_libjli - path to libjli.dylib
'''
class_name = str(class_name).encode("utf-8")
path_to_libjvm = str(path_to_libjvm).encode("utf-8")
path_to_libjli = str(path_to_libjli).encode("utf-8")
cdef:
JavaVMInitArgs args
JNIEnv *env
JB_Env jenv
int result
char *pclass_name = class_name
char *ppath_to_libjvm = path_to_libjvm
char *ppath_to_libjli = path_to_libjli
JavaVM **pvm = &self.vm
args.version = JNI_VERSION_1_4
args.nOptions = len(options)
args.options = <JavaVMOption *>malloc(sizeof(JavaVMOption)*args.nOptions)
if args.options == NULL:
raise MemoryError("Failed to allocate JavaVMInitArgs")
options = [str(option).encode("utf-8") for option in options]
for i, option in enumerate(options):
args.options[i].optionString = option
with nogil:
result = MacStartVM(pvm, &args, pclass_name, ppath_to_libjvm, ppath_to_libjli)
free(args.options)
if result != 0:
raise RuntimeError("Failed to create Java VM. Return code = %d"%result)
def attach(self):
'''Attach this thread to the VM returning an environment'''
cdef:
JNIEnv *env
JB_Env jenv
result = self.vm[0].AttachCurrentThread(self.vm, <void **>&env, NULL)
if result != 0:
raise RuntimeError("Failed to attach to current thread. Return code = %d"%result)
jenv = JB_Env()
jenv.env = env
return jenv
def attach_as_daemon(self):
'''Attach this thread as a daemon thread'''
cdef:
JNIEnv *env
JB_Env jenv
result = self.vm[0].AttachCurrentThreadAsDaemon(self.vm, <void *>&env, NULL)
if result != 0:
raise RuntimeError("Failed to attach to current thread. Return code = %d"%result)
jenv = JB_Env()
jenv.env = env
return jenv
def detach(self):
'''Detach this thread from the VM'''
self.vm[0].DetachCurrentThread(self.vm)
def destroy(self):
if self.vm != NULL:
StopVM(self.vm)
self.vm = NULL
cdef class JB_Env:
'''
Represents the Java VM and the Java execution environment as
returned by JNI_CreateJavaVM.
'''
cdef:
JNIEnv *env
def __init__(self):
self.env = NULL
def __repr__(self):
return "<JB_Env at 0x%x>"%(<size_t>(self.env))
def set_env(self, capsule):
'''Set the JNIEnv to a memory address
address - address as an integer representation of a string
'''
if not PyCapsule_CheckExact(capsule):
raise ValueError(
"set_env called with something other than a wrapped environment")
self.env = <JNIEnv *>PyCapsule_GetPointer(capsule, NULL)
if not self.env:
raise ValueError(
"set_env called with non-environment capsule")
def __dealloc__(self):
self.env = NULL
def dealloc_jobject(self, JB_Object jbo):
'''Deallocate an object as it goes out of scope
DON'T call this externally.
'''
self.env[0].DeleteGlobalRef(self.env, jbo.o)
jbo.gc_collect = False
def get_version(self):
'''Return the version number as a major / minor version tuple'''
cdef:
int version
version = self.env[0].GetVersion(self.env)
return (int(version / 65536), version % 65536)
def find_class(self, name):
'''Find a Java class by name
:param name: the class name with "/" as the path separator, e.g. "java/lang/String"
:return: a Java class object suitable for calls such as :py:meth:`.get_method_id`
'''
cdef:
jclass c
JB_Class result
utf8name = name.encode('utf-8')
c = self.env[0].FindClass(self.env, utf8name)
if c == NULL:
print("Failed to get class "+name)
return
cref = self.env[0].NewGlobalRef(self.env, c)
if cref == NULL:
return (None, MemoryError("Failed to make new global reference"))
self.env[0].DeleteLocalRef(self.env, c)
result = JB_Class()
result.c = cref
return result
def get_object_class(self, JB_Object o):
'''Return the class for an object
:param o: a Java object
:return: a Java class object suitable for calls such as :py:meth:`.get_method_id`
'''
cdef:
jclass c
JB_Class result
c = self.env[0].GetObjectClass(self.env, o.o)
result = JB_Class()
result.c = c
return result
def is_instance_of(self, JB_Object o, JB_Class c):
'''Return True if object is instance of class
:param o: a Java object
:param c: a Java class
:return: True if o is an instance of c otherwise False
'''
result = self.env[0].IsInstanceOf(self.env, o.o, c.c)
return result != 0
def exception_occurred(self):
'''Return a throwable if an exception occurred or None'''
cdef:
jobject t
t = self.env[0].ExceptionOccurred(self.env)
if t == NULL:
return
o, e = make_jb_object(self, t)
if e is not None:
raise e
return o
def exception_describe(self):
'''Print a stack trace of the last exception to stderr'''
self.env[0].ExceptionDescribe(self.env)
def exception_clear(self):
'''Clear the current exception'''
self.env[0].ExceptionClear(self.env)
def get_method_id(self, JB_Class c, name, sig):
'''Find the method ID for a method on a class
:param c: a class retrieved by find_class or get_object_class
:param name: the method name
:param sig: the calling signature,
e.g. "(ILjava/lang/String;)D" is a function that
returns a double and takes an integer, a long and
a string as arguments.
'''
cdef:
jmethodID id
__JB_MethodID result
utf8name = name.encode('utf-8')
utf8sig = sig.encode('utf-8')
if c is None:
raise ValueError("Class = None on call to get_method_id")
id = self.env[0].GetMethodID(self.env, c.c, utf8name, utf8sig)
if id == NULL:
return
result = __JB_MethodID()
result.id = id
result.sig = sig
result.is_static = False
return result
def get_static_method_id(self, JB_Class c, name, sig):
'''Find the method ID for a static method on a class
:param c: a class retrieved by find_class or get_object_class
:param name: the method name
:param sig: the calling signature,
e.g. "(ILjava/lang/String;)D" is a function that
returns a double and takes an integer, a long and
a string as arguments.
'''
cdef:
jmethodID id
__JB_MethodID result
utf8name = name.encode('utf-8')
utf8sig = sig.encode('utf-8')
id = self.env[0].GetStaticMethodID(self.env, c.c, utf8name, utf8sig)
if id == NULL:
return
result = __JB_MethodID()
result.id = id
result.sig = sig
result.is_static = True
return result
def from_reflected_method(self, JB_Object method, char *sig, is_static):
'''Get a method_id given an instance of java.lang.reflect.Method
:param method: a method, e.g. as retrieved from getDeclaredMethods
:param sig: signature of method
:param is_static: true if this is a static method
'''
cdef:
jmethodID id
__JB_MethodID result
id = self.env[0].FromReflectedMethod(self.env, method.o)
if id == NULL:
return
result = __JB_MethodID()
result.id = id
result.sig = sig
result.is_static = is_static
return result
def call_method(self, JB_Object o, __JB_MethodID m, *args):
'''Call a method on an object with arguments
:param o: object in question
:param m: the method ID from :py:meth:`.get_method_id`
:param \\*args: the arguments to the method call. Arguments
should appear in the same order as the
signature. Arguments will be coerced into the
type of the signature.
'''
cdef:
jvalue *values
jobject this = o.o
JNIEnv *jnienv = self.env
jboolean zresult
jbyte bresult
jchar cresult
jshort sresult
jint iresult
jlong jresult
jfloat fresult
jdouble dresult
jobject oresult
jmethodID m_id = m.id
if m is None:
raise ValueError("Method ID is None - check your method ID call")
if m.is_static:
raise ValueError("call_method called with a static method. Use"
" call_static_method instead")
sig = m.sig # m.sig should be unicode already, no need to decode
if sig[0] != '(':
raise ValueError("Bad function signature: %s"%m.sig)
arg_end = sig.find(')')
if arg_end == -1:
raise ValueError("Bad function signature: %s"%m.sig)
arg_sig = sig[1:arg_end]
error = fill_values(arg_sig, args, &values)
if error is not None:
raise error
sig = sig[arg_end+1:]
#
# Dispatch based on return code at end of sig
#
if sig == 'Z':
with nogil:
zresult = jnienv[0].CallBooleanMethodA(
jnienv, this, m_id, values)
result = zresult != 0
elif sig == 'B':
with nogil:
bresult = jnienv[0].CallByteMethodA(jnienv, this, m_id, values)
result = bresult
elif sig == 'C':
with nogil:
cresult = jnienv[0].CallCharMethodA(jnienv, this, m_id, values)
result = unichr(cresult)
elif sig == 'S':
with nogil:
sresult = jnienv[0].CallShortMethodA(jnienv, this, m_id, values)
result = sresult
elif sig == 'I':
with nogil:
iresult = jnienv[0].CallIntMethodA(jnienv, this, m_id, values)
result = iresult
elif sig == 'J':
with nogil:
jresult = jnienv[0].CallLongMethodA(jnienv, this, m_id, values)
result = jresult
elif sig == 'F':
with nogil:
fresult = jnienv[0].CallFloatMethodA(jnienv, this, m_id, values)
result = fresult