public inbox for
help / color / mirror / Atom feed
From: Tom de Vries <>
Subject: [binutils-gdb] [gdb/testsuite] Handle older python in gdb.python/
Date: Mon, 27 Jun 2022 10:48:02 +0000 (GMT)	[thread overview]
Message-ID: <> (raw);h=2135495484109f888167287cca249be9d84ed535

commit 2135495484109f888167287cca249be9d84ed535
Author: Tom de Vries <>
Date:   Mon Jun 27 12:47:26 2022 +0200

    [gdb/testsuite] Handle older python in gdb.python/
    With python 3.4, I run into:
    Traceback (most recent call last):^M
      File "<string>", line 1, in <module>^M
      "outputs/gdb.python/py-send-packet/", line 128, in \
        res = conn.send_packet(b"X%x,4:\x02\x02\x02\x02" % addr)^M
    TypeError: Could not convert Python object: b'X%x,4:\x02\x02\x02\x02'.^M
    Error while executing Python code.^M
    while with python 3.6 this works fine.
    The type of addr is <class 'gdb.Value'>, so the first thing to try is whether
    changing it into a string works:
        addr_str = "%x" % addr
        res = conn.send_packet(b"X%s,4:\x02\x02\x02\x02" % addr_str)
    which gets us the more detailed:
    TypeError: unsupported operand type(s) for %: 'bytes' and 'str'
    Fix this by avoiding the '%' operator in the byte literal, and use instead:
    def xpacket_header (addr):
        return ("X%x,4:" % addr).encode('ascii')
        res = conn.send_packet(xpacket_header(addr) + b"\x02\x02\x02\x02")
    Tested on x86_64-linux, with python 3.4 and 3.6, and a backported version was
    tested on the gdb-12-branch in combination with python 2.7.

 gdb/testsuite/gdb.python/ | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.python/ b/gdb/testsuite/gdb.python/
index a6adc8279cb..ae70b852538 100644
--- a/gdb/testsuite/gdb.python/
+++ b/gdb/testsuite/gdb.python/
@@ -114,6 +114,10 @@ def check_global_var(expected_val):
     if val != expected_val:
         raise gdb.GdbError("global_var is 0x%x, expected 0x%x" % (val, expected_val))
+# Return a bytes object representing an 'X' packet header with
+# address ADDR.
+def xpacket_header (addr):
+    return ("X%x,4:" % addr).encode('ascii')
 # Set the 'X' packet to the remote target to set a global variable.
 # Checks that we can send byte values.
@@ -125,7 +129,7 @@ def run_set_global_var_test():
     res = conn.send_packet("X%x,4:\x01\x01\x01\x01" % addr)
     assert isinstance(res, bytes)
-    res = conn.send_packet(b"X%x,4:\x02\x02\x02\x02" % addr)
+    res = conn.send_packet(xpacket_header(addr) + b"\x02\x02\x02\x02")
     assert isinstance(res, bytes)
@@ -142,7 +146,7 @@ def run_set_global_var_test():
     assert saw_error
     # Now we pass a bytes object, which will work.
-    res = conn.send_packet(b"X%x,4:\xff\xff\xff\xff" % addr)
+    res = conn.send_packet(xpacket_header(addr) + b"\xff\xff\xff\xff")
     print("set global_var test passed")

                 reply	other threads:[~2022-06-27 10:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* 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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).