From 7747a39ff4d33f722737ff57a1a3b69cca97909a Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 22 Sep 2023 11:35:22 -0400 Subject: [PATCH] tests: use a local `typing` module with Python 3.4 only This patch changes the strategy to support the `typing` module in tests. To make it possible to include compatible third-party/portable Python modules as is, Python needs to find the `typing` module. Python 3.5+ will find it, but Python 3.4 won't. To fix this: 1. `tests/utils/utils.sh` calls the Python interpreter specified by BT_TESTS_PYTHON_BIN to get the Python version. 2. The run_python() function in `tests/utils/utils.sh` adds the `tests/utils/python/typing` directory to `PYTHONPATH` if the version of Python is 3.4. 3. Move `tests/utils/python/local_typing.py` to `tests/utils/python/typing/typing.py`. 4. Remove `tests/utils/python/utils.py` (not needed anymore). Signed-off-by: Philippe Proulx Change-Id: Ifc939f617627ff11cd6a7cbf8642c7b33006d303 Reviewed-on: https://review.lttng.org/c/babeltrace/+/10915 Tested-by: jenkins --- LICENSE | 2 +- pyproject.toml | 4 ++-- setup.cfg | 2 +- .../python/{local_typing.py => typing/typing.py} | 0 tests/utils/python/utils.py | 10 ---------- tests/utils/utils.sh | 12 +++++++++++- 6 files changed, 15 insertions(+), 15 deletions(-) rename tests/utils/python/{local_typing.py => typing/typing.py} (100%) delete mode 100644 tests/utils/python/utils.py diff --git a/LICENSE b/LICENSE index e0e84e95..22b5a72e 100644 --- a/LICENSE +++ b/LICENSE @@ -103,7 +103,7 @@ According with: This applies to: - tests/utils/python/local_typing.py + tests/utils/python/typing/typing.py In addition, other licenses may also apply, see SPDX-License-Identifier in diff --git a/pyproject.toml b/pyproject.toml index 2dac3d6c..44afa406 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ | src/bindings/python/bt2/setup\.py$ | src/bindings/python/bt2/bt2/native_bt\.py$ | src/bindings/python/bt2/bt2/version\.py$ - | tests/utils/python/local_typing\.py$ + | tests/utils/python/typing/typing\.py$ | tests/utils/python/tap ) @@ -21,6 +21,6 @@ profile = "black" extend_skip_glob = [ "tests/utils/python/tap", - "tests/utils/python/local_typing.py", + "tests/utils/python/typing/typing.py", ] length_sort = true diff --git a/setup.cfg b/setup.cfg index 9fbc23cf..7788df8a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,4 +13,4 @@ ignore = E501,W503 # Has code to set up the DLL search path before imports. per-file-ignores = src/bindings/python/bt2/bt2/__init__.py:F401,E402 -exclude = tests/utils/python/tap tests/utils/python/local_typing.py +exclude = tests/utils/python/tap tests/utils/python/typing/typing.py diff --git a/tests/utils/python/local_typing.py b/tests/utils/python/typing/typing.py similarity index 100% rename from tests/utils/python/local_typing.py rename to tests/utils/python/typing/typing.py diff --git a/tests/utils/python/utils.py b/tests/utils/python/utils.py deleted file mode 100644 index 0e045fbf..00000000 --- a/tests/utils/python/utils.py +++ /dev/null @@ -1,10 +0,0 @@ -# SPDX-License-Identifier: MIT -# -# Copyright (c) 2023 EfficiOS, Inc. - -# The purpose of this import is to make the typing module easily accessible -# elsewhere, without having to do the try-except everywhere. -try: - import typing as typing_mod # noqa: F401 -except ImportError: - import local_typing as typing_mod # noqa: F401 diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index d40011a1..9f5adf5a 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -111,6 +111,8 @@ if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then fi export BT_TESTS_PYTHON_BIN +BT_TESTS_PYTHON_VERSION=$($BT_TESTS_PYTHON_BIN -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))') + if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then BT_TESTS_PYTHON_CONFIG_BIN="python3-config" fi @@ -305,7 +307,15 @@ check_coverage() { # Execute a shell command in the appropriate environment to access the Python # test utility modules in `tests/utils/python`. run_python() { - PYTHONPATH="${BT_TESTS_SRCDIR}/utils/python${PYTHONPATH:+:}${PYTHONPATH:-}" "$@" + local our_pythonpath="${BT_TESTS_SRCDIR}/utils/python" + + if [[ $BT_TESTS_PYTHON_VERSION = 3.4 ]]; then + # Add a local directory containing a `typing.py` to `PYTHONPATH` for + # Python 3.4 which doesn't offer the `typing` module. + our_pythonpath="$our_pythonpath:${BT_TESTS_SRCDIR}/utils/python/typing" + fi + + PYTHONPATH="${our_pythonpath}${PYTHONPATH:+:}${PYTHONPATH:-}" "$@" } # Execute a shell command in the appropriate environment to have access to the -- 2.34.1