gdb: startup commands to control Python extension language

Add two new commands to GDB that can be placed into the early
initialization to control how Python starts up.  The new options are:

  set python ignore-environment on|off
  set python dont-write-bytecode auto|on|off

  show python ignore-environment
  show python dont-write-bytecode

These can be used from GDB's startup file to control how the Python
extension language behaves.  These options are equivalent to the -E
and -B flags to python respectively, their descriptions from the
Python man page:

  -E     Ignore environment variables like PYTHONPATH and PYTHONHOME
         that modify the  behavior  of  the  interpreter.

  -B     Don't write .pyc files on import.

gdb/ChangeLog:

	* NEWS: Mention new commands.
	* python/python.c (python_ignore_environment): New static global.
	(show_python_ignore_environment): New function.
	(set_python_ignore_environment): New function.
	(python_dont_write_bytecode): New static global.
	(show_python_dont_write_bytecode): New function.
	(set_python_dont_write_bytecode): New function.
	(_initialize_python): Register new commands.

gdb/doc/ChangeLog:

	* python.texinfo (Python Commands): Mention new commands.

gdb/testsuite/ChangeLog:

	* gdb.python/py-startup-opt.exp: New file.
This commit is contained in:
Andrew Burgess 2020-08-27 16:53:13 +01:00
parent 041ca48e97
commit edeaceda7b
7 changed files with 289 additions and 0 deletions

View file

@ -1578,6 +1578,80 @@ python_command (const char *arg, int from_tty)
#endif /* HAVE_PYTHON */
/* When this is turned on before Python is initialised then Python will
ignore any environment variables related to Python. This is equivalent
to passing `-E' to the python program. */
static bool python_ignore_environment = false;
/* Implement 'show python ignore-environment'. */
static void
show_python_ignore_environment (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Python's ignore-environment setting is %s.\n"),
value);
}
/* Implement 'set python ignore-environment'. This sets Python's internal
flag no matter when the command is issued, however, if this is used
after Py_Initialize has been called then most of the environment will
already have been read. */
static void
set_python_ignore_environment (const char *args, int from_tty,
struct cmd_list_element *c)
{
#ifdef HAVE_PYTHON
Py_IgnoreEnvironmentFlag = python_ignore_environment ? 1 : 0;
#endif
}
/* When this is turned on before Python is initialised then Python will
not write `.pyc' files on import of a module. */
static enum auto_boolean python_dont_write_bytecode = AUTO_BOOLEAN_AUTO;
/* Implement 'show python dont-write-bytecode'. */
static void
show_python_dont_write_bytecode (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
if (python_dont_write_bytecode == AUTO_BOOLEAN_AUTO)
{
const char *auto_string
= (python_ignore_environment
|| getenv ("PYTHONDONTWRITEBYTECODE") == nullptr) ? "off" : "on";
fprintf_filtered (file,
_("Python's dont-write-bytecode setting is %s (currently %s).\n"),
value, auto_string);
}
else
fprintf_filtered (file, _("Python's dont-write-bytecode setting is %s.\n"),
value);
}
/* Implement 'set python dont-write-bytecode'. This sets Python's internal
flag no matter when the command is issued, however, if this is used
after Py_Initialize has been called then many modules could already
have been imported and their byte code written out. */
static void
set_python_dont_write_bytecode (const char *args, int from_tty,
struct cmd_list_element *c)
{
#ifdef HAVE_PYTHON
if (python_dont_write_bytecode == AUTO_BOOLEAN_AUTO)
Py_DontWriteBytecodeFlag
= (!python_ignore_environment
&& getenv ("PYTHONDONTWRITEBYTECODE") != nullptr) ? 1 : 0;
else
Py_DontWriteBytecodeFlag
= python_dont_write_bytecode == AUTO_BOOLEAN_TRUE ? 1 : 0;
#endif /* HAVE_PYTHON */
}
/* Lists for 'set python' commands. */
@ -1880,6 +1954,30 @@ message == an error message without a stack will be printed."),
NULL, NULL,
&user_set_python_list,
&user_show_python_list);
add_setshow_boolean_cmd ("ignore-environment", no_class,
&python_ignore_environment, _("\
Set whether the Python interpreter should ignore environment variables."), _(" \
Show whether the Python interpreter showlist ignore environment variables."), _(" \
When enabled GDB's Python interpreter will ignore any Python related\n \
flags in the environment. This is equivalent to passing `-E' to a\n \
python executable."),
set_python_ignore_environment,
show_python_ignore_environment,
&user_set_python_list,
&user_show_python_list);
add_setshow_auto_boolean_cmd ("dont-write-bytecode", no_class,
&python_dont_write_bytecode, _("\
Set whether the Python interpreter should ignore environment variables."), _(" \
Show whether the Python interpreter showlist ignore environment variables."), _(" \
When enabled GDB's Python interpreter will ignore any Python related\n \
flags in the environment. This is equivalent to passing `-E' to a\n \
python executable."),
set_python_dont_write_bytecode,
show_python_dont_write_bytecode,
&user_set_python_list,
&user_show_python_list);
}
#ifdef HAVE_PYTHON