]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-startup-opt.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-startup-opt.exp
1 # Copyright 2021-2024 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 # Test the flags within GDB that can be used to control how Python is
17 # initialized.
18
19 require allow_python_tests
20
21 # Return a list containing two directory paths for newly created home
22 # directories.
23 #
24 # The first directory is a HOME style home directory, it contains a
25 # .gdbearlyinit file containing CONTENT.
26 #
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.
30 #
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"]
36
37 file mkdir $home_dir
38 file mkdir "$xdg_home_dir/gdb"
39
40 # Write the content into the HOME directory.
41 set fd [open "$home_dir/.gdbearlyinit" w]
42 puts $fd $content
43 close $fd
44
45 # Copy this from the HOME directory into the XDG_CONFIG_HOME
46 # directory.
47 file copy -force "$home_dir/.gdbearlyinit" "$xdg_home_dir/gdb/gdbearlyinit"
48
49 return [list $home_dir $xdg_home_dir]
50 }
51
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 } {
55 gdb_start
56
57 gdb_test_no_output "python import sys"
58
59 foreach_with_prefix attr {ignore_environment dont_write_bytecode} {
60
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
65 # result here.
66 #
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.
71 #
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.
76 #
77 # And so, we adjust the expected results. Hopefully, between
78 # all GDB developers some will test GDB with this environment
79 # variable unset.
80 if { $attr == "dont_write_bytecode" \
81 && $exp_state == "off"
82 && [info exists ::env(PYTHONDONTWRITEBYTECODE)] } {
83 set answer "on"
84 } else {
85 set answer $exp_state
86 }
87
88 gdb_test_multiline "testname" \
89 "python" "" \
90 "if hasattr(sys, 'flags') and getattr(sys.flags, '${attr}', False):" "" \
91 " print (\"${attr} is on\")" "" \
92 "else:" "" \
93 " print (\"${attr} is off\")" "" \
94 "end" "${attr} is ${answer}"
95 }
96
97 gdb_exit
98 }
99
100 with_ansi_styling_terminal {
101 # Check the features are off by default.
102 test_python_settings "off"
103
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]
109
110 # Create two directories to use for the style setting test.
111 set dirs [setup_home_directories "style" \
112 [multi_line_input \
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]
117
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]
121
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"
127 }
128
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"
136 }
137 }
138 }