public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: dje@google.com (Doug Evans)
To: gdb-patches@sourceware.org
Subject: [patch] New function finish_python_initialization
Date: Mon, 01 Nov 2010 02:26:00 -0000	[thread overview]
Message-ID: <20101101022638.812792461AF@ruffy.mtv.corp.google.com> (raw)

Hi.

While implementing some commands in python I ran into some
order-of-initialization issues.

For example, any new "info" commands implemented in python
require the "info" command prefix.

This patch fixes things by moving the evaluation of the init python script
to after most of the gdb initialization.  That way the python
script can punt on further such issues.

I thought of renaming _initialize_python and explicitly calling it
later (and thus avoid having two python init routines) but that doesn't
work (at least not without more effort).
_initialize_python has to be called before initialize_progspace
(the python code has two progspace data keys).
And the contents of the new finish_python_initialization has to
be done after the call to init_cli_cmds (so that the "info" command
prefix is there).

I will check this in in two days if there are no objections.

2010-10-31  Doug Evans  <dje@google.com>

	* top.c: #include "python/python.h".
	(gdb_init): Add a comment regarding initialize_all_files.
	Call finish_python_initialization at the end.
	* python/python.h (finish_python_initialization): Declare.
	* python/python.c (finish_python_initialization): New function.
	(_initialize_python): Move python-implemented initialization there
	and call it.
	(GdbMethods): Use #ifdef HAVE_PYTHON for consistency.

Index: top.c
===================================================================
RCS file: /cvs/src/src/gdb/top.c,v
retrieving revision 1.184
diff -u -p -r1.184 top.c
--- top.c	11 Sep 2010 16:00:25 -0000	1.184
+++ top.c	1 Nov 2010 02:10:06 -0000
@@ -47,6 +47,7 @@
 #include "main.h"
 #include "event-loop.h"
 #include "gdbthread.h"
+#include "python/python.h"
 
 /* readline include files */
 #include "readline/readline.h"
@@ -1657,7 +1658,10 @@ gdb_init (char *argv0)
   init_cmd_lists ();		/* This needs to be done first */
   initialize_targets ();	/* Setup target_terminal macros for utils.c */
   initialize_utils ();		/* Make errors and warnings possible */
+
+  /* Here is where we call all the _initialize_foo routines.  */
   initialize_all_files ();
+
   /* This creates the current_program_space.  Do this after all the
      _initialize_foo routines have had a chance to install their
      per-sspace data keys.  Also do this before
@@ -1684,4 +1688,12 @@ gdb_init (char *argv0)
      deprecated_init_ui_hook.  */
   if (deprecated_init_ui_hook)
     deprecated_init_ui_hook (argv0);
+
+#ifdef HAVE_PYTHON
+  /* Python initialization can require various commands to be installed.
+     For example "info pretty-printer" needs the "info" prefix to be
+     installed.  Keep things simple and just do final python initialization
+     here.  */
+  finish_python_initialization ();
+#endif
 }
Index: python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.51
diff -u -p -r1.51 python.c
--- python/python.c	13 Oct 2010 13:24:39 -0000	1.51
+++ python/python.c	1 Nov 2010 02:10:07 -0000
@@ -997,9 +997,26 @@ Enables or disables printing of Python s
   gdbpy_doc_cst = PyString_FromString ("__doc__");
   gdbpy_enabled_cst = PyString_FromString ("enabled");
 
-  /* Remaining initialization is done in Python.
-     - create a couple objects which are used for Python's stdout and stderr
-     - provide function GdbSetPythonDirectory  */
+  /* Release the GIL while gdb runs.  */
+  PyThreadState_Swap (NULL);
+  PyEval_ReleaseLock ();
+
+#endif /* HAVE_PYTHON */
+}
+
+#ifdef HAVE_PYTHON
+
+/* Perform the remaining python initializations.
+   These must be done after GDB is at least mostly initialized.
+   E.g., The "info pretty-printer" command needs the "info" prefix
+   command installed.  */
+
+void
+finish_python_initialization (void)
+{
+  struct cleanup *cleanup;
+
+  cleanup = ensure_python_env (get_current_arch (), current_language);
 
   PyRun_SimpleString ("\
 import os\n\
@@ -1055,16 +1072,14 @@ def GdbSetPythonDirectory (dir):\n\
 GdbSetPythonDirectory (gdb.PYTHONDIR)\n\
 ");
 
-  /* Release the GIL while gdb runs.  */
-  PyThreadState_Swap (NULL);
-  PyEval_ReleaseLock ();
+  do_cleanups (cleanup);
+}
 
 #endif /* HAVE_PYTHON */
-}
 
 \f
 
-#if HAVE_PYTHON
+#ifdef HAVE_PYTHON
 
 static PyMethodDef GdbMethods[] =
 {
Index: python/python.h
===================================================================
RCS file: /cvs/src/src/gdb/python/python.h,v
retrieving revision 1.10
diff -u -p -r1.10 python.h
--- python/python.h	11 Jun 2010 15:36:09 -0000	1.10
+++ python/python.h	1 Nov 2010 02:10:07 -0000
@@ -24,6 +24,8 @@
 
 extern int gdbpy_global_auto_load;
 
+extern void finish_python_initialization (void);
+
 void eval_python_from_control_command (struct command_line *);
 
 void source_python_script (FILE *stream, const char *file);

             reply	other threads:[~2010-11-01  2:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-01  2:26 Doug Evans [this message]
2010-11-01 20:25 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101101022638.812792461AF@ruffy.mtv.corp.google.com \
    --to=dje@google.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).