2012-08-20 16:47:44 +00:00
|
|
|
'''
|
|
|
|
Pyjnius
|
|
|
|
=======
|
|
|
|
|
|
|
|
Accessing Java classes from Python.
|
|
|
|
|
|
|
|
All the documentation is available at: http://pyjnius.readthedocs.org
|
|
|
|
'''
|
|
|
|
|
2017-03-23 20:04:39 +00:00
|
|
|
__version__ = '1.1.1-dev'
|
2012-08-20 16:47:44 +00:00
|
|
|
|
2017-03-23 17:56:02 +00:00
|
|
|
from .jnius import * # noqa
|
|
|
|
from .reflect import * # noqa
|
2014-07-31 11:49:25 +00:00
|
|
|
|
|
|
|
# XXX monkey patch methods that cannot be in cython.
|
|
|
|
# Cython doesn't allow to set new attribute on methods it compiled
|
|
|
|
|
2015-03-02 10:22:05 +00:00
|
|
|
HASHCODE_MAX = 2 ** 31 - 1
|
|
|
|
|
2017-03-23 17:56:02 +00:00
|
|
|
|
2014-07-31 11:49:25 +00:00
|
|
|
class PythonJavaClass_(PythonJavaClass):
|
|
|
|
|
|
|
|
@java_method('()I', name='hashCode')
|
|
|
|
def hashCode(self):
|
2015-03-02 10:22:05 +00:00
|
|
|
return id(self) % HASHCODE_MAX
|
2014-07-31 11:49:25 +00:00
|
|
|
|
|
|
|
@java_method('()Ljava/lang/String;', name='hashCode')
|
|
|
|
def hashCode_(self):
|
2015-03-02 10:22:05 +00:00
|
|
|
return '{}'.format(self.hashCode())
|
2014-07-31 11:49:25 +00:00
|
|
|
|
|
|
|
@java_method('()Ljava/lang/String;', name='toString')
|
|
|
|
def toString(self):
|
|
|
|
return repr(self)
|
|
|
|
|
|
|
|
@java_method('(Ljava/lang/Object;)Z', name='equals')
|
|
|
|
def equals(self, other):
|
|
|
|
return self.hashCode() == other.hashCode()
|
|
|
|
|
2017-03-23 17:56:02 +00:00
|
|
|
|
2014-07-31 11:49:25 +00:00
|
|
|
PythonJavaClass = PythonJavaClass_
|
2016-10-26 15:04:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# from https://gist.github.com/tito/09c42fb4767721dc323d
|
2016-10-27 06:12:14 +00:00
|
|
|
import os
|
2016-10-26 15:12:55 +00:00
|
|
|
if "ANDROID_ARGUMENT" in os.environ:
|
|
|
|
# on android, catch all exception to ensure about a jnius.detach
|
|
|
|
import threading
|
|
|
|
import jnius
|
|
|
|
orig_thread_run = threading.Thread.run
|
|
|
|
|
|
|
|
def jnius_thread_hook(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return orig_thread_run(*args, **kwargs)
|
|
|
|
finally:
|
|
|
|
jnius.detach()
|
|
|
|
|
|
|
|
threading.Thread.run = jnius_thread_hook
|