[Lldb-commits] [lldb] r344474 - Fix double import of _lldb module.

Vadim Chugunov via lldb-commits lldb-commits at lists.llvm.org
Sun Oct 14 00:24:56 PDT 2018


Author: vadimcn
Date: Sun Oct 14 00:24:56 2018
New Revision: 344474

URL: http://llvm.org/viewvc/llvm-project?rev=344474&view=rev
Log:
Fix double import of _lldb module.

Fix llvm.org/pr39054:
- Register _lldb as a built-in module during initialization of script interpreter,
- Reverse the order of imports in __init__.py: first try to import by absolute name, which will find the built-in module in the context of lldb (and other hosts that embed liblldb), then try relative import, in case the module is being imported from Python interpreter.

This works for SWIG>=3.0.11; before that, SWIG did not support custom module import code.

Differential revision: https://reviews.llvm.org/D52404

Modified:
    lldb/trunk/scripts/lldb.swig
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Modified: lldb/trunk/scripts/lldb.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/lldb.swig?rev=344474&r1=344473&r2=344474&view=diff
==============================================================================
--- lldb/trunk/scripts/lldb.swig (original)
+++ lldb/trunk/scripts/lldb.swig Sun Oct 14 00:24:56 2018
@@ -41,12 +41,12 @@ Older swig versions will simply ignore t
 */
 %define MODULEIMPORT
 "try:
-    # Try a relative import first
-    from . import $module
+    # Try an absolute import first.  If we're being loaded from lldb,
+    # _lldb should be a built-in module.
+    import $module
 except ImportError:
-    # Maybe absolute import will work (if we're being loaded from lldb, it
-    # should).
-    import $module"
+    # Relative import should work if we are being loaded by Python.
+    from . import $module"
 %enddef
 // These versions will not generate working python modules, so error out early.
 #if SWIG_VERSION >= 0x030009 && SWIG_VERSION < 0x030011

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=344474&r1=344473&r2=344474&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Sun Oct 14 00:24:56 2018
@@ -132,6 +132,9 @@ public:
 
     InitializePythonHome();
 
+    // Register _lldb as a built-in module.
+    PyImport_AppendInittab("_lldb", g_swig_init_callback);
+
 // Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
 // calling `Py_Initialize` and `PyEval_InitThreads`.  < 3.2 requires that you
 // call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.




More information about the lldb-commits mailing list