Skip to content

[lldb] Reimplement PythonObject::Dump using the limited API #152055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"

#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Errno.h"
Expand Down Expand Up @@ -131,23 +132,30 @@ void StructuredPythonObject::Serialize(llvm::json::OStream &s) const {
// PythonObject

void PythonObject::Dump(Stream &strm) const {
if (m_py_obj) {
FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile);
if (file) {
::PyObject_Print(m_py_obj, file, 0);
const long length = ftell(file);
if (length) {
::rewind(file);
std::vector<char> file_contents(length, '\0');
const size_t length_read =
::fread(file_contents.data(), 1, file_contents.size(), file);
if (length_read > 0)
strm.Write(file_contents.data(), length_read);
}
::fclose(file);
}
} else
strm.PutCString("NULL");
if (!m_py_obj) {
strm << "NULL";
return;
}

PyObject *py_str = PyObject_Repr(m_py_obj);
if (!py_str)
return;

auto release_py_str = llvm::make_scope_exit([py_str] { Py_DECREF(py_str); });

PyObject *py_bytes = PyUnicode_AsEncodedString(py_str, "utf-8", "replace");
if (!py_bytes)
return;

auto release_py_bytes =
llvm::make_scope_exit([py_bytes] { Py_DECREF(py_bytes); });

char *buffer = nullptr;
Py_ssize_t length = 0;
if (PyBytes_AsStringAndSize(py_bytes, &buffer, &length) == -1)
return;

strm << llvm::StringRef(buffer, length);
}

PyObjectType PythonObject::GetObjectType() const {
Expand Down
Loading