From: Matthieu Longo <matthieu.longo@arm.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v2 4/6] gdb: new setters and getters for __dict__, and attributes
Date: Thu, 29 Jan 2026 10:42:52 +0000 [thread overview]
Message-ID: <2fa36bd8-5d84-4432-a6c7-77837f5dc850@arm.com> (raw)
In-Reply-To: <87qzr98vo4.fsf@tromey.com>
On 28/01/2026 18:00, Tom Tromey wrote:
>>>>>> "Matthieu" == Matthieu Longo <matthieu.longo@arm.com> writes:
>
> Matthieu> GDB is currently using the Python unlimited API. Migrating the codebase
> Matthieu> to the Python limited API would have for benefit to make a GDB build
> Matthieu> artifacts compatible with older and newer versions of Python that they
> Matthieu> were built with.
>
> Thanks again.
>
> Matthieu> + value = PyDict_GetItemWithError (dict.get (), attr);
> Matthieu> + if (value != nullptr)
> Matthieu> + return Py_NewRef (value);
> Matthieu> +
> Matthieu> + /* PyDict_GetItemWithError() has a misleading name that lets one believe that
> Matthieu> + the function sets PyExc_AttributeError when the key is not found, whereas
> Matthieu> + it actually does not and only returns NULL.
> Matthieu> + Not setting the exception for an unfound key causes the raising of another
> Matthieu> + exception:
> Matthieu> + <class 'SystemError'>: error return without exception set
> Matthieu> + Hence this surprising manual setting of an exception to makes everything
> Matthieu> + fit into place. */
> Matthieu> + PyErr_Format (PyExc_AttributeError,
> Matthieu> + "'%s' object has no attribute '%s'",
> Matthieu> + Py_TYPE (self)->tp_name,
> Matthieu> + PyUnicode_AsUTF8AndSize (attr, nullptr));
>
>
> It seems to me that there should be an additional check between the call
> to PyDict_GetItemWithError and PyErr_Format, like:
>
> if (PyErr_Occurred () != nullptr)
> return nullptr;
>
> Since according to the PyDict_GetItemWithError docs, this function can
> return null with the error set.
>
> With that change the comment could probably be shortened a bit since
> it's no longer surprising to have to set the exception.
>
> Tom
I missed that. Thanks for noticing.
What about this ?
value = PyDict_GetItemWithError (dict.get (), attr);
if (value != nullptr)
return Py_NewRef (value);
/* If PyDict_GetItemWithError() returns NULL because an error occurred, it
sets an exception. Propagate it by returning NULL. */
if (PyErr_Occurred () != nullptr)
return nullptr;
/* If the key is not found, PyDict_GetItemWithError() returns NULL without
setting an exception. Failing to set one here would later result in:
<class 'SystemError'>: error return without exception set
Therefore, we must explicitly raise an AttributeError in this case. */
PyErr_Format (PyExc_AttributeError,
"'%s' object has no attribute '%s'",
Py_TYPE (self)->tp_name,
PyUnicode_AsUTF8AndSize (attr, nullptr));
return nullptr;
Matthieu
next prev parent reply other threads:[~2026-01-29 10:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-28 12:02 Matthieu Longo
2026-01-28 18:00 ` Tom Tromey
2026-01-29 10:42 ` Matthieu Longo [this message]
2026-01-29 14:45 ` Tom Tromey
-- strict thread matches above, loose matches on Subject: below --
2026-01-27 17:02 [PATCH v2 0/6] gdb: minor fixes for Python limited C API support Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 4/6] gdb: new setters and getters for __dict__, and attributes Matthieu Longo
2026-01-27 19:06 ` Tom Tromey
2026-01-28 11:57 ` Matthieu Longo
2026-01-28 17:43 ` Matthieu Longo
2026-01-28 17:51 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2fa36bd8-5d84-4432-a6c7-77837f5dc850@arm.com \
--to=matthieu.longo@arm.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox