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

@@ -103,6 +103,44 @@ controlled using @code{set python print-stack}: if @code{full}, then
full Python stack printing is enabled; if @code{none}, then Python stack
and message printing is disabled; if @code{message}, the default, only
the message component of the error is printed.
@kindex set python ignore-environment
@item set python ignore-environment @r{[}on@r{|}off@r{]}
By default this option is @samp{off}, and, when @value{GDBN}
initializes its internal Python interpreter, the Python interpreter
will check the environment for variables that will effect how it
behaves, for example @env{PYTHONHOME}, and
@env{PYTHONPATH}@footnote{See the ENVIRONMENT VARIABLES section of
@command{man 1 python} for a comprehensive list.}.
If this option is set to @samp{on} before Python is initialized then
Python will ignore all such environment variables. As Python is
initialized early during @value{GDBN}'s startup process, then this
option must be placed into the early initialization file
(@pxref{Initialization Files}) to have the desired effect.
This option is equivalent to passing @option{-E} to the real
@command{python} executable.
@kindex set python dont-write-bytecode
@item set python dont-write-bytecode @r{[}auto@r{|}on@r{|}off@r{]}
When this option is @samp{off}, then, once @value{GDBN} has
initialized the Python interpreter, the interpreter will byte-compile
any Python modules that it imports and write the byte code to disk in
@file{.pyc} files.
If this option is set to @samp{on} before Python is initialized then
Python will no longer write the byte code to disk. As Python is
initialized early during @value{GDBN}'s startup process, then this
option must be placed into the early initialization file
(@pxref{Initialization Files}) to have the desired effect.
By default this option is set to @samp{auto}, in this mode Python will
check the environment variable @env{PYTHONDONTWRITEBYTECODE} to see
if it should write out byte-code or not.
This option is equivalent to passing @option{-B} to the real
@command{python} executable.
@end table
It is also possible to execute a Python script from the @value{GDBN}