Skip to content

Commit 5442507

Browse files
committed
Push down the SWIG module to avoid an import cycle
This is a reland of llvm#129135 (by dingxiangfei2009) with Vladislav (dzhidzhoev) fix on top. Fixes llvm#92603
1 parent 6152999 commit 5442507

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

lldb/bindings/python/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ endfunction()
6060
function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_target_dir)
6161
# Add a Post-Build Event to copy over Python files and create the symlink to
6262
# liblldb.so for the Python API(hardlink on Windows).
63+
# Note that Swig-generated code is located one level deeper in the `native`
64+
# module, in order to avoid cyclic importing.
6365
add_custom_target(${swig_target} ALL VERBATIM
64-
COMMAND ${CMAKE_COMMAND} -E make_directory ${lldb_python_target_dir}
66+
COMMAND ${CMAKE_COMMAND} -E make_directory ${lldb_python_target_dir}/native/
6567
DEPENDS ${lldb_python_bindings_dir}/lldb.py
6668
COMMENT "Python script sym-linking LLDB Python API")
6769

@@ -75,6 +77,8 @@ function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_tar
7577
"${LLDB_SOURCE_DIR}/source/Interpreter/embedded_interpreter.py"
7678
"${lldb_python_target_dir}")
7779

80+
create_python_package(${swig_target} ${lldb_python_target_dir} "native" FILES)
81+
7882
# Distribute the examples as python packages.
7983
create_python_package(
8084
${swig_target}
@@ -142,7 +146,7 @@ function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_tar
142146
endif()
143147
set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb${LLDB_PYTHON_EXT_SUFFIX}")
144148
create_relative_symlink(${swig_target} ${LIBLLDB_SYMLINK_DEST}
145-
${lldb_python_target_dir} ${LIBLLDB_SYMLINK_OUTPUT_FILE})
149+
${lldb_python_target_dir}/native/ ${LIBLLDB_SYMLINK_OUTPUT_FILE})
146150

147151

148152
if (NOT WIN32)

lldb/bindings/python/python.swig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ Older swig versions will simply ignore this setting.
5050
import $module
5151
except ImportError:
5252
# Relative import should work if we are being loaded by Python.
53-
from . import $module"
53+
# The cpython module built by swig is pushed one level down into
54+
# the native submodule, because at this point the interpreter
55+
# is still constructing the lldb module itself.
56+
# Simply importing anything using `from . import` constitutes
57+
# a cyclic importing.
58+
from .native import $module"
5459
%enddef
5560

5661
// The name of the module to be created.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ void ScriptInterpreterPython::SharedLibraryDirectoryHelper(
272272
// does.
273273
if (this_file.GetFileNameExtension() == ".pyd") {
274274
this_file.RemoveLastPathComponent(); // _lldb.pyd or _lldb_d.pyd
275+
this_file.RemoveLastPathComponent(); // native
275276
this_file.RemoveLastPathComponent(); // lldb
276277
llvm::StringRef libdir = LLDB_PYTHON_RELATIVE_LIBDIR;
277278
for (auto it = llvm::sys::path::begin(libdir),

0 commit comments

Comments
 (0)