From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EYWnJw3NmGnGNAMAWB0awg (envelope-from ) for ; Fri, 20 Feb 2026 16:07:25 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=HdcEM2Cw; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 9D69A1E0BA; Fri, 20 Feb 2026 16:07:25 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 273B11E08D for ; Fri, 20 Feb 2026 16:07:24 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id A668C4BA23EE for ; Fri, 20 Feb 2026 21:07:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A668C4BA23EE Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=HdcEM2Cw Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id 2C7394B9DB66 for ; Fri, 20 Feb 2026 21:03:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C7394B9DB66 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2C7394B9DB66 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771621434; cv=none; b=xptXImXzPs5ity6svq1FVs8ThuYUDZU33eWPa07rokkS4GIDFrGTjSX5xOZU4jj8uFzMVXl5pMj/XbeFmkv4DbOprZnuXYmlfaMlY+mB7mECiR97bypUrFNyJi1doDtv6WSjtAjSprBgL6PKYf3KWtPSDqdoZjpSGOcpbK2cVXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771621434; c=relaxed/simple; bh=Flt9nwe4iF6eGTY4PxWanzRxEZ97+YdigJ/9YjX/lng=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=cZKOzOr+uQg3i9q4kLMqs5/CSM2/cgtXWMDSRXi/juD5AZCUIvW/6zSAFzo34PSzba2dnYLg6qjTH9/SDRDzFWVAZ4+6VeHmPyc4/RoeBfmP9lQr/moXsMJNBUJ7Bsz7spzHvS2rl6c4iTl0QTizHmOdsMbZfh3pMS2Yx9FER6g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C7394B9DB66 Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-46391f4c1f9so1551544b6e.0 for ; Fri, 20 Feb 2026 13:03:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1771621433; x=1772226233; darn=sourceware.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=2dGD/KDdxwUoUBe1LVJeZqMb3BVJUNC3a1yGwvn+PMM=; b=HdcEM2Cwav4JumAhkVj1nR9NlJFzvaYjuVNV72N/RYJdlvV9Sz2UiCuuXfrLbSp1GN xaOJNHPLuBfPtIM1rkwZlBvq72UV4gJQ+uuJEr8BBgxrcnktvuFZdYM3f47Ku1YiZ20b b3b03YVW0FnI3nrhXnW4wkkTwPEWx3poGC6MquXQpK18kLdFBVj8TcLvpqpzBEdz4emm lwsHV/ntI0iOyMwuvd/53xJqOvpyPcCGUZzwKRObPKzHqy7ZZLE6fMZPZs2t4jI4864A Ex8rjPrF0Os1bUeFM/e5P5EMgmAIECk0E3AFNb6Gv7UFvE2X/qAQ/yGGnjQjccXsYkAf 4Ulw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771621433; x=1772226233; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=2dGD/KDdxwUoUBe1LVJeZqMb3BVJUNC3a1yGwvn+PMM=; b=n9Di1oUhx2EqShBCnQ+ZGS3IiEkuTKR5ZhqKHbeKpgmnZZtzwh2KYptBZf9OH6eGSx mXBGmmF0ODs7aXoWD0On5a5XTCY2ooE9sC1cLRa9fI/aByqZvTrtEw2EwaDBKoleyVeQ nY6E3zZel3q8T+S1r3IMP63dN7e26ayN4pKDoPh4XKdqZGfOa2ZNACxpuaCcObaUfXBc CwO3D7MkTh2wQ1AD2qDOWVToaZwEAVhQxlHe5TkFBeDK+QARtGbHZRMelHDgH7jpkBx7 ldLPez3TZKXN2JG05Ex8e7WbvpwShJPRiAThq/NPWFnzyA+dhLQvEmpf98d9tp/VTF0t aASQ== X-Gm-Message-State: AOJu0YwTtoGF77wSW03S7v4cYvbeyfZUCKEJAl4/MJiOnkLp0GimCc4X Bgo4CESDJPw4k39r5rlmAhiLIFj3sw4uT9Qpf3lU+kY+qWZACrmtfdHz/a7fjAyumBTirRR+UjW Srp0= X-Gm-Gg: AZuq6aJygV7f3ucSEMW785H1R8GbKtE8Yc6HsOZ8lVCrXBTjhS1g3KB8iN6j08YSMor /i0zGdPwPWCV6gjOyjxjnuPGkDG46r10PD+BLAFz0pThk2FRu/vewBlokkFG2hJ2W6UPvIkCX5c qjJYZ/ZE1O1cYWvnina7PVPeUxFgh0hJOZY1D39hb7QpJZw87N3MaA+47CJw6gKoBgREgpb7SRp Hzl0gqU7rPHZk2qbBLsTwj9CZrZRmqK90xGMTe0MXDtY1TwAyDL+YV+eWR3OAWZA0J82OkHsL7d BBBPBFtbhdRYQZzpqI8ACAk0Day38GiO1rmwkqjKv0ABjouVozHbwGuq0EQLQvr2xtcCXPyr8Vq WIfiXdkcWJXgIkLIicsquJVSogyjfmc3FApdDLx2YhnDS4r8uUXn89VQKpQ1OlHeWFNXxjhI70y eICMiARb2NwDePdgng7UmfcdvnQc0aAAIBILfdvMp8jFexVYfHrFF086s6 X-Received: by 2002:a05:6808:2384:b0:45c:8526:b56a with SMTP id 5614622812f47-4644612df9emr891788b6e.3.1771621433284; Fri, 20 Feb 2026 13:03:53 -0800 (PST) Received: from [192.168.0.26] (97-122-122-234.hlrn.qwest.net. [97.122.122.234]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4157d2d7826sm561630fac.10.2026.02.20.13.03.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Feb 2026 13:03:52 -0800 (PST) From: Tom Tromey Date: Fri, 20 Feb 2026 14:03:51 -0700 Subject: [PATCH 05/10] Return gdbpy_ref<> from value_to_value_object MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260220-python-safety-minor-v1-5-4c4b12e445af@adacore.com> References: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com> In-Reply-To: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.14.3 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This changes value_to_value_object to return a gdbpy_ref<>, using the type system to convey that a new reference is always returned. --- gdb/python/py-finishbreakpoint.c | 4 +-- gdb/python/py-frame.c | 8 ++--- gdb/python/py-function.c | 2 +- gdb/python/py-lazy-string.c | 4 +-- gdb/python/py-prettyprint.c | 4 +-- gdb/python/py-stopevent.c | 2 +- gdb/python/py-symbol.c | 4 +-- gdb/python/py-type.c | 6 ++-- gdb/python/py-unwind.c | 4 +-- gdb/python/py-value.c | 70 +++++++++++++++++++--------------------- gdb/python/py-xmethods.c | 16 ++++----- gdb/python/python-internal.h | 2 +- gdb/python/python.c | 4 +-- 13 files changed, 64 insertions(+), 66 deletions(-) diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c index 43dffd64adc..3370bb02580 100644 --- a/gdb/python/py-finishbreakpoint.c +++ b/gdb/python/py-finishbreakpoint.c @@ -122,7 +122,7 @@ bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj) if (ret) { - self_finishbp->return_value = value_to_value_object (ret); + self_finishbp->return_value = value_to_value_object (ret).release (); if (!self_finishbp->return_value) gdbpy_print_stack (); } @@ -264,7 +264,7 @@ bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs) /* Ignore Python errors at this stage. */ value *func_value = read_var_value (function, NULL, frame); self_bpfinish->function_value - = value_to_value_object (func_value); + = value_to_value_object (func_value).release (); PyErr_Clear (); self_bpfinish->func_symbol diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index d9af3639d29..68c2628c297 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -257,7 +257,7 @@ static PyObject * frapy_read_register (PyObject *self, PyObject *args, PyObject *kw) { PyObject *pyo_reg_id; - PyObject *result = nullptr; + gdbpy_ref<> result; static const char *keywords[] = { "register", nullptr }; if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, &pyo_reg_id)) @@ -289,7 +289,7 @@ frapy_read_register (PyObject *self, PyObject *args, PyObject *kw) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Implementation of gdb.Frame.block (self) -> gdb.Block. @@ -557,7 +557,7 @@ frapy_read_var (PyObject *self, PyObject *args, PyObject *kw) return NULL; } - PyObject *result = nullptr; + gdbpy_ref<> result; try { FRAPY_REQUIRE_VALID (self, frame); @@ -571,7 +571,7 @@ frapy_read_var (PyObject *self, PyObject *args, PyObject *kw) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Select this frame. */ diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c index ee60f08b65c..3bb81527a9c 100644 --- a/gdb/python/py-function.c +++ b/gdb/python/py-function.c @@ -45,7 +45,7 @@ convert_values_to_python (int argc, struct value **argv) for (i = 0; i < argc; ++i) { - gdbpy_ref<> elt (value_to_value_object (argv[i])); + gdbpy_ref<> elt = value_to_value_object (argv[i]); if (elt == NULL) return NULL; PyTuple_SetItem (result.get (), i, elt.release ()); diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c index 58cdea69f56..fe248f6e78f 100644 --- a/gdb/python/py-lazy-string.c +++ b/gdb/python/py-lazy-string.c @@ -109,7 +109,7 @@ stpy_convert_to_value (PyObject *self, PyObject *args) return NULL; } - PyObject *result = nullptr; + gdbpy_ref<> result; try { scoped_value_mark free_values; @@ -149,7 +149,7 @@ stpy_convert_to_value (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } static void diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c index 6900bf64585..0aa11d29094 100644 --- a/gdb/python/py-prettyprint.c +++ b/gdb/python/py-prettyprint.c @@ -591,7 +591,7 @@ gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang, gdbpy_enter enter_py (gdbarch, language); - gdbpy_ref<> val_obj (value_to_value_object (value)); + gdbpy_ref<> val_obj = value_to_value_object (value); if (val_obj == NULL) { print_stack_unless_memory_error (stream); @@ -662,7 +662,7 @@ apply_varobj_pretty_printer (PyObject *printer_obj, gdbpy_ref<> gdbpy_get_varobj_pretty_printer (struct value *value) { - gdbpy_ref<> val_obj (value_to_value_object (value)); + gdbpy_ref<> val_obj = value_to_value_object (value); if (val_obj == NULL) return NULL; diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c index 351c0479ee6..cefcfdd0761 100644 --- a/gdb/python/py-stopevent.c +++ b/gdb/python/py-stopevent.c @@ -85,7 +85,7 @@ py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) there's no API to add generic Python objects to a py_ui_out. */ if (return_value != nullptr) { - gdbpy_ref<> val (value_to_value_object (return_value)); + gdbpy_ref<> val = value_to_value_object (return_value); if (val == nullptr) return nullptr; if (PyDict_SetItemString (dict.get (), "finish-value", val.get ()) < 0) diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 492c386b7ee..80c7705aa68 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -282,7 +282,7 @@ sympy_value (PyObject *self, PyObject *args) return NULL; } - PyObject *result = nullptr; + gdbpy_ref<> result; try { if (frame_obj != NULL) @@ -308,7 +308,7 @@ sympy_value (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Given a symbol, and a symbol_object that has previously been diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index d1ebe852eaa..017937c1c0a 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -1045,7 +1045,7 @@ typy_template_argument (PyObject *self, PyObject *args) return NULL; } - PyObject *result = nullptr; + gdbpy_ref<> result; try { scoped_value_mark free_values; @@ -1057,7 +1057,7 @@ typy_template_argument (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* __repr__ implementation for gdb.Type. */ @@ -1236,7 +1236,7 @@ typy_optimized_out (PyObject *self, PyObject *args) struct type *type = ((type_object *) self)->type; scoped_value_mark free_values; - return value_to_value_object (value::allocate_optimized_out (type)); + return value_to_value_object (value::allocate_optimized_out (type)).release (); } /* Return a gdb.Field object for the field named by the argument. */ diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index 5465d3d6cf9..872cd00e84f 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -480,7 +480,7 @@ pending_framepy_read_register (PyObject *self, PyObject *args, PyObject *kw) if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) return nullptr; - PyObject *result = nullptr; + gdbpy_ref<> result; try { scoped_value_mark free_values; @@ -504,7 +504,7 @@ pending_framepy_read_register (PyObject *self, PyObject *args, PyObject *kw) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Implement PendingFrame.is_valid(). Return True if this pending frame diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index e19be1d2701..c0fcfb882e1 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -243,7 +243,7 @@ gdbpy_preserve_values (const struct extension_language_defn *extlang, static PyObject * valpy_dereference (PyObject *self, PyObject *args) { - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -258,7 +258,7 @@ valpy_dereference (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Given a value of a pointer type or a reference type, return the value @@ -272,7 +272,7 @@ valpy_dereference (PyObject *self, PyObject *args) static PyObject * valpy_referenced_value (PyObject *self, PyObject *args) { - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -301,7 +301,7 @@ valpy_referenced_value (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Return a value which is a reference to the value. */ @@ -309,7 +309,7 @@ valpy_referenced_value (PyObject *self, PyObject *args) static PyObject * valpy_reference_value (PyObject *self, PyObject *args, enum type_code refcode) { - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -324,7 +324,7 @@ valpy_reference_value (PyObject *self, PyObject *args, enum type_code refcode) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } static PyObject * @@ -344,7 +344,7 @@ valpy_rvalue_reference_value (PyObject *self, PyObject *args) static PyObject * valpy_to_array (PyObject *self, PyObject *args) { - PyObject *result = nullptr; + gdbpy_ref<> result; try { @@ -352,10 +352,7 @@ valpy_to_array (PyObject *self, PyObject *args) struct type *type = check_typedef (val->type ()); if (type->code () == TYPE_CODE_ARRAY) - { - result = self; - Py_INCREF (result); - } + result = gdbpy_ref<>::new_reference (self); else { val = value_to_array (val); @@ -370,7 +367,7 @@ valpy_to_array (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Return a "const" qualified version of the value. */ @@ -378,7 +375,7 @@ valpy_to_array (PyObject *self, PyObject *args) static PyObject * valpy_const_value (PyObject *self, PyObject *args) { - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -394,7 +391,7 @@ valpy_const_value (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Return "&value". */ @@ -411,7 +408,7 @@ valpy_get_address (PyObject *self, void *closure) scoped_value_mark free_values; res_val = value_addr (val_obj->value); - val_obj->address = value_to_value_object (res_val); + val_obj->address = value_to_value_object (res_val).release (); } catch (const gdb_exception_forced_quit &except) { @@ -807,7 +804,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw) static PyObject * valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) { - PyObject *type_obj, *result = NULL; + PyObject *type_obj; struct type *type; if (! PyArg_ParseTuple (args, "O", &type_obj)) @@ -821,6 +818,7 @@ valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) return NULL; } + gdbpy_ref<> result; try { struct value *val = ((value_object *) self)->value; @@ -844,7 +842,7 @@ valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Implementation of the "cast" method. */ @@ -1020,7 +1018,7 @@ valpy_getitem (PyObject *self, PyObject *key) gdb::unique_xmalloc_ptr field; struct type *base_class_type = NULL, *field_type = NULL; long bitpos = -1; - PyObject *result = NULL; + gdbpy_ref<> result; if (gdbpy_is_string (key)) { @@ -1153,7 +1151,7 @@ valpy_getitem (PyObject *self, PyObject *key) return gdbpy_handle_gdb_exception (nullptr, ex); } - return result; + return result.release (); } static int @@ -1173,7 +1171,7 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords) struct value *function = ((value_object *) self)->value; struct value **vargs = NULL; struct type *ftype = NULL; - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -1241,7 +1239,7 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Called by the Python interpreter to obtain string representation @@ -1439,10 +1437,10 @@ enum valpy_opcode of applying the operation specified by OPCODE to the given arguments. Throws a GDB exception on error. */ -static PyObject * +static gdbpy_ref<> valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other) { - PyObject *result = NULL; + gdbpy_ref<> result; struct value *arg1, *arg2; struct value *res_val = NULL; @@ -1565,7 +1563,7 @@ valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other) static PyObject * valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) { - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -1576,7 +1574,7 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } static PyObject * @@ -1628,7 +1626,7 @@ valpy_power (PyObject *self, PyObject *other, PyObject *unused) static PyObject * valpy_negative (PyObject *self) { - PyObject *result = NULL; + gdbpy_ref<> result; try { @@ -1644,13 +1642,13 @@ valpy_negative (PyObject *self) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } static PyObject * valpy_positive (PyObject *self) { - return value_to_value_object (((value_object *) self)->value); + return value_to_value_object (((value_object *) self)->value).release (); } static PyObject * @@ -1713,7 +1711,7 @@ valpy_nonzero (PyObject *self) static PyObject * valpy_invert (PyObject *self) { - PyObject *result = nullptr; + gdbpy_ref<> result; try { @@ -1726,7 +1724,7 @@ valpy_invert (PyObject *self) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Implements left shift for value objects. */ @@ -1972,7 +1970,7 @@ valpy_float (PyObject *self) /* Returns an object for a value, without releasing it from the all_values chain. */ -PyObject * +gdbpy_ref<> value_to_value_object (struct value *val) { value_object *val_obj; @@ -1990,7 +1988,7 @@ value_to_value_object (struct value *val) note_value (val_obj); } - return (PyObject *) val_obj; + return gdbpy_ref<> (val_obj); } /* Returns a borrowed reference to the struct value corresponding to @@ -2103,7 +2101,7 @@ gdbpy_history (PyObject *self, PyObject *args) if (!PyArg_ParseTuple (args, "i", &i)) return NULL; - PyObject *result = nullptr; + gdbpy_ref<> result; try { scoped_value_mark free_values; @@ -2115,7 +2113,7 @@ gdbpy_history (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Add a gdb.Value into GDB's history, and return (as an integer) the @@ -2163,7 +2161,7 @@ gdbpy_convenience_variable (PyObject *self, PyObject *args) if (!PyArg_ParseTuple (args, "s", &varname)) return NULL; - PyObject *result = nullptr; + gdbpy_ref<> result; bool found = false; try { @@ -2190,7 +2188,7 @@ gdbpy_convenience_variable (PyObject *self, PyObject *args) if (result == nullptr && !found) Py_RETURN_NONE; - return result; + return result.release (); } /* Set the value of a convenience variable. */ diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c index de6c9d0b07d..64f9807cfee 100644 --- a/gdb/python/py-xmethods.c +++ b/gdb/python/py-xmethods.c @@ -444,7 +444,7 @@ python_xmethod_worker::do_get_result_type (value *obj, if (!types_equal (obj_type, this_type)) obj = value_cast (this_type, obj); } - gdbpy_ref<> py_value_obj (value_to_value_object (obj)); + gdbpy_ref<> py_value_obj = value_to_value_object (obj); if (py_value_obj == NULL) { gdbpy_print_stack (); @@ -464,14 +464,14 @@ python_xmethod_worker::do_get_result_type (value *obj, for (i = 0; i < args.size (); i++) { - PyObject *py_value_arg = value_to_value_object (args[i]); + gdbpy_ref<> py_value_arg = value_to_value_object (args[i]); - if (py_value_arg == NULL) + if (py_value_arg == nullptr) { gdbpy_print_stack (); return EXT_LANG_RC_ERROR; } - PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg); + PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg.release ()); } gdbpy_ref<> py_result_type @@ -529,7 +529,7 @@ python_xmethod_worker::invoke (struct value *obj, if (!types_equal (obj_type, this_type)) obj = value_cast (this_type, obj); } - gdbpy_ref<> py_value_obj (value_to_value_object (obj)); + gdbpy_ref<> py_value_obj = value_to_value_object (obj); if (py_value_obj == NULL) { gdbpy_print_stack (); @@ -549,15 +549,15 @@ python_xmethod_worker::invoke (struct value *obj, for (i = 0; i < args.size (); i++) { - PyObject *py_value_arg = value_to_value_object (args[i]); + gdbpy_ref<> py_value_arg = value_to_value_object (args[i]); - if (py_value_arg == NULL) + if (py_value_arg == nullptr) { gdbpy_print_stack (); error (_("Error while executing Python code.")); } - PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg); + PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg.release ()); } gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker, diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index de4226868dc..c7e812d6893 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -500,7 +500,7 @@ gdbpy_ref<> symtab_to_symtab_object (struct symtab *symtab); gdbpy_ref<> symbol_to_symbol_object (struct symbol *sym); gdbpy_ref<> block_to_block_object (const struct block *block, struct objfile *objfile); -PyObject *value_to_value_object (struct value *v); +gdbpy_ref<> value_to_value_object (struct value *v); PyObject *type_to_type_object (struct type *); PyObject *frame_info_to_frame_object (const frame_info_ptr &frame); PyObject *symtab_to_linetable_object (PyObject *symtab); diff --git a/gdb/python/python.c b/gdb/python/python.c index d2a77890072..aec2c7559a8 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1073,7 +1073,7 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args, PyObject *kw) flags |= PARSER_LEAVE_BLOCK_ALONE; } - PyObject *result = nullptr; + gdbpy_ref<> result; try { scoped_value_mark free_values; @@ -1094,7 +1094,7 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args, PyObject *kw) return gdbpy_handle_gdb_exception (nullptr, except); } - return result; + return result.release (); } /* Implementation of gdb.invalidate_cached_frames. */ -- 2.53.0