Skip to content

Commit

Permalink
Remove Python 2 support, six dependency (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdb committed Oct 16, 2022
1 parent 453b9b0 commit ee4e9c2
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 462 deletions.
4 changes: 1 addition & 3 deletions jnius/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@
from .jnius import * # noqa
from .reflect import * # noqa

from six import with_metaclass

# XXX monkey patch methods that cannot be in cython.
# Cython doesn't allow to set new attribute on methods it compiled

HASHCODE_MAX = 2 ** 31 - 1


class PythonJavaClass_(with_metaclass(MetaJavaBase, PythonJavaClass)):
class PythonJavaClass_(PythonJavaClass, metaclass=MetaJavaBase):

@java_method('()I', name='hashCode')
def hashCode(self):
Expand Down
12 changes: 2 additions & 10 deletions jnius/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@
from shlex import split
from logging import getLogger
from textwrap import dedent
from shutil import which

log = getLogger(__name__)

PY2 = sys.version_info.major < 3

if PY2:
from distutils.spawn import find_executable as which
else:
from shutil import which

machine = machine() # not expected to change at runtime

# This dictionary converts values from platform.machine()
Expand Down Expand Up @@ -368,9 +362,7 @@ def get_osx_framework():
stdout=PIPE, shell=True
).communicate()[0]

if not PY2:
framework = framework.decode('utf-8')

framework = framework.decode('utf-8')
return framework.strip()


Expand Down
10 changes: 1 addition & 9 deletions jnius/jnius_compat.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@
Handle Python 2 vs 3 differences here.
'''

from cpython.version cimport PY_MAJOR_VERSION

cdef int PY2 = PY_MAJOR_VERSION < 3

# because Cython's basestring doesn't work with isinstance() properly
# and has differences between Python 2 and Python 3 runtime behavior
# so it's not really usable unless some bug in the upstream is fixed
# (tested with Cython==0.29.2)
cdef tuple base_string
if PY_MAJOR_VERSION < 3:
base_string = (bytes, unicode)
else:
base_string = (bytes, str)
cdef tuple base_string = (bytes, str)


cdef unicode to_unicode(object arg):
Expand Down
70 changes: 19 additions & 51 deletions jnius/jnius_conversion.pxi
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from cpython.version cimport PY_MAJOR_VERSION

activeLambdaJavaProxies = set()

cdef jstringy_arg(argtype):
Expand Down Expand Up @@ -142,12 +140,7 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar
if py_arg is None:
j_args[index].l = NULL
continue
if isinstance(py_arg, basestring) and PY_MAJOR_VERSION < 3:
if argtype == '[B':
py_arg = map(ord, py_arg)
elif argtype == '[C':
py_arg = list(py_arg)
if isinstance(py_arg, str) and PY_MAJOR_VERSION >= 3 and argtype == '[C':
if isinstance(py_arg, str) and argtype == '[C':
py_arg = list(py_arg)
if isinstance(py_arg, ByteArray) and argtype != '[B':
raise JavaException(
Expand Down Expand Up @@ -260,10 +253,7 @@ cdef convert_jstring_to_python(JNIEnv *j_env, jstring j_string):
finally:
j_env[0].ReleaseStringChars(j_env, j_string, j_chars)

if PY_MAJOR_VERSION < 3:
return py_uni.encode('utf-8')
else:
return py_uni
return py_uni

cdef convert_jarray_to_python(JNIEnv *j_env, definition, jobject j_object):
cdef jboolean iscopy
Expand Down Expand Up @@ -311,10 +301,7 @@ cdef convert_jarray_to_python(JNIEnv *j_env, definition, jobject j_object):
j_chars = j_env[0].GetCharArrayElements(
j_env, j_object, &iscopy)

if PY_MAJOR_VERSION < 3:
ret = [chr(<char>j_chars[i]) for i in range(array_size)]
else:
ret = [chr(j_chars[i]) for i in range(array_size)]
ret = [chr(j_chars[i]) for i in range(array_size)]
j_env[0].ReleaseCharArrayElements(
j_env, j_object, j_chars, 0)

Expand Down Expand Up @@ -551,24 +538,14 @@ cdef jobject convert_python_to_jobject(JNIEnv *j_env, definition, obj) except *:
definition[1:-1], obj))

elif definition[0] == '[':
if PY_MAJOR_VERSION < 3:
conversions = {
int: 'I',
bool: 'Z',
long: 'J',
float: 'F',
unicode: 'Ljava/lang/String;',
bytes: 'Ljava/lang/String;'
}
else:
conversions = {
int: 'I',
bool: 'Z',
long: 'J',
float: 'F',
unicode: 'Ljava/lang/String;',
bytes: 'B'
}
conversions = {
int: 'I',
bool: 'Z',
long: 'J',
float: 'F',
unicode: 'Ljava/lang/String;',
bytes: 'B'
}
retclass = j_env[0].FindClass(j_env, 'java/lang/Object')
retobject = j_env[0].NewObjectArray(j_env, len(obj), retclass, NULL)
for index, item in enumerate(obj):
Expand Down Expand Up @@ -661,23 +638,14 @@ cdef jobject convert_pyarray_to_java(JNIEnv *j_env, definition, pyarray) except
if definition == 'Ljava/lang/Object;' and len(pyarray) > 0:
# then the method will accept any array type as param
# let's be as precise as we can
if PY_MAJOR_VERSION < 3:
conversions = {
int: 'I',
bool: 'Z',
long: 'J',
float: 'F',
basestring: 'Ljava/lang/String;',
}
else:
conversions = {
int: 'I',
bool: 'Z',
long: 'J',
float: 'F',
bytes: 'B',
str: 'Ljava/lang/String;',
}
conversions = {
int: 'I',
bool: 'Z',
long: 'J',
float: 'F',
bytes: 'B',
str: 'Ljava/lang/String;',
}
for _type, override in conversions.iteritems():
if isinstance(pyarray[0], _type):
definition = override
Expand Down
31 changes: 9 additions & 22 deletions jnius/jnius_export_class.pxi
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from cpython cimport PyObject
from cpython.version cimport PY_MAJOR_VERSION
from warnings import warn


Expand Down Expand Up @@ -225,7 +224,7 @@ class MetaJavaClass(MetaJavaBase):
cdef JavaMethod jm
cdef JavaMultipleMethod jmm
cdef jboolean resolve_static = True
for name, value in items_compat(classDict):
for name, value in classDict.items():
if isinstance(value, JavaMethod):
jm = value
if not jm.is_static:
Expand All @@ -239,7 +238,7 @@ class MetaJavaClass(MetaJavaBase):

# search all the static JavaField within our class, and resolve them
cdef JavaField jf
for name, value in items_compat(classDict):
for name, value in classDict.items():
if not isinstance(value, JavaField):
continue
jf = value
Expand Down Expand Up @@ -403,7 +402,7 @@ cdef class JavaClass(object):
cdef JavaMultipleMethod jmm
cdef JNIEnv *j_env = get_jnienv()
cdef jboolean resolve_static = False
for name, value in items_compat(self.__class__.__dict__):
for name, value in self.__class__.__dict__.items():
if isinstance(value, JavaMethod):
jm = value
if jm.is_static:
Expand All @@ -419,7 +418,7 @@ cdef class JavaClass(object):
# search all the JavaField within our class, and resolve them
cdef JavaField jf
cdef JNIEnv *j_env = get_jnienv()
for name, value in items_compat(self.__class__.__dict__):
for name, value in self.__class__.__dict__.items():
if not isinstance(value, JavaField):
continue
jf = value
Expand Down Expand Up @@ -586,10 +585,7 @@ cdef class JavaField(object):
elif r == 'C':
j_char = j_env[0].GetCharField(
j_env, j_self, self.j_field)
if PY_MAJOR_VERSION < 3:
ret = chr(<char>j_char)
else:
ret = chr(j_char)
ret = chr(j_char)
elif r == 'S':
j_short = j_env[0].GetShortField(
j_env, j_self, self.j_field)
Expand Down Expand Up @@ -713,10 +709,7 @@ cdef class JavaField(object):
elif r == 'C':
j_char = j_env[0].GetStaticCharField(
j_env, self.j_cls, self.j_field)
if PY_MAJOR_VERSION < 3:
ret = chr(<char>j_char)
else:
ret = chr(j_char)
ret = chr(j_char)
elif r == 'S':
j_short = j_env[0].GetStaticShortField(
j_env, self.j_cls, self.j_field)
Expand Down Expand Up @@ -929,10 +922,7 @@ cdef class JavaMethod(object):
with nogil:
j_char = j_env[0].CallCharMethodA(
j_env, j_self, self.j_method, j_args)
if PY_MAJOR_VERSION < 3:
ret = chr(<char>j_char)
else:
ret = chr(j_char)
ret = chr(j_char)
elif r == 'S':
with nogil:
j_short = j_env[0].CallShortMethodA(
Expand Down Expand Up @@ -1020,10 +1010,7 @@ cdef class JavaMethod(object):
with nogil:
j_char = j_env[0].CallStaticCharMethodA(
j_env, self.j_cls, self.j_method, j_args)
if PY_MAJOR_VERSION < 3:
ret = chr(<char>j_char)
else:
ret = chr(j_char)
ret = chr(j_char)
elif r == 'S':
with nogil:
j_short = j_env[0].CallStaticShortMethodA(
Expand Down Expand Up @@ -1142,7 +1129,7 @@ cdef class JavaMultipleMethod(object):
else:
methods = self.static_methods

for signature, jm in items_compat(methods):
for signature, jm in methods.items():
# store signatures for the exception
found_signatures.append(signature)

Expand Down
6 changes: 1 addition & 5 deletions jnius/jnius_export_func.pxi
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from cpython.version cimport PY_MAJOR_VERSION


def cast(destclass, obj):
cdef JavaClass jc
cdef JavaClass jobj = obj
from .reflect import autoclass
if (PY_MAJOR_VERSION < 3 and isinstance(destclass, base_string)) or \
(PY_MAJOR_VERSION >=3 and isinstance(destclass, str)):
if isinstance(destclass, str):
jc = autoclass(destclass)(noinstance=True)
else:
jc = destclass(noinstance=True)
Expand Down
1 change: 0 additions & 1 deletion jnius/jnius_jvm_desktop.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import sys
import os
from os.path import join
from jnius.env import get_java_setup
from cpython.version cimport PY_MAJOR_VERSION

# on desktop, we need to create an env :)
# example taken from http://www.inonit.com/cygwin/jni/invocationApi/c.html
Expand Down
4 changes: 1 addition & 3 deletions jnius/jnius_proxy.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ cdef jobject py_invoke0(JNIEnv *j_env, jobject j_this, jobject j_proxy, jobject
if ret_signature == 'Ljava/lang/Object;':
# generic object, try to manually convert it
tp = type(ret)
if PY2 and tp == int:
jtype = 'I'
elif (PY2 and tp == long) or tp == int:
if tp == int:
jtype = 'J'
elif tp == float:
jtype = 'D'
Expand Down
28 changes: 4 additions & 24 deletions jnius/jnius_utils.pxi
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
cdef str_for_c(s):
if PY2:
if isinstance(s, unicode):
return s.encode('utf-8')
else:
return s
else:
return s.encode('utf-8')

cdef items_compat(d):
if not PY2:
return d.items()
else:
return d.iteritems()
return s.encode('utf-8')

cdef parse_definition(definition):
# not a function, just a field
Expand Down Expand Up @@ -370,11 +358,7 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
continue

# if it's a string, accept any python string
if r == 'java/lang/String' and isinstance(arg, base_string) and PY2:
score += 10
continue

if r == 'java/lang/String' and isinstance(arg, str) and not PY2:
if r == 'java/lang/String' and isinstance(arg, str):
score += 10
continue

Expand Down Expand Up @@ -440,15 +424,11 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
score += 10
continue

if (r == '[B' or r == '[C') and isinstance(arg, base_string) and PY2:
score += 10
continue

if (r == '[B') and isinstance(arg, bytes) and not PY2:
if (r == '[B') and isinstance(arg, bytes):
score += 10
continue

if (r == '[C') and isinstance(arg, str) and not PY2:
if (r == '[C') and isinstance(arg, str):
score += 10
continue

Expand Down

0 comments on commit ee4e9c2

Please sign in to comment.