From 33bd3dfbfe7a189db201a984903c52c465332299 Mon Sep 17 00:00:00 2001 From: v-korovin Date: Thu, 29 Feb 2024 21:55:50 +0300 Subject: Probe bytes and str environment keys 58bed0cef82a2a691f8f949d16c3d3b03d94f627 --- library/python/runtime_py3/importer.pxi | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'library/python/runtime_py3') diff --git a/library/python/runtime_py3/importer.pxi b/library/python/runtime_py3/importer.pxi index 0bef940b709..493c17204d0 100644 --- a/library/python/runtime_py3/importer.pxi +++ b/library/python/runtime_py3/importer.pxi @@ -9,7 +9,6 @@ import __res as __resource _b = lambda x: x if isinstance(x, bytes) else utf_8_encode(x)[0] _s = lambda x: x if isinstance(x, str) else utf_8_decode(x)[0] -env_entry_point = b'Y_PYTHON_ENTRY_POINT' env_source_root = b'Y_PYTHON_SOURCE_ROOT' cfg_source_root = b'arcadia-source-root' env_extended_source_search = b'Y_PYTHON_EXTENDED_SOURCE_SEARCH' @@ -17,12 +16,28 @@ res_ya_ide_venv = b'YA_IDE_VENV' executable = sys.executable or 'Y_PYTHON' sys.modules['run_import_hook'] = __resource +def _probe(environ_dict, key, default_value=None): + """ Probe bytes and str variants for environ. + This is because in python3: + * _os (nt) on windows returns str, + * _os (posix) on linux return bytes + For more information check: + * https://github.com/python/cpython/blob/main/Lib/importlib/_bootstrap_external.py#L34 + * YA-1700 + """ + keys = [_b(key), _s(key)] + for key in keys: + if key in environ_dict: + return _b(environ_dict[key]) + + return _b(default_value) if isinstance(default_value, str) else default_value + # This is the prefix in contrib/tools/python3/Lib/ya.make. py_prefix = b'py/' py_prefix_len = len(py_prefix) YA_IDE_VENV = __resource.find(res_ya_ide_venv) -Y_PYTHON_EXTENDED_SOURCE_SEARCH = _os.environ.get(env_extended_source_search) or YA_IDE_VENV +Y_PYTHON_EXTENDED_SOURCE_SEARCH = _probe(_os.environ, env_extended_source_search) or YA_IDE_VENV def _init_venv(): @@ -64,7 +79,7 @@ def _init_venv(): def _get_source_root(): - env_value = _os.environ.get(env_source_root) + env_value = _probe(_os.environ, env_source_root) if env_value or not YA_IDE_VENV: return env_value -- cgit v1.3