]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: remove some additional PyObject_IsTrue calls
authorAndrew Burgess <aburgess@redhat.com>
Sun, 10 Nov 2024 14:34:58 +0000 (14:34 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Thu, 14 Nov 2024 19:34:43 +0000 (19:34 +0000)
After the previous commit I audited all our uses of PyObject_IsTrue
looking for places where we were missing an error check.  I did find
some that are missing error checks in places where we really should
have error checks, and I'll fix those in later commits.

This commit however, focuses on those locations where PyObject_IsTrue
is called, there is no error check, and the error check isn't really
necessary because we already know that the object we are dealing with
is of type PyBool_Type.

Inline with the previous commit, in these cases I have removed the
PyObject_IsTrue call, and replaced it with a comparison against
Py_True.  In one location where it is not obvious that the object we
have is PyBool_Type I've added an assert, but in the other cases the
comparison to Py_True immediately follows a PyBool_Check call, so an
assert would be redundant.

I've added a test for the gdb.Value.format_string styling argument
being passed a non-bool value as this wasn't previously being tested,
though this new test will pass before and after this commit.

There should be no functional change after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/python/py-disasm.c
gdb/python/py-value.c
gdb/testsuite/gdb.python/py-format-string.exp

index 7b64436d8bf3ba82037972b45c596a16045bc09a..58e2efe0819ab0c955a8b8fda0c50a83fa183616 100644 (file)
@@ -650,7 +650,7 @@ disasmpy_set_enabled (PyObject *self, PyObject *args, PyObject *kw)
       return nullptr;
     }
 
-  python_print_insn_enabled = PyObject_IsTrue (newstate);
+  python_print_insn_enabled = newstate == Py_True;
   Py_RETURN_NONE;
 }
 
index 119bf9f76d767018a14d0f42a05cb8288816ec64..eef3841924fabf31d5b203c7227e020c3e27a0a1 100644 (file)
@@ -815,7 +815,9 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
        }
     }
 
-  string_file stb (PyObject_IsTrue (styling_obj));
+  /* We force styling_obj to be a 'bool' when we parse the args above.  */
+  gdb_assert (PyBool_Check (styling_obj));
+  string_file stb (styling_obj == Py_True);
 
   try
     {
@@ -1988,9 +1990,8 @@ convert_value_from_python (PyObject *obj)
     {
       if (PyBool_Check (obj))
        {
-         cmp = PyObject_IsTrue (obj);
-         if (cmp >= 0)
-           value = value_from_longest (builtin_type_pybool, cmp);
+         cmp = obj == Py_True ? 1 : 0;
+         value = value_from_longest (builtin_type_pybool, cmp);
        }
       else if (PyLong_Check (obj))
        {
index d0349c546ad5099c9368f0a923ba90f24f729fd7..9102391b0be7ec6019e1056e5983c993b4913110 100644 (file)
@@ -1131,6 +1131,11 @@ proc_with_prefix test_invalid_args {} {
 proc test_styling {} {
     gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=True, raw=True))" \
        "{[style x variable] = 42, [style y variable] = 12}"
+
+    gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=None, raw=True))" \
+       [multi_line \
+            "Python Exception <class 'TypeError'>: argument 8 must be bool, not None" \
+            "Error occurred in Python: argument 8 must be bool, not None" ] \
 }
 
 # Test the gdb.print_options API.