gdb/python: remove Python 2 support

New in this version:

 - Add a PY_MAJOR_VERSION check in configure.ac / AC_TRY_LIBPYTHON.  If
   the user passes --with-python=python2, this will cause a configure
   failure saying that GDB only supports Python 3.

Support for Python 2 is a maintenance burden for any patches touching
Python support.  Among others, the differences between Python 2 and 3
string and integer types are subtle.  It requires a lot of effort and
thinking to get something that behaves correctly on both.  And that's if
the author and reviewer of the patch even remember to test with Python
2.

See this thread for an example:

  https://sourceware.org/pipermail/gdb-patches/2021-December/184260.html

So, remove Python 2 support.  Update the documentation to state that GDB
can be built against Python 3 (as opposed to Python 2 or 3).

Update all the spots that use:

 - sys.version_info
 - IS_PY3K
 - PY_MAJOR_VERSION
 - gdb_py_is_py3k

... to only keep the Python 3 portions and drop the use of some
now-removed compatibility macros.

I did not update the configure script more than just removing the
explicit references to Python 2.  We could maybe do more there, like
check the Python version and reject it if that version is not
supported.  Otherwise (with this patch), things will only fail at
compile time, so it won't really be clear to the user that they are
trying to use an unsupported Python version.  But I'm a bit lost in the
configure code that checks for Python, so I kept that for later.

Change-Id: I75b0f79c148afbe3c07ac664cfa9cade052c0c62
This commit is contained in:
Simon Marchi 2021-12-23 20:20:46 -05:00 committed by Simon Marchi
parent e52a16f2aa
commit edae3fd660
28 changed files with 63 additions and 504 deletions

View file

@ -77,11 +77,7 @@ gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \
"read str address" 0
gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \
"read str contents" 1
if { $gdb_py_is_py3k == 0 } {
gdb_py_test_silent_cmd "python a = 'a'" "" 0
} else {
gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0
}
gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0
gdb_py_test_silent_cmd "python str\[1\] = a" "change str" 0
gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \
"write str" 1

View file

@ -17,27 +17,23 @@ import sys
import gdb
import gdb.types
# Following is for Python 3 compatibility...
if sys.version_info[0] > 2:
long = int
class cons_pp(object):
def __init__(self, val):
self._val = val
def to_string(self):
if long(self._val) == 0:
if int(self._val) == 0:
return "nil"
elif long(self._val["type"]) == 0:
elif int(self._val["type"]) == 0:
return "( . )"
else:
return "%d" % self._val["atom"]["ival"]
def children(self):
if long(self._val) == 0:
if int(self._val) == 0:
return []
elif long(self._val["type"]) == 0:
elif int(self._val["type"]) == 0:
return [("atom", self._val["atom"])]
else:
return [

View file

@ -85,11 +85,7 @@ with_test_prefix "instruction " {
gdb_test "python print(i.number)" "1"
gdb_test "python print(i.sal)" "symbol and line for .*"
gdb_test "python print(i.pc)" "$decimal"
if { $gdb_py_is_py3k == 0 } {
gdb_test "python print(repr(i.data))" "<read-only buffer for $hex,.*>"
} else {
gdb_test "python print(repr(i.data))" "<memory at $hex>"
}
gdb_test "python print(repr(i.data))" "<memory at $hex>"
gdb_test "python print(i.decoded)" ".*"
gdb_test "python print(i.size)" "$decimal"
gdb_test "python print(i.is_speculative)" "False"

View file

@ -81,17 +81,9 @@ def run_send_packet_test():
# the 'maint packet' command so that the output from the two sources
# can be compared.
def bytes_to_string(byte_array):
# Python 2/3 compatibility. We need a function that can give us
# the value of a single element in BYTE_ARRAY as an integer.
if sys.version_info[0] > 2:
value_of_single_byte = int
else:
value_of_single_byte = ord
res = ""
for b in byte_array:
b = value_of_single_byte(b)
b = int(b)
if b >= 32 and b <= 126:
res = res + ("%c" % b)
else:
@ -136,39 +128,23 @@ def run_set_global_var_test():
res = conn.send_packet(b"X%x,4:\x02\x02\x02\x02" % addr)
assert isinstance(res, bytes)
check_global_var(0x02020202)
if sys.version_info[0] > 2:
# On Python 3 this first attempt will not work as we're
# passing a Unicode string containing non-ascii characters.
saw_error = False
try:
res = conn.send_packet("X%x,4:\xff\xff\xff\xff" % addr)
except UnicodeError:
saw_error = True
except:
assert False
assert saw_error
check_global_var(0x02020202)
# Now we pass a bytes object, which will work.
res = conn.send_packet(b"X%x,4:\xff\xff\xff\xff" % addr)
check_global_var(0xFFFFFFFF)
else:
# On Python 2 we need to force the creation of a Unicode
# string, but, with that done, we expect to see the same error
# as on Python 3; the unicode string contains non-ascii
# characters.
saw_error = False
try:
res = conn.send_packet(unicode("X%x,4:\xff\xff\xff\xff") % addr)
except UnicodeError:
saw_error = True
except:
assert False
assert saw_error
check_global_var(0x02020202)
# Now we pass a plain string, which, on Python 2, is the same
# as a bytes object, this, we expect to work.
# This first attempt will not work as we're passing a Unicode string
# containing non-ascii characters.
saw_error = False
try:
res = conn.send_packet("X%x,4:\xff\xff\xff\xff" % addr)
check_global_var(0xFFFFFFFF)
except UnicodeError:
saw_error = True
except:
assert False
assert saw_error
check_global_var(0x02020202)
# Now we pass a bytes object, which will work.
res = conn.send_packet(b"X%x,4:\xff\xff\xff\xff" % addr)
check_global_var(0xFFFFFFFF)
print("set global_var test passed")

View file

@ -57,14 +57,11 @@ runto [gdb_get_line_number "Break to end."]
# Test gdb.solib_name
gdb_test "p &func1" "" "func1 address"
gdb_py_test_silent_cmd "python func1 = gdb.history(0)" "Aquire func1 address" 1
if { $gdb_py_is_py3k == 1 } {
gdb_py_test_silent_cmd "python long = int" "" 0
}
gdb_test "python print (gdb.solib_name(long(func1)))" "py-shared-sl.sl" "test func1 solib location"
gdb_test "python print (gdb.solib_name(int(func1)))" "py-shared-sl.sl" "test func1 solib location"
gdb_test "p &main" "" "main address"
gdb_py_test_silent_cmd "python main = gdb.history(0)" "Aquire main address" 1
gdb_test "python print (gdb.solib_name(long(main)))" "None" "test main solib location"
gdb_test "python print (gdb.solib_name(int(main)))" "None" "test main solib location"
if {[is_lp64_target]} {
gdb_test "python print (len(\[gdb.solib_name(0xffffffffffffffff)\]))" "1"

View file

@ -47,33 +47,19 @@ proc build_inferior {exefile lang} {
proc test_value_creation {} {
global gdb_prompt
global gdb_py_is_py3k
gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1
gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1
gdb_py_test_silent_cmd "python i = gdb.Value (3,None)" "create integer value, with None type" 1
if { $gdb_py_is_py3k == 0 } {
gdb_py_test_silent_cmd "python i = gdb.Value (5L)" "create long value" 1
}
gdb_py_test_silent_cmd "python l = gdb.Value(0xffffffff12345678)" "create large unsigned 64-bit value" 1
if { $gdb_py_is_py3k == 0 } {
gdb_test "python print long(l)" "18446744069720004216" "large unsigned 64-bit int conversion to python"
} else {
gdb_test "python print (int(l))" "18446744069720004216" "large unsigned 64-bit int conversion to python"
}
gdb_test "python print (int(l))" "18446744069720004216" "large unsigned 64-bit int conversion to python"
gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1
gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1
gdb_test "python print (a)" "\"string test\"" "print 8-bit string"
gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string"
if { $gdb_py_is_py3k == 0 } {
gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1
gdb_test "python print (a)" "\"unicode test\"" "print Unicode string"
gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of unicode string"
}
# Test address attribute is None in a non-addressable value
gdb_test "python print ('result = %s' % i.address)" "= None" "test address attribute in non-addressable value"
}
@ -92,7 +78,6 @@ proc test_value_reinit {} {
proc test_value_numeric_ops {} {
global gdb_prompt
global gdb_py_is_py3k
gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
@ -144,9 +129,6 @@ proc test_value_numeric_ops {} {
gdb_test_no_output "python b = gdb.history (0)" ""
gdb_test "python print(int(b))" "5" "convert pointer to int"
if {!$gdb_py_is_py3k} {
gdb_test "python print(long(b))" "5" "convert pointer to long"
}
gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer"
gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value"
@ -156,17 +138,11 @@ proc test_value_numeric_ops {} {
"result = r" "use value as string index"
gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \
"result = 1" "use value as tuple index"
if {!$gdb_py_is_py3k} {
gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
"result = 1" "use value as array index"
}
gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
"result = 1" "use value as array index"
gdb_test "python print('%x' % int(gdb.parse_and_eval('-1ull')))" \
"f+" "int conversion respect type sign"
if {!$gdb_py_is_py3k} {
gdb_test "python print('%x' % long(gdb.parse_and_eval('-1ull')))" \
"f+" "long conversion respect type sign"
}
# Test some invalid operations.
@ -242,7 +218,6 @@ proc test_value_compare {} {
proc test_value_in_inferior {} {
global gdb_prompt
global testfile
global gdb_py_is_py3k
gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
gdb_continue_to_breakpoint "break to inspect struct and union"
@ -253,9 +228,6 @@ proc test_value_in_inferior {} {
gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value s from history" 1
gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name"
if { $gdb_py_is_py3k == 0 } {
gdb_test "python print ('result = ' + str(s\[u'a'\]))" " = 3" "access element inside struct using unicode name"
}
# Test dereferencing the argv pointer
@ -533,13 +505,8 @@ proc test_value_hash {} {
}
proc test_float_conversion {} {
global gdb_py_is_py3k
gdb_test "python print(int(gdb.Value(0)))" "0"
gdb_test "python print(int(gdb.Value(2.5)))" "2"
if {!$gdb_py_is_py3k} {
gdb_test "python print(long(gdb.Value(0)))" "0"
gdb_test "python print(long(gdb.Value(2.5)))" "2"
}
gdb_test "python print(float(gdb.Value(2.5)))" "2\\.5"
gdb_test "python print(float(gdb.Value(0)))" "0\\.0"
}
@ -565,7 +532,6 @@ proc prepare_type_and_buffer {} {
proc test_value_from_buffer {} {
global gdb_prompt
global gdb_py_is_py3k
prepare_type_and_buffer
gdb_test "python v=gdb.Value(b,tp); print(v)" "1" \