Index: lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -185,11 +185,52 @@ enum class PyInitialValue { Invalid, Empty }; +// DOC: https://docs.python.org/3/c-api/arg.html#building-values template struct PythonFormat; -template <> struct PythonFormat { - static constexpr char format = 'K'; - static auto get(unsigned long long value) { return value; } +template <> struct PythonFormat { + static constexpr char format = 's'; + static auto get(char *value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'b'; + static auto get(char value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'B'; + static auto get(unsigned char value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'h'; + static auto get(short value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'H'; + static auto get(unsigned short value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'i'; + static auto get(int value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'I'; + static auto get(unsigned int value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'l'; + static auto get(long value) { return value; } +}; + +template <> struct PythonFormat { + static constexpr char format = 'k'; + static auto get(unsigned long value) { return value; } }; template <> struct PythonFormat { @@ -197,6 +238,11 @@ static auto get(long long value) { return value; } }; +template <> struct PythonFormat { + static constexpr char format = 'K'; + static auto get(unsigned long long value) { return value; } +}; + template <> struct PythonFormat { static constexpr char format = 'O'; static auto get(PyObject *value) { return value; } Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -1501,50 +1501,27 @@ StructuredData::ObjectSP os_plugin_object_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_register_info"; - if (!os_plugin_object_sp) - return StructuredData::DictionarySP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::DictionarySP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return StructuredData::DictionarySP(); + return {}; - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected expected_py_return = + implementor.CallMethod("get_register_info"); - return StructuredData::DictionarySP(); - } - - if (PyErr_Occurred()) - PyErr_Clear(); + if (!expected_py_return) + return {}; - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); + PythonObject py_return = expected_py_return.get(); - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } if (py_return.get()) { PythonDictionary result_dict(PyRefType::Borrowed, py_return.get()); return result_dict.CreateStructuredDictionary(); @@ -1555,51 +1532,26 @@ StructuredData::ArraySP ScriptInterpreterPythonImpl::OSPlugin_ThreadsInfo( StructuredData::ObjectSP os_plugin_object_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - - static char callee_name[] = "get_thread_info"; - if (!os_plugin_object_sp) - return StructuredData::ArraySP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::ArraySP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return StructuredData::ArraySP(); - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + return {}; - return StructuredData::ArraySP(); - } + llvm::Expected expected_py_return = + implementor.CallMethod("get_thread_info"); - if (PyErr_Occurred()) - PyErr_Clear(); + if (!expected_py_return) + return {}; - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = expected_py_return.get(); if (py_return.get()) { PythonList result_list(PyRefType::Borrowed, py_return.get()); @@ -1613,56 +1565,31 @@ StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_register_data"; - static char *param_format = - const_cast(GetPythonValueFormatString(tid)); - if (!os_plugin_object_sp) - return StructuredData::StringSP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::StringSP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return StructuredData::StringSP(); - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - return StructuredData::StringSP(); - } + return {}; - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected expected_py_return = + implementor.CallMethod("get_register_data", tid); - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, param_format, tid)); + if (!expected_py_return) + return {}; - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = expected_py_return.get(); if (py_return.get()) { PythonBytes result(PyRefType::Borrowed, py_return.get()); return result.CreateStructuredString(); } - return StructuredData::StringSP(); + return {}; } StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_CreateThread( @@ -1670,52 +1597,26 @@ lldb::addr_t context) { Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "create_thread"; - std::string param_format; - param_format += GetPythonValueFormatString(tid); - param_format += GetPythonValueFormatString(context); - if (!os_plugin_object_sp) - return StructuredData::DictionarySP(); + return {}; StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric(); if (!generic) - return nullptr; + return {}; PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return StructuredData::DictionarySP(); - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return StructuredData::DictionarySP(); + return {}; - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - return StructuredData::DictionarySP(); - } + llvm::Expected expected_py_return = + implementor.CallMethod("create_thread", tid, context); - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return(PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, - ¶m_format[0], tid, context)); + if (!expected_py_return) + return {}; - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = expected_py_return.get(); if (py_return.get()) { PythonDictionary result_dict(PyRefType::Borrowed, py_return.get()); @@ -2436,51 +2337,29 @@ Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - static char callee_name[] = "get_type_name"; - - ConstString ret_val; - bool got_string = false; - std::string buffer; - if (!implementor_sp) - return ret_val; + return {}; StructuredData::Generic *generic = implementor_sp->GetAsGeneric(); if (!generic) - return ret_val; + return {}; + PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue()); if (!implementor.IsAllocated()) - return ret_val; - - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return ret_val; + return {}; - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); - return ret_val; - } + llvm::Expected expected_py_return = + implementor.CallMethod("get_type_name"); - if (PyErr_Occurred()) - PyErr_Clear(); + if (!expected_py_return) + return {}; - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); + PythonObject py_return = expected_py_return.get(); - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + ConstString ret_val; + bool got_string = false; + std::string buffer; if (py_return.IsAllocated() && PythonString::Check(py_return.get())) { PythonString py_string(PyRefType::Borrowed, py_return.get()); @@ -2986,8 +2865,6 @@ Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_short_help"; - if (!cmd_obj_sp) return false; @@ -2997,34 +2874,13 @@ if (!implementor.IsAllocated()) return false; - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); + llvm::Expected expected_py_return = + implementor.CallMethod("get_short_help"); - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return false; - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + if (!expected_py_return) return false; - } - if (PyErr_Occurred()) - PyErr_Clear(); - - // Right now we know this function exists and is callable. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // If it fails, print the error but otherwise go on. - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = expected_py_return.get(); if (py_return.IsAllocated() && PythonString::Check(py_return.get())) { PythonString py_string(PyRefType::Borrowed, py_return.get()); @@ -3087,13 +2943,10 @@ bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject( StructuredData::GenericSP cmd_obj_sp, std::string &dest) { - bool got_string = false; dest.clear(); Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); - static char callee_name[] = "get_long_help"; - if (!cmd_obj_sp) return false; @@ -3103,36 +2956,15 @@ if (!implementor.IsAllocated()) return false; - PythonObject pmeth(PyRefType::Owned, - PyObject_GetAttrString(implementor.get(), callee_name)); - - if (PyErr_Occurred()) - PyErr_Clear(); - - if (!pmeth.IsAllocated()) - return false; - - if (PyCallable_Check(pmeth.get()) == 0) { - if (PyErr_Occurred()) - PyErr_Clear(); + llvm::Expected expected_py_return = + implementor.CallMethod("get_long_help"); + if (!expected_py_return) return false; - } - if (PyErr_Occurred()) - PyErr_Clear(); - - // right now we know this function exists and is callable.. - PythonObject py_return( - PyRefType::Owned, - PyObject_CallMethod(implementor.get(), callee_name, nullptr)); - - // if it fails, print the error but otherwise go on - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } + PythonObject py_return = expected_py_return.get(); + bool got_string = false; if (py_return.IsAllocated() && PythonString::Check(py_return.get())) { PythonString str(PyRefType::Borrowed, py_return.get()); llvm::StringRef str_data(str.GetString());