[2/5] gdb/python: move PyLong_From* calls into py-utils.c

Message ID 2e8e11d00b1eea71a99a49d5860deb3392372c91.1622321523.git.andrew.burgess@embecosm.com
State New
Headers show
Series
  • Fix for an assertion when unwinding with inline frames
Related show

Commit Message

Andrew Burgess May 29, 2021, 8:57 p.m.
We already have two helper functions in py-utils.c:

  gdb_py_object_from_longest (LONGEST l)
  gdb_py_object_from_ulongest (ULONGEST l)

these wrap around calls to either PyLong_FromLongLong,
PyLong_FromLong, or PyInt_From_Long (if Python 2 is being used).

There is one place in gdb/python/* where a call to PyLong_FromLong was
added outside of the above utility functions, this was done in the
recent commit:

  commit 55789354fcbaf879f3ca8475b647b2747dec486e
  Date:   Fri May 14 11:56:31 2021 +0200

      gdb/python: add a 'connection_num' attribute to Inferior objects

In this commit I propose that we move this new call to PyLong_FromLong
into a new utility function gdb_py_object_from_int.  The biggest win I
see here is that we have a consistent interface for converting number
like things into Python objects, however, I have made it such that on
Python 2 we will call PyInt_FromLong, so there is a slight change in
behaviour here, which seems to be inline with how we handle the
LONGEST types.

Beyond the PyLong/PyInt change described above there should be no user
visible changes from this commit.

gdb/ChangeLog:

	* python/py-inferior.c (infpy_get_connection_num): Call new
	function gdb_py_object_from_int.
	* python/py-utils.c (gdb_py_object_from_int): New function.
	* python/python-internal.h (gdb_py_object_from_int): Declare.
---
 gdb/ChangeLog                |  7 +++++++
 gdb/python/py-inferior.c     |  2 +-
 gdb/python/py-utils.c        | 12 ++++++++++++
 gdb/python/python-internal.h |  2 ++
 4 files changed, 22 insertions(+), 1 deletion(-)

-- 
2.25.4

Comments

Tom Tromey June 7, 2021, 2:53 p.m. | #1
>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:


Andrew> gdb/ChangeLog:

Andrew> 	* python/py-inferior.c (infpy_get_connection_num): Call new
Andrew> 	function gdb_py_object_from_int.
Andrew> 	* python/py-utils.c (gdb_py_object_from_int): New function.
Andrew> 	* python/python-internal.h (gdb_py_object_from_int): Declare.

This seems fine to me.

It would also probably be ok to just rely on promotion and always use
gdb_py_object_from_longest.

Tom
Andrew Burgess June 21, 2021, 7:42 p.m. | #2
* Tom Tromey <tom@tromey.com> [2021-06-07 08:53:52 -0600]:

> >>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:

> 

> Andrew> gdb/ChangeLog:

> 

> Andrew> 	* python/py-inferior.c (infpy_get_connection_num): Call new

> Andrew> 	function gdb_py_object_from_int.

> Andrew> 	* python/py-utils.c (gdb_py_object_from_int): New function.

> Andrew> 	* python/python-internal.h (gdb_py_object_from_int): Declare.

> 

> This seems fine to me.

> 

> It would also probably be ok to just rely on promotion and always use

> gdb_py_object_from_longest.


I took this advice and pushed the patch below.

Thanks,
Andrew

---

commit 8b9c48b287d42d1c816f441e4273dcb8c7af1876
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Wed May 26 21:28:11 2021 +0100

    gdb/python: move PyLong_From* calls into py-utils.c
    
    We already have two helper functions in py-utils.c:
    
      gdb_py_object_from_longest (LONGEST l)
      gdb_py_object_from_ulongest (ULONGEST l)
    
    these wrap around calls to either PyLong_FromLongLong,
    PyLong_FromLong, or PyInt_From_Long (if Python 2 is being used).
    
    There is one place in gdb/python/* where a call to PyLong_FromLong was
    added outside of the above utility functions, this was done in the
    recent commit:
    
      commit 55789354fcbaf879f3ca8475b647b2747dec486e
      Date:   Fri May 14 11:56:31 2021 +0200
    
          gdb/python: add a 'connection_num' attribute to Inferior objects
    
    In this commit I replace the direct use of PyLong_FromLong with a call
    to gdb_py_object_from_longest.  The only real change with this commit,
    is that, for Python 2, we will now end up calling PyInt_FromLong
    instead of PyLong_FromLong, but this should be invisible to the user.
    For Python 3 there should be absolutely no change.
    
    gdb/ChangeLog:
    
            * python/py-inferior.c (infpy_get_connection_num): Call
            gdb_py_object_from_longest instead of PyLong_FromLong directly.

diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 336c6426b8c..39efa804d80 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -441,7 +441,7 @@ infpy_get_connection_num (PyObject *self, void *closure)
   if (target == nullptr)
     Py_RETURN_NONE;
 
-  return PyLong_FromLong (target->connection_number);
+  return gdb_py_object_from_longest (target->connection_number).release ();
 }
 
 static PyObject *

Patch

diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 336c6426b8c..bdbde0b3e61 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -441,7 +441,7 @@  infpy_get_connection_num (PyObject *self, void *closure)
   if (target == nullptr)
     Py_RETURN_NONE;
 
-  return PyLong_FromLong (target->connection_number);
+  return gdb_py_object_from_int (target->connection_number).release ();
 }
 
 static PyObject *
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 10c4173efcd..c3f712debcb 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -335,6 +335,18 @@  gdb_py_object_from_ulongest (ULONGEST l)
 #endif
 }
 
+/* Convert an int I to the appropriate Python object.  */
+
+gdbpy_ref<>
+gdb_py_object_from_int (int i)
+{
+#ifdef IS_PY3K
+  return gdbpy_ref<> (PyLong_FromLong (i));
+#else
+  return gdbpy_ref<> (PyInt_FromLong (l));
+#endif
+}
+
 /* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts
    the value into an out parameter.  */
 
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 690d2fb43c0..91b5bea58eb 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -747,6 +747,8 @@  int get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
 
 gdbpy_ref<> gdb_py_object_from_longest (LONGEST l);
 gdbpy_ref<> gdb_py_object_from_ulongest (ULONGEST l);
+gdbpy_ref<> gdb_py_object_from_int (int i);
+
 int gdb_py_int_as_long (PyObject *, long *);
 
 PyObject *gdb_py_generic_dict (PyObject *self, void *closure);