1 # Copyright 2021-2024 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Test the flags within GDB that can be used to control how Python is
19 require allow_python_tests
21 # Return a list containing two directory paths for newly created home
24 # The first directory is a HOME style home directory, it contains a
25 # .gdbearlyinit file containing CONTENT.
27 # The second directory is an XDG_CONFIG_HOME style home directory, it
28 # contains a sub-directory gdb/, inside which is a file gdbearlyinit
29 # that also contains CONTENT.
31 # The PREFIX is used in both directory names and should be unique for
32 # each call to this function.
33 proc setup_home_directories { prefix content } {
34 set home_dir [standard_output_file "${prefix}-home"]
35 set xdg_home_dir [standard_output_file "${prefix}-xdg"]
38 file mkdir "$xdg_home_dir/gdb"
40 # Write the content into the HOME directory.
41 set fd [open "$home_dir/.gdbearlyinit" w]
45 # Copy this from the HOME directory into the XDG_CONFIG_HOME
47 file copy -force "$home_dir/.gdbearlyinit" "$xdg_home_dir/gdb/gdbearlyinit"
49 return [list $home_dir $xdg_home_dir]
52 # Start GDB and check the status of the Python system flags that we
53 # can control from within GDB.
54 proc test_python_settings { exp_state } {
57 gdb_test_no_output "python import sys"
59 foreach_with_prefix attr {ignore_environment dont_write_bytecode} {
61 # If we are checking 'dont_write_bytecode', and we are
62 # expecting this attribute to be 'off', then, if the user has
63 # PYTHONDONTWRITEBYTECODE set in their environment, the result
64 # will be 'on' instead of 'off', so override the expected
67 # The reason for this is, 'set python dont-write-bytecode' by
68 # default is set to 'auto', which means, so long as 'set
69 # python ignore-environment' is 'off', GDB will check for the
70 # above environment variable.
72 # We could unset the environment variable, but until Python
73 # 3.8 there was no way to control where .pyc files are placed,
74 # and it feels bad to cause .pyc files to be created within
75 # the users filesystem when they clearly don't want them.
77 # And so, we adjust the expected results. Hopefully, between
78 # all GDB developers some will test GDB with this environment
80 if { $attr == "dont_write_bytecode" \
81 && $exp_state == "off"
82 && [info exists ::env(PYTHONDONTWRITEBYTECODE)] } {
88 gdb_test_multiline "testname" \
90 "if hasattr(sys, 'flags') and getattr(sys.flags, '${attr}', False):" "" \
91 " print (\"${attr} is on\")" "" \
93 " print (\"${attr} is off\")" "" \
94 "end" "${attr} is ${answer}"
100 with_ansi_styling_terminal {
101 # Check the features are off by default.
102 test_python_settings "off"
104 # Create an empty directory we can use as HOME for some of the
105 # tests below. When we set XDG_CONFIG_HOME we still need to point
106 # HOME at something otherwise GDB complains that it doesn't know
107 # where to create the index cache.
108 set empty_home_dir [standard_output_file fake-empty-home]
110 # Create two directories to use for the style setting test.
111 set dirs [setup_home_directories "style" \
113 "set python dont-write-bytecode on" \
114 "set python ignore-environment on"]]
115 set home_dir [lindex $dirs 0]
116 set xdg_home_dir [lindex $dirs 1]
118 # Now arrange to use the fake home directory early init file.
119 save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
120 set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
122 with_test_prefix "using HOME config" {
123 # Now test GDB when using the HOME directory.
124 set env(HOME) $home_dir
125 unset -nocomplain env(XDG_CONFIG_HOME)
126 test_python_settings "on"
129 with_test_prefix "using XDG_CONFIG_HOME config" {
130 # Now test using the XDG_CONFIG_HOME folder. We still need to
131 # have a HOME directory set otherwise GDB will issue an error
132 # about not knowing where to place the index cache.
133 set env(XDG_CONFIG_HOME) $xdg_home_dir
134 set env(HOME) $empty_home_dir
135 test_python_settings "on"