11import os
22import re
33import shlex
4+ import shutil
45import subprocess
56import sys
67import sysconfig
78import unittest
89from test import support
910
1011
12+ GDB_PROGRAM = shutil .which ('gdb' ) or 'gdb'
13+
1114# Location of custom hooks file in a repository checkout.
1215CHECKOUT_HOOK_PATH = os .path .join (os .path .dirname (sys .executable ),
1316 'python-gdb.py' )
@@ -27,7 +30,7 @@ def clean_environment():
2730# Temporary value until it's initialized by get_gdb_version() below
2831GDB_VERSION = (0 , 0 )
2932
30- def run_gdb (* args , exitcode = 0 , ** env_vars ):
33+ def run_gdb (* args , exitcode = 0 , check = True , ** env_vars ):
3134 """Runs gdb in --batch mode with the additional arguments given by *args.
3235
3336 Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
@@ -36,7 +39,7 @@ def run_gdb(*args, exitcode=0, **env_vars):
3639 if env_vars :
3740 env .update (env_vars )
3841
39- cmd = ['gdb' ,
42+ cmd = [GDB_PROGRAM ,
4043 # Batch mode: Exit after processing all the command files
4144 # specified with -x/--command
4245 '--batch' ,
@@ -59,7 +62,7 @@ def run_gdb(*args, exitcode=0, **env_vars):
5962
6063 stdout = proc .stdout
6164 stderr = proc .stderr
62- if proc .returncode != exitcode :
65+ if check and proc .returncode != exitcode :
6366 cmd_text = shlex .join (cmd )
6467 raise Exception (f"{ cmd_text } failed with exit code { proc .returncode } , "
6568 f"expected exit code { exitcode } :\n "
@@ -72,10 +75,10 @@ def run_gdb(*args, exitcode=0, **env_vars):
7275def get_gdb_version ():
7376 try :
7477 stdout , stderr = run_gdb ('--version' )
75- except OSError :
78+ except OSError as exc :
7679 # This is what "no gdb" looks like. There may, however, be other
7780 # errors that manifest this way too.
78- raise unittest .SkipTest ("Couldn't find gdb program on the path" )
81+ raise unittest .SkipTest (f "Couldn't find gdb program on the path: { exc } " )
7982
8083 # Regex to parse:
8184 # 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7
@@ -106,7 +109,8 @@ def check_usable_gdb():
106109 # disallow this without a customized .gdbinit.
107110 stdout , stderr = run_gdb (
108111 '--eval-command=python import sys; print(sys.version_info)' ,
109- '--args' , sys .executable )
112+ '--args' , sys .executable ,
113+ check = False )
110114
111115 if "auto-loading has been declined" in stderr :
112116 raise unittest .SkipTest (
@@ -144,6 +148,7 @@ def setup_module():
144148 print (f"gdb version { GDB_VERSION [0 ]} .{ GDB_VERSION [1 ]} :" )
145149 for line in GDB_VERSION_TEXT .splitlines ():
146150 print (" " * 4 + line )
151+ print (f" path: { GDB_PROGRAM } " )
147152 print ()
148153
149154
0 commit comments