Skip to content

[dotest] Simplify logic to find the Python path #1907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 30 additions & 62 deletions lldb/packages/Python/lldbsuite/test/dotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,68 +560,33 @@ def setupSysPath():
configuration.skip_categories.append("lldb-vscode")

lldbPythonDir = None # The directory that contains 'lldb/__init__.py'
if configuration.lldb_framework_path:
lldbtest_config.lldb_framework_path = configuration.lldb_framework_path
candidatePath = os.path.join(
configuration.lldb_framework_path, 'Resources', 'Python')
if os.path.isfile(os.path.join(candidatePath, 'lldb/__init__.py')):
lldbPythonDir = candidatePath
if not lldbPythonDir:
print(
'Resources/Python/lldb/__init__.py was not found in ' +
configuration.lldb_framework_path)
sys.exit(-1)
else:
# If our lldb supports the -P option, use it to find the python path:
init_in_python_dir = os.path.join('lldb', '__init__.py')

lldb_dash_p_result = subprocess.check_output(
[lldbtest_config.lldbExec, "-P"], stderr=subprocess.STDOUT, universal_newlines=True)

if lldb_dash_p_result and not lldb_dash_p_result.startswith(
("<", "lldb: invalid option:")) and not lldb_dash_p_result.startswith("Traceback"):
lines = lldb_dash_p_result.splitlines()

# Workaround for readline vs libedit issue on FreeBSD. If stdout
# is not a terminal Python executes
# rl_variable_bind ("enable-meta-key", "off");
# This produces a warning with FreeBSD's libedit because the
# enable-meta-key variable is unknown. Not an issue on Apple
# because cpython commit f0ab6f9f0603 added a #ifndef __APPLE__
# around the call. See http://bugs.python.org/issue19884 for more
# information. For now we just discard the warning output.
if len(lines) >= 1 and lines[0].startswith(
"bind: Invalid command"):
lines.pop(0)

# Taking the last line because lldb outputs
# 'Cannot read termcap database;\nusing dumb terminal settings.\n'
# before the path
if len(lines) >= 1 and os.path.isfile(
os.path.join(lines[-1], init_in_python_dir)):
lldbPythonDir = lines[-1]
if "freebsd" in sys.platform or "linux" in sys.platform:
os.environ['LLDB_LIB_DIR'] = os.path.join(
lldbPythonDir, '..', '..')

if not lldbPythonDir:
print(
"Unable to load lldb extension module. Possible reasons for this include:")
print(" 1) LLDB was built with LLDB_ENABLE_PYTHON=0")
print(
" 2) PYTHONPATH and PYTHONHOME are not set correctly. PYTHONHOME should refer to")
print(
" the version of Python that LLDB built and linked against, and PYTHONPATH")
print(
" should contain the Lib directory for the same python distro, as well as the")
print(" location of LLDB\'s site-packages folder.")
print(
" 3) A different version of Python than that which was built against is exported in")
print(" the system\'s PATH environment variable, causing conflicts.")
print(
" 4) The executable '%s' could not be found. Please check " %
lldbtest_config.lldbExec)
print(" that it exists and is executable.")

# If our lldb supports the -P option, use it to find the python path:
lldb_dash_p_result = subprocess.check_output([lldbtest_config.lldbExec, "-P"], universal_newlines=True)
if lldb_dash_p_result:
for line in lldb_dash_p_result.splitlines():
if os.path.isdir(line) and os.path.exists(os.path.join(line, 'lldb', '__init__.py')):
lldbPythonDir = line
break

if not lldbPythonDir:
print(
"Unable to load lldb extension module. Possible reasons for this include:")
print(" 1) LLDB was built with LLDB_ENABLE_PYTHON=0")
print(
" 2) PYTHONPATH and PYTHONHOME are not set correctly. PYTHONHOME should refer to")
print(
" the version of Python that LLDB built and linked against, and PYTHONPATH")
print(
" should contain the Lib directory for the same python distro, as well as the")
print(" location of LLDB\'s site-packages folder.")
print(
" 3) A different version of Python than that which was built against is exported in")
print(" the system\'s PATH environment variable, causing conflicts.")
print(
" 4) The executable '%s' could not be found. Please check " %
lldbtest_config.lldbExec)
print(" that it exists and is executable.")

if lldbPythonDir:
lldbPythonDir = os.path.normpath(lldbPythonDir)
Expand All @@ -635,6 +600,9 @@ def setupSysPath():

lldbPythonDir = os.path.abspath(lldbPythonDir)

if "freebsd" in sys.platform or "linux" in sys.platform:
os.environ['LLDB_LIB_DIR'] = os.path.join(lldbPythonDir, '..', '..')

# If tests need to find LLDB_FRAMEWORK, now they can do it
os.environ["LLDB_FRAMEWORK"] = os.path.dirname(
os.path.dirname(lldbPythonDir))
Expand Down