xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/lib/gdb-python.exp (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1# Copyright 2010-2023 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# Utilities for python-scripting related tests.
17
18# Run a command in GDB, and report a failure if a Python exception is thrown.
19# If report_pass is true, report a pass if no exception is thrown.
20
21proc gdb_py_test_silent_cmd { cmd name report_pass } {
22    global gdb_prompt
23
24    gdb_test_multiple $cmd $name {
25	-re "Traceback.*$gdb_prompt $" { fail $name }
26	-re "$gdb_prompt $"            { if $report_pass { pass $name } }
27    }
28}
29
30# Return the result of python expression EXPR.  DEFAULT is returned if
31# there's an error.  TEST is the test message to use.  It can be
32# omitted, in which case a test message is built from EXP.  This is
33# modeled after get_integer_valueof.
34
35proc get_python_valueof { exp default {test ""} } {
36    global gdb_prompt
37
38    if {$test == ""} {
39	set test "get python valueof \"${exp}\""
40    }
41
42    set val ${default}
43    gdb_test_multiple "python print (\"valueof: %s\" % (${exp}))" "$test" {
44	-re "valueof: (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
45	    set val $expect_out(1,string)
46	    pass "$test"
47	}
48	timeout {
49	    fail "$test (timeout)"
50	}
51    }
52    return ${val}
53}
54
55# Return true if Python module NAME is available, otherwise, return
56# false.
57
58proc gdb_py_module_available { name } {
59    set available "unknown"
60    gdb_test_multiple "python import ${name}" "" {
61	-re -wrap "ModuleNotFoundError: No module named '${name}'.*" {
62	    set available false
63	}
64	-re -wrap "ImportError: No module named '?${name}'?.*" {
65	    set available false
66	}
67	-re -wrap "python import ${name}" {
68	    set available true
69	}
70    }
71
72    if { $available == "unknown" } {
73	perror "unexpected output from python import"
74	set available false
75    }
76
77    return ${available}
78}
79