aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-09-02 21:17:18 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-12-08 17:34:50 +0000
commit06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e (patch)
tree62f873df87c7c675557a179e0c4c83fe9f3087bc /contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
parentcf037972ea8863e2bab7461d77345367d2c1e054 (diff)
parent7fa27ce4a07f19b07799a767fc29416f3b625afb (diff)
Diffstat (limited to 'contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp')
-rw-r--r--contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp51
1 files changed, 37 insertions, 14 deletions
diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 22918561692c..eee2f6f5d43f 100644
--- a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -25,6 +25,7 @@
#include "llvm/Support/Errno.h"
#include <cstdio>
+#include <variant>
using namespace lldb_private;
using namespace lldb;
@@ -101,7 +102,7 @@ Expected<long long> PythonObject::AsLongLong() const {
return r;
}
-Expected<long long> PythonObject::AsUnsignedLongLong() const {
+Expected<unsigned long long> PythonObject::AsUnsignedLongLong() const {
if (!m_py_obj)
return nullDeref();
assert(!PyErr_Occurred());
@@ -117,6 +118,7 @@ Expected<unsigned long long> PythonObject::AsModuloUnsignedLongLong() const {
return nullDeref();
assert(!PyErr_Occurred());
unsigned long long r = PyLong_AsUnsignedLongLongMask(m_py_obj);
+ // FIXME: We should fetch the exception message and hoist it.
if (PyErr_Occurred())
return exception();
return r;
@@ -267,9 +269,15 @@ StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
case PyObjectType::Boolean:
return PythonBoolean(PyRefType::Borrowed, m_py_obj)
.CreateStructuredBoolean();
- case PyObjectType::Integer:
- return PythonInteger(PyRefType::Borrowed, m_py_obj)
- .CreateStructuredInteger();
+ case PyObjectType::Integer: {
+ StructuredData::IntegerSP int_sp =
+ PythonInteger(PyRefType::Borrowed, m_py_obj).CreateStructuredInteger();
+ if (std::holds_alternative<StructuredData::UnsignedIntegerSP>(int_sp))
+ return std::get<StructuredData::UnsignedIntegerSP>(int_sp);
+ if (std::holds_alternative<StructuredData::SignedIntegerSP>(int_sp))
+ return std::get<StructuredData::SignedIntegerSP>(int_sp);
+ return nullptr;
+ };
case PyObjectType::List:
return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray();
case PyObjectType::String:
@@ -459,17 +467,32 @@ void PythonInteger::SetInteger(int64_t value) {
}
StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const {
- StructuredData::IntegerSP result(new StructuredData::Integer);
- // FIXME this is really not ideal. Errors are silently converted to 0
- // and overflows are silently wrapped. But we'd need larger changes
- // to StructuredData to fix it, so that's how it is for now.
- llvm::Expected<unsigned long long> value = AsModuloUnsignedLongLong();
- if (!value) {
+ StructuredData::UnsignedIntegerSP uint_sp = CreateStructuredUnsignedInteger();
+ return uint_sp ? StructuredData::IntegerSP(uint_sp)
+ : CreateStructuredSignedInteger();
+}
+
+StructuredData::UnsignedIntegerSP
+PythonInteger::CreateStructuredUnsignedInteger() const {
+ StructuredData::UnsignedIntegerSP result = nullptr;
+ llvm::Expected<unsigned long long> value = AsUnsignedLongLong();
+ if (!value)
llvm::consumeError(value.takeError());
- result->SetValue(0);
- } else {
- result->SetValue(value.get());
- }
+ else
+ result = std::make_shared<StructuredData::UnsignedInteger>(value.get());
+
+ return result;
+}
+
+StructuredData::SignedIntegerSP
+PythonInteger::CreateStructuredSignedInteger() const {
+ StructuredData::SignedIntegerSP result = nullptr;
+ llvm::Expected<long long> value = AsLongLong();
+ if (!value)
+ llvm::consumeError(value.takeError());
+ else
+ result = std::make_shared<StructuredData::SignedInteger>(value.get());
+
return result;
}