Subject: bug#43450: 28.0.50; Python shell doesn't echo if shebang

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 30 Sep 2020 18:50:16 +0200

Gunnar Horrigmo <[email-masked]> writes:

> Open a .py file
> M-x run-python RET
> In .py file, write some statement, say "1+2" and evaluate it with
> (f.ex.) python-shell-send-statement. Observe that python shell echoes
> the result, "3", and prompts on a new line.
>
> Add a shebang to the top of .py file (f.ex. "#!/usr/bin/pyton3" on my
> system). Evaluate the same statement as over. Observe that python shell
> doesn't echo the result, but does print a new prompt, but without a
> newline. 

I can reproduce this in Emacs 28, but it doesn't seem to be triggered by
the shebang:  Adding some blank lines to the top of the buffer also
triggers the error.  Or ... anything:

(python-shell-buffer-substring 2 (point) nil)
=> "# -*- coding: utf-8 -*-

1+2"

(python-shell-buffer-substring 1 (point) nil)
=> "1+2"

         (fillstr (when (not starts-at-point-min-p)
                    (concat
                     (format "# -*- coding: %s -*-\n" encoding)
                     (make-string
                      ;; Subtract 2 because of the coding cookie.
                      (- (line-number-at-pos start) 2) ?\n)))))

This was added by (in a slightly different form):

commit 2dd5163d764f395eb31a2306dba385d123af4aba
Author:     Fabián Ezequiel Gallina <[email-masked]>
AuthorDate: Sat Dec 27 01:30:21 2014 -0300

    python.el: Handle file encoding for shell.
    
    * lisp/progmodes/python.el (python-rx-constituents): Add coding-cookie.
    (python-shell--save-temp-file): Write file with proper encoding.
    (python-shell-buffer-substring): Add coding cookie for detected
    encoding to generated content.  Fix blank lines when removing
    if-name-main block.
    (python-shell-send-file): Handle file encoding.
    (python-info-encoding-from-cookie)
    (python-info-encoding): New functions.

I've added Fabián to the Cc, perhaps he has some insight here...

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no