Skip to content

[gdb] Allow gdb.Value to take a Buffer plus a Type#16006

Merged
srittau merged 2 commits into
python:mainfrom
timo:gdb_value_init_from_buffer_with_type
Jul 11, 2026
Merged

[gdb] Allow gdb.Value to take a Buffer plus a Type#16006
srittau merged 2 commits into
python:mainfrom
timo:gdb_value_init_from_buffer_with_type

Conversation

@timo

@timo timo commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Support for this was introduced in a commit in 2019, which I think means it's been available since gdb 9.1

Commit that introduced the feature: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=fe07eca59d0544eb6c56c3559da9ceece23cae6e

If the type value passed is None or there is no second argument, it falls back to its "convert value from python" logic which tries a few things (bool, long, float, unicode string, gdb.Value, gdb.LazyString).

If instead the type is passed and is not None, it will always go through the buffer protocol, which interestingly means that you can't pass a gdb.Value nor a gdb.LazyString to the variant of the constructor with a type.

(gdb) python-interactive
>>> val = gdb.Value(99)
>>> t = gdb.lookup_type("uint64_t")
>>> t
<gdb.Type code=TYPE_CODE_TYPEDEF name=uint64_t>
>>> gdb.Value(val, t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    gdb.Value(val, t)
    ~~~~~~~~~^^^^^^^^
TypeError: Object must support the python buffer protocol.
>>> argv = gdb.selected_frame().read_var("argv")
>>> argv
<gdb.Value object at 0x7f4721a789b0>
>>> argv[0].lazy_string()
<gdb.LazyString object at 0x7f47235be3a0>
>>> ls = argv[0].lazy_string()
>>> gdb.Value(ls, t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    gdb.Value(ls, t)
    ~~~~~~~~~^^^^^^^
TypeError: Object must support the python buffer protocol.

timo and others added 2 commits July 11, 2026 18:22
Support for this was introduced in a commit in 2019, which I think
means it's been available since gdb 9.1

If a type is passed that is not None, it always goes through the
buffer protocol, which interestingly means you can't pass
an existing gdb.Value together with a type. You have to use the
cast method for that purpose.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 8e3110c into python:main Jul 11, 2026
44 checks passed
@timo timo deleted the gdb_value_init_from_buffer_with_type branch July 11, 2026 18:10
@timo

timo commented Jul 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants