Skip to content

Commit 209a788

Browse files
committed
[lldb] Reimplement __str__ in SBStructuredDataExtensions.i
Follow up to llvm#155061 and llvm#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`.
1 parent 7753f61 commit 209a788

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lldb/bindings/interface/SBStructuredDataExtensions.i

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ STRING_EXTENSION_OUTSIDE(SBStructuredData)
3838
else:
3939
raise TypeError(f"cannot subscript {self.type_name(data_type)} type")
4040

41+
def __str__(self):
42+
data_type = self.GetType()
43+
if data_type in (
44+
eStructuredDataTypeString,
45+
eStructuredDataTypeInteger,
46+
eStructuredDataTypeSignedInteger,
47+
eStructuredDataTypeFloat,
48+
):
49+
return str(self.dynamic)
50+
else:
51+
return repr(self)
52+
4153
def __bool__(self):
4254
data_type = self.GetType()
4355
if data_type == eStructuredDataTypeInvalid:

lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
Test some SBStructuredData API.
33
"""
44

5-
65
import lldb
76
from lldbsuite.test.decorators import *
87
from lldbsuite.test.lldbtest import *
98
from lldbsuite.test import lldbutil
109

1110
import json
1211

12+
1313
class TestStructuredDataAPI(TestBase):
1414
NO_DEBUG_INFO_TESTCASE = True
1515

@@ -346,7 +346,7 @@ def array_struct_test(self, dict_struct):
346346
self.fail("wrong output: " + str(output))
347347

348348
def test_round_trip_scalars(self):
349-
for original in (0, 11, -1, 0.0, 4.5, -0.25, True, False):
349+
for original in (0, 11, -1, 0.0, 4.5, -0.25, "", "dirk", True, False):
350350
constructor = type(original)
351351
data = lldb.SBStructuredData()
352352
data.SetFromJSON(json.dumps(original))
@@ -359,6 +359,19 @@ def test_dynamic(self):
359359
data.SetFromJSON(json.dumps(original))
360360
self.assertEqual(data.dynamic, original)
361361

362+
def test_round_trip_string(self):
363+
# No 0.0, it inherently does not round trip.
364+
for original in (0, 11, -1, 4.5, -0.25, "", "dirk", True, False):
365+
data = lldb.SBStructuredData()
366+
data.SetFromJSON(json.dumps(original))
367+
self.assertEqual(str(data), str(original))
368+
369+
def test_str(self):
370+
for original in ([15], {"id": 23}, None):
371+
data = lldb.SBStructuredData()
372+
data.SetFromJSON(json.dumps(original))
373+
self.assertTrue(str(data))
374+
362375
def test_round_trip_int(self):
363376
for original in (0, 11, -1):
364377
data = lldb.SBStructuredData()

0 commit comments

Comments
 (0)