Skip to content

Commit d6e8057

Browse files
committed
[lldb] Improve error message for modules with dots or dashes
LLDB does not like to import Python files with dashes or dots in their name. While the former are technically allowed, it is discouraged. Dots are allowed for subpackages but not in module names. This patch improves the user experience by printing a useful error. Before this patch: error: module importing failed: SyntaxError('invalid syntax', ('<string>', 1, 11, 'import foo-bar\n')) After this patch: error: module importing failed: Python discourages dashes in module names: foo-bar rdar://74263511 [1] https://www.python.org/dev/peps/pep-0008/#package-and-module-names Differential revision: https://reviews.llvm.org/D96833
1 parent 8a783e6 commit d6e8057

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,6 +2781,7 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
27812781
};
27822782

27832783
std::string module_name(pathname);
2784+
bool possible_package = false;
27842785

27852786
if (extra_search_dir) {
27862787
if (llvm::Error e = ExtendSysPath(extra_search_dir.GetPath())) {
@@ -2805,6 +2806,7 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
28052806
return false;
28062807
}
28072808
// Not a filename, probably a package of some sort, let it go through.
2809+
possible_package = true;
28082810
} else if (is_directory(st) || is_regular_file(st)) {
28092811
if (module_file.GetDirectory().IsEmpty()) {
28102812
error.SetErrorString("invalid directory name");
@@ -2831,6 +2833,18 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
28312833
module_name.resize(module_name.length() - 4);
28322834
}
28332835

2836+
if (!possible_package && module_name.find('.') != llvm::StringRef::npos) {
2837+
error.SetErrorStringWithFormat(
2838+
"Python does not allow dots in module names: %s", module_name.c_str());
2839+
return false;
2840+
}
2841+
2842+
if (module_name.find('-') != llvm::StringRef::npos) {
2843+
error.SetErrorStringWithFormat(
2844+
"Python discourages dashes in module names: %s", module_name.c_str());
2845+
return false;
2846+
}
2847+
28342848
// check if the module is already import-ed
28352849
StreamString command_stream;
28362850
command_stream.Clear();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# REQUIRES: python
2+
3+
# RUN: rm -rf %t && mkdir -p %t
4+
# RUN: echo "print('Rene Magritte')" >> %t/foo.py
5+
# RUN: echo "print('Jan van Eyck')" >> %t/foo-bar.py
6+
# RUN: echo "print('Pieter Bruegel the Elder')" >> %t/foo.bar.py
7+
# RUN: echo "print('Pieter Bruegel the Younger')" >> %t/foo.bar
8+
9+
# RUN: %lldb --script-language python -o 'command script import %t/foo.py' 2>&1 | FileCheck %s --check-prefix MAGRITTE
10+
# MAGRITTE: Rene Magritte
11+
12+
# RUN: %lldb --script-language python -o 'command script import %t/foo-bar.py' 2>&1 | FileCheck %s --check-prefix VANEYCK
13+
# VANEYCK-NOT: Jan van Eyck
14+
# VANEYCK: module importing failed: Python discourages dashes in module names: foo-bar
15+
16+
# RUN: %lldb --script-language python -o 'command script import %t/foo.bar.py' 2>&1 | FileCheck %s --check-prefix BRUEGEL
17+
# RUN: %lldb --script-language python -o 'command script import %t/foo.bar' 2>&1 | FileCheck %s --check-prefix BRUEGEL
18+
# BRUEGEL-NOT: Pieter Bruegel the Elder
19+
# BRUEGEL-NOT: Pieter Bruegel the Younger
20+
# BRUEGEL: module importing failed: Python does not allow dots in module names: foo.bar

0 commit comments

Comments
 (0)