Skip to content

Commit 396139a

Browse files
Push down the swig module to avoid an import cycle (#129135)
Fix #92603 This replaces #113066. I finally came back to this issue and it seems that this approach is still very promising. As requested, I have added a short explanation as to why CPython module should be moved into a submodule. cc @JDevlieghere who reviewed on the previous PR earlier.
1 parent 95e460a commit 396139a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lldb/bindings/python/CMakeLists.txt

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

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

79+
create_python_package(${swig_target} ${lldb_python_target_dir} "native" FILES)
80+
7781
# Distribute the examples as python packages.
7882
create_python_package(
7983
${swig_target}
@@ -141,7 +145,7 @@ function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_tar
141145
endif()
142146
set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb${LLDB_PYTHON_EXT_SUFFIX}")
143147
create_relative_symlink(${swig_target} ${LIBLLDB_SYMLINK_DEST}
144-
${lldb_python_target_dir} ${LIBLLDB_SYMLINK_OUTPUT_FILE})
148+
${lldb_python_target_dir}/native/ ${LIBLLDB_SYMLINK_OUTPUT_FILE})
145149

146150

147151
if (NOT WIN32)

lldb/bindings/python/python.swig

+6-1
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.

0 commit comments

Comments
 (0)