From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 6eLAAGMBu2jQdBcAWB0awg (envelope-from ) for ; Fri, 05 Sep 2025 11:27:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1757086051; bh=eprT+yYbdaC8AbmZh3f9M3uAcGWIoc0agrnx3wQNsk8=; h=Date:Subject:To:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=WC9Ht1R/tI+4s5gxsYNcEBaRLVf/32h/ZRp5xAehD2OVl890OYAFPYdZQjVJMQ0tM H6iuMrMRrAnTzdnxCuBWeOlIzRi8lpegVNycoXE9VLBKIiuhIiSqpSfzb70i63S+uS I+w0tYH45g5DS3or4JdnkYqcYlcPHSaIOc+liByE= Received: by simark.ca (Postfix, from userid 112) id F2FA61E023; Fri, 05 Sep 2025 11:27:30 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_LOW,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no autolearn_force=no version=4.0.1 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=iyrImYZc; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 484741E023 for ; Fri, 05 Sep 2025 11:27:30 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E62A13857806 for ; Fri, 5 Sep 2025 15:27:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E62A13857806 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=iyrImYZc Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id E643E3857820 for ; Fri, 5 Sep 2025 15:26:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E643E3857820 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E643E3857820 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757085966; cv=none; b=buC5N/r2oAw4WCxCebWGZbAyGrMFg4rpu3y3mPKewWyrs+U9Rl4847bal+n/0StjHndSDLmkHW3rrCa29JbXShpfR5gsjbE80WqmX/nWds3sIAfWYebedDmdrSCWlOjaFBgnA+5fccnejnUI+dNp7iehFYfybHgdVyxTXUtYsCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757085966; c=relaxed/simple; bh=eprT+yYbdaC8AbmZh3f9M3uAcGWIoc0agrnx3wQNsk8=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=WgDp6y4r+xKnTvJii5vhMfEkI/IBpGH6zbPv3m74PldXU2nhMEnxm5yDGE9W7DSt1s0Aq8CZIsekSz0guUXxwNS/naLV0hPiC+BA4hjlIMFPhQyYY2QI0s8Q4E1VGe4WN6ylNqtkU1yabtv6cgQ+rQkDU9VDGsQxGs7lNVj8ANs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E643E3857820 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1757085965; bh=eprT+yYbdaC8AbmZh3f9M3uAcGWIoc0agrnx3wQNsk8=; h=Date:Subject:To:References:From:In-Reply-To:From; b=iyrImYZcZ+z7zhHfGsb7fH8zGRSklsUqzrjYCIdjWJhboohS64sfEJuPkCB0xEbSb /w81pnArcjRMIa2uqUMgBPYR55if8m04nJdfwOGNJtkRviSGEAHxqIjcyY+4bQkxGA hqPfoGlJBRXkWDqqNt9QOObjRmTf+8mLVh4e/ORU= Received: by simark.ca (Postfix) id 8685F1E023; Fri, 05 Sep 2025 11:26:05 -0400 (EDT) Message-ID: <84ce5145-0c06-487b-81f5-c6c232f5949e@simark.ca> Date: Fri, 5 Sep 2025 11:26:04 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Allow conversion of 128-bit integers to Python To: Tom Tromey , gdb-patches@sourceware.org References: <20250905132926.460147-1-tom@tromey.com> Content-Language: fr From: Simon Marchi In-Reply-To: <20250905132926.460147-1-tom@tromey.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 On 9/5/25 9:29 AM, Tom Tromey wrote: > Currently, trying to convert a 128-bit integer from a gdb.Value to a > Python integer will fail. This is surprising because Python uses > bigints internally. > > The bug here is that valpy_long uses value_as_long, which fails for > anything wider than LONGEST. This patch fixes the problem by using > the recommended Python API. > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33366 > --- > gdb/python/py-value.c | 41 +++++++++++++++++++++++---- > gdb/testsuite/gdb.python/py-value.exp | 12 ++++++++ > 2 files changed, 47 insertions(+), 6 deletions(-) > > diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c > index 833ce26d5a3..973785b5e4a 100644 > --- a/gdb/python/py-value.c > +++ b/gdb/python/py-value.c > @@ -1866,7 +1866,7 @@ valpy_long (PyObject *self) > { > struct value *value = ((value_object *) self)->value; > struct type *type = value->type (); > - LONGEST l = 0; > + PyObject *result; > > try > { > @@ -1882,17 +1882,46 @@ valpy_long (PyObject *self) > && type->code () != TYPE_CODE_PTR) > error (_("Cannot convert value to long.")); > > - l = value_as_long (value); > + gdb::array_view contents = value->contents (); > +#if PY_VERSION_HEX >= 0x030d0000 > + int flags = (type_byte_order (type) == BFD_ENDIAN_BIG > + ? Py_ASNATIVEBYTES_BIG_ENDIAN > + : Py_ASNATIVEBYTES_LITTLE_ENDIAN); > + if (type->is_unsigned ()) > + flags |= Py_ASNATIVEBYTES_UNSIGNED_BUFFER; > + result = PyLong_FromNativeBytes (contents.data (), contents.size (), > + flags); > +#else > + /* We need this roundabout approach because int.from_bytes > + requires "signed" to be a keyword arg. */ > + gdbpy_ref<> args > + (Py_BuildValue ("(y#s)", contents.data (), > + (Py_ssize_t) contents.size (), > + (type_byte_order (type) == BFD_ENDIAN_BIG > + ? "big" : "little"))); > + if (args == nullptr) > + return nullptr; > + > + gdbpy_ref<> kwargs (Py_BuildValue ("{sO}", "signed", > + type->is_unsigned () > + ? Py_False : Py_True)); > + if (kwargs == nullptr) > + return nullptr; > + > + gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) &PyLong_Type, > + "from_bytes")); > + if (callable == nullptr) > + return nullptr; If we are forced to use kwargs, I think we might as well just use kwargs for all the args. Can you add some comments above the Python API calls, just to explain at a high level what this is doing, so that someone who doesn't know the Python API by heart can follow what is happening? Something like: /* args = (bytes, byteorder) */ gdbpy_ref<> args (Py_BuildValue ("(y#s)", contents.data (), (Py_ssize_t) contents.size (), (type_byte_order (type) == BFD_ENDIAN_BIG ? "big" : "little"))); if (args == nullptr) return nullptr; /* kwargs = {"signed": signed} */ gdbpy_ref<> kwargs (Py_BuildValue ("{sO}", "signed", type->is_unsigned () ? Py_False : Py_True)); if (kwargs == nullptr) return nullptr; /* Get `int.from_bytes`. */ gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) &PyLong_Type, "from_bytes")); if (callable == nullptr) return nullptr; /* Call `int.from_byte(*args, **kwargs)`. */ result = PyObject_Call (callable.get (), args.get (), kwargs.get ()); Approved-By: Simon Marchi Simon