Re: [PATCH v2 9/9] gdb/python: add accessor helpers for __dict__ in Python extension objects

Matthieu Longo
2026-03-06T14:35:16Z

On 03/03/2026 19:02, Tom Tromey wrote:
>>>>>> Matthieu Longo <matthieu.longo@arm.com> writes:
>
>> A previous patch centralized the creation logic; this patch focuses
>> on centralizing the access to the dictionary. It introduces two new
>> macros:
>
> Thanks.
>
>
>> + #define gdbpy_dict_wrapper_PyGetSetDef(object_name) \
>> + { \
>> + "__dict__", /* name */ \
>> + (getter) gdb_py_generic_dict_getter, \
>> + (setter) nullptr, \
>> + "The __dict__ for this " object_name ".", /* doc */ \
>> + nullptr, /* closure */ \
>> + }
>> +
>> + #define gdbpy_dict_wrapper_PyTypeObject_tps \
>> + /*tp_getattro*/ \
>> + gdb_py_generic_getattro, \
>> + /*tp_setattro*/ \
>> + gdb_py_generic_setattro
>
> I wouldn't indent the '#define'. But I tend to think it would be better
> to just use constexpr functions here.
>

For gdbpy_dict_wrapper_PyGetSetDef, I tried to use a constexpr function at first, but there is an issue.
I am not aware of an easy way to concatenate strings with a constexpr in C++17.

This code requires C++20.

static constexpr PyGetSetDef
configure_dict_getter (std::string_view object_name)
{
return {
"__dict__", /* name */
(getter) gdb_py_generic_dict_getter,
(setter) nullptr,
std::views::join (std::array {
"The __dict__ for this "sv, object_name, "."sv
}).data (), /* doc */
nullptr, /* closure */
}
}

Please let me know if you know a way of making it compatible with C++17.

Regarding gdbpy_dict_wrapper_PyTypeObject_tps, I am not sure how to expand the array in the calling code using a constexpr function.
Please let me know if you know a solution for it.

> I'd also suggest changing the name, the "PyGetSetDef" suffix is kind of
> un-gdb-ish and anyway not really needed for a function, since this will
> be clear from the return type.
>

If the code is convertible to C++17, configure_dict_getter seems a good name.
Otherwise, gdbpy_dict_wrapper_configure_dict_getter ?

Regarding gdbpy_dict_wrapper_PyTypeObject_tps, what about gdbpy_dict_wrapper_getsetattro ?

> You can see an example of doing this in a similar-ish context here
>
> https://sourceware.org/pipermail/gdb-patches/2026-February/225253.html
>
> Tom

Matthieu