Skip to content

Commit

Permalink
[lldb] Remove lexical block and fix formatting LoadScriptingModule (NFC)
Browse files Browse the repository at this point in the history
  • Loading branch information
JDevlieghere committed Oct 14, 2020
1 parent 1197ee3 commit 3b33b41
Showing 1 changed file with 121 additions and 124 deletions.
245 changes: 121 additions & 124 deletions lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
Expand Up @@ -2734,149 +2734,146 @@ uint64_t replace_all(std::string &str, const std::string &oldStr,
bool ScriptInterpreterPythonImpl::LoadScriptingModule(
const char *pathname, bool init_session, lldb_private::Status &error,
StructuredData::ObjectSP *module_sp) {
namespace fs = llvm::sys::fs;

if (!pathname || !pathname[0]) {
error.SetErrorString("invalid pathname");
return false;
}

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

{
FileSpec target_file(pathname);
FileSystem::Instance().Resolve(target_file);
FileSystem::Instance().Collect(target_file);
std::string basename(target_file.GetFilename().GetCString());

StreamString command_stream;

// Before executing Python code, lock the GIL.
Locker py_lock(this,
Locker::AcquireLock |
(init_session ? Locker::InitSession : 0) |
Locker::NoSTDIN,
Locker::FreeAcquiredLock |
(init_session ? Locker::TearDownSession : 0));
namespace fs = llvm::sys::fs;
fs::file_status st;
std::error_code ec = status(target_file.GetPath(), st);

if (ec || st.type() == fs::file_type::status_error ||
st.type() == fs::file_type::type_unknown ||
st.type() == fs::file_type::file_not_found) {
// if not a valid file of any sort, check if it might be a filename still
// dot can't be used but / and \ can, and if either is found, reject
if (strchr(pathname, '\\') || strchr(pathname, '/')) {
error.SetErrorString("invalid pathname");
return false;
}
basename = pathname; // not a filename, probably a package of some sort,
// let it go through
} else if (is_directory(st) || is_regular_file(st)) {
if (target_file.GetDirectory().IsEmpty()) {
error.SetErrorString("invalid directory name");
return false;
}
FileSpec target_file(pathname);
FileSystem::Instance().Resolve(target_file);
FileSystem::Instance().Collect(target_file);
std::string basename(target_file.GetFilename().GetCString());

std::string directory = target_file.GetDirectory().GetCString();
replace_all(directory, "\\", "\\\\");
replace_all(directory, "'", "\\'");

// now make sure that Python has "directory" in the search path
StreamString command_stream;
command_stream.Printf("if not (sys.path.__contains__('%s')):\n "
"sys.path.insert(1,'%s');\n\n",
directory.c_str(), directory.c_str());
bool syspath_retval =
ExecuteMultipleLines(command_stream.GetData(),
ScriptInterpreter::ExecuteScriptOptions()
.SetEnableIO(false)
.SetSetLLDBGlobals(false))
.Success();
if (!syspath_retval) {
error.SetErrorString("Python sys.path handling failed");
return false;
}
StreamString command_stream;

} else {
error.SetErrorString("no known way to import this module specification");
// Before executing Python code, lock the GIL.
Locker py_lock(this,
Locker::AcquireLock |
(init_session ? Locker::InitSession : 0) | Locker::NoSTDIN,
Locker::FreeAcquiredLock |
(init_session ? Locker::TearDownSession : 0));
fs::file_status st;
std::error_code ec = status(target_file.GetPath(), st);

if (ec || st.type() == fs::file_type::status_error ||
st.type() == fs::file_type::type_unknown ||
st.type() == fs::file_type::file_not_found) {
// if not a valid file of any sort, check if it might be a filename still
// dot can't be used but / and \ can, and if either is found, reject
if (strchr(pathname, '\\') || strchr(pathname, '/')) {
error.SetErrorString("invalid pathname");
return false;
}

// Strip .py or .pyc extension
llvm::StringRef extension = target_file.GetFileNameExtension().GetCString();
if (!extension.empty()) {
if (extension == ".py")
basename.resize(basename.length() - 3);
else if (extension == ".pyc")
basename.resize(basename.length() - 4);
basename = pathname; // not a filename, probably a package of some sort,
// let it go through
} else if (is_directory(st) || is_regular_file(st)) {
if (target_file.GetDirectory().IsEmpty()) {
error.SetErrorString("invalid directory name");
return false;
}

// check if the module is already import-ed
command_stream.Clear();
command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
bool does_contain = false;
// this call will succeed if the module was ever imported in any Debugger
// in the lifetime of the process in which this LLDB framework is living
bool was_imported_globally =
(ExecuteOneLineWithReturn(
command_stream.GetData(),
ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
ScriptInterpreter::ExecuteScriptOptions()
.SetEnableIO(false)
.SetSetLLDBGlobals(false)) &&
does_contain);
// this call will fail if the module was not imported in this Debugger
// before
command_stream.Clear();
command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
bool was_imported_locally = GetSessionDictionary()
.GetItemForKey(PythonString(basename))
.IsAllocated();

bool was_imported = (was_imported_globally || was_imported_locally);

// now actually do the import
command_stream.Clear();
std::string directory = target_file.GetDirectory().GetCString();
replace_all(directory, "\\", "\\\\");
replace_all(directory, "'", "\\'");

if (was_imported) {
if (!was_imported_locally)
command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
basename.c_str());
else
command_stream.Printf("reload_module(%s)", basename.c_str());
} else
command_stream.Printf("import %s", basename.c_str());

error = ExecuteMultipleLines(command_stream.GetData(),
ScriptInterpreter::ExecuteScriptOptions()
.SetEnableIO(false)
.SetSetLLDBGlobals(false));
if (error.Fail())
return false;

// if we are here, everything worked
// call __lldb_init_module(debugger,dict)
if (!LLDBSwigPythonCallModuleInit(basename.c_str(),
m_dictionary_name.c_str(), debugger_sp)) {
error.SetErrorString("calling __lldb_init_module failed");
// now make sure that Python has "directory" in the search path
StreamString command_stream;
command_stream.Printf("if not (sys.path.__contains__('%s')):\n "
"sys.path.insert(1,'%s');\n\n",
directory.c_str(), directory.c_str());
bool syspath_retval =
ExecuteMultipleLines(command_stream.GetData(),
ScriptInterpreter::ExecuteScriptOptions()
.SetEnableIO(false)
.SetSetLLDBGlobals(false))
.Success();
if (!syspath_retval) {
error.SetErrorString("Python sys.path handling failed");
return false;
}

if (module_sp) {
// everything went just great, now set the module object
command_stream.Clear();
command_stream.Printf("%s", basename.c_str());
void *module_pyobj = nullptr;
if (ExecuteOneLineWithReturn(
command_stream.GetData(),
ScriptInterpreter::eScriptReturnTypeOpaqueObject,
&module_pyobj) &&
module_pyobj)
*module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
}
} else {
error.SetErrorString("no known way to import this module specification");
return false;
}

return true;
// Strip .py or .pyc extension
llvm::StringRef extension = target_file.GetFileNameExtension().GetCString();
if (!extension.empty()) {
if (extension == ".py")
basename.resize(basename.length() - 3);
else if (extension == ".pyc")
basename.resize(basename.length() - 4);
}

// check if the module is already import-ed
command_stream.Clear();
command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
bool does_contain = false;
// this call will succeed if the module was ever imported in any Debugger
// in the lifetime of the process in which this LLDB framework is living
bool was_imported_globally =
(ExecuteOneLineWithReturn(
command_stream.GetData(),
ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
ScriptInterpreter::ExecuteScriptOptions()
.SetEnableIO(false)
.SetSetLLDBGlobals(false)) &&
does_contain);
// this call will fail if the module was not imported in this Debugger
// before
command_stream.Clear();
command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
bool was_imported_locally = GetSessionDictionary()
.GetItemForKey(PythonString(basename))
.IsAllocated();

bool was_imported = (was_imported_globally || was_imported_locally);

// now actually do the import
command_stream.Clear();

if (was_imported) {
if (!was_imported_locally)
command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
basename.c_str());
else
command_stream.Printf("reload_module(%s)", basename.c_str());
} else
command_stream.Printf("import %s", basename.c_str());

error = ExecuteMultipleLines(command_stream.GetData(),
ScriptInterpreter::ExecuteScriptOptions()
.SetEnableIO(false)
.SetSetLLDBGlobals(false));
if (error.Fail())
return false;

// if we are here, everything worked
// call __lldb_init_module(debugger,dict)
if (!LLDBSwigPythonCallModuleInit(basename.c_str(), m_dictionary_name.c_str(),
debugger_sp)) {
error.SetErrorString("calling __lldb_init_module failed");
return false;
}

if (module_sp) {
// everything went just great, now set the module object
command_stream.Clear();
command_stream.Printf("%s", basename.c_str());
void *module_pyobj = nullptr;
if (ExecuteOneLineWithReturn(
command_stream.GetData(),
ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj) &&
module_pyobj)
*module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
}

return true;
}

bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) {
Expand Down

0 comments on commit 3b33b41

Please sign in to comment.