[2/2,gdb/python] Handle empty PYTHONDONTWRITEBYTECODE

Message ID 20241125101010.9825-3-tdevries@suse.de
State Committed
Headers
Series Handle empty PYTHONDONTWRITEBYTECODE |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom de Vries Nov. 25, 2024, 10:10 a.m. UTC
  When using PYTHONDONTWRITEBYTECODE with an empty string we get:
...
$ PYTHONDONTWRITEBYTECODE= gdb -q -batch -ex "show python dont-write-bytecode"
Python's dont-write-bytecode setting is auto (currently on).
...

This is incorrect, it should be off.

The actual setting is correct, that was already fixed in commit 24d2cbc42cc
("set/show python dont-write-bytecode fixes"), in function
python_write_bytecode.

Fix this by:
- factoring out new function env_python_dont_write_bytecode out of
  python_write_bytecode, and
- using it in show_python_dont_write_bytecode.

Tested on x86_64-linux, using test-case gdb.python/py-startup-opt.exp and:
- PYTHONDONTWRITEBYTECODE=
- PYTHONDONTWRITEBYTECODE=1
- unset PYTHONDONTWRITEBYTECODE

PR python/32389
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32389
---
 gdb/python/python.c                         | 22 +++++++++++++++------
 gdb/testsuite/gdb.python/py-startup-opt.exp |  8 ++++++++
 2 files changed, 24 insertions(+), 6 deletions(-)
  

Patch

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 3dc56d5695d..1d448efc54f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2130,6 +2130,17 @@  set_python_ignore_environment (const char *args, int from_tty,
    not write `.pyc' files on import of a module.  */
 static enum auto_boolean python_dont_write_bytecode = AUTO_BOOLEAN_AUTO;
 
+
+/* Return true if environment variable PYTHONDONTWRITEBYTECODE is set to a
+   non-empty string.  */
+
+static bool
+env_python_dont_write_bytecode ()
+{
+  const char *envvar = getenv ("PYTHONDONTWRITEBYTECODE");
+  return envvar != nullptr && envvar[0] != '\0';
+}
+
 /* Implement 'show python dont-write-bytecode'.  */
 
 static void
@@ -2139,8 +2150,10 @@  show_python_dont_write_bytecode (struct ui_file *file, int from_tty,
   if (python_dont_write_bytecode == AUTO_BOOLEAN_AUTO)
     {
       const char *auto_string
-	= (python_ignore_environment
-	   || getenv ("PYTHONDONTWRITEBYTECODE") == nullptr) ? "off" : "on";
+	= ((python_ignore_environment
+	    || !env_python_dont_write_bytecode ())
+	   ? "off"
+	   : "on");
 
       gdb_printf (file,
 		  _("Python's dont-write-bytecode setting is %s (currently %s).\n"),
@@ -2166,10 +2179,7 @@  python_write_bytecode ()
       if (python_ignore_environment)
 	wbc = 1;
       else
-	{
-	  const char *pdwbc = getenv ("PYTHONDONTWRITEBYTECODE");
-	  wbc = (pdwbc == nullptr || pdwbc[0] == '\0') ? 1 : 0;
-	}
+	wbc = env_python_dont_write_bytecode () ? 0 : 1;
     }
   else
     wbc = python_dont_write_bytecode == AUTO_BOOLEAN_TRUE ? 0 : 1;
diff --git a/gdb/testsuite/gdb.python/py-startup-opt.exp b/gdb/testsuite/gdb.python/py-startup-opt.exp
index 014aff5725a..df35a378658 100644
--- a/gdb/testsuite/gdb.python/py-startup-opt.exp
+++ b/gdb/testsuite/gdb.python/py-startup-opt.exp
@@ -93,6 +93,14 @@  proc test_python_settings { exp_state } {
 	    "else:" "" \
 	    "  print (\"${attr} is off\")" "" \
 	    "end" "${attr} is ${answer}"
+
+	if { $attr == "dont_write_bytecode" && $exp_state == "off" } {
+	    set setting dont-write-bytecode
+	    set show_setting \
+		"Python's $setting setting is auto (currently $answer)."
+	    gdb_test "show python $setting" \
+		[string_to_regexp $show_setting]
+	}
     }
 
     gdb_exit