Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [patch][python] Auto-load gdb.command and gdb.function modules
@ 2011-08-04 16:53 Phil Muldoon
  2011-08-04 17:12 ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Phil Muldoon @ 2011-08-04 16:53 UTC (permalink / raw)
  To: gdb-patches


This patch enables module auto-importing for the gdb.command and
gdb.function name-spaces.  Any Python file (which is not __init__.py)
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.

OK?

Cheers,

Phil


2011-08-04  Phil Muldoon  <pmuldoon@redhat.com>

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

2011-08-04  Phil Muldoon  <pmuldoon@redhat.com>

	* 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.
+
 @menu
 * Python Commands::             Accessing Python from @value{GDBN}.
 * Python API::                  Accessing @value{GDBN} from Python.
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 43975c2..38e58fb 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -13,6 +13,27 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import gdb.command.pretty_printers
+# Auto load all functions/commands.
 
-gdb.command.pretty_printers.register_pretty_printer_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 != '__init__.py',\
+     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/pretty_printers.py b/gdb/python/lib/gdb/command/pretty_printers.py
index 86923d7..00aa390 100644
--- a/gdb/python/lib/gdb/command/pretty_printers.py
+++ b/gdb/python/lib/gdb/command/pretty_printers.py
@@ -368,3 +368,5 @@ def register_pretty_printer_commands():
     InfoPrettyPrinter()
     EnablePrettyPrinter()
     DisablePrettyPrinter()
+
+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\
 \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\
 \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/__init__.py'\n\
+  ipy = os.path.join (gdb.PYTHONDIR, 'gdb', '__init__.py')\n\
   if os.path.exists (ipy):\n\
     execfile (ipy)\n\
 \n\


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch][python] Auto-load gdb.command and gdb.function modules
  2011-08-04 16:53 [patch][python] Auto-load gdb.command and gdb.function modules Phil Muldoon
@ 2011-08-04 17:12 ` Tom Tromey
  2011-08-04 18:31   ` Phil Muldoon
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2011-08-04 17:12 UTC (permalink / raw)
  To: pmuldoon; +Cc: gdb-patches

>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:

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

Phil> Tested on X86-64 with no regressions.

Phil> OK?

Phil> +module_dict = {\
Phil> +'gdb.function': os.path.join(gdb.PYTHONDIR, 'gdb', 'function'),\
Phil> +'gdb.command': os.path.join(gdb.PYTHONDIR, 'gdb', 'command')\
Phil> +}

You don't need the trailing "\"s, please remove them.
I think the lines in the dict should be indented, that looks better.

Phil> +     py_files = filter(lambda x: x.endswith('.py') and x != '__init__.py',\
Phil> +     os.listdir(location))

Indentation of the second line looks wrong.
I don't think you need that "\".

Phil> +     for py_file in py_files:
Phil> +       py_file = py_file.rstrip('.py')
Phil> +       py_file = module + '.' + py_file
Phil> +       try:
Phil> +         exec('import ' + py_file)
Phil> +       except:
Phil> +         print 'Error importing: ', py_file

It seems like this should print the real error from the exception.

Tom


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch][python] Auto-load gdb.command and gdb.function modules
  2011-08-04 17:12 ` Tom Tromey
@ 2011-08-04 18:31   ` Phil Muldoon
  2011-08-04 18:59     ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Phil Muldoon @ 2011-08-04 18:31 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Tom Tromey <tromey@redhat.com> writes:


> Phil> +module_dict = {\
> Phil> +'gdb.function': os.path.join(gdb.PYTHONDIR, 'gdb', 'function'),\
> Phil> +'gdb.command': os.path.join(gdb.PYTHONDIR, 'gdb', 'command')\
> Phil> +}
>
> You don't need the trailing "\"s, please remove them.
> I think the lines in the dict should be indented, that looks better.

OK, and to other format issues.

> Phil> +         print 'Error importing: ', py_file

OK.

Revised patch attached.  ChangeLogs remain the same.

--

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.
+
 @menu
 * Python Commands::             Accessing Python from @value{GDBN}.
 * Python API::                  Accessing @value{GDBN} from Python.
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 43975c2..b7b4145 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -13,6 +13,29 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import gdb.command.pretty_printers
+import traceback
 
-gdb.command.pretty_printers.register_pretty_printer_commands()
+# 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 != '__init__.py',
+                       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 >> sys.stderr, traceback.format_exc()
diff --git a/gdb/python/lib/gdb/command/pretty_printers.py b/gdb/python/lib/gdb/command/pretty_printers.py
index 86923d7..00aa390 100644
--- a/gdb/python/lib/gdb/command/pretty_printers.py
+++ b/gdb/python/lib/gdb/command/pretty_printers.py
@@ -368,3 +368,5 @@ def register_pretty_printer_commands():
     InfoPrettyPrinter()
     EnablePrettyPrinter()
     DisablePrettyPrinter()
+
+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\
 \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\
 \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/__init__.py'\n\
+  ipy = os.path.join (gdb.PYTHONDIR, 'gdb', '__init__.py')\n\
   if os.path.exists (ipy):\n\
     execfile (ipy)\n\
 \n\


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch][python] Auto-load gdb.command and gdb.function modules
  2011-08-04 18:31   ` Phil Muldoon
@ 2011-08-04 18:59     ` Tom Tromey
  2011-08-08 11:58       ` Phil Muldoon
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2011-08-04 18:59 UTC (permalink / raw)
  To: pmuldoon; +Cc: gdb-patches

>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:

Phil> +       py_file = py_file.rstrip('.py')

The argument to rstrip is a set of characters; I think this will do the
wrong thing for, e.g., "p.py".
You can just use py_file[:-3] instead.
The code bits are ok with that change.

Tom


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch][python] Auto-load gdb.command and gdb.function modules
  2011-08-04 18:59     ` Tom Tromey
@ 2011-08-08 11:58       ` Phil Muldoon
  2011-08-08 14:10         ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Phil Muldoon @ 2011-08-08 11:58 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, eliz

Tom Tromey <tromey@redhat.com> writes:

>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>
> Phil> +       py_file = py_file.rstrip('.py')
>
> The argument to rstrip is a set of characters; I think this will do the
> wrong thing for, e.g., "p.py".
> You can just use py_file[:-3] instead.
> The code bits are ok with that change.

Done.  I also forgot to include a NEWS entry, so I included a patch for
that too.  This needs a doc review.

Cheers

Phil

--

diff --git a/gdb/NEWS b/gdb/NEWS
index b0da564..da55850 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -23,6 +23,11 @@
   ** A prompt subsitution hook (prompt_hook) is now available to the
      Python API.
 
+  ** Python commands and convenience-functions located in
+    'data-directory'/python/gdb/command and
+    'data-directory'/python/gdb/function are now automatically loaded
+     on GDB start-up.
+
 * libthread-db-search-path now supports two special values: $sdir and $pdir.
   $sdir specifies the default system locations of shared libraries.
   $pdir specifies the directory where the libpthread used by the application
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.
+
 @menu
 * Python Commands::             Accessing Python from @value{GDBN}.
 * Python API::                  Accessing @value{GDBN} from Python.
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 43975c2..1902132 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -13,6 +13,29 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import gdb.command.pretty_printers
+import traceback
 
-gdb.command.pretty_printers.register_pretty_printer_commands()
+# 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 != '__init__.py',
+                       os.listdir(location))
+
+     for py_file in py_files:
+       # Construct from foo.py, gdb.module.foo
+       py_file = module + '.' + py_file[:-3]
+       try:
+         exec('import ' + py_file)
+       except:
+         print >> sys.stderr, traceback.format_exc()
diff --git a/gdb/python/lib/gdb/command/pretty_printers.py b/gdb/python/lib/gdb/command/pretty_printers.py
index 86923d7..00aa390 100644
--- a/gdb/python/lib/gdb/command/pretty_printers.py
+++ b/gdb/python/lib/gdb/command/pretty_printers.py
@@ -368,3 +368,5 @@ def register_pretty_printer_commands():
     InfoPrettyPrinter()
     EnablePrettyPrinter()
     DisablePrettyPrinter()
+
+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\
 \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\
 \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/__init__.py'\n\
+  ipy = os.path.join (gdb.PYTHONDIR, 'gdb', '__init__.py')\n\
   if os.path.exists (ipy):\n\
     execfile (ipy)\n\
 \n\


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch][python] Auto-load gdb.command and gdb.function modules
  2011-08-08 11:58       ` Phil Muldoon
@ 2011-08-08 14:10         ` Eli Zaretskii
  2011-08-09 12:46           ` Phil Muldoon
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2011-08-08 14:10 UTC (permalink / raw)
  To: pmuldoon; +Cc: tromey, gdb-patches

> From: Phil Muldoon <pmuldoon@redhat.com>
> Cc: gdb-patches@sourceware.org, eliz@gnu.org
> Reply-to: pmuldoon@redhat.com
> Date: Mon, 08 Aug 2011 12:57:45 +0100
> 
> Done.  I also forgot to include a NEWS entry, so I included a patch for
> that too.  This needs a doc review.

Fine with me, thanks.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch][python] Auto-load gdb.command and gdb.function modules
  2011-08-08 14:10         ` Eli Zaretskii
@ 2011-08-09 12:46           ` Phil Muldoon
  0 siblings, 0 replies; 7+ messages in thread
From: Phil Muldoon @ 2011-08-09 12:46 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Phil Muldoon <pmuldoon@redhat.com>
>> Cc: gdb-patches@sourceware.org, eliz@gnu.org
>> Reply-to: pmuldoon@redhat.com
>> Date: Mon, 08 Aug 2011 12:57:45 +0100
>> 
>> Done.  I also forgot to include a NEWS entry, so I included a patch for
>> that too.  This needs a doc review.
>
> Fine with me, thanks.

So committed, thanks.

Cheers

Phil


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-08-09 12:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-04 16:53 [patch][python] Auto-load gdb.command and gdb.function modules Phil Muldoon
2011-08-04 17:12 ` Tom Tromey
2011-08-04 18:31   ` Phil Muldoon
2011-08-04 18:59     ` Tom Tromey
2011-08-08 11:58       ` Phil Muldoon
2011-08-08 14:10         ` Eli Zaretskii
2011-08-09 12:46           ` Phil Muldoon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox