]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Allow setting a parameter to raise gdb.GdbError
authorTom Tromey <tom@tromey.com>
Sat, 15 Sep 2018 07:09:22 +0000 (01:09 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 24 Sep 2018 05:15:12 +0000 (23:15 -0600)
A convention in the Python layer is that raising a gdb.GdbError will
not print the Python stack -- instead the exception is treated as any
other gdb exception.

PR python/18852 asks that this treatment be extended the the
get_set_value method of gdb.Parameter.  This makes sense, because it
lets Python-created parameters act like gdb parameters.

2018-09-23  Tom Tromey  <tom@tromey.com>

PR python/18852:
* python/py-param.c (get_set_value): Use gdbpy_handle_exception.

gdb/doc/ChangeLog
2018-09-23  Tom Tromey  <tom@tromey.com>

PR python/18852:
* python.texi (Parameters In Python): Document exception behavior
of get_set_string.

gdb/testsuite/ChangeLog
2018-09-23  Tom Tromey  <tom@tromey.com>

PR python/18852:
* gdb.python/py-parameter.exp: Add test for parameter that throws
on "set".

gdb/ChangeLog
gdb/doc/ChangeLog
gdb/doc/python.texi
gdb/python/py-param.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-parameter.exp

index cbbd146fc94a812cca1a16b19ba0113930e56899..e8005c0255420ae0845e3834bb0da35a82f68060 100644 (file)
@@ -1,3 +1,8 @@
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
+       PR python/18852:
+       * python/py-param.c (get_set_value): Use gdbpy_handle_exception.
+
 2018-09-23  Tom Tromey  <tom@tromey.com>
 
        * python/py-function.c (fnpy_call): Use gdbpy_handle_exception.
index 6d12553a28cd406a132f890d4ff0bf98baad5f42..4285a4d8bb6a63153fce289920fc17f5ece9a171 100644 (file)
@@ -1,3 +1,9 @@
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
+       PR python/18852:
+       * python.texi (Parameters In Python): Document exception behavior
+       of get_set_string.
+
 2018-09-18  John Baldwin  <jhb@FreeBSD.org>
 
        * gdb.texinfo (info proc): Remove "running".
index e98178a1216c8e4668f78a3946d02f82bf55cf4c..1035be33f001fa591b949e4282163dfa9a54108c 100644 (file)
@@ -3828,6 +3828,30 @@ example, @kbd{set foo off}).  The @code{value} attribute has already
 been populated with the new value and may be used in output.  This
 method must return a string.  If the returned string is not empty,
 @value{GDBN} will present it to the user.
+
+If this method raises the @code{gdb.GdbError} exception
+(@pxref{Exception Handling}), then @value{GDBN} will print the
+exception's string and the @code{set} command will fail.  Note,
+however, that the @code{value} attribute will not be reset in this
+case.  So, if your parameter must validate values, it should store the
+old value internally and reset the exposed value, like so:
+
+@smallexample
+class ExampleParam (gdb.Parameter):
+   def __init__ (self, name):
+      super (ExampleParam, self).__init__ (name,
+                   gdb.COMMAND_DATA,
+                   gdb.PARAM_BOOLEAN)
+      self.value = True
+      self.saved_value = True
+   def validate(self):
+      return False
+   def get_set_string (self):
+      if not self.validate():
+        self.value = self.saved_value
+        raise gdb.GdbError('Failed to validate')
+      self.saved_value = self.value
+@end smallexample
 @end defun
 
 @defun Parameter.get_show_string (self, svalue)
index 0f0214befe00559efe98a34109ffd9f72166f86c..bff5d60fc26cb8e1742600323f57f52909f79649 100644 (file)
@@ -396,10 +396,7 @@ get_set_value (const char *args, int from_tty,
     {
       set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
       if (! set_doc_string)
-       {
-         gdbpy_print_stack ();
-         return;
-       }
+       gdbpy_handle_exception ();
     }
 
   const char *str = set_doc_string.get ();
index 4a624ddd662310e560151e2fcbcea98352ecc0cf..877edc0cb5cd5ddd07014aed5a8268ea5bff005a 100644 (file)
@@ -1,3 +1,9 @@
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
+       PR python/18852:
+       * gdb.python/py-parameter.exp: Add test for parameter that throws
+       on "set".
+
 2018-09-23  Tom Tromey  <tom@tromey.com>
 
        PR python/17284:
index 0508544f9d9c9f1ee3f330c47ac0e65d25eee3db..9a36e6372deac48a4de283e5a4bdfefb804a5ea9 100644 (file)
@@ -203,3 +203,18 @@ foreach kind {PARAM_ZUINTEGER PARAM_ZUINTEGER_UNLIMITED} {
            "check that PARAM_ZUINTEGER value is -1 after setting"
     }
 }
+
+gdb_py_test_multiple "Throwing gdb parameter" \
+    "python" "" \
+    "class TestThrowParam (gdb.Parameter):" "" \
+    "   def __init__ (self, name):" "" \
+    "      super (TestThrowParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_STRING)" "" \
+    "      self.value = True" "" \
+    "   def get_set_string (self):" "" \
+    "      raise gdb.GdbError('Ordinary gdb error')" "" \
+    "test_throw_param = TestThrowParam ('print test-throw-param')" ""\
+    "end"
+
+gdb_test "set print test-throw-param whoops" \
+    "Ordinary gdb error" \
+    "gdb.GdbError does not show Python stack"