EDIT 3 {
I don't believe the root of the problem has really been found. This problem manifests independently of any terminals. It also occurs in graphical Emacs. I decided to try to trace what Emacs was doing using a pre-command-hook:
(defvar *event-log* nil)
(defun log-commands nil
(let ((buf (current-buffer)))
(cl-push `(,buf ,this-command ,(this-command-keys))
*event-log*)))
(add-hook 'pre-command-hook 'log-commands)
This causes Emacs to log every single keypress to an in-memory list.
From there, I opened a Python source file and put the cursor where the block appears:
def foobar(baz,qux):
try:
quux.xyzzy()▋
except:
pass
From there, I added .foobar
and then tried to delete it by pressing Backspace twice (enough to reproduce the behavior and also revert back to the correct behavior). The resulting event log, which is in reverse chronological order:
(#<buffer foobar.py> python-indent-dedent-line-backspace "^?")
(#<buffer foobar.py> delete-char "^D")
(#<buffer foobar.py> self-insert-command "r")
(#<buffer foobar.py> self-insert-command "a")
(#<buffer foobar.py> self-insert-command "b")
(#<buffer foobar.py> self-insert-command "o")
(#<buffer foobar.py> self-insert-command "o")
(#<buffer foobar.py> self-insert-command "f")
(#<buffer foobar.py> self-insert-command ".")
It clearly shows that the same key is being received as two distinct keys, and therefore it's executing two different functions. Why this happens remains a mystery, but because it can happen in a GUI session, we can rule out the possibility of terminal settings being to blame. Also note that it's impossible for things like terminal settings or X11 keymap bindings to suddenly change as a result of Emacs keypresses, and even if it was, the behavior would not be consistent between terminals, local X11 sessions, and X11-forwarded sessions.