llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

<details>
<summary>Changes</summary>

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`.


---
Full diff: https://github.com/llvm/llvm-project/pull/156725.diff


2 Files Affected:

- (modified) lldb/bindings/interface/SBStructuredDataExtensions.i (+12) 
- (modified) lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py 
(+15-2) 


``````````diff
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()

``````````

</details>


https://github.com/llvm/llvm-project/pull/156725
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to