From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1028) id 0CC513858413; Mon, 25 Jul 2022 19:05:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0CC513858413 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Kevin Buettner To: gdb-cvs@sourceware.org Subject: [binutils-gdb] set/show python dont-write-bytecode fixes X-Act-Checkin: binutils-gdb X-Git-Author: Kevin Buettner X-Git-Refname: refs/heads/master X-Git-Oldrev: 23aa2befce75966acd388b810e139922857533fa X-Git-Newrev: 24d2cbc42ccf08dba79394635db3be084eed9062 Message-Id: <20220725190535.0CC513858413@sourceware.org> Date: Mon, 25 Jul 2022 19:05:35 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2022 19:05:35 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D24d2cbc42ccf= 08dba79394635db3be084eed9062 commit 24d2cbc42ccf08dba79394635db3be084eed9062 Author: Kevin Buettner Date: Mon Jul 25 12:04:10 2022 -0700 set/show python dont-write-bytecode fixes =20 GDB uses the environment variable PYTHONDONTWRITEBYTECODE to determine whether or not to write the result of byte-compiling python modules when the "python dont-write-bytecode" setting is "auto". Simon noticed that GDB's implementation doesn't follow the Python documentation. =20 At present, GDB only checks for the existence of this environment variable. That is not sufficient though. Regarding PYTHONDONTWRITEBYTECODE, this document... =20 https://docs.python.org/3/using/cmdline.html =20 ...says: =20 If this is set to a non-empty string, Python won't try to write .pyc files on the import of source modules. =20 This commit fixes GDB's handling of PYTHONDONTWRITEBYTECODE by adding an empty string check. =20 This commit also corrects the set/show command documentation for "python dont-write-bytecode". The current doc was just a copy of that for set/show python ignore-environment. =20 During his review of an earlier version of this patch, Eli Zaretskii asked that the help text that I proposed for "set/show python dont-write-bytecode" be expanded. I've done that in addition to clarifying the documentation of this option in the GDB manual. Diff: --- gdb/doc/python.texi | 9 +++++++-- gdb/python/python.c | 32 +++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index eeb847aeaa8..cdf7db91602 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -134,9 +134,14 @@ initialized early during @value{GDBN}'s startup proces= s, then this option must be placed into the early initialization file (@pxref{Initialization Files}) to have the desired effect. =20 -By default this option is set to @samp{auto}, in this mode Python will -check the environment variable @env{PYTHONDONTWRITEBYTECODE} to see +By default this option is set to @samp{auto}. In this mode, provided +the @code{python ignore-environment} setting is @samp{off}, the +environment variable @env{PYTHONDONTWRITEBYTECODE} is examined to see if it should write out byte-code or not. +@env{PYTHONDONTWRITEBYTECODE} is considered to be off/disabled either +when set to the empty string or when the environment variable doesn't +exist. All other settings, including those which don't seem to make +sense, indicate that it's on/enabled. =20 This option is equivalent to passing @option{-B} to the real @command{python} executable. diff --git a/gdb/python/python.c b/gdb/python/python.c index c7d4157b70c..7997bee2c69 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1866,8 +1866,15 @@ python_write_bytecode () int wbc =3D 0; =20 if (python_dont_write_bytecode =3D=3D AUTO_BOOLEAN_AUTO) - wbc =3D (!python_ignore_environment - && getenv ("PYTHONDONTWRITEBYTECODE") !=3D nullptr) ? 0 : 1; + { + if (python_ignore_environment) + wbc =3D 1; + else + { + const char *pdwbc =3D getenv ("PYTHONDONTWRITEBYTECODE"); + wbc =3D (pdwbc =3D=3D nullptr || pdwbc[0] =3D=3D '\0') ? 1 : 0; + } + } else wbc =3D python_dont_write_bytecode =3D=3D AUTO_BOOLEAN_TRUE ? 0 : 1; =20 @@ -2346,11 +2353,22 @@ python executable."), =20 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 whether the Python interpreter should avoid byte-compiling python modu= les."), _("\ +Show whether the Python interpreter should avoid byte-compiling python mod= ules."), _("\ +When enabled, GDB's embedded Python interpreter won't byte-compile python\= n\ +modules. In order to take effect, this setting must be enabled in an earl= y\n\ +initialization file, i.e. those run via the --early-init-eval-command or\n\ +-eix command line options. A 'set python dont-write-bytecode on' command\= n\ +can also be issued directly from the GDB command line via the\n\ +--early-init-eval-command or -eiex command line options.\n\ +\n\ +This setting defaults to 'auto'. In this mode, provided the 'python\n\ +ignore-environment' setting is 'off', the environment variable\n\ +PYTHONDONTWRITEBYTECODE is examined to determine whether or not to\n\ +byte-compile python modules. PYTHONDONTWRITEBYTECODE is considered to be\= n\ +off/disabled either when set to the empty string or when the\n\ +environment variable doesn't exist. All other settings, including those\n\ +which don't seem to make sense, indicate that it's on/enabled."), set_python_dont_write_bytecode, show_python_dont_write_bytecode, &user_set_python_list,