Subject: bug#29592: 25.3; python does not print input or output in the inferior process

From: Ben Sturmfels <[email-masked]>
Date: Sat, 26 Feb 2022 10:00:35 +1100

The GitHub ticket below for elpy also mentions this issue and includes some
"quick-and-dirty" that does seem to address the issue. The code works,
but I get an extra line break in the REPL when evaluating lines other
than the first line of the buffer.

https://github.com/jorgenschaefer/elpy/issues/924#issuecomment-314922197

> This is not an elpy issue, it also occurs with plain python-mode. It
> seems to be caused by certain versions of Python only. A quick-and-dirty
> fix is (in .emacs):
> 
> (defun python-shell-append-to-output (string)
>   (let ((buffer (current-buffer)))
>     (set-buffer (process-buffer (python-shell-get-process)))
>     (let ((oldpoint (point)))
>       (goto-char (process-mark (python-shell-get-process)))
>       (insert string)
>       (set-marker (process-mark (python-shell-get-process)) (point))
>       (goto-char oldpoint))
>     (set-buffer buffer)))
> 
> (defadvice python-shell-send-string
>     (around advice-python-shell-send-string activate)
>   (interactive)
>   (let* ((append-string1
>          (if (string-match "import codecs, os;__pyfile = codecs.open.*$" string)
>              (replace-match "" nil nil string)
>            string))
>         (append-string2
>          (if (string-match "^# -\\*- coding: utf-8 -\\*-\n*$" append-string1)
>              (replace-match "" nil nil append-string1)
>            append-string1))
>         (append-string
>          (if (string-match "^\n*$" append-string2)
>              (replace-match "" nil nil append-string2)
>            append-string2)))  
>     (python-shell-append-to-output
>      (concat (string-trim-right append-string) "\n")))
>   (if (called-interactively-p 'any)
>       (call-interactively (ad-get-orig-definition 'python-shell-send-string))
>     ad-do-it))