From patchwork Sun Jan 15 13:42:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 18906 Received: (qmail 127453 invoked by alias); 15 Jan 2017 13:43:13 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 127406 invoked by uid 89); 15 Jan 2017 13:43:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS, T_FILL_THIS_FORM_SHORT autolearn=no version=3.3.2 spammy=3267, 2326, CATCH, H*RU:10.0.90.82 X-HELO: gproxy7-pub.mail.unifiedlayer.com Received: from gproxy7-pub.mail.unifiedlayer.com (HELO gproxy7-pub.mail.unifiedlayer.com) (70.40.196.235) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Sun, 15 Jan 2017 13:43:04 +0000 Received: (qmail 30698 invoked by uid 0); 15 Jan 2017 13:43:02 -0000 Received: from unknown (HELO CMOut01) (10.0.90.82) by gproxy7.mail.unifiedlayer.com with SMTP; 15 Jan 2017 13:43:02 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id Ydix1u00b2f2jeq01dj00W; Sun, 15 Jan 2017 06:43:00 -0700 X-Authority-Analysis: v=2.1 cv=H75InYoi c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IgFoBzBjUZAA:10 a=zstS-IiYAAAA:8 a=ubL2wKK9pDufVuLy4-YA:9 a=GaH23mFGNJjrQHOb:21 a=5BfTnyCZoi9aFiA9:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-16-146-181.hlrn.qwest.net ([174.16.146.181]:40602 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1cSl5G-0002Lj-FB; Sun, 15 Jan 2017 06:42:58 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 3/5] Introduce gdbpy_subclass and use it to simplify some logic Date: Sun, 15 Jan 2017 06:42:51 -0700 Message-Id: <20170115134253.24018-4-tom@tromey.com> In-Reply-To: <20170115134253.24018-1-tom@tromey.com> References: <20170115134253.24018-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1cSl5G-0002Lj-FB X-Source-Sender: 174-16-146-181.hlrn.qwest.net (bapiya.Home) [174.16.146.181]:40602 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== This introduces a new typed variant of gdbpy_ref, called gdbpy_subclass. This is then used to simplify logic in various parts of the Python layer; for example removing repeated error code or removing gotos. I wouldn't mind a better than than "gdb_subclass". One idea was to use gdb_ref with a default template parameter, and then change the existing uses of "gdb_ref" to "gdb_ref<>". 2017-01-15 Tom Tromey * python/py-cmd.c (cmdpy_destroyer): Use gdbpy_subclass. * python/py-breakpoint.c (gdbpy_breakpoint_deleted): Use gdbpy_subclass. * python/py-type.c (field_new): Use gdbpy_subclass. * python/py-symtab.c (symtab_and_line_to_sal_object): Use gdbpy_subclass. * python/py-progspace.c (pspy_new): Use gdbpy_subclass. (py_free_pspace): Likewise. (pspace_to_pspace_object): Likewise. * python/py-objfile.c (objfpy_new): Use gdbpy_subclass. (py_free_objfile): Likewise. (objfile_to_objfile_object): Likewise. * python/py-inferior.c (delete_thread_object): Use gdbpy_subclass. (infpy_read_memory): Likewise. (py_free_inferior): Likewise. * python/py-evtregistry.c (create_eventregistry_object): Use gdbpy_subclass. * python/py-event.c (create_event_object): Use gdbpy_subclass. * python/py-ref.h (gdbpy_ref_policy): Now a template class. (gdbpy_subclass): New typedef. --- gdb/ChangeLog | 24 ++++++++++++++++++++++++ gdb/python/py-breakpoint.c | 8 +++----- gdb/python/py-cmd.c | 5 +---- gdb/python/py-event.c | 16 +++++----------- gdb/python/py-evtregistry.c | 17 +++++++---------- gdb/python/py-frame.c | 9 ++++----- gdb/python/py-inferior.c | 26 +++++++++----------------- gdb/python/py-objfile.c | 41 +++++++++++++++++------------------------ gdb/python/py-progspace.c | 40 +++++++++++++++++----------------------- gdb/python/py-ref.h | 14 ++++++++++---- gdb/python/py-symtab.c | 17 +++++++---------- gdb/python/py-type.c | 12 +++++------- 12 files changed, 109 insertions(+), 120 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 08670fb..61e4ca4 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,29 @@ 2017-01-15 Tom Tromey + * python/py-cmd.c (cmdpy_destroyer): Use gdbpy_subclass. + * python/py-breakpoint.c (gdbpy_breakpoint_deleted): Use + gdbpy_subclass. + * python/py-type.c (field_new): Use gdbpy_subclass. + * python/py-symtab.c (symtab_and_line_to_sal_object): Use + gdbpy_subclass. + * python/py-progspace.c (pspy_new): Use gdbpy_subclass. + (py_free_pspace): Likewise. + (pspace_to_pspace_object): Likewise. + * python/py-objfile.c (objfpy_new): Use gdbpy_subclass. + (py_free_objfile): Likewise. + (objfile_to_objfile_object): Likewise. + * python/py-inferior.c (delete_thread_object): Use + gdbpy_subclass. + (infpy_read_memory): Likewise. + (py_free_inferior): Likewise. + * python/py-evtregistry.c (create_eventregistry_object): Use + gdbpy_subclass. + * python/py-event.c (create_event_object): Use gdbpy_subclass. + * python/py-ref.h (gdbpy_ref_policy): Now a template class. + (gdbpy_subclass): New typedef. + +2017-01-15 Tom Tromey + * ui-file.h (mem_fileopen): Return ui_file_up. (ui_file_up): New typedef. (ui_file_deleter): New struct. diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index f086725..f95a396 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -913,25 +913,23 @@ gdbpy_breakpoint_deleted (struct breakpoint *b) int num = b->number; PyGILState_STATE state; struct breakpoint *bp = NULL; - gdbpy_breakpoint_object *bp_obj; state = PyGILState_Ensure (); bp = get_breakpoint (num); if (bp) { - bp_obj = bp->py_bp_object; - if (bp_obj) + gdbpy_subclass bp_obj (bp->py_bp_object); + if (bp_obj != NULL) { if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted)) { - if (evpy_emit_event ((PyObject *) bp_obj, + if (evpy_emit_event ((PyObject *) bp_obj.get (), gdb_py_events.breakpoint_deleted) < 0) gdbpy_print_stack (); } bp_obj->bp = NULL; --bppy_live; - Py_DECREF (bp_obj); } } PyGILState_Release (state); diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 28c391c..b798c8a 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -95,14 +95,11 @@ cmdpy_dont_repeat (PyObject *self, PyObject *args) static void cmdpy_destroyer (struct cmd_list_element *self, void *context) { - cmdpy_object *cmd; - gdbpy_enter enter_py (get_current_arch (), current_language); /* Release our hold on the command object. */ - cmd = (cmdpy_object *) context; + gdbpy_subclass cmd ((cmdpy_object *) context); cmd->command = NULL; - Py_DECREF (cmd); /* We allocated the name, doc string, and perhaps the prefix name. */ diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c index a50da1f..1369fb4 100644 --- a/gdb/python/py-event.c +++ b/gdb/python/py-event.c @@ -30,21 +30,15 @@ evpy_dealloc (PyObject *self) PyObject * create_event_object (PyTypeObject *py_type) { - event_object *event_obj; - - event_obj = PyObject_New (event_object, py_type); - if (!event_obj) - goto fail; + gdbpy_subclass event_obj (PyObject_New (event_object, py_type)); + if (event_obj == NULL) + return NULL; event_obj->dict = PyDict_New (); if (!event_obj->dict) - goto fail; - - return (PyObject*) event_obj; + return NULL; - fail: - Py_XDECREF (event_obj); - return NULL; + return (PyObject*) event_obj.release (); } /* Add the attribute ATTR to the event object EVENT. In diff --git a/gdb/python/py-evtregistry.c b/gdb/python/py-evtregistry.c index 81bf927..023a7ce 100644 --- a/gdb/python/py-evtregistry.c +++ b/gdb/python/py-evtregistry.c @@ -20,6 +20,7 @@ #include "defs.h" #include "command.h" #include "py-events.h" +#include "py-ref.h" events_object gdb_py_events; @@ -79,22 +80,18 @@ evregpy_disconnect (PyObject *self, PyObject *function) eventregistry_object * create_eventregistry_object (void) { - eventregistry_object *eventregistry_obj; + gdbpy_subclass + eventregistry_obj (PyObject_New (eventregistry_object, + &eventregistry_object_type)); - eventregistry_obj = PyObject_New (eventregistry_object, - &eventregistry_object_type); - - if (!eventregistry_obj) + if (eventregistry_obj == NULL) return NULL; eventregistry_obj->callbacks = PyList_New (0); if (!eventregistry_obj->callbacks) - { - Py_DECREF (eventregistry_obj); - return NULL; - } + return NULL; - return eventregistry_obj; + return eventregistry_obj.release (); } static void diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index 3594be2..8b07f72 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -28,6 +28,7 @@ #include "symfile.h" #include "objfiles.h" #include "user-regs.h" +#include "py-ref.h" typedef struct { PyObject_HEAD @@ -361,9 +362,8 @@ frapy_function (PyObject *self, PyObject *args) PyObject * frame_info_to_frame_object (struct frame_info *frame) { - frame_object *frame_obj; - - frame_obj = PyObject_New (frame_object, &frame_object_type); + gdbpy_subclass frame_obj + (PyObject_New (frame_object, &frame_object_type)); if (frame_obj == NULL) return NULL; @@ -389,13 +389,12 @@ frame_info_to_frame_object (struct frame_info *frame) } CATCH (except, RETURN_MASK_ALL) { - Py_DECREF (frame_obj); gdbpy_convert_exception (except); return NULL; } END_CATCH - return (PyObject *) frame_obj; + return (PyObject *) frame_obj.release (); } /* Implementation of gdb.Frame.older (self) -> gdb.Frame. diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 9e10d62..e2f21d4 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -307,7 +307,6 @@ add_thread_object (struct thread_info *tp) static void delete_thread_object (struct thread_info *tp, int ignore) { - inferior_object *inf_obj; struct threadlist_entry **entry, *tmp; if (!gdb_python_initialized) @@ -315,9 +314,9 @@ delete_thread_object (struct thread_info *tp, int ignore) gdbpy_enter enter_py (python_gdbarch, python_language); - inf_obj - = (inferior_object *) find_inferior_object (ptid_get_pid (tp->ptid)); - if (!inf_obj) + gdbpy_subclass inf_obj + ((inferior_object *) find_inferior_object (ptid_get_pid (tp->ptid))); + if (inf_obj == NULL) return; /* Find thread entry in its inferior's thread_list. */ @@ -327,10 +326,7 @@ delete_thread_object (struct thread_info *tp, int ignore) break; if (!*entry) - { - Py_DECREF (inf_obj); - return; - } + return; tmp = *entry; tmp->thread_obj->thread = NULL; @@ -339,7 +335,6 @@ delete_thread_object (struct thread_info *tp, int ignore) inf_obj->nthreads--; Py_DECREF (tmp->thread_obj); - Py_DECREF (inf_obj); xfree (tmp); } @@ -446,7 +441,6 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) { CORE_ADDR addr, length; gdb_byte *buffer = NULL; - membuf_object *membuf_obj; PyObject *addr_obj, *length_obj, *result; static char *keywords[] = { "address", "length", NULL }; @@ -471,7 +465,8 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) } END_CATCH - membuf_obj = PyObject_New (membuf_object, &membuf_object_type); + gdbpy_subclass membuf_obj (PyObject_New (membuf_object, + &membuf_object_type)); if (membuf_obj == NULL) { xfree (buffer); @@ -483,12 +478,11 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) membuf_obj->length = length; #ifdef IS_PY3K - result = PyMemoryView_FromObject ((PyObject *) membuf_obj); + result = PyMemoryView_FromObject ((PyObject *) membuf_obj.get ()); #else - result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0, + result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj.get (), 0, Py_END_OF_BUFFER); #endif - Py_DECREF (membuf_obj); return result; } @@ -776,7 +770,7 @@ infpy_dealloc (PyObject *obj) static void py_free_inferior (struct inferior *inf, void *datum) { - inferior_object *inf_obj = (inferior_object *) datum; + gdbpy_subclass inf_obj ((inferior_object *) datum); struct threadlist_entry *th_entry, *th_tmp; if (!gdb_python_initialized) @@ -797,8 +791,6 @@ py_free_inferior (struct inferior *inf, void *datum) } inf_obj->nthreads = 0; - - Py_DECREF ((PyObject *) inf_obj); } /* Implementation of gdb.selected_inferior() -> gdb.Inferior. diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index 694fa09..136dfa7 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -24,6 +24,7 @@ #include "language.h" #include "build-id.h" #include "symtab.h" +#include "py-ref.h" typedef struct { @@ -227,18 +228,16 @@ objfpy_initialize (objfile_object *self) static PyObject * objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) { - objfile_object *self = (objfile_object *) type->tp_alloc (type, 0); + gdbpy_subclass self + ((objfile_object *) type->tp_alloc (type, 0)); - if (self) + if (self != NULL) { - if (!objfpy_initialize (self)) - { - Py_DECREF (self); - return NULL; - } + if (!objfpy_initialize (self.get ())) + return NULL; } - return (PyObject *) self; + return (PyObject *) self.release (); } PyObject * @@ -612,11 +611,9 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw) static void py_free_objfile (struct objfile *objfile, void *datum) { - objfile_object *object = (objfile_object *) datum; - gdbpy_enter enter_py (get_objfile_arch (objfile), current_language); + gdbpy_subclass object ((objfile_object *) datum); object->objfile = NULL; - Py_DECREF ((PyObject *) object); } /* Return a borrowed reference to the Python object of type Objfile @@ -627,26 +624,22 @@ py_free_objfile (struct objfile *objfile, void *datum) PyObject * objfile_to_objfile_object (struct objfile *objfile) { - objfile_object *object; - - object = (objfile_object *) objfile_data (objfile, objfpy_objfile_data_key); - if (!object) + gdbpy_subclass object + ((objfile_object *) objfile_data (objfile, objfpy_objfile_data_key)); + if (object == NULL) { - object = PyObject_New (objfile_object, &objfile_object_type); - if (object) + object.reset (PyObject_New (objfile_object, &objfile_object_type)); + if (object != NULL) { - if (!objfpy_initialize (object)) - { - Py_DECREF (object); - return NULL; - } + if (!objfpy_initialize (object.get ())) + return NULL; object->objfile = objfile; - set_objfile_data (objfile, objfpy_objfile_data_key, object); + set_objfile_data (objfile, objfpy_objfile_data_key, object.get ()); } } - return (PyObject *) object; + return (PyObject *) object.release (); } int diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index b0d9458..70e052b 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -24,6 +24,7 @@ #include "objfiles.h" #include "language.h" #include "arch-utils.h" +#include "py-ref.h" typedef struct { @@ -128,18 +129,16 @@ pspy_initialize (pspace_object *self) static PyObject * pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) { - pspace_object *self = (pspace_object *) type->tp_alloc (type, 0); + gdbpy_subclass self + ((pspace_object *) type->tp_alloc (type, 0)); - if (self) + if (self != NULL) { - if (!pspy_initialize (self)) - { - Py_DECREF (self); - return NULL; - } + if (!pspy_initialize (self.get ())) + return NULL; } - return (PyObject *) self; + return (PyObject *) self.release (); } PyObject * @@ -323,7 +322,7 @@ pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore) static void py_free_pspace (struct program_space *pspace, void *datum) { - pspace_object *object = (pspace_object *) datum; + gdbpy_subclass object ((pspace_object *) datum); /* This is a fiction, but we're in a nasty spot: The pspace is in the process of being deleted, we can't rely on anything in it. Plus this is one time when the current program space and current inferior @@ -337,7 +336,6 @@ py_free_pspace (struct program_space *pspace, void *datum) gdbpy_enter enter_py (arch, current_language); object->pspace = NULL; - Py_DECREF ((PyObject *) object); } /* Return a borrowed reference to the Python object of type Pspace @@ -348,26 +346,22 @@ py_free_pspace (struct program_space *pspace, void *datum) PyObject * pspace_to_pspace_object (struct program_space *pspace) { - pspace_object *object; - - object = (pspace_object *) program_space_data (pspace, pspy_pspace_data_key); - if (!object) + gdbpy_subclass object + ((pspace_object *) program_space_data (pspace, pspy_pspace_data_key)); + if (object == NULL) { - object = PyObject_New (pspace_object, &pspace_object_type); - if (object) + object.reset (PyObject_New (pspace_object, &pspace_object_type)); + if (object != NULL) { - if (!pspy_initialize (object)) - { - Py_DECREF (object); - return NULL; - } + if (!pspy_initialize (object.get ())) + return NULL; object->pspace = pspace; - set_program_space_data (pspace, pspy_pspace_data_key, object); + set_program_space_data (pspace, pspy_pspace_data_key, object.get ()); } } - return (PyObject *) object; + return (PyObject *) object.release (); } int diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h index b2479bf..3430768 100644 --- a/gdb/python/py-ref.h +++ b/gdb/python/py-ref.h @@ -1,6 +1,6 @@ /* Python reference-holding class - Copyright (C) 2016 Free Software Foundation, Inc. + Copyright (C) 2016, 2017 Free Software Foundation, Inc. This file is part of GDB. @@ -23,20 +23,26 @@ #include "common/gdb_ref_ptr.h" /* A policy class for gdb::ref_ptr for Python reference counting. */ +template struct gdbpy_ref_policy { - static void incref (PyObject *ptr) + static void incref (T *ptr) { Py_INCREF (ptr); } - static void decref (PyObject *ptr) + static void decref (T *ptr) { Py_DECREF (ptr); } }; /* A gdb::ref_ptr that has been specialized for Python objects. */ -typedef gdb::ref_ptr gdbpy_ref; +typedef gdb::ref_ptr > gdbpy_ref; + +/* A gdb::ref_ptr that has been specialized for "subclasses" of Python + objects. */ +template using gdbpy_subclass + = gdb::ref_ptr >; #endif /* GDB_PYTHON_REF_H */ diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c index 9fdb42f..770c898 100644 --- a/gdb/python/py-symtab.c +++ b/gdb/python/py-symtab.c @@ -24,6 +24,7 @@ #include "python-internal.h" #include "objfiles.h" #include "block.h" +#include "py-ref.h" typedef struct stpy_symtab_object { PyObject_HEAD @@ -436,19 +437,15 @@ symtab_to_symtab_object (struct symtab *symtab) PyObject * symtab_and_line_to_sal_object (struct symtab_and_line sal) { - sal_object *sal_obj; - - sal_obj = PyObject_New (sal_object, &sal_object_type); - if (sal_obj) + gdbpy_subclass sal_obj (PyObject_New (sal_object, + &sal_object_type)); + if (sal_obj != NULL) { - if (set_sal (sal_obj, sal) < 0) - { - Py_DECREF (sal_obj); - return NULL; - } + if (set_sal (sal_obj.get (), sal) < 0) + return NULL; } - return (PyObject *) sal_obj; + return (PyObject *) sal_obj.release (); } /* Return struct symtab_and_line reference that is wrapped by this diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index 235e8b5..cc1bbf5 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -130,18 +130,16 @@ field_dealloc (PyObject *obj) static PyObject * field_new (void) { - field_object *result = PyObject_New (field_object, &field_object_type); + gdbpy_subclass result (PyObject_New (field_object, + &field_object_type)); - if (result) + if (result != NULL) { result->dict = PyDict_New (); if (!result->dict) - { - Py_DECREF (result); - result = NULL; - } + return NULL; } - return (PyObject *) result; + return (PyObject *) result.release (); }