From 209a788919a75e899ab32fb5e815de11d8655dd9 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Wed, 3 Sep 2025 11:18:56 -0700 Subject: [PATCH] [lldb] Reimplement __str__ in SBStructuredDataExtensions.i Follow up to #155061 and #156721. After discussing with @medismailben, the ideal course of to have a `__str__`, however, instead of throwing an exception, the fallback behavior calls `__repr__` (`GetDescription`). The main value of this is that `str(string_data)` will produce the string itself, not a quoted string as returned by `__repr__`/`GetDescription`. --- .../interface/SBStructuredDataExtensions.i | 12 ++++++++++++ .../sbstructureddata/TestStructuredDataAPI.py | 17 +++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lldb/bindings/interface/SBStructuredDataExtensions.i b/lldb/bindings/interface/SBStructuredDataExtensions.i index 9def366a4cf11..35aa2044eb1ac 100644 --- a/lldb/bindings/interface/SBStructuredDataExtensions.i +++ b/lldb/bindings/interface/SBStructuredDataExtensions.i @@ -38,6 +38,18 @@ STRING_EXTENSION_OUTSIDE(SBStructuredData) else: raise TypeError(f"cannot subscript {self.type_name(data_type)} type") + def __str__(self): + data_type = self.GetType() + if data_type in ( + eStructuredDataTypeString, + eStructuredDataTypeInteger, + eStructuredDataTypeSignedInteger, + eStructuredDataTypeFloat, + ): + return str(self.dynamic) + else: + return repr(self) + def __bool__(self): data_type = self.GetType() if data_type == eStructuredDataTypeInvalid: diff --git a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py index 275ac03a5a86d..b55f17c151ad5 100644 --- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py +++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py @@ -2,7 +2,6 @@ Test some SBStructuredData API. """ - import lldb from lldbsuite.test.decorators import * from lldbsuite.test.lldbtest import * @@ -10,6 +9,7 @@ import json + class TestStructuredDataAPI(TestBase): NO_DEBUG_INFO_TESTCASE = True @@ -346,7 +346,7 @@ def array_struct_test(self, dict_struct): self.fail("wrong output: " + str(output)) def test_round_trip_scalars(self): - for original in (0, 11, -1, 0.0, 4.5, -0.25, True, False): + for original in (0, 11, -1, 0.0, 4.5, -0.25, "", "dirk", True, False): constructor = type(original) data = lldb.SBStructuredData() data.SetFromJSON(json.dumps(original)) @@ -359,6 +359,19 @@ def test_dynamic(self): data.SetFromJSON(json.dumps(original)) self.assertEqual(data.dynamic, original) + def test_round_trip_string(self): + # No 0.0, it inherently does not round trip. + for original in (0, 11, -1, 4.5, -0.25, "", "dirk", True, False): + data = lldb.SBStructuredData() + data.SetFromJSON(json.dumps(original)) + self.assertEqual(str(data), str(original)) + + def test_str(self): + for original in ([15], {"id": 23}, None): + data = lldb.SBStructuredData() + data.SetFromJSON(json.dumps(original)) + self.assertTrue(str(data)) + def test_round_trip_int(self): for original in (0, 11, -1): data = lldb.SBStructuredData()