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: Wed, 28 Jan 2026 11:57:35 +0000 [thread overview]
Message-ID: <ac92a5fb-92b6-4869-9ccd-49a4565d03a2@arm.com> (raw)
In-Reply-To: <878qdian9r.fsf@tromey.com>
On 27/01/2026 19:06, 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.
>
> Matthieu> This patch prepares the ground for migrating the existing C extension
> Matthieu> types from static types to heap-allocated ones, by removing the
> Matthieu> dependency on tp_dictoffset, which is unavailable when using the limited
> Matthieu> API.
>
> Thanks.
>
> Matthieu> - gdbpy__dict__wrapper: a base class for C extension objects that own a
> Matthieu> __dict__.
>
> I think a double underscore in a name is reserved by the C++
> implementation, so "gdbpy__dict__wrapper" can't be used. I think the
> same name with single underscores would be fine.
>
Renamed.
> Matthieu> - gdb_py_generic_dict_setter: a __dict__ setter provided for completeness.
> Matthieu> It should not be used, as __dict__ should be read-only after the object
> Matthieu> initialization.
>
> If it shouldn't be used, it should just not exist, I think.
>
Removed.
> Matthieu> -using thread_map_t
> Matthieu> - = gdb::unordered_map<thread_info *, gdbpy_ref<thread_object>>;
> Matthieu> -
> Matthieu> -struct inferior_object
> Matthieu> -{
> Matthieu> - PyObject_HEAD
>
> This moved to python-internal.h, but I don't understand why that move
> is needed.
>
> Matthieu> + /* Compute the address of the __dict__ attribute for the given PyObject.
> Matthieu> + The CLOSURE argument is unused. */
> Matthieu> + static PyObject **
> Matthieu> + compute_addr (PyObject *self, void *closure ATTRIBUTE_UNUSED)
>
> In classes there doesn't have to be a newline before the method name.
> Also since the closure isn't used here it should probably just be
> dropped. If it's needed in the future we can add it then, but I'm going
> to guess it won't be.
>
Fixed.
> Matthieu> +/* Generic attribute getter function similar to PyObject_GenericGetAttr () but
> Matthieu> + that should be used when the object has a dictionary __dict__. */
> Matthieu> +PyObject *
> Matthieu> +gdb_py_generic_getattro (PyObject *self, PyObject *attr)
> Matthieu> +{
> Matthieu> + PyObject *value = PyObject_GenericGetAttr (self, attr);
> Matthieu> + if (value != nullptr)
> Matthieu> + return value;
> Matthieu> +
> Matthieu> + if (! PyErr_ExceptionMatches (PyExc_AttributeError))
> Matthieu> + return nullptr;
> Matthieu> +
> Matthieu> + /* Clear previous AttributeError set by PyObject_GenericGetAttr. */
> Matthieu> + PyErr_Clear();
> Matthieu> +
Also, in both gdb_py_generic_getattro() and gdb_py_generic_setattro(), I moved the look-up of __dict__ before PyErr_Clear().
If the look-up of __dict__ were to fail for some reason, we still want the exception raised by PyObject_GenericGetAttr / PyObject_GenericSetAttr to be there.
> Matthieu> + gdbpy_ref<> dict (gdb_py_generic_dict_getter (self, nullptr));
> Matthieu> + value = PyDict_GetItemWithError (dict.get (), attr);
>
> I think this has to do check 'dict == nullptr' before this call.
>
Indeed, I forgot that. Fixed.
> Matthieu> + if (value != nullptr)
> Matthieu> + return Py_NewRef (value);
> Matthieu> +
> Matthieu> + PyErr_Format (PyExc_AttributeError,
> Matthieu> + "'%s' object has no attribute '%s'",
> Matthieu> + Py_TYPE (self)->tp_name,
> Matthieu> + PyUnicode_AsUTF8AndSize (attr, nullptr));
> Matthieu> + return nullptr;
>
> PyDict_GetItemWithError sets the error, so is this just here to make a
> nicer message? That's fine if so, though I am not sure if you have to
> clear the existing error here first?
>
https://github.com/python/cpython/blob/08d7bd28fecca524c648dda240022add704b8f8a/Objects/dictobject.c#L2505
/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
This returns NULL *with* an exception set if an exception occurred.
It returns NULL *without* an exception set if the key wasn't present.
*/
PyDict_GetItemWithError has a misleading name, because if the key is not found, no PyExc_AttributeError is set.
This situation results in a new exception being raised:
Python Exception <class 'SystemError'>: error return without exception set
Hence this surprising manual setting of an exception to makes everything fit into place.
I am going to add a comment to clarify the intent, because I had even forgotten why I did this in the first place.
> Matthieu> + gdbpy_ref<> dict (gdb_py_generic_dict_getter (self, nullptr));
> Matthieu> + /* Delete the old value (if there is one). */
> Matthieu> + PyObject *old_value = PyDict_GetItem (dict.get (), attr);
>
> dict==nullptr check
>
Fixed.
> Matthieu> + if (old_value != nullptr)
> Matthieu> + Py_DECREF (old_value);
> Matthieu> + /* Set the new value. */
> Matthieu> + return PyDict_SetItem (dict.get (), attr, value);
>
> I think this ordering means self-assignment can break. IMO it's better
> to use gdbpy_ref to manage the lifetime of the old value.
>
It seems that I got the whole thing wrong with my assumption that I needed to decref the old value.
This is already managed in insertdict() called from PyDict_SetItem()
https://github.com/python/cpython/blob/08d7bd28fecca524c648dda240022add704b8f8a/Objects/dictobject.c#L1934
This PyDict_GetItem () is useless, I will remove it.
> Matthieu> +#if PY_VERSION_HEX < 0x030a0000
> Matthieu> +static inline PyObject*
> Matthieu> +Py_NewRef (PyObject *obj)
>
> The return type is missing a space before the "*"
>
Fixed.
> Tom
Since all others patches are approved with the small modifications you suggested, I am going to only post a new revision of this patch in this same thread to avoid a new round.
Matthieu
next prev parent reply other threads:[~2026-01-28 11:59 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
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 1/6] Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString Matthieu Longo
2026-01-27 17:54 ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 2/6] Python limited API: migrate PyImport_ExtendInittab Matthieu Longo
2026-01-27 17:54 ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 3/6] gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF Matthieu Longo
2026-01-27 18:01 ` Tom Tromey
2026-01-27 18:29 ` Tom Tromey
2026-01-28 11:58 ` 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 [this message]
2026-01-28 17:43 ` Matthieu Longo
2026-01-28 17:51 ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 5/6] gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* Matthieu Longo
2026-01-27 18:28 ` Tom Tromey
2026-01-28 11:58 ` Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 6/6] gdb: make remaining Python extension objects inherit from PyObject Matthieu Longo
2026-01-27 18:29 ` Tom Tromey
2026-01-28 11:58 ` Matthieu Longo
2026-01-28 12:02 [PATCH v2 4/6] gdb: new setters and getters for __dict__, and attributes Matthieu Longo
2026-01-28 18:00 ` Tom Tromey
2026-01-29 10:42 ` Matthieu Longo
2026-01-29 14:45 ` 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=ac92a5fb-92b6-4869-9ccd-49a4565d03a2@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