Skip to content

Conversation

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Sep 3, 2025

__str__ was implemented in #155061, however its behavior was limited to only a some kinds of SBStructuredData. That was a breaking change, and this change removes that implementation of __str__, relying on the existing behavior which calls GetDescription.

@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2025

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

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

2 Files Affected:

  • (modified) lldb/bindings/interface/SBStructuredDataExtensions.i (-12)
  • (modified) lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py (+3-8)
diff --git a/lldb/bindings/interface/SBStructuredDataExtensions.i b/lldb/bindings/interface/SBStructuredDataExtensions.i
index 6c452a0da3852..9def366a4cf11 100644
--- a/lldb/bindings/interface/SBStructuredDataExtensions.i
+++ b/lldb/bindings/interface/SBStructuredDataExtensions.i
@@ -52,18 +52,6 @@ STRING_EXTENSION_OUTSIDE(SBStructuredData)
         else:
             raise TypeError("cannot convert generic to bool")
 
-    def __str__(self):
-        data_type = self.GetType()
-        if data_type in (
-            eStructuredDataTypeString,
-            eStructuredDataTypeInteger,
-            eStructuredDataTypeSignedInteger,
-            eStructuredDataTypeFloat,
-        ):
-            return str(self.dynamic)
-        else:
-            raise TypeError(f"cannot convert {self.type_name(data_type)} to string")
-
     def __int__(self):
         data_type = self.GetType()
         if data_type in (
diff --git a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
index 6c00588636516..275ac03a5a86d 100644
--- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -48,6 +48,8 @@ def structured_data_api_test(self):
         s.Clear()
         error = example.GetDescription(s)
         self.assertSuccess(error, "GetDescription works")
+        # Ensure str() doesn't raise an exception.
+        self.assertTrue(str(example))
         if not "key_float" in s.GetData():
             self.fail("FAILED: could not find key_float in description output")
 
@@ -344,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, "", "dirk", True, False):
+        for original in (0, 11, -1, 0.0, 4.5, -0.25, True, False):
             constructor = type(original)
             data = lldb.SBStructuredData()
             data.SetFromJSON(json.dumps(original))
@@ -357,13 +359,6 @@ 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"):
-            data = lldb.SBStructuredData()
-            data.SetFromJSON(json.dumps(original))
-            self.assertEqual(str(data), str(original))
-
     def test_round_trip_int(self):
         for original in (0, 11, -1):
             data = lldb.SBStructuredData()

@adrian-prantl adrian-prantl merged commit 722339d into llvm:main Sep 3, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 3, 2025

LLVM Buildbot has detected a new failure on builder cross-project-tests-sie-ubuntu-dwarf5 running on doug-worker-1b while building lldb at step 6 "test-build-unified-tree-check-cross-project".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/163/builds/25825

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-cross-project) failure: test (failure)
******************** TEST 'cross-project-tests :: debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp' FAILED ********************
Exit Code: 2

Command Output (stdout):
--
optnone-struct-and-methods.cpp: (1.0000)

## BEGIN ##
[1, "main", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 102, 7, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [2, "(anonymous namespace)::A::A()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 69, 5, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [3, "(anonymous namespace)::A::A()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 70, 3, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
[4, "main", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 103, 16, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [5, "(anonymous namespace)::A::getData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 89, 5, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   .   [6, "(anonymous namespace)::A::getOtherData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 46, 47, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [7, "(anonymous namespace)::A::getData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 89, 5, "StopReason.STEP", "StepKind.SAME", []]
.   [8, "(anonymous namespace)::A::getData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 90, 5, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   .   [9, "(anonymous namespace)::A::setOtherData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 63, 5, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   .   [10, "(anonymous namespace)::A::setOtherData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 64, 32, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   .   [11, "(anonymous namespace)::A::setOtherData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 65, 3, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [12, "(anonymous namespace)::A::getData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 91, 12, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   .   [13, "(anonymous namespace)::A::getOtherData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 46, 47, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [14, "(anonymous namespace)::A::getData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 91, 30, "StopReason.STEP", "StepKind.HORIZONTAL_FORWARD", []]
[15, "main", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 104, 3, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [16, "(anonymous namespace)::A::~A()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 82, 6, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   .   [17, "(anonymous namespace)::A::getOtherData()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 46, 47, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [18, "(anonymous namespace)::A::~A()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 82, 24, "StopReason.STEP", "StepKind.HORIZONTAL_FORWARD", []]
.   [19, "(anonymous namespace)::A::~A()", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 83, 3, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
[20, "main", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-struct-and-methods.cpp", 105, 3, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
## END (20 steps) ##


  optnone-struct-and-methods.cpp:70 [_data.a.float_ptr == -1] ExpectValue [0/7]
    expected encountered watches:
      true

  optnone-struct-and-methods.cpp:70 [_data.a.raw_ptr == -1] ExpectValue [0/7]
    expected encountered watches:
      true

  optnone-struct-and-methods.cpp:70 [a] ExpectValue [0/7]
    expected encountered watches:
      -1

  optnone-struct-and-methods.cpp:70 [a_global_ptr[0]] ExpectValue [0/7]
    expected encountered watches:
      3405691582

  optnone-struct-and-methods.cpp:70 [a_global_ptr[1]] ExpectValue [0/7]
    expected encountered watches:
      4276993775
...

kastiglione added a commit to kastiglione/llvm-project that referenced this pull request Sep 3, 2025
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`.
kastiglione added a commit that referenced this pull request Sep 3, 2025
…" (#156728)

Reverts #155061 (and #156721) which caused Crashlog shell tests to break.
@kastiglione kastiglione deleted the lldb-Revert-custom-__str__-in-SBStructuredDataExtensions.i branch September 3, 2025 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants