[Bug python/34485] [gdb/python] gdbpy_initialize_gdb_readline misses error handling

"cvs-commit at gcc dot gnu.org via Gdb-prs" <[email protected]>
Newsgroups gmane.comp.gdb.bugs.discuss
Message-ID <[email protected]/bugzilla/>
https://sourceware.org/bugzilla/show_bug.cgi?id=34485

--- Comment #3 from Sourceware Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tom de Vries <[email protected]>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=25f1035f403c929a38d822f0525ec1bb180fffb5

commit 25f1035f403c929a38d822f0525ec1bb180fffb5
Author: Tom de Vries <[email protected]>
Date:   Wed Aug 12 23:11:46 2026 +0200

    [gdb/python] Handle error in gdbpy_initialize_gdb_readline

    On Fedora Rawhide aarch64-linux, with test-case
gdb.python/py-failed-init.exp
    I ran into:
    ...
    builtin_spawn $build/gdb/gdb -nw -nx -q -iex set height 0 -iex set width 0
\
      -data-directory $build/gdb/data-directory -iex set interactive-mode on
    WARN: Could not find the standard library directory! The Python 'home' \
      directory was set to 'foo', is this correct?
    Error occurred computing Python error message.
    $build/gdb/gdb: warning:
    Could not load the Python gdb module from
`$build/gdb/data-directory/python'.
    Limited Python support is available from the _gdb module.
    Suggest passing --data-directory=/path/to/gdb/data-directory.
    (gdb) set height 0
    (gdb) set width 0
    (gdb) dir
    Reinitialize source path to empty? (y or n) y
    Source directories searched: $cdir:$cwd
    (gdb) dir $src/gdb/testsuite/gdb.python
    Source directories searched: $src/gdb/testsuite/gdb.python:$cdir:$cwd
    (gdb) python print (1)
    1
    (gdb) FAIL: $exp: gdb-command<python print (1)>
    quit
    Exception ignored on threading shutdown:
    Traceback (most recent call last):
      File "<string>", line 2, in <module>
    ModuleNotFoundError: No module named 'importlib'
    PASS: $exp: quit
    ...

    The test-case tries to break python:
    ...
    save_vars { env(PYTHONHOME) } {
        setenv PYTHONHOME foo
        clean_restart
    }
    ...
    enough to get it to this point:
    ...
    gdb_test "python print (1)" \
        "Python not initialized"
    ...
    but apparently, that doesn't work anymore in this python version:
    ...
    $ python --version
    Python 3.15.0b4
    ...

    The test-case needs updating, and I've submitted a testsuite patch [1] for
    that.

    The next question is why we're seeing a ModuleNotFoundError on quit.

    I investigated this, and found that it originates from
    gdbpy_initialize_gdb_readline, where we do:
    ...
       if (eval_python_command (code, Py_file_input) == 0)
        PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
    ...
    but don't report and reset the python error state, so instead the error is
    reported by Py_Finalize.

    Fix this by:
    - making sure that the error is reported immediately, though in the form of
a
      warning rather than an error, and
    - disabling the python-interactive command if gdbpy_initialize_gdb_readline
      fails, to avoid broken readline behavior in a python-interactive session.

    Also make the test-case a bit stricter by checking that there's no output
when
    quitting.

    Tested on aarch64-linux.

    Approved-By: Tom Tromey <[email protected]>

    Changes in v2:
    - use gdbpy_print_stack instead of PyErr_Print/PyErr_Clear
    - Fix error/warning message by ensure that command name is double-quoted
and
      displayed using command_style

    Versions:
    - v1 https://sourceware.org/pipermail/gdb-patches/2026-August/229261.html

    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34485

    [1] https://sourceware.org/pipermail/gdb-patches/2026-August/229193.html

-- 
You are receiving this mail because:
You are on the CC list for the bug.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.