public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add support for  gdb.PYTHONDIR as $gdb_datadir/python.
@ 2010-06-24 18:19 Joel Brobecker
  2010-06-24 18:39 ` Doug Evans
  2010-06-24 19:14 ` Tom Tromey
  0 siblings, 2 replies; 6+ messages in thread
From: Joel Brobecker @ 2010-06-24 18:19 UTC (permalink / raw)
  To: gdb-patches; +Cc: Joel Brobecker

Here is a new stab at implementing a "pythondir":

Python scripts to be used by GDB can be stored in that directory,
and will be automatically found by the interpreter when importing them.
This patch also sets up <gdb_pythondir>/gdb as the directory where gdb
submodules can be stored.  For now, there is nothing there, but it can
now be added easily without further code changes.

The difference with the previous patch is that this pythondir is no
longer changeable during configure - it's now hardcoded.

Another tiny differnce is that I named the associated "variable"
in the gdb module as "PYTHONDIR", rather than "pythondir".  This is
because Python does not have constants and the usual convention to
name variables that are supposed to be constant is to use all-upper-case.


gdb/ChangeLog:
2010-06-24  Joel Brobecker  <brobecker@adacore.com>

        * python/python.c (_initialize_python): Add new "constant"
        PYTHONDIR in gdb module.  Insert this path at the head of
        sys.path. Set gdb.__path__ to gdb.PYTHONDIR + '/gdb' and
        exec its __init__.py script if it exists in that directory.

Tested on x86_64-linux, no regression.  I also verified by hand that
sys.path has the expected path at the start of the list, and that an
import of a script in that directory works as expected.

I guess documentation and NEWS will have to be written if this patch
goes in...

---
 gdb/python/python.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 31880c1..0018816 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -668,6 +668,13 @@ Enables or disables printing of Python stack traces."),
   PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
   PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
   PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);
+  {
+    char *gdb_pythondir;
+
+    gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL);
+    PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir);
+    xfree (gdb_pythondir);
+  }
 
   gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
   PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
@@ -720,6 +727,13 @@ class GdbOutputFile:\n\
 \n\
 sys.stderr = GdbOutputFile()\n\
 sys.stdout = GdbOutputFile()\n\
+\n\
+sys.path.insert(0, gdb.PYTHONDIR)\n\
+gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
+from os.path import exists\n\
+ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\
+if exists (ipy):\n\
+  execfile (ipy)\n\
 ");
 
   /* Release the GIL while gdb runs.  */
-- 
1.7.1

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

* Re: [PATCH] Add support for gdb.PYTHONDIR as $gdb_datadir/python.
  2010-06-24 18:19 [PATCH] Add support for gdb.PYTHONDIR as $gdb_datadir/python Joel Brobecker
@ 2010-06-24 18:39 ` Doug Evans
  2010-06-24 19:06   ` Tom Tromey
  2010-06-24 19:14 ` Tom Tromey
  1 sibling, 1 reply; 6+ messages in thread
From: Doug Evans @ 2010-06-24 18:39 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

On Thu, Jun 24, 2010 at 11:18 AM, Joel Brobecker <brobecker@adacore.com> wrote:
> Here is a new stab at implementing a "pythondir":
>
> Python scripts to be used by GDB can be stored in that directory,
> and will be automatically found by the interpreter when importing them.
> This patch also sets up <gdb_pythondir>/gdb as the directory where gdb
> submodules can be stored.  For now, there is nothing there, but it can
> now be added easily without further code changes.
>
> The difference with the previous patch is that this pythondir is no
> longer changeable during configure - it's now hardcoded.
>
> Another tiny differnce is that I named the associated "variable"
> in the gdb module as "PYTHONDIR", rather than "pythondir".  This is
> because Python does not have constants and the usual convention to
> name variables that are supposed to be constant is to use all-upper-case.
>
>
> gdb/ChangeLog:
> 2010-06-24  Joel Brobecker  <brobecker@adacore.com>
>
>        * python/python.c (_initialize_python): Add new "constant"
>        PYTHONDIR in gdb module.  Insert this path at the head of
>        sys.path. Set gdb.__path__ to gdb.PYTHONDIR + '/gdb' and
>        exec its __init__.py script if it exists in that directory.
>
> Tested on x86_64-linux, no regression.  I also verified by hand that
> sys.path has the expected path at the start of the list, and that an
> import of a script in that directory works as expected.
>
> I guess documentation and NEWS will have to be written if this patch
> goes in...
>
> ---
>  gdb/python/python.c |   14 ++++++++++++++
>  1 files changed, 14 insertions(+), 0 deletions(-)
>
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 31880c1..0018816 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -668,6 +668,13 @@ Enables or disables printing of Python stack traces."),
>   PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
>   PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
>   PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);
> +  {
> +    char *gdb_pythondir;
> +
> +    gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL);
> +    PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir);
> +    xfree (gdb_pythondir);
> +  }
>
>   gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
>   PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
> @@ -720,6 +727,13 @@ class GdbOutputFile:\n\
>  \n\
>  sys.stderr = GdbOutputFile()\n\
>  sys.stdout = GdbOutputFile()\n\
> +\n\
> +sys.path.insert(0, gdb.PYTHONDIR)\n\
> +gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
> +from os.path import exists\n\
> +ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\
> +if exists (ipy):\n\
> +  execfile (ipy)\n\
>  ");
>
>   /* Release the GIL while gdb runs.  */
> --
> 1.7.1
>
>

Hi.

Looks great to me with one nit.

The execfile of __init__.py feels odd.  I haven't seen it done
elsewhere and I wonder if it's needed.
[I know python has __init__.py, I just don't know if we have to
*explicitly* exec it.]

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

* Re: [PATCH] Add support for gdb.PYTHONDIR as $gdb_datadir/python.
  2010-06-24 19:06   ` Tom Tromey
@ 2010-06-24 19:06     ` Doug Evans
  0 siblings, 0 replies; 6+ messages in thread
From: Doug Evans @ 2010-06-24 19:06 UTC (permalink / raw)
  To: tromey; +Cc: Joel Brobecker, gdb-patches

On Thu, Jun 24, 2010 at 12:05 PM, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Doug" == Doug Evans <dje@google.com> writes:
>
> Doug> The execfile of __init__.py feels odd.  I haven't seen it done
> Doug> elsewhere and I wonder if it's needed.
> Doug> [I know python has __init__.py, I just don't know if we have to
> Doug> *explicitly* exec it.]
>
> Normally, no, but in this case we do because the "gdb" module is created
> in C code, not via an import.

If a comment to that effect is added to the code, then I'm happy with
the patch. :-)

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

* Re: [PATCH] Add support for gdb.PYTHONDIR as $gdb_datadir/python.
  2010-06-24 18:39 ` Doug Evans
@ 2010-06-24 19:06   ` Tom Tromey
  2010-06-24 19:06     ` Doug Evans
  0 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2010-06-24 19:06 UTC (permalink / raw)
  To: Doug Evans; +Cc: Joel Brobecker, gdb-patches

>>>>> "Doug" == Doug Evans <dje@google.com> writes:

Doug> The execfile of __init__.py feels odd.  I haven't seen it done
Doug> elsewhere and I wonder if it's needed.
Doug> [I know python has __init__.py, I just don't know if we have to
Doug> *explicitly* exec it.]

Normally, no, but in this case we do because the "gdb" module is created
in C code, not via an import.

Tom

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

* Re: [PATCH] Add support for  gdb.PYTHONDIR as $gdb_datadir/python.
  2010-06-24 18:19 [PATCH] Add support for gdb.PYTHONDIR as $gdb_datadir/python Joel Brobecker
  2010-06-24 18:39 ` Doug Evans
@ 2010-06-24 19:14 ` Tom Tromey
  2010-06-24 22:53   ` Joel Brobecker
  1 sibling, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2010-06-24 19:14 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel> 2010-06-24  Joel Brobecker  <brobecker@adacore.com>
Joel>         * python/python.c (_initialize_python): Add new "constant"
Joel>         PYTHONDIR in gdb module.  Insert this path at the head of
Joel>         sys.path. Set gdb.__path__ to gdb.PYTHONDIR + '/gdb' and
Joel>         exec its __init__.py script if it exists in that directory.

This is fine with me.

Tom

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

* Re: [PATCH] Add support for  gdb.PYTHONDIR as $gdb_datadir/python.
  2010-06-24 19:14 ` Tom Tromey
@ 2010-06-24 22:53   ` Joel Brobecker
  0 siblings, 0 replies; 6+ messages in thread
From: Joel Brobecker @ 2010-06-24 22:53 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 494 bytes --]

> Joel> 2010-06-24  Joel Brobecker  <brobecker@adacore.com>
> Joel>         * python/python.c (_initialize_python): Add new "constant"
> Joel>         PYTHONDIR in gdb module.  Insert this path at the head of
> Joel>         sys.path. Set gdb.__path__ to gdb.PYTHONDIR + '/gdb' and
> Joel>         exec its __init__.py script if it exists in that directory.
> 
> This is fine with me.

Thanks! Following Doug's comments, here is the patch that I just
checked in.

NEWS and docs next.

-- 
Joel

[-- Attachment #2: pythondir-commented.diff --]
[-- Type: text/x-diff, Size: 2502 bytes --]

commit 2de53ec24130ae79b8f5eb138185173f900ccb29
Author: Joel Brobecker <brobecker@adacore.com>
Date:   Thu Jun 24 10:59:55 2010 -0700

    Add support for  gdb.PYTHONDIR as $gdb_datadir/python.
    
    Python scripts to be used by GDB can be stored in that directory,
    and will be automatically found by the interpreter when importing them.
    This patch also sets up <gdb_pythondir>/gdb as the directory where gdb
    submodules can be stored.  For now, there is nothing there, but it can
    now be added easily without further code changes.
    
    gdb/ChangeLog:
    2010-06-24  Joel Brobecker  <brobecker@adacore.com>
    
            * python/python.c (_initialize_python): Add new "constant"
            PYTHONDIR in gdb module.  Insert this path at the head of
            sys.path. Set gdb.__path__ to gdb.PYTHONDIR + '/gdb' and
            exec its __init__.py script if it exists in that directory.

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 31880c1..18b2cdc 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -668,6 +668,13 @@ Enables or disables printing of Python stack traces."),
   PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
   PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
   PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);
+  {
+    char *gdb_pythondir;
+
+    gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL);
+    PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir);
+    xfree (gdb_pythondir);
+  }
 
   gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
   PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
@@ -720,6 +727,20 @@ class GdbOutputFile:\n\
 \n\
 sys.stderr = GdbOutputFile()\n\
 sys.stdout = GdbOutputFile()\n\
+\n\
+# GDB's python scripts are stored inside gdb.PYTHONDIR.  So insert\n\
+# that directory name at the start of sys.path to allow the Python\n\
+# interpreter to find them.\n\
+sys.path.insert(0, gdb.PYTHONDIR)\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 exists.\n\
+gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
+from os.path import exists\n\
+ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\
+if exists (ipy):\n\
+  execfile (ipy)\n\
 ");
 
   /* Release the GIL while gdb runs.  */

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

end of thread, other threads:[~2010-06-24 22:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-24 18:19 [PATCH] Add support for gdb.PYTHONDIR as $gdb_datadir/python Joel Brobecker
2010-06-24 18:39 ` Doug Evans
2010-06-24 19:06   ` Tom Tromey
2010-06-24 19:06     ` Doug Evans
2010-06-24 19:14 ` Tom Tromey
2010-06-24 22:53   ` Joel Brobecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).