This is the mail archive of the gdb-patches@sourceware.org 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]

Re: [PATCH 2/2] Add gdb_libdir/python-<version> in front of Python search path.


On Thu, May 20, 2010 at 11:40 AM, Joel Brobecker <brobecker@adacore.com> wrote:
> This patch puts gdb_libdir/python-<version> at the beginning of
> the python search path. ?This allows GDB to use its own copy/version
> of the Python library.
>
> 2010-05-20 ?Joel Brobecker ?<brobecker@adacore.com>
>
> ? ? ? ?* python.c (_initialize_python): Add variable libdir in gdb module.
> ? ? ? ?Insert gdb_libdir/python-<version> at head of python search path.
>
> Tested on x86_64-linux. No regression.
>
> ---
> ?gdb/python/python.c | ? ?6 ++++++
> ?1 files changed, 6 insertions(+), 0 deletions(-)
>
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 3110328..fa76838 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -654,6 +654,7 @@ 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);
> + ?PyModule_AddStringConstant (gdb_module, "libdir", (char*) gdb_libdir);
>
> ? gdbpy_initialize_auto_load ();
> ? gdbpy_initialize_values ();
> @@ -702,6 +703,11 @@ class GdbOutputFile:\n\
> ?\n\
> ?sys.stderr = GdbOutputFile()\n\
> ?sys.stdout = GdbOutputFile()\n\
> +\n\
> +# Put gdb_libdir/python-<version> in the python search path. ?This allows\n\
> +# us to provide GDB bundled with its own python runtime (the .py/pyc files).\n\
> +sys.path.insert(0, gdb.libdir + \"/python-%d.%d\" % (sys.version_info[0],\n\
> + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sys.version_info[1]))\n\
> ?");
>
> ? /* Release the GIL while gdb runs. ?*/
> --
> 1.7.1
>
>

Hi.
I think(!) this is a better approach.  Could be wrong of course. :-)
I think adding one directory to sys.path is insufficient, and from
what I've read it seems like Py_SetProgramName is the preferred
solution to this problem.

This patch doesn't currently handle your case, where the relationship
between the location of python and gdb at build time is different than
at runtime.  I think the solution to that is another config parameter
to specify where to find python at run time.  Currently, when given a
path, --with-python specifies where to find python at build time, and,
for now, also at runtime.
I can create another patch to add this new option
(--with-python-libdir or some such) if you think it's worth pursuing.
Regardless of whether --with-python-libdir is useful to add to the FSF
tree, I think the attached patch is worthy.

Note that Python's initialization assumes a particular layout that
your current plan violates (I think!).
[ref Modules/getpath.c in the python sources]
/foo/bin/python
/foo/lib/pythonX.Y/...

The attached patch will work with your --with-gdb-libdir, but python
would need to be installed as ${gdb_libdir}/lib/pythonX.Y [or
${gdb_libdir}/foo/lib/pythonX.Y].  The important part is to not
separate "lib" from "pythonX.Y".  [ref: variable lib_python in
Modules/getpath.c]

Tested with:
python configure --prefix=/mumble/lib/gdb
gdb configure --prefix=/mumble --with-python=/mumble/lib/gdb
and then renaming /mumble to /mumble2
and then trying "python import itertools" after starting gdb.

2010-05-21  Doug Evans  <dje@google.com>

        Allow python to find its files if moved from original location.
        * acinclude.m4 (GDB_AC_DEFINE_RELOCATABLE): New function.
        (GDB_AC_WITH_DIR): Call it.
        * configure.ac: If a path is provided to --with-python,
        define WITH_PYTHON_PATH.
        * config.in: Regenerate.
        * configure: Regenerate.
        * defs.h (python_libdir): Declare.
        * main.c (python_libdir): Define.
        (captured_main): Initialize python_libdir.
        * python/python.c (_initialize_python): If configured with
        --with-python=/path, call Py_SetProgramName to make sure python
        can find its libraries and modules.

Attachment: gdb-100521-with-python-path-1.patch.txt
Description: Text document


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