From a99fa3375daeac28a941d817a3d474c2debd8b3a Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 4 Oct 2024 22:37:29 +0000 Subject: [PATCH] the impresources.files traversible is not threadsafe --- copyparty/util.py | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/copyparty/util.py b/copyparty/util.py index c51a7f8f..1e99092d 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -3588,6 +3588,10 @@ def hidedir(dp) -> None: try: + if sys.version_info < (3, 10): + # py3.8 doesn't have .files + # py3.9 has broken .is_file + raise ImportError() import importlib.resources as impresources except ImportError: try: @@ -3618,37 +3622,17 @@ def stat_resource(E: EnvParams, name: str): return None -def _find_impresource_cold(E: EnvParams, name: str): - global _rescache_imp, _find_impresource - +def _find_impresource(E: EnvParams, name: str): assert impresources # !rm try: - _rescache_imp = impresources.files(E.pkg) + files = impresources.files(E.pkg) except ImportError: return None - _find_impresource = _find_impresource_warm - return _find_impresource(E, name) + return files.joinpath(name) -def _find_impresource_warm(E: EnvParams, name: str): - if not _rescache_imp: - return None - - try: - return _rescache_res[name] - except: - if len(_rescache_res) > 999: - _rescache_res.clear() - ret = _rescache_imp.joinpath(name) - _rescache_res[name] = ret - return ret - - -_find_impresource = _find_impresource_cold -_rescache_imp = None _rescache_has = {} -_rescache_res = {} def _has_resource(E: EnvParams, name: str):