python: Unset Python environment variables
authorJeffery To <jeffery.to@gmail.com>
Thu, 2 Mar 2023 15:35:00 +0000 (23:35 +0800)
committerJeffery To <jeffery.to@gmail.com>
Thu, 30 Mar 2023 04:19:04 +0000 (12:19 +0800)
This will prevent the user's environment variables from affecting host
Python, removing the need to manually override these variables.

It is also not necessary to set PYTHONPATH (when not working on target
Python packages) because the given directories are already included in
Python's search path by default.

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
lang/python/python3-host.mk
lang/python/python3-package.mk

index 8a05c332cf453e9a927a2978c869244ed461e2ec..fdf6658fd85ee8e00664c4f776722ef0d2db7abb 100644 (file)
 python3_mk_path:=$(dir $(lastword $(MAKEFILE_LIST)))
 include $(python3_mk_path)python3-version.mk
 
+# Unset environment variables
+
+# https://docs.python.org/3/using/cmdline.html#environment-variables
+unexport \
+       PYTHONHOME \
+       PYTHONPATH \
+       PYTHONSAFEPATH \
+       PYTHONPLATLIBDIR \
+       PYTHONSTARTUP \
+       PYTHONOPTIMIZE \
+       PYTHONBREAKPOINT \
+       PYTHONDEBUG \
+       PYTHONINSPECT \
+       PYTHONUNBUFFERED \
+       PYTHONVERBOSE \
+       PYTHONCASEOK \
+       PYTHONDONTWRITEBYTECODE \
+       PYTHONPYCACHEPREFIX \
+       PYTHONHASHSEED \
+       PYTHONINTMAXSTRDIGITS \
+       PYTHONIOENCODING \
+       PYTHONNOUSERSITE \
+       PYTHONUSERBASE \
+       PYTHONEXECUTABLE \
+       PYTHONWARNINGS \
+       PYTHONFAULTHANDLER \
+       PYTHONTRACEMALLOC \
+       PYTHONPROFILEIMPORTTIME \
+       PYTHONASYNCIODEBUG \
+       PYTHONMALLOC \
+       PYTHONMALLOCSTATS \
+       PYTHONLEGACYWINDOWSFSENCODING \
+       PYTHONLEGACYWINDOWSSTDIO \
+       PYTHONCOERCECLOCALE \
+       PYTHONDEVMODE \
+       PYTHONUTF8 \
+       PYTHONWARNDEFAULTENCODING \
+       PYTHONNODEBUGRANGES
+
+# https://docs.python.org/3/using/cmdline.html#debug-mode-variables
+unexport \
+       PYTHONTHREADDEBUG \
+       PYTHONDUMPREFS \
+       PYTHONDUMPREFSFILE
+
 HOST_PYTHON3_DIR:=$(STAGING_DIR_HOSTPKG)
 HOST_PYTHON3_INC_DIR:=$(HOST_PYTHON3_DIR)/include/python$(PYTHON3_VERSION)
 HOST_PYTHON3_LIB_DIR:=$(HOST_PYTHON3_DIR)/lib/python$(PYTHON3_VERSION)
@@ -31,10 +76,7 @@ HOST_PYTHON3_VARS = \
        LDSHARED="$(HOSTCC) -shared" \
        CFLAGS="$(HOST_CFLAGS)" \
        CPPFLAGS="$(HOST_CPPFLAGS) -I$(HOST_PYTHON3_INC_DIR)" \
-       LDFLAGS="$(HOST_LDFLAGS) -lpython$(PYTHON3_VERSION) -Wl$(comma)-rpath$(comma)$(STAGING_DIR_HOSTPKG)/lib" \
-       PYTHONPATH="$(HOST_PYTHON3PATH)" \
-       PYTHONDONTWRITEBYTECODE=0 \
-       PYTHONOPTIMIZE=""
+       LDFLAGS="$(HOST_LDFLAGS) -lpython$(PYTHON3_VERSION) -Wl$(comma)-rpath$(comma)$(STAGING_DIR_HOSTPKG)/lib"
 
 # $(1) => directory of python script
 # $(2) => python script and its arguments
index a24892ac5704468bead769c263d9de574797e3c8..8477d8c75d02c019ac502395d375a4f4155a3e65 100644 (file)
@@ -42,7 +42,6 @@ PYTHON3_VARS = \
        __PYVENV_LAUNCHER__="/usr/bin/$(PYTHON3)" \
        PYTHONPATH="$(PYTHON3PATH)" \
        PYTHONDONTWRITEBYTECODE=1 \
-       PYTHONOPTIMIZE="" \
        _python_sysroot="$(STAGING_DIR)" \
        _python_prefix="/usr" \
        _python_exec_prefix="/usr"