This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH 3/5] Make Python inferior-related internal functions return inferior_object*
- From: Simon Marchi <simon dot marchi at ericsson dot com>
- To: <gdb-patches at sourceware dot org>
- Cc: Simon Marchi <simon dot marchi at polymtl dot ca>
- Date: Mon, 23 Jan 2017 17:40:02 -0500
- Subject: [PATCH 3/5] Make Python inferior-related internal functions return inferior_object*
- Authentication-results: sourceware.org; auth=none
- Authentication-results: spf=none (sender IP is ) smtp.mailfrom=simon dot marchi at ericsson dot com;
- References: <20170123224004.8893-1-simon.marchi@ericsson.com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
From: Simon Marchi <simon.marchi@polymtl.ca>
This patch changes the functions that return an Inferior Python object
so they return an inferior_object pointer instead of a generic PyObject
pointer. This should remove some casting back and forth from PyObject*
to inferior_object* in our codebase, helping a bit for type-safety.
On the other hand, this requires adding some casts from inferior_object*
to PyObject* when we deal with the Python API, which obviously accepts
only PyObject*. I think this is a more appropriate place for casts
though.
It defines and uses gdbpy_inf_ref, a specialized version of
gdbpy_ref_base for inferior_object.
The next patch changes those functions again to make them return a
gdbpy_inf_ref, but I think that this intermediary step is useful to
verify the correctness, since the two changes have different goals
(type-safety for this one, reference counting for the next one).
One point I am not sure about is whether it breaks the gcc Python
checking plugin:
extern PyTypeObject inferior_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object");
It says _FOR_TYPEDEF, but the inferior_object type is no longer a
typedef, because I need to forward declare the type in
python-internal.h, and it's not possible to forward-declare a typedef.
I tried to use the cpychecker plugin but have to admit I am bit lost. I
understand some manual fiddling with the gcc flags are required in order
load the plugin (which can be done with gcc-with-cpychecker). It would
be nice (if it's not the case yet) to have a configure switch to
automatically enable that for the files in the python subdirectory.
gdb/ChangeLog:
* python/py-ref.h (gdbpy_inf_ref): New typedef.
* python/python-internal.h (struct inferior_object):
Forward-declare.
(thread_object) <inf_obj>: Change type to inferior_object*.
(find_inferior_object): Change return type to inferior_object*.
(inferior_to_inferior_object): Likewise.
* python/py-exitedevent.c (create_exited_event_object): Use
gdbpy_inf_ref.
* python/py-inferior.c (inferior_object): Change from typedef +
anonymous struct to simple struct.
(inferior_to_inferior_object): Change return type to
inferior_object*.
(find_inferior_object): Likewise.
(find_thread_object): Use gdbpy_inf_ref.
(build_inferior_list): Likewise.
(gdbpy_selected_inferior): Add cast.
* python/py-infthread.c (thpy_get_inferior): Add cast.
---
gdb/python/py-exitedevent.c | 4 ++--
gdb/python/py-inferior.c | 18 +++++++++---------
gdb/python/py-infthread.c | 2 +-
gdb/python/py-ref.h | 1 +
gdb/python/python-internal.h | 7 ++++---
5 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 4590077f8b..30dce9030b 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -42,10 +42,10 @@ create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
return NULL;
}
- gdbpy_ref inf_obj (inferior_to_inferior_object (inf));
+ gdbpy_inf_ref inf_obj (inferior_to_inferior_object (inf));
if (inf_obj == NULL || evpy_add_attribute (exited_event.get (),
"inferior",
- inf_obj.get ()) < 0)
+ inf_obj.get_py_obj ()) < 0)
return NULL;
return exited_event.release ();
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 5c215fef44..b6b43af7cd 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -35,7 +35,7 @@ struct threadlist_entry {
struct threadlist_entry *next;
};
-typedef struct
+struct inferior_object
{
PyObject_HEAD
@@ -48,7 +48,7 @@ typedef struct
/* Number of threads in the list. */
int nthreads;
-} inferior_object;
+};
extern PyTypeObject inferior_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object");
@@ -207,7 +207,7 @@ python_new_objfile (struct objfile *objfile)
representing INFERIOR. If the object has already been created,
return it and increment the reference count, otherwise, create it.
Return NULL on failure. */
-PyObject *
+inferior_object *
inferior_to_inferior_object (struct inferior *inferior)
{
inferior_object *inf_obj;
@@ -233,13 +233,13 @@ inferior_to_inferior_object (struct inferior *inferior)
else
Py_INCREF ((PyObject *)inf_obj);
- return (PyObject *) inf_obj;
+ return inf_obj;
}
/* Finds the Python Inferior object for the given PID. Returns a
reference, or NULL if PID does not match any inferior object. */
-PyObject *
+inferior_object *
find_inferior_object (int pid)
{
struct inferior *inf = find_inferior_pid (pid);
@@ -260,7 +260,7 @@ find_thread_object (ptid_t ptid)
if (pid == 0)
return NULL;
- gdbpy_ref inf_obj (find_inferior_object (pid));
+ gdbpy_inf_ref inf_obj (find_inferior_object (pid));
if (inf_obj == NULL)
return NULL;
@@ -409,12 +409,12 @@ static int
build_inferior_list (struct inferior *inf, void *arg)
{
PyObject *list = (PyObject *) arg;
- gdbpy_ref inferior (inferior_to_inferior_object (inf));
+ gdbpy_inf_ref inferior (inferior_to_inferior_object (inf));
if (inferior == NULL)
return 0;
- return PyList_Append (list, inferior.get ()) ? 1 : 0;
+ return PyList_Append (list, inferior.get_py_obj ()) ? 1 : 0;
}
/* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
@@ -815,7 +815,7 @@ py_free_inferior (struct inferior *inf, void *datum)
PyObject *
gdbpy_selected_inferior (PyObject *self, PyObject *args)
{
- return inferior_to_inferior_object (current_inferior ());
+ return (PyObject *) inferior_to_inferior_object (current_inferior ());
}
int
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index 5482bf9ea1..c7553310c3 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -163,7 +163,7 @@ thpy_get_inferior (PyObject *self, void *ignore)
THPY_REQUIRE_VALID (thread_obj);
- return thread_obj->inf_obj;
+ return (PyObject *) thread_obj->inf_obj;
}
/* Implementation of InferiorThread.switch ().
diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index 8b3b7732cc..b212ef195f 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -67,5 +67,6 @@ public:
/* Specializations of gdbpy_ref_base for concrete Python object types. */
typedef gdbpy_ref_base<PyObject> gdbpy_ref;
+typedef gdbpy_ref_base<inferior_object> gdbpy_inf_ref;
#endif /* GDB_PYTHON_REF_H */
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index ac98f12ab3..62a834d403 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -303,6 +303,7 @@ typedef struct gdbpy_breakpoint_object
constructor and the breakpoint-created hook function. */
extern gdbpy_breakpoint_object *bppy_pending_object;
+struct inferior_object;
typedef struct
{
@@ -312,7 +313,7 @@ typedef struct
struct thread_info *thread;
/* The Inferior object to which this thread belongs. */
- PyObject *inf_obj;
+ inferior_object *inf_obj;
} thread_object;
extern struct cmd_list_element *set_python_list;
@@ -420,8 +421,8 @@ PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch);
thread_object *create_thread_object (struct thread_info *tp);
thread_object *find_thread_object (ptid_t ptid)
CPYCHECKER_RETURNS_BORROWED_REF;
-PyObject *find_inferior_object (int pid);
-PyObject *inferior_to_inferior_object (struct inferior *inferior);
+inferior_object *find_inferior_object (int pid);
+inferior_object *inferior_to_inferior_object (struct inferior *inferior);
const struct block *block_object_to_block (PyObject *obj);
struct symbol *symbol_object_to_symbol (PyObject *obj);
--
2.11.0