[Lldb-commits] [lldb] r253085 - Modernize FormatBacktrace() and make portable for Python 3.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 13 13:28:45 PST 2015


Author: zturner
Date: Fri Nov 13 15:28:45 2015
New Revision: 253085

URL: http://llvm.org/viewvc/llvm-project?rev=253085&view=rev
Log:
Modernize FormatBacktrace() and make portable for Python 3.

Modified:
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=253085&r1=253084&r2=253085&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp Fri Nov 13 15:28:45 2015
@@ -825,6 +825,14 @@ PythonModule::AddModule(llvm::StringRef
     return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str()));
 }
 
+
+PythonModule
+PythonModule::ImportModule(llvm::StringRef module)
+{
+    std::string str = module.str();
+    return PythonModule(PyRefType::Owned, PyImport_ImportModule(str.c_str()));
+}
+
 bool
 PythonModule::Check(PyObject *py_obj)
 {

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=253085&r1=253084&r2=253085&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h Fri Nov 13 15:28:45 2015
@@ -415,6 +415,9 @@ class PythonModule : public PythonObject
     static PythonModule
     AddModule(llvm::StringRef module);
 
+    static PythonModule
+    ImportModule(llvm::StringRef module);
+
     // Bring in the no-argument base class version
     using PythonObject::Reset;
 

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp?rev=253085&r1=253084&r2=253085&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp Fri Nov 13 15:28:45 2015
@@ -146,6 +146,7 @@ PythonExceptionState::Format() const
     }
     else
     {
+        __debugbreak();
         // Otherwise, append some information about why we were unable to
         // obtain the backtrace.
         PythonString bt_error = bt_error_state.GetValue().Str();
@@ -157,50 +158,43 @@ PythonExceptionState::Format() const
 std::string
 PythonExceptionState::ReadBacktrace() const
 {
-    PythonObject traceback_module;
-    PythonObject stringIO_module;
-    PythonObject stringIO_builder;
-    PythonObject stringIO_buffer;
-    PythonObject printTB;
-    PythonObject printTB_args;
-    PythonObject printTB_result;
-    PythonObject stringIO_getvalue;
-    PythonObject printTB_string;
-
+    __debugbreak();
     std::string retval("backtrace unavailable");
 
+    auto traceback_module = PythonModule::ImportModule("traceback");
+#if PY_MAJOR_VERSION >= 3
+    auto stringIO_module = PythonModule::ImportModule("io");
+#else
+    auto stringIO_module = PythonModule::ImportModule("StringIO");
+#endif
     if (!m_traceback.IsAllocated())
         return retval;
 
-    traceback_module.Reset(PyRefType::Owned, PyImport_ImportModule("traceback"));
-    stringIO_module.Reset(PyRefType::Owned, PyImport_ImportModule("StringIO"));
     if (!traceback_module.IsAllocated() || !stringIO_module.IsAllocated())
         return retval;
 
-    stringIO_builder.Reset(PyRefType::Owned, PyObject_GetAttrString(stringIO_module.get(), "StringIO"));
+    auto stringIO_builder = stringIO_module.ResolveName<PythonCallable>("StringIO");
     if (!stringIO_builder.IsAllocated())
         return retval;
 
-    stringIO_buffer.Reset(PyRefType::Owned, PyObject_CallObject(stringIO_builder.get(), nullptr));
+    auto stringIO_buffer = stringIO_builder();
     if (!stringIO_buffer.IsAllocated())
         return retval;
 
-    printTB.Reset(PyRefType::Owned, PyObject_GetAttrString(traceback_module.get(), "print_tb"));
+    auto printTB = traceback_module.ResolveName<PythonCallable>("print_tb");
     if (!printTB.IsAllocated())
         return retval;
 
-    printTB_args.Reset(PyRefType::Owned, Py_BuildValue("OOO", m_traceback.get(), Py_None, stringIO_buffer.get()));
-    printTB_result.Reset(PyRefType::Owned, PyObject_CallObject(printTB.get(), printTB_args.get()));
-    stringIO_getvalue.Reset(PyRefType::Owned, PyObject_GetAttrString(stringIO_buffer.get(), "getvalue"));
+    auto printTB_result = printTB(m_traceback.get(), Py_None, stringIO_buffer.get());
+    auto stringIO_getvalue = stringIO_buffer.ResolveName<PythonCallable>("getvalue");
     if (!stringIO_getvalue.IsAllocated())
         return retval;
 
-    printTB_string.Reset(PyRefType::Owned, PyObject_CallObject(stringIO_getvalue.get(), nullptr));
+    auto printTB_string = stringIO_getvalue().AsType<PythonString>();
     if (!printTB_string.IsAllocated())
         return retval;
 
-    PythonString str(PyRefType::Borrowed, printTB_string.get());
-    llvm::StringRef string_data(str.GetString());
+    llvm::StringRef string_data(printTB_string.GetString());
     retval.assign(string_data.data(), string_data.size());
 
     return retval;




More information about the lldb-commits mailing list