From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4667 invoked by alias); 20 Nov 2016 20:42:10 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 3532 invoked by uid 89); 20 Nov 2016 20:42:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy=sal, 893,8, 8938, UD:python.c X-HELO: gproxy5-pub.mail.unifiedlayer.com Received: from gproxy5-pub.mail.unifiedlayer.com (HELO gproxy5-pub.mail.unifiedlayer.com) (67.222.38.55) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Sun, 20 Nov 2016 20:41:48 +0000 Received: (qmail 23189 invoked by uid 0); 20 Nov 2016 20:41:46 -0000 Received: from unknown (HELO cmgw4) (10.0.90.85) by gproxy5.mail.unifiedlayer.com with SMTP; 20 Nov 2016 20:41:46 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id ALhj1u00W2f2jeq01Lhmms; Sun, 20 Nov 2016 13:41:46 -0700 X-Authority-Analysis: v=2.1 cv=Zpp+dbLG 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=L24OOQBejmoA:10 a=zstS-IiYAAAA:8 a=sZ0vxUEcPMg-C3_fEvYA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-16-143-211.hlrn.qwest.net ([174.16.143.211]:40590 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_1) (envelope-from ) id 1c8Yvn-0005f5-82; Sun, 20 Nov 2016 13:41:43 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 08/13] Use gdbpy_ref in python.c Date: Sun, 20 Nov 2016 20:42:00 -0000 Message-Id: <1479674496-14000-9-git-send-email-tom@tromey.com> In-Reply-To: <1479674496-14000-1-git-send-email-tom@tromey.com> References: <1479674496-14000-1-git-send-email-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1c8Yvn-0005f5-82 X-Source-Sender: 174-16-143-211.hlrn.qwest.net (bapiya.Home) [174.16.143.211]:40590 X-Source-Auth: tom+tromey.com X-Email-Count: 10 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-SW-Source: 2016-11/txt/msg00591.txt.bz2 This changes more places in python.c to use gdbpy_ref. Additionally, previously gdbpy_apply_type_printers would return EXT_LANG_RC_ERROR if a type printer returned None. However, that doesn't seem correct to me; this patch changes it to return EXT_LANG_RC_NOP in this case. 2016-11-20 Tom Tromey * python/python.c (eval_python_command, gdbpy_decode_line) (gdbpy_run_events, gdbpy_start_type_printers) (gdbpy_apply_type_printers): Use gdbpy_ref. --- gdb/ChangeLog | 6 +++ gdb/python/python.c | 120 +++++++++++++++++++++++----------------------------- 2 files changed, 59 insertions(+), 67 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 92fee8c..ab0cefb 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,11 @@ 2016-11-20 Tom Tromey + * python/python.c (eval_python_command, gdbpy_decode_line) + (gdbpy_run_events, gdbpy_start_type_printers) + (gdbpy_apply_type_printers): Use gdbpy_ref. + +2016-11-20 Tom Tromey + * python/py-param.c (get_doc_string, compute_enum_values): Use gdbpy_ref. diff --git a/gdb/python/python.c b/gdb/python/python.c index 7a38ca2..83b9805 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -264,7 +264,7 @@ gdbpy_check_quit_flag (const struct extension_language_defn *extlang) static int eval_python_command (const char *command) { - PyObject *m, *d, *v; + PyObject *m, *d; m = PyImport_AddModule ("__main__"); if (m == NULL) @@ -273,11 +273,10 @@ eval_python_command (const char *command) d = PyModule_GetDict (m); if (d == NULL) return -1; - v = PyRun_StringFlags (command, Py_single_input, d, d, NULL); + gdbpy_ref v (PyRun_StringFlags (command, Py_single_input, d, d, NULL)); if (v == NULL) return -1; - Py_DECREF (v); #ifndef IS_PY3K if (Py_FlushLine ()) PyErr_Clear (); @@ -672,9 +671,8 @@ gdbpy_decode_line (PyObject *self, PyObject *args) struct symtab_and_line sal; char *arg = NULL; struct cleanup *cleanups; - PyObject *result = NULL; - PyObject *return_result = NULL; - PyObject *unparsed = NULL; + gdbpy_ref result; + gdbpy_ref unparsed; struct event_location *location = NULL; if (! PyArg_ParseTuple (args, "|s", &arg)) @@ -723,9 +721,12 @@ gdbpy_decode_line (PyObject *self, PyObject *args) { int i; - result = PyTuple_New (sals.nelts); - if (! result) - goto error; + result.reset (PyTuple_New (sals.nelts)); + if (result == NULL) + { + do_cleanups (cleanups); + return NULL; + } for (i = 0; i < sals.nelts; ++i) { PyObject *obj; @@ -733,50 +734,47 @@ gdbpy_decode_line (PyObject *self, PyObject *args) obj = symtab_and_line_to_sal_object (sals.sals[i]); if (! obj) { - Py_DECREF (result); - goto error; + do_cleanups (cleanups); + return NULL; } - PyTuple_SetItem (result, i, obj); + PyTuple_SetItem (result.get (), i, obj); } } else { - result = Py_None; + result.reset (Py_None); Py_INCREF (Py_None); } - return_result = PyTuple_New (2); - if (! return_result) + gdbpy_ref return_result (PyTuple_New (2)); + if (return_result == NULL) { - Py_DECREF (result); - goto error; + do_cleanups (cleanups); + return NULL; } if (arg != NULL && strlen (arg) > 0) { - unparsed = PyString_FromString (arg); + unparsed.reset (PyString_FromString (arg)); if (unparsed == NULL) { - Py_DECREF (result); - Py_DECREF (return_result); - return_result = NULL; - goto error; + do_cleanups (cleanups); + return NULL; } } else { - unparsed = Py_None; + unparsed.reset (Py_None); Py_INCREF (Py_None); } - PyTuple_SetItem (return_result, 0, unparsed); - PyTuple_SetItem (return_result, 1, result); + PyTuple_SetItem (return_result.get (), 0, unparsed.release ()); + PyTuple_SetItem (return_result.get (), 1, result.release ()); - error: do_cleanups (cleanups); - return return_result; + return return_result.release (); } /* Parse a string and evaluate it as an expression. */ @@ -893,8 +891,6 @@ gdbpy_run_events (int error, gdb_client_data client_data) while (gdbpy_event_list) { - PyObject *call_result; - /* Dispatching the event might push a new element onto the event loop, so we update here "atomically enough". */ struct gdbpy_event *item = gdbpy_event_list; @@ -903,11 +899,10 @@ gdbpy_run_events (int error, gdb_client_data client_data) gdbpy_event_list_end = &gdbpy_event_list; /* Ignore errors. */ - call_result = PyObject_CallObject (item->event, NULL); + gdbpy_ref call_result (PyObject_CallObject (item->event, NULL)); if (call_result == NULL) PyErr_Clear (); - Py_XDECREF (call_result); Py_DECREF (item->event); xfree (item); } @@ -1327,36 +1322,33 @@ static void gdbpy_start_type_printers (const struct extension_language_defn *extlang, struct ext_lang_type_printers *ext_printers) { - PyObject *type_module, *func = NULL, *printers_obj = NULL; + PyObject *printers_obj = NULL; if (!gdb_python_initialized) return; gdbpy_enter enter_py (get_current_arch (), current_language); - type_module = PyImport_ImportModule ("gdb.types"); + gdbpy_ref type_module (PyImport_ImportModule ("gdb.types")); if (type_module == NULL) { gdbpy_print_stack (); - goto done; + return; } - func = PyObject_GetAttrString (type_module, "get_type_recognizers"); + gdbpy_ref func (PyObject_GetAttrString (type_module.get (), + "get_type_recognizers")); if (func == NULL) { gdbpy_print_stack (); - goto done; + return; } - printers_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL); + printers_obj = PyObject_CallFunctionObjArgs (func.get (), (char *) NULL); if (printers_obj == NULL) gdbpy_print_stack (); else ext_printers->py_type_printers = printers_obj; - - done: - Py_XDECREF (type_module); - Py_XDECREF (func); } /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE @@ -1371,8 +1363,6 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang, const struct ext_lang_type_printers *ext_printers, struct type *type, char **prettied_type) { - PyObject *type_obj, *type_module = NULL, *func = NULL; - PyObject *result_obj = NULL; PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers; gdb::unique_xmalloc_ptr result; @@ -1384,53 +1374,49 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang, gdbpy_enter enter_py (get_current_arch (), current_language); - type_obj = type_to_type_object (type); + gdbpy_ref type_obj (type_to_type_object (type)); if (type_obj == NULL) { gdbpy_print_stack (); - goto done; + return EXT_LANG_RC_ERROR; } - type_module = PyImport_ImportModule ("gdb.types"); + gdbpy_ref type_module (PyImport_ImportModule ("gdb.types")); if (type_module == NULL) { gdbpy_print_stack (); - goto done; + return EXT_LANG_RC_ERROR; } - func = PyObject_GetAttrString (type_module, "apply_type_recognizers"); + gdbpy_ref func (PyObject_GetAttrString (type_module.get (), + "apply_type_recognizers")); if (func == NULL) { gdbpy_print_stack (); - goto done; + return EXT_LANG_RC_ERROR; } - result_obj = PyObject_CallFunctionObjArgs (func, printers_obj, - type_obj, (char *) NULL); + gdbpy_ref result_obj (PyObject_CallFunctionObjArgs (func.get (), printers_obj, + type_obj.get (), + (char *) NULL)); if (result_obj == NULL) { gdbpy_print_stack (); - goto done; + return EXT_LANG_RC_ERROR; } - if (result_obj != Py_None) - { - result = python_string_to_host_string (result_obj); - if (result == NULL) - gdbpy_print_stack (); - } + if (result_obj == Py_None) + return EXT_LANG_RC_NOP; - done: - Py_XDECREF (type_obj); - Py_XDECREF (type_module); - Py_XDECREF (func); - Py_XDECREF (result_obj); - if (result != NULL) + result = python_string_to_host_string (result_obj.get ()); + if (result == NULL) { - *prettied_type = result.release (); - return EXT_LANG_RC_OK; + gdbpy_print_stack (); + return EXT_LANG_RC_ERROR; } - return EXT_LANG_RC_ERROR; + + *prettied_type = result.release (); + return EXT_LANG_RC_OK; } /* Free the result of start_type_printers. -- 2.7.4