Skip to content

Commit 3b33b41

Browse files
committed
[lldb] Remove lexical block and fix formatting LoadScriptingModule (NFC)
1 parent 1197ee3 commit 3b33b41

File tree

1 file changed

+121
-124
lines changed

1 file changed

+121
-124
lines changed

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Lines changed: 121 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,149 +2734,146 @@ uint64_t replace_all(std::string &str, const std::string &oldStr,
27342734
bool ScriptInterpreterPythonImpl::LoadScriptingModule(
27352735
const char *pathname, bool init_session, lldb_private::Status &error,
27362736
StructuredData::ObjectSP *module_sp) {
2737+
namespace fs = llvm::sys::fs;
2738+
27372739
if (!pathname || !pathname[0]) {
27382740
error.SetErrorString("invalid pathname");
27392741
return false;
27402742
}
27412743

27422744
lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
27432745

2744-
{
2745-
FileSpec target_file(pathname);
2746-
FileSystem::Instance().Resolve(target_file);
2747-
FileSystem::Instance().Collect(target_file);
2748-
std::string basename(target_file.GetFilename().GetCString());
2749-
2750-
StreamString command_stream;
2751-
2752-
// Before executing Python code, lock the GIL.
2753-
Locker py_lock(this,
2754-
Locker::AcquireLock |
2755-
(init_session ? Locker::InitSession : 0) |
2756-
Locker::NoSTDIN,
2757-
Locker::FreeAcquiredLock |
2758-
(init_session ? Locker::TearDownSession : 0));
2759-
namespace fs = llvm::sys::fs;
2760-
fs::file_status st;
2761-
std::error_code ec = status(target_file.GetPath(), st);
2762-
2763-
if (ec || st.type() == fs::file_type::status_error ||
2764-
st.type() == fs::file_type::type_unknown ||
2765-
st.type() == fs::file_type::file_not_found) {
2766-
// if not a valid file of any sort, check if it might be a filename still
2767-
// dot can't be used but / and \ can, and if either is found, reject
2768-
if (strchr(pathname, '\\') || strchr(pathname, '/')) {
2769-
error.SetErrorString("invalid pathname");
2770-
return false;
2771-
}
2772-
basename = pathname; // not a filename, probably a package of some sort,
2773-
// let it go through
2774-
} else if (is_directory(st) || is_regular_file(st)) {
2775-
if (target_file.GetDirectory().IsEmpty()) {
2776-
error.SetErrorString("invalid directory name");
2777-
return false;
2778-
}
2746+
FileSpec target_file(pathname);
2747+
FileSystem::Instance().Resolve(target_file);
2748+
FileSystem::Instance().Collect(target_file);
2749+
std::string basename(target_file.GetFilename().GetCString());
27792750

2780-
std::string directory = target_file.GetDirectory().GetCString();
2781-
replace_all(directory, "\\", "\\\\");
2782-
replace_all(directory, "'", "\\'");
2783-
2784-
// now make sure that Python has "directory" in the search path
2785-
StreamString command_stream;
2786-
command_stream.Printf("if not (sys.path.__contains__('%s')):\n "
2787-
"sys.path.insert(1,'%s');\n\n",
2788-
directory.c_str(), directory.c_str());
2789-
bool syspath_retval =
2790-
ExecuteMultipleLines(command_stream.GetData(),
2791-
ScriptInterpreter::ExecuteScriptOptions()
2792-
.SetEnableIO(false)
2793-
.SetSetLLDBGlobals(false))
2794-
.Success();
2795-
if (!syspath_retval) {
2796-
error.SetErrorString("Python sys.path handling failed");
2797-
return false;
2798-
}
2751+
StreamString command_stream;
27992752

2800-
} else {
2801-
error.SetErrorString("no known way to import this module specification");
2753+
// Before executing Python code, lock the GIL.
2754+
Locker py_lock(this,
2755+
Locker::AcquireLock |
2756+
(init_session ? Locker::InitSession : 0) | Locker::NoSTDIN,
2757+
Locker::FreeAcquiredLock |
2758+
(init_session ? Locker::TearDownSession : 0));
2759+
fs::file_status st;
2760+
std::error_code ec = status(target_file.GetPath(), st);
2761+
2762+
if (ec || st.type() == fs::file_type::status_error ||
2763+
st.type() == fs::file_type::type_unknown ||
2764+
st.type() == fs::file_type::file_not_found) {
2765+
// if not a valid file of any sort, check if it might be a filename still
2766+
// dot can't be used but / and \ can, and if either is found, reject
2767+
if (strchr(pathname, '\\') || strchr(pathname, '/')) {
2768+
error.SetErrorString("invalid pathname");
28022769
return false;
28032770
}
2804-
2805-
// Strip .py or .pyc extension
2806-
llvm::StringRef extension = target_file.GetFileNameExtension().GetCString();
2807-
if (!extension.empty()) {
2808-
if (extension == ".py")
2809-
basename.resize(basename.length() - 3);
2810-
else if (extension == ".pyc")
2811-
basename.resize(basename.length() - 4);
2771+
basename = pathname; // not a filename, probably a package of some sort,
2772+
// let it go through
2773+
} else if (is_directory(st) || is_regular_file(st)) {
2774+
if (target_file.GetDirectory().IsEmpty()) {
2775+
error.SetErrorString("invalid directory name");
2776+
return false;
28122777
}
28132778

2814-
// check if the module is already import-ed
2815-
command_stream.Clear();
2816-
command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
2817-
bool does_contain = false;
2818-
// this call will succeed if the module was ever imported in any Debugger
2819-
// in the lifetime of the process in which this LLDB framework is living
2820-
bool was_imported_globally =
2821-
(ExecuteOneLineWithReturn(
2822-
command_stream.GetData(),
2823-
ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
2824-
ScriptInterpreter::ExecuteScriptOptions()
2825-
.SetEnableIO(false)
2826-
.SetSetLLDBGlobals(false)) &&
2827-
does_contain);
2828-
// this call will fail if the module was not imported in this Debugger
2829-
// before
2830-
command_stream.Clear();
2831-
command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
2832-
bool was_imported_locally = GetSessionDictionary()
2833-
.GetItemForKey(PythonString(basename))
2834-
.IsAllocated();
2835-
2836-
bool was_imported = (was_imported_globally || was_imported_locally);
2837-
2838-
// now actually do the import
2839-
command_stream.Clear();
2779+
std::string directory = target_file.GetDirectory().GetCString();
2780+
replace_all(directory, "\\", "\\\\");
2781+
replace_all(directory, "'", "\\'");
28402782

2841-
if (was_imported) {
2842-
if (!was_imported_locally)
2843-
command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
2844-
basename.c_str());
2845-
else
2846-
command_stream.Printf("reload_module(%s)", basename.c_str());
2847-
} else
2848-
command_stream.Printf("import %s", basename.c_str());
2849-
2850-
error = ExecuteMultipleLines(command_stream.GetData(),
2851-
ScriptInterpreter::ExecuteScriptOptions()
2852-
.SetEnableIO(false)
2853-
.SetSetLLDBGlobals(false));
2854-
if (error.Fail())
2855-
return false;
2856-
2857-
// if we are here, everything worked
2858-
// call __lldb_init_module(debugger,dict)
2859-
if (!LLDBSwigPythonCallModuleInit(basename.c_str(),
2860-
m_dictionary_name.c_str(), debugger_sp)) {
2861-
error.SetErrorString("calling __lldb_init_module failed");
2783+
// now make sure that Python has "directory" in the search path
2784+
StreamString command_stream;
2785+
command_stream.Printf("if not (sys.path.__contains__('%s')):\n "
2786+
"sys.path.insert(1,'%s');\n\n",
2787+
directory.c_str(), directory.c_str());
2788+
bool syspath_retval =
2789+
ExecuteMultipleLines(command_stream.GetData(),
2790+
ScriptInterpreter::ExecuteScriptOptions()
2791+
.SetEnableIO(false)
2792+
.SetSetLLDBGlobals(false))
2793+
.Success();
2794+
if (!syspath_retval) {
2795+
error.SetErrorString("Python sys.path handling failed");
28622796
return false;
28632797
}
28642798

2865-
if (module_sp) {
2866-
// everything went just great, now set the module object
2867-
command_stream.Clear();
2868-
command_stream.Printf("%s", basename.c_str());
2869-
void *module_pyobj = nullptr;
2870-
if (ExecuteOneLineWithReturn(
2871-
command_stream.GetData(),
2872-
ScriptInterpreter::eScriptReturnTypeOpaqueObject,
2873-
&module_pyobj) &&
2874-
module_pyobj)
2875-
*module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
2876-
}
2799+
} else {
2800+
error.SetErrorString("no known way to import this module specification");
2801+
return false;
2802+
}
28772803

2878-
return true;
2804+
// Strip .py or .pyc extension
2805+
llvm::StringRef extension = target_file.GetFileNameExtension().GetCString();
2806+
if (!extension.empty()) {
2807+
if (extension == ".py")
2808+
basename.resize(basename.length() - 3);
2809+
else if (extension == ".pyc")
2810+
basename.resize(basename.length() - 4);
2811+
}
2812+
2813+
// check if the module is already import-ed
2814+
command_stream.Clear();
2815+
command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
2816+
bool does_contain = false;
2817+
// this call will succeed if the module was ever imported in any Debugger
2818+
// in the lifetime of the process in which this LLDB framework is living
2819+
bool was_imported_globally =
2820+
(ExecuteOneLineWithReturn(
2821+
command_stream.GetData(),
2822+
ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
2823+
ScriptInterpreter::ExecuteScriptOptions()
2824+
.SetEnableIO(false)
2825+
.SetSetLLDBGlobals(false)) &&
2826+
does_contain);
2827+
// this call will fail if the module was not imported in this Debugger
2828+
// before
2829+
command_stream.Clear();
2830+
command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
2831+
bool was_imported_locally = GetSessionDictionary()
2832+
.GetItemForKey(PythonString(basename))
2833+
.IsAllocated();
2834+
2835+
bool was_imported = (was_imported_globally || was_imported_locally);
2836+
2837+
// now actually do the import
2838+
command_stream.Clear();
2839+
2840+
if (was_imported) {
2841+
if (!was_imported_locally)
2842+
command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
2843+
basename.c_str());
2844+
else
2845+
command_stream.Printf("reload_module(%s)", basename.c_str());
2846+
} else
2847+
command_stream.Printf("import %s", basename.c_str());
2848+
2849+
error = ExecuteMultipleLines(command_stream.GetData(),
2850+
ScriptInterpreter::ExecuteScriptOptions()
2851+
.SetEnableIO(false)
2852+
.SetSetLLDBGlobals(false));
2853+
if (error.Fail())
2854+
return false;
2855+
2856+
// if we are here, everything worked
2857+
// call __lldb_init_module(debugger,dict)
2858+
if (!LLDBSwigPythonCallModuleInit(basename.c_str(), m_dictionary_name.c_str(),
2859+
debugger_sp)) {
2860+
error.SetErrorString("calling __lldb_init_module failed");
2861+
return false;
28792862
}
2863+
2864+
if (module_sp) {
2865+
// everything went just great, now set the module object
2866+
command_stream.Clear();
2867+
command_stream.Printf("%s", basename.c_str());
2868+
void *module_pyobj = nullptr;
2869+
if (ExecuteOneLineWithReturn(
2870+
command_stream.GetData(),
2871+
ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj) &&
2872+
module_pyobj)
2873+
*module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
2874+
}
2875+
2876+
return true;
28802877
}
28812878

28822879
bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) {

0 commit comments

Comments
 (0)