forked from ninia/jep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyjmethod.c
837 lines (717 loc) · 26.3 KB
/
pyjmethod.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
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
/*
jep - Java Embedded Python
Copyright (c) 2004-2021 JEP AUTHORS.
This file is licensed under the the zlib/libpng License.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Jep.h"
/*
* https://bugs.python.org/issue2897
* structmember.h must be included to use PyMemberDef
*/
#include "structmember.h"
// called internally to make new PyJMethodObject instances.
// throws python exception and returns NULL on error.
PyJMethodObject* PyJMethod_New(JNIEnv *env, jobject rmethod)
{
jstring jname = NULL;
PyObject *pyname = NULL;
PyJMethodObject *pym = NULL;
if (PyType_Ready(&PyJMethod_Type) < 0) {
return NULL;
}
jname = java_lang_reflect_Member_getName(env, rmethod);
if (process_java_exception(env) || !jname) {
return NULL;
}
pyname = jstring_As_PyString(env, jname);
(*env)->DeleteLocalRef(env, jname);
pym = PyObject_NEW(PyJMethodObject, &PyJMethod_Type);
pym->rmethod = (*env)->NewGlobalRef(env, rmethod);
pym->parameters = NULL;
pym->lenParameters = -1;
pym->pyMethodName = pyname;
pym->isStatic = -1;
pym->returnTypeId = -1;
return pym;
}
// 1 if successful, 0 if failed.
static int pyjmethod_init(JNIEnv *env, PyJMethodObject *self)
{
jobject returnType = NULL;
jobjectArray paramArray = NULL;
jint modifier = -1;
jboolean isStatic = JNI_FALSE;
if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) {
process_java_exception(env);
return 0;
}
self->methodId = (*env)->FromReflectedMethod(env, self->rmethod);
returnType = java_lang_reflect_Method_getReturnType(env, self->rmethod);
if (process_java_exception(env) || !returnType) {
goto EXIT_ERROR;
}
self->returnTypeId = get_jtype(env, returnType);
if (process_java_exception(env)) {
goto EXIT_ERROR;
}
paramArray = java_lang_reflect_Method_getParameterTypes(env, self->rmethod);
if (process_java_exception(env) || !paramArray) {
goto EXIT_ERROR;
}
self->parameters = (*env)->NewGlobalRef(env, paramArray);
self->lenParameters = (*env)->GetArrayLength(env, paramArray);
modifier = java_lang_reflect_Member_getModifiers(env, self->rmethod);
if (process_java_exception(env)) {
goto EXIT_ERROR;
}
isStatic = java_lang_reflect_Modifier_isStatic(env, modifier);
if (process_java_exception(env)) {
goto EXIT_ERROR;
}
if (isStatic == JNI_TRUE) {
self->isStatic = 1;
} else {
self->isStatic = 0;
}
(*env)->PopLocalFrame(env, NULL);
return 1;
EXIT_ERROR:
(*env)->PopLocalFrame(env, NULL);
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError, "Unknown");
}
return 0;
}
static void pyjmethod_dealloc(PyJMethodObject *self)
{
#if USE_DEALLOC
JNIEnv *env = pyembed_get_env();
if (env) {
if (self->parameters) {
(*env)->DeleteGlobalRef(env, self->parameters);
}
if (self->rmethod) {
(*env)->DeleteGlobalRef(env, self->rmethod);
}
}
Py_CLEAR(self->pyMethodName);
PyObject_Del(self);
#endif
}
int PyJMethod_Check(PyObject *obj)
{
return PyObject_TypeCheck(obj, &PyJMethod_Type);
}
int PyJMethod_GetParameterCount(PyJMethodObject *method, JNIEnv *env)
{
if (!method->parameters && !pyjmethod_init(env, method)) {
return -1;
}
return method->lenParameters;
}
int PyJMethod_CheckArguments(PyJMethodObject* method, JNIEnv *env,
PyObject* args)
{
int matchTotal = 1;
int parampos;
if (PyJMethod_GetParameterCount(method, env) != (PyTuple_Size(args) - 1)) {
return 0;
}
for (parampos = 0; parampos < method->lenParameters; parampos += 1) {
PyObject* param = PyTuple_GetItem(args, parampos + 1);
int paramTypeId;
int match;
jclass paramType = (jclass) (*env)->GetObjectArrayElement(env,
method->parameters, parampos);
if (process_java_exception(env) || !paramType) {
matchTotal = -1;
break;
}
paramTypeId = get_jtype(env, paramType);
match = pyarg_matches_jtype(env, param, paramType, paramTypeId);
(*env)->DeleteLocalRef(env, paramType);
if (PyErr_Occurred()) {
matchTotal = -1;
break;
}
matchTotal += match;
}
return matchTotal;
}
// pyjmethod_call. where the magic happens.
//
// okay, some of the magic -- we already the methodId, so we don't have
// to reflect. we just have to parse the arguments from python,
// check them against the java args, and call the java function.
//
// easy. :-)
static PyObject* pyjmethod_call(PyJMethodObject *self,
PyObject *args,
PyObject *keywords)
{
JNIEnv *env = NULL;
Py_ssize_t lenPyArgsGiven = 0;
int lenJArgsExpected = 0;
/* The number of normal arguments before any varargs */
int lenJArgsNormal = 0;
PyObject *firstArg = NULL;
PyJObject *instance = NULL;
PyObject *result = NULL;
int pos = 0;
jvalue *jargs = NULL;
/* if params includes pyjarray instance */
int foundArray = 0;
if (keywords != NULL && PyDict_Size(keywords) > 0) {
PyErr_Format(PyExc_RuntimeError, "Keywords are not supported.");
return NULL;
}
lenPyArgsGiven = PyTuple_Size(args);
env = pyembed_get_env();
lenJArgsExpected = PyJMethod_GetParameterCount(self, env);
if (lenJArgsExpected == -1) {
return NULL;
}
/* Python gives one more arg than java expects for self/this. */
if (lenJArgsExpected != lenPyArgsGiven - 1) {
jboolean varargs = java_lang_reflect_Method_isVarArgs(env, self->rmethod);
if (process_java_exception(env)) {
return NULL;
}
if (!varargs || lenJArgsExpected > lenPyArgsGiven) {
PyErr_Format(PyExc_RuntimeError,
"Invalid number of arguments: %i, expected %i.",
(int) lenPyArgsGiven,
lenJArgsExpected + 1);
return NULL;
}
/* The last argument will be handled as varargs, so not a normal arg */
lenJArgsNormal = lenJArgsExpected - 1;
} else {
/* No varargs, all args are normal */
lenJArgsNormal = lenJArgsExpected;
}
firstArg = PyTuple_GetItem(args, 0);
if (!PyJObject_Check(firstArg)) {
PyErr_SetString(PyExc_RuntimeError,
"First argument to a java method must be a java object.");
return NULL;
}
instance = (PyJObject*) firstArg;
// validate we can call this method
if (!instance->object && self->isStatic != JNI_TRUE) {
PyErr_Format(PyExc_RuntimeError,
"Instantiate this class before "
"calling an object method.");
return NULL;
}
if ((*env)->PushLocalFrame(env, JLOCAL_REFS + lenJArgsExpected) != 0) {
process_java_exception(env);
return NULL;
}
jargs = (jvalue *) PyMem_Malloc(sizeof(jvalue) * lenJArgsExpected);
if (jargs == NULL) {
(*env)->PopLocalFrame(env, NULL);
return PyErr_NoMemory();
}
for (pos = 0; pos < lenJArgsNormal; pos++) {
PyObject *param = NULL;
int paramTypeId = -1;
jclass paramType = (jclass) (*env)->GetObjectArrayElement(env,
self->parameters, pos);
param = PyTuple_GetItem(args, pos + 1);
if (PyErr_Occurred()) {
goto EXIT_ERROR;
}
paramTypeId = get_jtype(env, paramType);
if (paramTypeId == JARRAY_ID) {
foundArray = 1;
}
jargs[pos] = convert_pyarg_jvalue(env, param, paramType, paramTypeId, pos);
if (PyErr_Occurred()) {
if (pos == (lenJArgsExpected - 1)
&& PyErr_ExceptionMatches(PyExc_TypeError)) {
jboolean varargs = java_lang_reflect_Method_isVarArgs(env, self->rmethod);
if ((*env)->ExceptionOccurred(env)) {
/* Cannot convert to python since there is already a python exception */
(*env)->ExceptionClear(env);
goto EXIT_ERROR;
}
if (varargs) {
/* Retry the last arg as array for varargs */
PyErr_Clear();
lenJArgsNormal -= 1;
} else {
goto EXIT_ERROR;
}
}
}
(*env)->DeleteLocalRef(env, paramType);
}
if (lenJArgsNormal + 1 == lenJArgsExpected) {
/* Need to process last arg as varargs. */
PyObject *param = NULL;
jclass paramType = (jclass) (*env)->GetObjectArrayElement(env,
self->parameters, lenJArgsExpected - 1);
if (lenPyArgsGiven == lenJArgsExpected) {
/*
* Python args are normally one longer than expected to allow for
* this/self so if it isn't then nothing was given for the varargs
*/
param = PyTuple_New(0);
} else {
param = PyTuple_GetSlice(args, lenJArgsExpected, lenPyArgsGiven);
}
if (PyErr_Occurred()) {
goto EXIT_ERROR;
}
jargs[lenJArgsExpected - 1] = convert_pyarg_jvalue(env, param, paramType,
JARRAY_ID,
lenJArgsExpected - 1);
Py_DecRef(param);
if (PyErr_Occurred()) {
goto EXIT_ERROR;
}
(*env)->DeleteLocalRef(env, paramType);
}
// ------------------------------ call based off return type
switch (self->returnTypeId) {
case JSTRING_ID: {
jstring jstr;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
jstr = (jstring) (*env)->CallStaticObjectMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
// not static, a method on class then.
if (!instance->object)
jstr = (jstring) (*env)->CallObjectMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else
jstr = (jstring) (*env)->CallObjectMethodA(
env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env) && jstr != NULL) {
result = jstring_As_PyString(env, jstr);
(*env)->DeleteLocalRef(env, jstr);
}
break;
}
case JARRAY_ID: {
jobjectArray obj;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
obj = (jobjectArray) (*env)->CallStaticObjectMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
obj = (jobjectArray) (*env)->CallObjectMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else
obj = (jobjectArray) (*env)->CallObjectMethodA(
env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env) && obj != NULL) {
result = pyjarray_new(env, obj);
}
break;
}
case JCLASS_ID: {
jobject obj;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
obj = (*env)->CallStaticObjectMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
obj = (*env)->CallObjectMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
obj = (*env)->CallObjectMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env) && obj != NULL) {
result = PyJClass_Wrap(env, obj);
}
break;
}
case JOBJECT_ID: {
jobject obj;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
obj = (*env)->CallStaticObjectMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
obj = (*env)->CallObjectMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
obj = (*env)->CallObjectMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env) && obj != NULL) {
result = jobject_As_PyObject(env, obj);
}
break;
}
case JINT_ID: {
jint ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticIntMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallIntMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallIntMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jint_As_PyObject(ret);
}
break;
}
case JBYTE_ID: {
jbyte ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticByteMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallByteMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallByteMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jbyte_As_PyObject(ret);
}
break;
}
case JCHAR_ID: {
jchar ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticCharMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallCharMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallCharMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jchar_As_PyObject(ret);
}
break;
}
case JSHORT_ID: {
jshort ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticShortMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallShortMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallShortMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jshort_As_PyObject(ret);
}
break;
}
case JDOUBLE_ID: {
jdouble ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticDoubleMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallDoubleMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallDoubleMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jdouble_As_PyObject(ret);
}
break;
}
case JFLOAT_ID: {
jfloat ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticFloatMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallFloatMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallFloatMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jfloat_As_PyObject(ret);
}
break;
}
case JLONG_ID: {
jlong ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticLongMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallLongMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallLongMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jlong_As_PyObject(ret);
}
break;
}
case JBOOLEAN_ID: {
jboolean ret;
Py_BEGIN_ALLOW_THREADS;
if (self->isStatic)
ret = (*env)->CallStaticBooleanMethodA(
env,
instance->clazz,
self->methodId,
jargs);
else {
if (!instance->object)
ret = (*env)->CallBooleanMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallBooleanMethodA(env,
instance->object,
self->methodId,
jargs);
}
Py_END_ALLOW_THREADS;
if (!process_java_exception(env)) {
result = jboolean_As_PyObject(ret);
}
break;
}
default:
Py_BEGIN_ALLOW_THREADS;
// i hereby anoint thee a void method
if (self->isStatic)
(*env)->CallStaticVoidMethodA(env,
instance->clazz,
self->methodId,
jargs);
else
(*env)->CallVoidMethodA(env,
instance->object,
self->methodId,
jargs);
Py_END_ALLOW_THREADS;
process_java_exception(env);
break;
}
PyMem_Free(jargs);
(*env)->PopLocalFrame(env, NULL);
if (PyErr_Occurred()) {
return NULL;
}
// re pin array objects if needed
if (foundArray) {
for (pos = 0; pos < lenJArgsNormal; pos++) {
PyObject *param = PyTuple_GetItem(args, pos + 1); /* borrowed */
if (param && pyjarray_check(param)) {
pyjarray_pin((PyJArrayObject *) param);
}
}
}
if (result == NULL) {
Py_RETURN_NONE;
}
return result;
EXIT_ERROR:
PyMem_Free(jargs);
(*env)->PopLocalFrame(env, NULL);
return NULL;
}
static PyObject* pyjmethod_descr_get(PyObject *func, PyObject *obj,
PyObject *type)
{
if (obj == Py_None || obj == NULL) {
Py_INCREF(func);
return func;
}
return PyMethod_New(func, obj);
}
static PyMemberDef pyjmethod_members[] = {
{
"__name__", T_OBJECT_EX, offsetof(PyJMethodObject, pyMethodName), READONLY,
"method name"
},
{NULL} /* Sentinel */
};
PyTypeObject PyJMethod_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"jep.PyJMethod",
sizeof(PyJMethodObject),
0,
(destructor) pyjmethod_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc) pyjmethod_call, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"jmethod", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
pyjmethod_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
pyjmethod_descr_get, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
NULL, /* tp_new */
};