Fix test breakages caused by removal of gdb_py_test_multiple.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-function.exp
CommitLineData
b811d2c2 1# Copyright (C) 2009-2020 Free Software Foundation, Inc.
bc3b79fd
TJB
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# This file is part of the GDB testsuite. It tests the mechanism
17# exposing convenience functions to Python.
18
f39a75d0 19load_lib gdb-python.exp
bc3b79fd
TJB
20
21# Start with a fresh gdb.
22
23gdb_exit
24gdb_start
25gdb_reinitialize_dir $srcdir/$subdir
26
f6bbabf0
PM
27# Skip all tests if Python scripting is not enabled.
28if { [skip_python_tests] } { continue }
bc3b79fd 29
2a17c803 30gdb_test_multiline "input convenience function" \
bc3b79fd
TJB
31 "python" "" \
32 "class test_func (gdb.Function):" "" \
33 " def __init__ (self):" "" \
34 " super (test_func, self).__init__ (\"test_func\")" "" \
35 " def invoke (self, arg):" "" \
36 " return \"test_func output, arg = %s\" % arg.string ()" "" \
37 "test_func ()" "" \
38 "end" ""
39
40gdb_test "print \$test_func (\"ugh\")" "= \"test_func output, arg = ugh\"" "call function"
cc924cad
TJB
41
42# Test returning a gdb.Value from the function. This segfaulted GDB at one point.
43
2a17c803 44gdb_test_multiline "input value-returning convenience function" \
cc924cad
TJB
45 "python" "" \
46 "class Double (gdb.Function):" "" \
47 " def __init__ (self):" "" \
48 " super (Double, self).__init__ (\"double\")" "" \
49 " def invoke (self, n):" "" \
50 " return n*2" "" \
51 "Double ()" "" \
52 "end" ""
53
d7df6549
AB
54# Different languages can have different parsers, so lets check that
55# internal functions are understood by every language. Place auto
56# last in the list so we end up back in 'auto' language mode.
57foreach lang [concat [gdb_supported_languages] auto] {
58 gdb_test_no_output "set language $lang"
59 gdb_test "print \$double (1)" "= 2" "call value-returning function, language = $lang"
60}
329719ec 61
2a17c803 62gdb_test_multiline "input int-returning function" \
329719ec
TT
63 "python" "" \
64 "class Yes(gdb.Function):" "" \
65 " def __init__(self):" "" \
66 " gdb.Function.__init__(self, 'yes')" "" \
67 " def invoke(self):" "" \
68 " return 1" "" \
69 "Yes ()" "" \
70 "end" ""
71
72gdb_test "print \$yes() && \$yes()" " = 1" "call yes with &&"
73gdb_test "print \$yes() || \$yes()" " = 1" "call yes with ||"
05775840 74
2a17c803 75gdb_test_multiline "Test GDBError" \
05775840
PM
76 "python" "" \
77 "class GDBError(gdb.Function):" "" \
78 " def __init__(self):" "" \
79 " gdb.Function.__init__(self, 'gdberror')" "" \
80 " def invoke(self):" "" \
81 " raise gdb.GdbError(\"This is a GdbError\")" "" \
82 "GDBError ()" "" \
83 "end" ""
84
85gdb_test "print \$gdberror()" "This is a GdbError.*" \
86 "Test GdbError. There should not be a stack trace"
87
2a17c803 88gdb_test_multiline "Test Normal Error" \
05775840
PM
89 "python" "" \
90 "class NormalError(gdb.Function):" "" \
91 " def __init__(self):" "" \
92 " gdb.Function.__init__(self, 'normalerror')" "" \
93 " def invoke(self):" "" \
94 " raise RuntimeError(\"This is a Normal Error\")" "" \
95 "NormalError ()" "" \
96 "end" ""
97
80b6e756 98gdb_test_no_output "set python print-stack full"
05775840
PM
99gdb_test "print \$normalerror()" "Traceback.*File.*line 5.*in invoke.*RuntimeError.*This is a Normal Error.*" \
100 "Test a Runtime error. There should be a stack trace."
028d0ed5 101
2a17c803 102gdb_test_multiline "input command-calling function" \
028d0ed5
TJB
103 "python" "" \
104 "class CallCommand(gdb.Function):" "" \
105 " def __init__(self):" "" \
106 " gdb.Function.__init__(self, 'call_command')" "" \
107 " def invoke(self):" "" \
108 " return gdb.execute('print 1', to_string=True)" "" \
109 "CallCommand ()" "" \
110 "end" ""
111
9f058c10 112gdb_test_no_output "set var \$foo = \$call_command()" "setting a value from a function which executes a command."
028d0ed5 113# There was a bug where GDB would segfault in the second call, so try calling again.
9f058c10 114gdb_test_no_output "set var \$foo = \$call_command()" "setting a value from a function which executes a command, again."
This page took 1.322087 seconds and 4 git commands to generate.