This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] source -p
- From: Tom Tromey <tromey at redhat dot com>
- To: Project Archer <archer at sourceware dot org>
- Date: Thu, 06 Nov 2008 14:09:47 -0700
- Subject: [python] source -p
- Reply-to: Tom Tromey <tromey at redhat dot com>
This adds the -p option to 'source' and also makes .py files
automatically be interpreted as python code.
I'm ambivalent about this but we were talking about it recently and I
thought we could try it for a while to see how it works.
Tom
2008-11-06 Tom Tromey <tromey@redhat.com>
* python/python.c (source_python_script): New function.
* python/python.h (source_python_script): Declare.
* cli/cli-cmds.c (find_argument): New function.
(source_command): Always run cleanup. Use find_argument. Handle
-p argument. Use make_cleanup_restore_integer.
(source_verbose_cleanup): Remove.
(source_python): New global.
Include python.h.
(init_cli_cmds): Update.
2008-11-06 Tom Tromey <tromey@redhat.com>
* gdb.texinfo (File Options): Document -x on .py files.
(Command Files): Document source -p.
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index d9d2c56..92a59a3 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -45,6 +45,8 @@
#include "cli/cli-setshow.h"
#include "cli/cli-cmds.h"
+#include "python/python.h"
+
#ifdef TUI
#include "tui/tui.h" /* For tui_active et.al. */
#endif
@@ -178,6 +180,7 @@ struct cmd_list_element *showchecklist;
/* Command tracing state. */
+static int source_python = 0;
int source_verbose = 0;
int trace_commands = 0;
@@ -437,6 +440,7 @@ source_script (char *file, int from_tty)
struct cleanup *old_cleanups;
char *full_pathname = NULL;
int fd;
+ int is_python;
if (file == NULL || *file == 0)
{
@@ -465,8 +469,16 @@ source_script (char *file, int from_tty)
return;
}
+ is_python = source_python;
+ if (strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
+ is_python = 1;
+
stream = fdopen (fd, FOPEN_RT);
- script_from_file (stream, file);
+
+ if (is_python)
+ source_python_script (stream, file);
+ else
+ script_from_file (stream, file);
do_cleanups (old_cleanups);
}
@@ -480,15 +492,30 @@ source_verbose_cleanup (void *old_value)
xfree (old_value);
}
+/* A helper for source_command. Look for an argument in *ARGS.
+ Update *ARGS by stripping leading whitespace. If an argument is
+ found, return it (a character). Otherwise, return 0. */
+static int
+find_argument (char **args)
+{
+ int result = 0;
+ while (isspace ((*args)[0]))
+ ++*args;
+ if ((*args)[0] == '-' && isalpha ((*args)[1]))
+ {
+ result = (*args)[1];
+ *args += 3;
+ }
+ return result;
+}
+
static void
source_command (char *args, int from_tty)
{
struct cleanup *old_cleanups;
- char *file = args;
- int *old_source_verbose = xmalloc (sizeof(int));
- *old_source_verbose = source_verbose;
- old_cleanups = make_cleanup (source_verbose_cleanup, old_source_verbose);
+ old_cleanups = make_cleanup_restore_integer (&source_verbose);
+ make_cleanup_restore_integer (&source_python);
/* -v causes the source command to run in verbose mode.
We still have to be able to handle filenames with spaces in a
@@ -496,23 +523,28 @@ source_command (char *args, int from_tty)
if (args)
{
- /* Make sure leading white space does not break the comparisons. */
- while (isspace(args[0]))
- args++;
-
- /* Is -v the first thing in the string? */
- if (args[0] == '-' && args[1] == 'v' && isspace (args[2]))
+ while (1)
{
- source_verbose = 1;
-
- /* Trim -v and whitespace from the filename. */
- file = &args[3];
- while (isspace (file[0]))
- file++;
+ int arg = find_argument (&args);
+ if (!arg)
+ break;
+ switch (arg)
+ {
+ case 'v':
+ source_verbose = 1;
+ break;
+ case 'p':
+ source_python = 1;
+ break;
+ default:
+ error (_("unrecognized option -%c"), arg);
+ }
}
}
- source_script (file, from_tty);
+ source_script (args, from_tty);
+
+ do_cleanups (old_cleanups);
}
@@ -1274,6 +1306,8 @@ Commands defined in this way may have up to ten arguments."));
Read commands from a file named FILE.\n\
Optional -v switch (before the filename) causes each command in\n\
FILE to be echoed as it is executed.\n\
+Optional -p switch (before the filename) causes FILE to be evaluated\n\
+as Python code.\n\
Note that the file \"%s\" is read automatically in this way\n\
when GDB is started."), gdbinit);
c = add_cmd ("source", class_support, source_command,
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 071c912..a7038b5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -962,8 +962,10 @@ Connect to process ID @var{number}, as with the @code{attach} command.
@itemx -x @var{file}
@cindex @code{--command}
@cindex @code{-x}
-Execute @value{GDBN} commands from file @var{file}. @xref{Command
-Files,, Command files}.
+Execute commands from file @var{file}. If @var{file} ends in
+@samp{.py}, then the file is evaluated as Python code. If Python
+support is not enabled in this @value{GDBN}, then an error occurs.
+@xref{Command Files,, Command files}.
@item -eval-command @var{command}
@itemx -ex @var{command}
@@ -17380,7 +17382,7 @@ command:
@table @code
@kindex source
@cindex execute commands from a file
-@item source [@code{-v}] @var{filename}
+@item source [@code{-v}] [@code{-p}] @var{filename}
Execute the command file @var{filename}.
@end table
@@ -17397,6 +17399,11 @@ If @code{-v}, for verbose mode, is given then @value{GDBN} displays
each command as it is executed. The option must be given before
@var{filename}, and is interpreted as part of the filename anywhere else.
+If @var{filename} ends in @samp{.py}, or if @code{-p}, for Python, is
+given then @value{GDBN} evaluates the contents of the file as Python
+code. If Python support is not compiled in to @value{GDBN}, then an
+error occurs.
+
Commands that would ask for confirmation if used interactively proceed
without asking when used in a command file. Many @value{GDBN} commands that
normally print messages to say what they are doing omit the messages
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 175c0ba..9f4448b 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -543,6 +543,13 @@ run_python_script (int argc, char **argv)
exit (0);
}
+void
+source_python_script (FILE *stream, char *file)
+{
+ PyRun_SimpleFile (stream, file);
+ fclose (stream);
+}
+
/* The "current" objfile. This is set when gdb detects that a new
@@ -1006,6 +1013,13 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
return NULL;
}
+void
+source_python_script (FILE *stream)
+{
+ fclose (stream);
+ error (_("Python scripting is not supported in this copy of GDB."));
+}
+
#endif /* HAVE_PYTHON */
diff --git a/gdb/python/python.h b/gdb/python/python.h
index bcba616..ef0560c 100644
--- a/gdb/python/python.h
+++ b/gdb/python/python.h
@@ -26,6 +26,8 @@ extern struct value *values_in_python;
void eval_python_from_control_command (struct command_line *);
+void source_python_script (FILE *stream, char *file);
+
void run_python_script (int argc, char **argv);
char *apply_pretty_printer (struct value *, struct value **);