fix import in python 3.8/windows

This commit is contained in:
Gabriel Pettier 2019-11-29 22:39:32 +01:00
parent 4a60858133
commit 9ee1183cda
6 changed files with 68 additions and 11 deletions

View File

@ -22,8 +22,6 @@ jobs:
# exclude problematic combinations
exclude:
- os: windows-latest
python: '3.8'
- os: windows-latest
python: '2.7'
- os: macOs-latest

View File

@ -7,7 +7,7 @@ jobs:
matrix:
python:
- '2.7'
# - '3.6'
- '3.6'
- '3.7'
- '3.8'
java:
@ -26,8 +26,6 @@ jobs:
# exclude problematic combinations
exclude:
- os: windows-latest
python: '3.8'
- os: windows-latest
python: '2.7'
- os: macOs-latest

View File

@ -9,9 +9,38 @@ All the documentation is available at: http://pyjnius.readthedocs.org
__version__ = '1.2.1.dev3'
from .env import get_jnius_lib_location
from .jnius import * # noqa
from .reflect import * # noqa
from .env import get_jnius_lib_location, get_jdk_home
import os
import sys
if sys.platform == 'win32' and sys.version_info >= (3, 8):
path = os.path.dirname(__file__)
jdk_home = get_jdk_home(sys.platform)
with os.add_dll_directory(path):
for suffix in (
('bin', 'client'),
('bin', 'server'),
('jre', 'bin', 'client'),
('jre', 'bin', 'server'),
):
path = os.path.join(jdk_home, *suffix)
if not os.path.isdir(path):
continue
with os.add_dll_directory(path):
try:
from .jnius import * # noqa
from .reflect import * # noqa
except Exception as e:
pass
else:
break
else:
raise Exception("Unable to create jni env, no jvm dll found.")
else:
from .jnius import * # noqa
from .reflect import * # noqa
from six import with_metaclass
# XXX monkey patch methods that cannot be in cython.

View File

@ -10,6 +10,7 @@ from platform import machine
from subprocess import Popen, check_output, PIPE
from shlex import split
PY2 = sys.version_info.major < 3
machine = machine() # not expected to change at runtime
@ -140,6 +141,7 @@ def get_jdk_home(platform):
return jdk_home
def get_osx_framework():
framework = Popen(
'/usr/libexec/java_home',

View File

@ -1,3 +1,7 @@
import sys
import os
from os.path import join
from jnius.env import get_jdk_home
from cpython.version cimport PY_MAJOR_VERSION
# on desktop, we need to create an env :)
@ -43,7 +47,31 @@ cdef void create_jnienv() except *:
args.nOptions = len(optarr)
args.ignoreUnrecognized = JNI_FALSE
ret = JNI_CreateJavaVM(&jvm, <void **>&_platform_default_env, &args)
if sys.version_info >= (3, 8):
# uh, let's see if this works and cleanup later
jdk_home = get_jdk_home('win32')
for suffix in (
('bin', 'client'),
('bin', 'server'),
('jre', 'bin', 'client'),
('jre', 'bin', 'server'),
):
path = join(jdk_home, *suffix)
if not os.path.isdir(path):
continue
with os.add_dll_directory(path):
try:
ret = JNI_CreateJavaVM(&jvm, <void **>&_platform_default_env, &args)
except Exception as e:
pass
else:
break
else:
raise Exception("Unable to create jni env, no jvm dll found.")
else:
ret = JNI_CreateJavaVM(&jvm, <void **>&_platform_default_env, &args)
free(options)
if ret != JNI_OK:

View File

@ -2,7 +2,7 @@ include "config.pxi"
import os
from shlex import split
from subprocess import check_output
from os.path import dirname
from os.path import dirname, join
from os import readlink
from sys import platform
from .env import get_jnius_lib_location
@ -64,6 +64,7 @@ cdef void create_jnienv() except *:
cdef JavaVMOption *options
cdef int ret
cdef bytes py_bytes
cdef void *handle
import jnius_config
JAVA_HOME = os.getenv('JAVA_HOME') or find_java_home()
@ -80,7 +81,8 @@ cdef void create_jnienv() except *:
ELSE:
lib_path = str_for_c(os.path.join(JAVA_HOME, JNIUS_LIB_SUFFIX))
cdef void *handle = dlopen(lib_path, RTLD_NOW | RTLD_GLOBAL)
handle = dlopen(lib_path, RTLD_NOW | RTLD_GLOBAL)
if handle == NULL:
raise SystemError("Error calling dlopen({0}: {1}".format(lib_path, dlerror()))