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

Andrew Burgess
2021-05-29T20:57:24Z

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(-)

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);
--
2.25.4