[Bf-blender-cvs] [7c2f0074f3f] blender-v2.82-release: Python: disable environment variables by default

Campbell Barton noreply at git.blender.org
Thu Jan 16 17:40:13 CET 2020


Commit: 7c2f0074f3fe2411daa7a6e351d7cbc535246871
Author: Campbell Barton
Date:   Thu Jan 16 21:11:05 2020 +1100
Branches: blender-v2.82-release
https://developer.blender.org/rB7c2f0074f3fe2411daa7a6e351d7cbc535246871

Python: disable environment variables by default

This avoids the problem where Blender doesn't start because
the PYTHONPATH points to an incompatible Python version,
see T72807.

Previously we chose to assume people who set the PYTHONPATH know what
they're doing, however users may have set this for non Blender projects.
So it's not obvious that this is the cause of Blender not to launch
on their system.

To use Python's environment vars, pass the argument:
--python-use-system-env

Note that this only impacts Python run-time environment variables
documented in `python --help`, Access from `os.environ` remains.

===================================================================

M	source/blender/python/BPY_extern.h
M	source/blender/python/intern/bpy_interface.c
M	source/creator/creator_args.c

===================================================================

diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index be2fd6c4e53..810c86c115a 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -51,6 +51,7 @@ int BPY_is_pyconstraint(struct Text *text);
 void BPY_python_start(int argc, const char **argv);
 void BPY_python_end(void);
 void BPY_python_reset(struct bContext *C);
+void BPY_python_use_system_env(void);
 
 /* global interpreter lock */
 
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 9acf05abfe2..28afab1a6eb 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -84,6 +84,9 @@ CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_RNA, "bpy.rna");
  * stop bpy_context_clear from invalidating. */
 static int py_call_level = 0;
 
+/* Set by command line arguments before Python starts. */
+static bool py_use_system_env = false;
+
 // #define TIME_PY_RUN // simple python tests. prints on exit.
 
 #ifdef TIME_PY_RUN
@@ -276,6 +279,10 @@ void BPY_python_start(int argc, const char **argv)
    * While harmless, it's noisy. */
   Py_FrozenFlag = 1;
 
+  /* Only use the systems environment variables when explicitly requested.
+   * Since an incorrect 'PYTHONPATH' causes difficult to debug errors, see: T72807. */
+  Py_IgnoreEnvironmentFlag = !py_use_system_env;
+
   Py_Initialize();
 
   // PySys_SetArgv(argc, argv);  /* broken in py3, not a huge deal */
@@ -408,6 +415,12 @@ void BPY_python_reset(bContext *C)
   BPY_modules_load_user(C);
 }
 
+void BPY_python_use_system_env(void)
+{
+  BLI_assert(!Py_IsInitialized());
+  py_use_system_env = true;
+}
+
 static void python_script_error_jump_text(struct Text *text)
 {
   int lineno;
diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c
index 6e3988a5ea6..c7df4888c0d 100644
--- a/source/creator/creator_args.c
+++ b/source/creator/creator_args.c
@@ -559,6 +559,7 @@ static int arg_handle_print_help(int UNUSED(argc), const char **UNUSED(argv), vo
   BLI_argsPrintArgDoc(ba, "--python-expr");
   BLI_argsPrintArgDoc(ba, "--python-console");
   BLI_argsPrintArgDoc(ba, "--python-exit-code");
+  BLI_argsPrintArgDoc(ba, "--python-use-system-env");
   BLI_argsPrintArgDoc(ba, "--addons");
 
   printf("\n");
@@ -1907,6 +1908,17 @@ static int arg_handle_python_exit_code_set(int argc, const char **argv, void *UN
   }
 }
 
+static const char arg_handle_python_use_system_env_set_doc[] =
+    "\n\t"
+    "Allow Python to use system environment variables such as 'PYTHONPATH'.";
+static int arg_handle_python_use_system_env_set(int UNUSED(argc),
+                                                const char **UNUSED(argv),
+                                                void *UNUSED(data))
+{
+  BPY_python_use_system_env();
+  return 0;
+}
+
 static const char arg_handle_addons_set_doc[] =
     "<addon(s)>\n"
     "\tComma separated list of add-ons (no spaces).";
@@ -2188,6 +2200,9 @@ void main_args_setup(bContext *C, bArgs *ba)
       ba, 1, NULL, "--env-system-scripts", CB_EX(arg_handle_env_system_set, scripts), NULL);
   BLI_argsAdd(ba, 1, NULL, "--env-system-python", CB_EX(arg_handle_env_system_set, python), NULL);
 
+  BLI_argsAdd(
+      ba, 1, NULL, "--python-use-system-env", CB(arg_handle_python_use_system_env_set), NULL);
+
   /* second pass: custom window stuff */
   BLI_argsAdd(ba, 2, "-p", "--window-geometry", CB(arg_handle_window_geometry), NULL);
   BLI_argsAdd(ba, 2, "-w", "--window-border", CB(arg_handle_with_borders), NULL);



More information about the Bf-blender-cvs mailing list