From d3525a34bb793ff77cc62149449a0bbfacf057c1 Mon Sep 17 00:00:00 2001 From: Craig Macdonald Date: Sun, 1 Oct 2023 14:23:04 +0100 Subject: [PATCH] Remove pkg_resources for Python >=3.9 (#673) * WIP: addresses pkg_resources deprecation * more fixes * attempt for py3.9 * paths to strings * One path not many * Update jnius_config.py - use path obj * Be verbose during testing * Print for baseline too * Remove prints * drop unused import * revert pytest change * address review feedback --- jnius_config.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/jnius_config.py b/jnius_config.py index c5f1ad3..3ade536 100644 --- a/jnius_config.py +++ b/jnius_config.py @@ -66,11 +66,27 @@ def get_classpath(): "Retrieves the classpath the JVM will use." from os import environ from os.path import realpath + import sys global classpath # add a path to java classes packaged with jnius - from pkg_resources import resource_filename - return_classpath = [realpath(resource_filename(__name__, 'jnius/src'))] + if sys.version_info >= (3, 9): + from contextlib import ExitStack + import importlib.resources + import atexit + + # see https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-filename + file_manager = ExitStack() + atexit.register(file_manager.close) + # importlib.resources.files is only available from Python 3.9 + # use https://github.com/python/importlib_resources/issues/60 not __name__ as jnius_config.py is not a package + resource_path = importlib.resources.files('jnius') / 'src' + return_classpath = [ str(resource_path) ] + file_manager.enter_context(importlib.resources.as_file(resource_path)) + else: + from pkg_resources import resource_filename + return_classpath = [realpath(resource_filename(__name__, 'jnius/src'))] + if classpath is not None: return_classpath = classpath + return_classpath