From 90fe61ced1c9aa4afb263326e336330d15603fbf Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Mon, 29 Nov 2021 13:53:06 +0000 Subject: gdb/python: don't use the 'p' format for parsing args When running the gdb.python/py-arch.exp tests on a GDB built against Python 2 I ran into some errors. The problem is that this test script exercises the gdb.Architecture.integer_type method, and this method uses 'p' as an argument format specifier in a call to gdb_PyArg_ParseTupleAndKeywords. Unfortunately this specified was only added in Python 3.3, so will cause an error for earlier versions of Python. This commit switches to use the 'O' specifier to collect a PyObject, and then uses PyObject_IsTrue to convert the object to a boolean. An earlier version of this patch incorrectly switched from using 'p' to use 'i', however, it was pointed out during review that this would cause some changes in behaviour, for example both of these will work with 'p', but not with 'i': gdb.selected_inferior().architecture().integer_type(32, None) gdb.selected_inferior().architecture().integer_type(32, "foo") The new approach of using 'O' works fine with these cases. I've added some new tests to cover both of the above. There should be no user visible changes after this commit. --- gdb/python/py-arch.c | 11 ++++++++--- gdb/testsuite/gdb.python/py-arch.exp | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index aa9a652..824ad0b 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -276,12 +276,17 @@ static PyObject * archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw) { static const char *keywords[] = { "size", "signed", NULL }; - int size, is_signed = 1; + int size; + PyObject *is_signed_obj = nullptr; - if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "i|p", keywords, - &size, &is_signed)) + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "i|O", keywords, + &size, &is_signed_obj)) return nullptr; + /* Assume signed by default. */ + bool is_signed = (is_signed_obj == nullptr + || PyObject_IsTrue (is_signed_obj)); + struct gdbarch *gdbarch; ARCHPY_REQUIRE_VALID (self, gdbarch); diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp index f3bf01d..14dc1bf 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -64,7 +64,7 @@ if { ![is_address_zero_readable] } { } foreach size {0 1 2 3 4 8 16} { - foreach sign {"" ", True" ", False"} { + foreach sign {"" ", True" ", False" ", None" ", \"blah\""} { set fullsize [expr 8 * $size] gdb_test_no_output "python t = arch.integer_type($fullsize$sign)" \ "get integer type for $size$sign" -- cgit v1.1