From: Tom Tromey <tom@tromey.com>
To: Matthieu Longo <matthieu.longo@arm.com>
Cc: <gdb-patches@sourceware.org>, Tom Tromey <tom@tromey.com>
Subject: Re: [PATCH v2 4/6] gdb: new setters and getters for __dict__, and attributes
Date: Tue, 27 Jan 2026 12:06:24 -0700 [thread overview]
Message-ID: <878qdian9r.fsf@tromey.com> (raw)
In-Reply-To: <20260127170215.1803582-5-matthieu.longo@arm.com> (Matthieu Longo's message of "Tue, 27 Jan 2026 17:02:13 +0000")
>>>>> "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.
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.
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.
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> +
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.
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?
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
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.
Matthieu> +#if PY_VERSION_HEX < 0x030a0000
Matthieu> +static inline PyObject*
Matthieu> +Py_NewRef (PyObject *obj)
The return type is missing a space before the "*"
Tom
next prev parent reply other threads:[~2026-01-27 19:06 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 [this message]
2026-01-28 11:57 ` Matthieu Longo
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=878qdian9r.fsf@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
--cc=matthieu.longo@arm.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