This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch][python] Auto-load gdb.command and gdb.function modules

This patch enables module auto-importing for the gdb.command and
gdb.function name-spaces.  Any Python file (which is not
located in {data-directory}/gdb/python/command and
{data-directory}/gdb/python/function is now automatically imported, and
available immediately upon GDB start.

Tested on X86-64 with no regressions.




2011-08-04  Phil Muldoon  <>

	* python/lib/gdb/command/  Self-register
	* python/python.c (finish_python_initialization): Use os.path.join.
	* python/lib/gdb/ Add automatic module loading code.
	Remove explicit import and registration

2011-08-04  Phil Muldoon  <>

	* gdb.texinfo (Python): Document command and function module
          auto-loading on start-up.


diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 35fa075..9a0c8db 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -20845,6 +20845,12 @@ This directory, known as the @dfn{python directory},
 is automatically added to the Python Search Path in order to allow
 the Python interpreter to locate all scripts installed at this location.
+Additionally, @value{GDBN} commands and convenience functions which
+are written in Python and are located in the 
+@file{@var{data-directory}/python/gdb/command} or 
+@file{@var{data-directory}/python/gdb/function} directories are
+automatically imported when @value{GDBN} starts.
 * Python Commands::             Accessing Python from @value{GDBN}.
 * Python API::                  Accessing @value{GDBN} from Python.
diff --git a/gdb/python/lib/gdb/ b/gdb/python/lib/gdb/
index 43975c2..38e58fb 100644
--- a/gdb/python/lib/gdb/
+++ b/gdb/python/lib/gdb/
@@ -13,6 +13,27 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <>.
-import gdb.command.pretty_printers
+# Auto load all functions/commands.
+# Modules to auto-load, and the paths where those modules exist.
+module_dict = {\
+'gdb.function': os.path.join(gdb.PYTHONDIR, 'gdb', 'function'),\
+'gdb.command': os.path.join(gdb.PYTHONDIR, 'gdb', 'command')\
+# Iterate the dictionary, collating the Python files in each module
+# path.  Construct the module name, and import.
+for module, location in module_dict.iteritems():
+  if os.path.exists(location):
+     py_files = filter(lambda x: x.endswith('.py') and x != '',\
+     os.listdir(location))
+     for py_file in py_files:
+       py_file = py_file.rstrip('.py')
+       py_file = module + '.' + py_file
+       try:
+         exec('import ' + py_file)
+       except:
+         print 'Error importing: ', py_file
diff --git a/gdb/python/lib/gdb/command/ b/gdb/python/lib/gdb/command/
index 86923d7..00aa390 100644
--- a/gdb/python/lib/gdb/command/
+++ b/gdb/python/lib/gdb/command/
@@ -368,3 +368,5 @@ def register_pretty_printer_commands():
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 16c0a6e..03edce9 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1302,13 +1302,13 @@ def GdbSetPythonDirectory (dir):\n\
   sys.path.insert (0, gdb.PYTHONDIR)\n\
   # Tell python where to find submodules of gdb.\n\
-  gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
+  gdb.__path__ = [os.path.join (gdb.PYTHONDIR, 'gdb')]\n\
   # The gdb module is implemented in C rather than in Python.  As a result,\n\
   # the associated __init.py__ script is not not executed by default when\n\
   # the gdb module gets imported.  Execute that script manually if it\n\
   # exists.\n\
-  ipy = gdb.PYTHONDIR + '/gdb/'\n\
+  ipy = os.path.join (gdb.PYTHONDIR, 'gdb', '')\n\
   if os.path.exists (ipy):\n\
     execfile (ipy)\n\

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]