1 # Copyright 2021 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
21 # Skip all tests if Python scripting is not enabled.
22 if { [skip_python_tests] } { continue }
26 # Return a list containing two directory paths for newly created home
29 # The first directory is a HOME style home directory, it contains a
30 # .gdbearlyinit file containing CONTENT.
32 # The second directory is an XDG_CONFIG_HOME style home directory, it
33 # contains a sub-directory gdb/, inside which is a file gdbearlyinit
34 # that also contains CONTENT.
36 # The PREFIX is used in both directory names and should be unique for
37 # each call to this function.
38 proc setup_home_directories { prefix content } {
39 set home_dir [standard_output_file "${prefix}-home"]
40 set xdg_home_dir [standard_output_file "${prefix}-xdg"]
43 file mkdir "$xdg_home_dir/gdb"
45 # Write the content into the HOME directory.
46 set fd [open "$home_dir/.gdbearlyinit" w]
50 # Copy this from the HOME directory into the XDG_CONFIG_HOME
52 file copy -force "$home_dir/.gdbearlyinit" "$xdg_home_dir/gdb/gdbearlyinit"
54 return [list $home_dir $xdg_home_dir]
57 # Start GDB and check the status of the Python system flags that we
58 # can control from within GDB.
59 proc test_python_settings { exp_state } {
62 gdb_test_no_output "python import sys"
64 foreach_with_prefix attr {ignore_environment dont_write_bytecode} {
66 # If we are checking 'dont_write_bytecode', and we are
67 # expecting this attribute to be 'off', then, if the user has
68 # PYTHONDONTWRITEBYTECODE set in their environment, the result
69 # will be 'on' instead of 'off', so override the expected
72 # The reason for this is, 'set python dont-write-bytecode' by
73 # default is set to 'auto', which means, so long as 'set
74 # python ignore-environment' is 'off', GDB will check for the
75 # above environment variable.
77 # We could unset the environment variable, but until Python
78 # 3.8 there was no way to control where .pyc files are placed,
79 # and it feels bad to cause .pyc files to be created within
80 # the users filesystem when they clearly don't want them.
82 # And so, we adjust the expected results. Hopefully, between
83 # all GDB developers some will test GDB with this environment
85 if { $attr == "dont_write_bytecode" \
86 && $exp_state == "off"
87 && [info exists ::env(PYTHONDONTWRITEBYTECODE)] } {
93 gdb_test_multiline "testname" \
95 "if hasattr(sys, 'flags') and getattr(sys.flags, '${attr}', False):" "" \
96 " print (\"${attr} is on\")" "" \
98 " print (\"${attr} is off\")" "" \
99 "end" "${attr} is ${answer}"
105 save_vars { env(TERM) } {
106 # We need an ANSI-capable terminal to get the output.
109 # Check the features are off by default.
110 test_python_settings "off"
112 # Create an empty directory we can use as HOME for some of the
113 # tests below. When we set XDG_CONFIG_HOME we still need to point
114 # HOME at something otherwise GDB complains that it doesn't know
115 # where to create the index cache.
116 set empty_home_dir [standard_output_file fake-empty-home]
118 # Create two directories to use for the style setting test.
119 set dirs [setup_home_directories "style" \
121 "set python dont-write-bytecode on" \
122 "set python ignore-environment on"]]
123 set home_dir [lindex $dirs 0]
124 set xdg_home_dir [lindex $dirs 1]
126 # Now arrange to use the fake home directory early init file.
127 save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
128 set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
130 with_test_prefix "using HOME config" {
131 # Now test GDB when using the HOME directory.
132 set env(HOME) $home_dir
133 unset -nocomplain env(XDG_CONFIG_HOME)
134 test_python_settings "on"
137 with_test_prefix "using XDG_CONFIG_HOME config" {
138 # Now test using the XDG_CONFIG_HOME folder. We still need to
139 # have a HOME directory set otherwise GDB will issue an error
140 # about not knowing where to place the index cache.
141 set env(XDG_CONFIG_HOME) $xdg_home_dir
142 set env(HOME) $empty_home_dir
143 test_python_settings "on"