Skip to content

Commit 8407fc7

Browse files
authored
Remove python long to support Cython >= 3.1.x (#756)
* remove long mentions * change debug=True output
1 parent f6050f0 commit 8407fc7

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

jnius/jnius_conversion.pxi

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar
3838
cdef JavaClass jc
3939
cdef PythonJavaClass pc
4040
cdef int index
41-
from ctypes import c_long as long
4241

4342
for index, argtype in enumerate(definition_args):
4443
py_arg = args[index]
@@ -63,7 +62,7 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar
6362
j_args[index].l = NULL
6463

6564
# numeric types
66-
elif isinstance(py_arg, (int, long)):
65+
elif isinstance(py_arg, int):
6766
j_args[index].l = convert_python_to_jobject(
6867
j_env, 'Ljava/lang/Integer;', py_arg
6968
)
@@ -468,7 +467,6 @@ cdef jobject convert_python_to_jobject(JNIEnv *j_env, definition, obj) except *:
468467
cdef JavaClassStorage jcs
469468
cdef PythonJavaClass pc
470469
cdef int index
471-
from ctypes import c_long as long
472470

473471
if definition[0] == 'V':
474472
return NULL
@@ -481,7 +479,7 @@ cdef jobject convert_python_to_jobject(JNIEnv *j_env, definition, obj) except *:
481479
return convert_pystr_to_java(j_env, to_unicode(obj))
482480

483481
# numeric types
484-
elif isinstance(obj, (int, long)) and \
482+
elif isinstance(obj, int) and \
485483
definition in (
486484
'Ljava/lang/Integer;',
487485
'Ljava/lang/Number;',
@@ -543,7 +541,6 @@ cdef jobject convert_python_to_jobject(JNIEnv *j_env, definition, obj) except *:
543541
conversions = {
544542
int: 'I',
545543
bool: 'Z',
546-
long: 'J',
547544
float: 'F',
548545
unicode: 'Ljava/lang/String;',
549546
bytes: 'B'
@@ -634,7 +631,6 @@ cdef jobject convert_pyarray_to_java(JNIEnv *j_env, definition, pyarray) except
634631
cdef jclass j_class
635632
cdef JavaObject jo
636633
cdef JavaClass jc
637-
from ctypes import c_long as long
638634

639635
cdef ByteArray a_bytes
640636

@@ -644,7 +640,6 @@ cdef jobject convert_pyarray_to_java(JNIEnv *j_env, definition, pyarray) except
644640
conversions = {
645641
int: 'I',
646642
bool: 'Z',
647-
long: 'J',
648643
float: 'F',
649644
bytes: 'B',
650645
str: 'Ljava/lang/String;',

jnius/jnius_export_class.pxi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ cdef class JavaClass(object):
292292
cdef jmethodID constructor = NULL
293293
cdef JNIEnv *j_env = get_jnienv()
294294
cdef list found_definitions = []
295+
debug = kwargs.get("debug", False)
295296

296297
# get the constructor definition if exist
297298
definitions = [('()V', False)]
@@ -357,6 +358,9 @@ cdef class JavaClass(object):
357358
)
358359
scores.sort()
359360
score, definition, d_ret, d_args, args_ = scores[-1]
361+
if debug:
362+
print(scores)
363+
print("Selected %s for invocation" % definition)
360364

361365
try:
362366
# convert python arguments to java arguments
@@ -1131,6 +1135,7 @@ cdef class JavaMultipleMethod(object):
11311135
cdef dict methods
11321136
cdef int max_sign_args
11331137
cdef list found_signatures = []
1138+
debug = kwargs.get("debug", False)
11341139

11351140
if self.j_self:
11361141
methods = self.instance_methods
@@ -1166,6 +1171,9 @@ cdef class JavaMultipleMethod(object):
11661171
)
11671172
scores.sort()
11681173
score, signature = scores[-1]
1174+
if debug:
1175+
print(scores)
1176+
print("Selected %s for invocation" % signature)
11691177

11701178
jm = methods[signature]
11711179
jm.j_self = self.j_self

jnius/jnius_utils.pxi

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
278278
cdef JavaClass jc
279279
cdef int args_len = len(args)
280280
cdef int sign_args_len = len(sign_args)
281-
from ctypes import c_long as long
282281

283282
if args_len != sign_args_len and not is_varargs:
284283
# if the number of arguments expected is not the same
@@ -301,27 +300,26 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
301300
r = sign_args[index]
302301
arg = args[index]
303302

304-
if r == 'Z':
303+
if r == 'Z': # boolean
305304
if not isinstance(arg, bool):
306305
return -1
307306
score += 10
308307
continue
309308

310-
if r == 'B':
309+
if r == 'B': # byte
311310
if not isinstance(arg, int):
312311
return -1
313312
score += 10
314313
continue
315314

316-
if r == 'C':
315+
if r == 'C': # char
317316
if not isinstance(arg, str) or len(arg) != 1:
318317
return -1
319318
score += 10
320319
continue
321320

322-
if r == 'S' or r == 'I':
323-
if isinstance(arg, int) or (
324-
(isinstance(arg, long) and arg < 2147483648)):
321+
if r == 'S': # short
322+
if isinstance(arg, int) and arg <= 32767 and arg >= -32768:
325323
score += 10
326324
continue
327325
elif isinstance(arg, float):
@@ -330,8 +328,8 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
330328
else:
331329
return -1
332330

333-
if r == 'J':
334-
if isinstance(arg, int) or isinstance(arg, long):
331+
if r == 'I': # int
332+
if isinstance(arg, int) and arg <= 2147483647 and arg >= -2147483648:
335333
score += 10
336334
continue
337335
elif isinstance(arg, float):
@@ -340,7 +338,17 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
340338
else:
341339
return -1
342340

343-
if r == 'F' or r == 'D':
341+
if r == 'J': # long
342+
if isinstance(arg, int):
343+
score += 10
344+
continue
345+
elif isinstance(arg, float):
346+
score += 5
347+
continue
348+
else:
349+
return -1
350+
351+
if r == 'F' or r == 'D': # float or double
344352
if isinstance(arg, int):
345353
score += 5
346354
continue
@@ -350,7 +358,7 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
350358
else:
351359
return -1
352360

353-
if r[0] == 'L':
361+
if r[0] == 'L': # classname
354362

355363
r = r[1:-1]
356364

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jnius;
2+
3+
public class SignatureTest {
4+
5+
public static class IntOrLong {
6+
boolean was_long;
7+
public IntOrLong(int i) { was_long = false; }
8+
public IntOrLong(long l) { was_long = true; }
9+
}
10+
11+
public static class ShortOrLong {
12+
boolean was_short;
13+
public ShortOrLong(short i) { was_short = true; }
14+
public ShortOrLong(long i) { was_short = false; }
15+
}
16+
17+
public static class ShortOnly {
18+
public ShortOnly(short i) { }
19+
public ShortOnly(boolean o) { } // we need alternative constructor to force calculate_score
20+
}
21+
22+
}

tests/test_signature.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,26 @@ def test_params(self):
172172
sig = signature(jvoid, [JArray(jint), JArray(jboolean)])
173173
self.assertEqual(sig, "([I[Z)V")
174174

175+
def test_calc_signature(self):
176+
import sys
177+
clz = autoclass("org.jnius.SignatureTest$IntOrLong")
178+
obj = clz(0, debug=True) # could be int or long
179+
# this isnt truly deterministic, the two possible methods are tied for score
180+
self.assertTrue(obj.was_long)
181+
182+
obj = clz(sys.maxsize)
183+
self.assertTrue(obj.was_long)
184+
185+
obj = clz(-1 * sys.maxsize)
186+
self.assertTrue(obj.was_long)
187+
188+
clz = autoclass("org.jnius.SignatureTest$ShortOrLong")
189+
obj = clz(0) # could be short or long
190+
# this isnt truly deterministic, the two possible methods are tied for score
191+
self.assertTrue(obj.was_short)
192+
193+
obj = clz(sys.maxsize)
194+
self.assertFalse(obj.was_short)
195+
196+
autoclass("org.jnius.SignatureTest$ShortOnly")(0) # this should work as short
197+
autoclass("org.jnius.SignatureTest$ShortOnly")(0.) # this float should be cast to short

0 commit comments

Comments
 (0)