From patchwork Sat Sep 8 20:42:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29258 Received: (qmail 90595 invoked by alias); 8 Sep 2018 20:42:28 -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 90581 invoked by uid 89); 8 Sep 2018 20:42:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=TRY X-HELO: gateway36.websitewelcome.com Received: from gateway36.websitewelcome.com (HELO gateway36.websitewelcome.com) (192.185.199.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 08 Sep 2018 20:42:26 +0000 Received: from cm15.websitewelcome.com (cm15.websitewelcome.com [100.42.49.9]) by gateway36.websitewelcome.com (Postfix) with ESMTP id 1DAB6400C2F14 for ; Sat, 8 Sep 2018 14:47:10 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id yk3Zf57A7bXuJyk3gfOiYw; Sat, 08 Sep 2018 15:42:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version :Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=HdkjJueaYvsvK9PYJnKpqpeJjk6vZKBU8SmQSV22+to=; b=LPHnbeM/nE/4fUMxfp168C8084 iqOvelJqY6omZ6RLZdnakXSwFcG0b+yh3BPK2D6qjJJV/6wogb/BN3gvPfxCNp97l7ydsQ40F5rPU BTBW5OlBv/NamazB1FPRE27xi; Received: from 75-166-85-72.hlrn.qwest.net ([75.166.85.72]:47170 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fyk3Y-002OM9-H2; Sat, 08 Sep 2018 15:42:12 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Allow a pretty-printer without a to_string method Date: Sat, 8 Sep 2018 14:42:10 -0600 Message-Id: <20180908204210.22927-1-tom@tromey.com> PR python/16047 points out that, while the documentation says that the to_string method is optional for a pretty-printer, the code disagrees and throws an exception. This patch fixes the problem. varobj is already ok here. Tested on x86-64 Fedora 26. gdb/ChangeLog 2018-09-08 Tom Tromey PR python/16047: * python/py-prettyprint.c (pretty_print_one_value): Check for to_string method. gdb/testsuite/ChangeLog 2018-09-08 Tom Tromey PR python/16047: * gdb.python/py-prettyprint.py (pp_int_typedef3): New class. (register_pretty_printers): Register new printer. * gdb.python/py-prettyprint.exp (run_lang_tests): Add int_type3 test. * gdb.python/py-prettyprint.c (int_type3): New typedef. (an_int_type3): New global. --- gdb/ChangeLog | 6 ++++++ gdb/python/py-prettyprint.c | 25 +++++++++++++++---------- gdb/testsuite/ChangeLog | 10 ++++++++++ gdb/testsuite/gdb.python/py-prettyprint.c | 2 ++ gdb/testsuite/gdb.python/py-prettyprint.exp | 2 ++ gdb/testsuite/gdb.python/py-prettyprint.py | 10 ++++++++++ 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c index a8a84899309..df0266b2613 100644 --- a/gdb/python/py-prettyprint.c +++ b/gdb/python/py-prettyprint.c @@ -195,18 +195,23 @@ pretty_print_one_value (PyObject *printer, struct value **out_value) *out_value = NULL; TRY { - result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, - NULL)); - if (result != NULL) + if (!PyObject_HasAttr (printer, gdbpy_to_string_cst)) + result = gdbpy_ref<>::new_reference (Py_None); + else { - if (! gdbpy_is_string (result.get ()) - && ! gdbpy_is_lazy_string (result.get ()) - && result != Py_None) + result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, + NULL)); + if (result != NULL) { - *out_value = convert_value_from_python (result.get ()); - if (PyErr_Occurred ()) - *out_value = NULL; - result = NULL; + if (! gdbpy_is_string (result.get ()) + && ! gdbpy_is_lazy_string (result.get ()) + && result != Py_None) + { + *out_value = convert_value_from_python (result.get ()); + if (PyErr_Occurred ()) + *out_value = NULL; + result = NULL; + } } } } diff --git a/gdb/testsuite/gdb.python/py-prettyprint.c b/gdb/testsuite/gdb.python/py-prettyprint.c index 35ef5e0756b..ef5b0c3c5ea 100644 --- a/gdb/testsuite/gdb.python/py-prettyprint.c +++ b/gdb/testsuite/gdb.python/py-prettyprint.c @@ -271,10 +271,12 @@ bug_14741() when looking for a printer. */ typedef int int_type; typedef int_type int_type2; +typedef int_type int_type3; int an_int = -1; int_type an_int_type = 1; int_type2 an_int_type2 = 2; +int_type3 an_int_type3 = 3; int main () diff --git a/gdb/testsuite/gdb.python/py-prettyprint.exp b/gdb/testsuite/gdb.python/py-prettyprint.exp index 2671cb8471b..fe7774fa886 100644 --- a/gdb/testsuite/gdb.python/py-prettyprint.exp +++ b/gdb/testsuite/gdb.python/py-prettyprint.exp @@ -127,6 +127,8 @@ proc run_lang_tests {exefile lang} { gdb_test "print (int_type) an_int_type2" " = type=int_type, val=2" gdb_test "print (int_type2) an_int_type2" " = type=int_type2, val=2" + gdb_test "print (int_type3) an_int_type2" " = {s = 27}" + gdb_continue_to_end } diff --git a/gdb/testsuite/gdb.python/py-prettyprint.py b/gdb/testsuite/gdb.python/py-prettyprint.py index 7357f05cc93..91592b4ec79 100644 --- a/gdb/testsuite/gdb.python/py-prettyprint.py +++ b/gdb/testsuite/gdb.python/py-prettyprint.py @@ -253,6 +253,15 @@ class pp_int_typedef (object): def to_string(self): return "type=%s, val=%s" % (self.val.type, int(self.val)) +class pp_int_typedef3 (object): + "A printer without a to_string method" + + def __init__(self, val): + self.val = val + + def children(self): + yield 's', 27 + def lookup_function (val): "Look-up and return a pretty-printer that can print val." @@ -362,6 +371,7 @@ def register_pretty_printers (): typedefs_pretty_printers_dict[re.compile ('^int_type$')] = pp_int_typedef typedefs_pretty_printers_dict[re.compile ('^int_type2$')] = pp_int_typedef + typedefs_pretty_printers_dict[re.compile ('^int_type3$')] = pp_int_typedef3 # Dict for struct types with typedefs fully stripped. pretty_printers_dict = {}