This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python][commit] Use tp_richcompare instead of Frame.equals tocompare frames.


Hi,

Just committed the following, implementing a suggestion from Tromey when
reviewing the frames patch.
-- 
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center


commit 4cbedfdc756e827c0cad49a43e68917487988b3c
Author: Thiago Jung Bauermann <bauerman@br.ibm.com>
Date:   Sun Mar 29 23:34:57 2009 -0300

    Use tp_richcompare instead of Frame.equals to compare frames.
    
    gdb/
    	* python/python-frame.c (frapy_equal_p): Remove function.
    	(frapy_richcompare): New function.
    	(frame_object_methods): Remove `equals' element.
    	(frame_object_type): Set tp_richcompare element with
    	frapy_richcompare function.
    
    gdb/testsuite/
    	* gdb.python/python-frame.exp: Change tests using Frame.equals
    	to use the == operator instead.
    
    gdb/doc/
    
    	* gdb.texinfo (Frames In Python): Document that Frame objects
    	can be compared with the == operator.  Remove documentation
    	for the equals method.

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d7aeb5a..4c92495 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19485,6 +19485,14 @@ while its corresponding frame exists in the inferior's stack.  If you try
 to use an invalid frame object, @value{GDBN} will throw a @code{RuntimeError}
 exception.
 
+Two @code{gdb.Frame} objects can be compared for equality with the @code{==}
+operator, like:
+
+@smallexample
+(@value{GDBP}) python print gdb.newest_frame() == gdb.selected_frame ()
+True
+@end smallexample
+
 The following frame-related functions are available in the @code{gdb} module:
 
 @defun frames
@@ -19508,10 +19516,6 @@ frames, as expressed by the given @var{reason} code (an integer, see the
 A @code{gdb.Frame} object has the following methods:
 
 @table @code
-@defmethod Frame equals frame
-Compare frames.
-@end defmethod
-
 @defmethod Frame is_valid
 Returns true if the @code{gdb.Frame} object is valid, false if not.
 A frame object can become invalid if the frame it refers to doesn't
diff --git a/gdb/python/python-frame.c b/gdb/python/python-frame.c
index e4267d1..6c33e65 100644
--- a/gdb/python/python-frame.c
+++ b/gdb/python/python-frame.c
@@ -109,31 +109,6 @@ frapy_is_valid (PyObject *self, PyObject *args)
   Py_RETURN_TRUE;
 }
 
-/* Implementation of gdb.Frame.equals (self, other) -> Boolean. */
-
-static PyObject *
-frapy_equal_p (PyObject *self, PyObject *args)
-{
-  int equalp = 0;	  /* Initialize to appease gcc warning.  */
-  frame_object *self_frame = (frame_object *) self;
-  frame_object *other;
-  volatile struct gdb_exception except;
-
-  if (!PyArg_ParseTuple (args, "O!", &frame_object_type, &other))
-    return NULL;
-
-  TRY_CATCH (except, RETURN_MASK_ALL)
-    {
-      equalp = frame_id_eq (self_frame->frame_id, other->frame_id);
-    }
-  GDB_PY_HANDLE_EXCEPTION (except);
-
-  if (equalp)
-    Py_RETURN_TRUE;
-
-  Py_RETURN_FALSE;
-}
-
 /* Implementation of gdb.Frame.name (self) -> String.
    Returns the name of the function corresponding to this frame.  */
 
@@ -581,6 +556,31 @@ gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
   return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
 }
 
+/* Implements the equality comparison for Frame objects.
+   All other comparison operators will throw a TypeError Python exception,
+   as they aren't valid for frames.  */
+
+static PyObject *
+frapy_richcompare (PyObject *self, PyObject *other, int op)
+{
+  if (!PyObject_TypeCheck (other, &frame_object_type))
+    {
+      PyErr_SetString (PyExc_TypeError, "Frame object expected in comparison.");
+      return NULL;
+    }
+  else if (op != Py_EQ)
+    {
+      PyErr_SetString (PyExc_TypeError, "Invalid comparison for gdb.Frame.");
+      return NULL;
+    }
+
+  if (frame_id_eq (((frame_object *) self)->frame_id,
+		   ((frame_object *) other)->frame_id))
+    Py_RETURN_TRUE;
+
+  Py_RETURN_FALSE;
+}
+
 /* Sets up the Frame API in the gdb module.  */
 
 void
@@ -616,9 +616,6 @@ gdbpy_initialize_frames (void)
 
 
 static PyMethodDef frame_object_methods[] = {
-  { "equals", frapy_equal_p, METH_VARARGS,
-    "equals (frame) -> Boolean.\n\
-Compare this frame to the given frame." },
   { "is_valid", frapy_is_valid, METH_NOARGS,
     "is_valid () -> Boolean.\n\
 Return true if this frame is valid, false if not." },
@@ -680,7 +677,7 @@ static PyTypeObject frame_object_type = {
   "GDB frame object",		  /* tp_doc */
   0,				  /* tp_traverse */
   0,				  /* tp_clear */
-  0,				  /* tp_richcompare */
+  frapy_richcompare,		  /* tp_richcompare */
   0,				  /* tp_weaklistoffset */
   0,				  /* tp_iter */
   0,				  /* tp_iternext */
diff --git a/gdb/testsuite/gdb.python/python-frame.exp b/gdb/testsuite/gdb.python/python-frame.exp
index 93a3d21..cbf1095 100644
--- a/gdb/testsuite/gdb.python/python-frame.exp
+++ b/gdb/testsuite/gdb.python/python-frame.exp
@@ -70,8 +70,8 @@ gdb_test "python print frames" "\\(<gdb.Frame object at 0x\[\[:xdigit:\]\]+>, <g
 gdb_py_test_silent_cmd "python f0 = frames\[0\]" "get first frame" 0
 gdb_py_test_silent_cmd "python f1 = frames\[1\]" "get second frame" 0
 
-gdb_test "python print 'result =', f0.equals (f1)" " = False" "test equals (false)"
-gdb_test "python print 'result =', f0.equals (f0)" " = True" "test equals (true)"
+gdb_test "python print 'result =', f0 == f1" " = False" "test equality comparison (false)"
+gdb_test "python print 'result =', f0 == f0" " = True" "test equality comparison (true)"
 gdb_test "python print 'result =', f0.is_valid ()" " = True" "test Frame.is_valid"
 gdb_test "python print 'result =', f0.name ()" " = f2" "test Frame.name"
 gdb_test "python print 'result =', f0.type () == gdb.NORMAL_FRAME" " = True" "test Frame.type"
@@ -79,14 +79,14 @@ gdb_test "python print 'result =', f0.unwind_stop_reason () == gdb.FRAME_UNWIND_
 gdb_test "python print 'result =', gdb.frame_stop_reason_string (gdb.FRAME_UNWIND_INNER_ID)" " = previous frame inner to this frame \\(corrupt stack\\?\\)" "test gdb.frame_stop_reason_string"
 gdb_test "python print 'result =', f0.pc ()" " = \[0-9\]+" "test Frame.pc"
 gdb_test "python print 'result =', f0.function ()" " = symbol for f2" "test Frame.function"
-gdb_test "python print 'result =', f0.older ().equals (f1)" " = True" "test Frame.older"
-gdb_test "python print 'result =', f1.newer ().equals (f0)" " = True" "test Frame.newer"
+gdb_test "python print 'result =', f0.older () == f1" " = True" "test Frame.older"
+gdb_test "python print 'result =', f1.newer () == f0" " = True" "test Frame.newer"
 gdb_test "python print 'result =', f0.read_var ('variable_which_surely_doesnt_exist')" \
   "ValueError: variable 'variable_which_surely_doesnt_exist' not found.*Error while executing Python code." \
   "test Frame.read_var - error"
 gdb_test "python print 'result =', f0.read_var ('a')" " = 1" "test Frame.read_var - success"
 
-gdb_test "python print 'result =', gdb.newest_frame ().equals (f0)" " = True" "test gdb.newest_frame"
-gdb_test "python print 'result =', gdb.selected_frame ().equals (f1)" " = True" "test gdb.selected_frame"
+gdb_test "python print 'result =', gdb.newest_frame () == f0" " = True" "test gdb.newest_frame"
+gdb_test "python print 'result =', gdb.selected_frame () == f1" " = True" "test gdb.selected_frame"
 
 gdb_test "python print 'result =', f0.block ()" "<gdb.Block object at 0x\[\[:xdigit:\]\]+>" "test Frame.block"



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]