1 //===-- ScriptedPythonInterface.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Host/Config.h"
10 #include "lldb/Utility/Log.h"
11 #include "lldb/Utility/Logging.h"
12 #include "lldb/lldb-enumerations.h"
13 
14 #if LLDB_ENABLE_PYTHON
15 
16 // LLDB Python header must be included first
17 #include "lldb-python.h"
18 
19 #include "SWIGPythonBridge.h"
20 #include "ScriptInterpreterPythonImpl.h"
21 #include "ScriptedPythonInterface.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 ScriptedPythonInterface::ScriptedPythonInterface(
27     ScriptInterpreterPythonImpl &interpreter)
28     : ScriptedInterface(), m_interpreter(interpreter) {}
29 
30 Status
31 ScriptedPythonInterface::GetStatusFromMethod(llvm::StringRef method_name) {
32   Status error;
33   Dispatch<Status>(method_name, error);
34 
35   return error;
36 }
37 
38 template <>
39 Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
40     python::PythonObject &p, Status &error) {
41   if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
42           LLDBSWIGPython_CastPyObjectToSBError(p.get())))
43     error = m_interpreter.GetStatusFromSBError(*sb_error);
44   else
45     error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
46 
47   return error;
48 }
49 
50 template <>
51 lldb::DataExtractorSP
52 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
53     python::PythonObject &p, Status &error) {
54   lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
55       LLDBSWIGPython_CastPyObjectToSBData(p.get()));
56 
57   if (!sb_data) {
58     error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
59     return nullptr;
60   }
61 
62   return m_interpreter.GetDataExtractorFromSBData(*sb_data);
63 }
64 
65 #endif
66