Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG: Remove prepended entry from sys.path
Python prepends the directory of the script being executed to `sys.path` and this can cause other packages to be shadowed, e.g. the builtin `statistics` module being shadowed by `asv.statistics` because the path of the `asv` package is prepended when the benchmark runner script is executed. There was a prior fix for this but it was removed as part of refactoring and splitting into `asv_runner`. Python 3.11+ has a mechanism to avoid this via use of `-P` on the command line or setting the `PYTHONSAFEPATH` environment variable. This doesn't really help though as support for older versions of Python is required. This patch does, however, check `sys.flags.safe_path` to avoid fixing up the path if this feature is being used. This further highlights that the original approach is not particularly robust as it removed the first item unconditionally and sometimes tried to add it back in. Instead this patch checks for and removes the path only if it matches the directory that the runner script resides in. Other alternatives could be to consider running with the `-I` command line flag to force isolated mode (Python 3.4+), although that might cause other unexpected breakage as it implies `-E` (which ignores all `PYTHON*` environment variables), etc. See the following links for details: - https://docs.python.org/3/library/sys.html#sys.path - https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSAFEPATH - https://docs.python.org/3/using/cmdline.html#cmdoption-P - https://docs.python.org/3/using/cmdline.html#cmdoption-I Regression in 5ab2d29.
- Loading branch information