on GNU EMACS's python-mode, loading entire buffer

Meredith Montgomery mmontgomery at levado.to
Mon Oct 3 13:13:29 EDT 2022


Stephen Berman <stephen.berman at gmx.net> writes:

> On Sun, 04 Sep 2022 16:47:07 -0300 Meredith Montgomery
> <mmontgomery at levado.to> wrote:
>
>> Meredith Montgomery <mmontgomery at levado.to> writes:
>>
>>> Meredith Montgomery <mmontgomery at levado.to> writes:
>>>
>>> [...]
>>>
>>>> I would also be interested in a command that restarts the REPL afresh
>>>> and reloads my buffer --- sort of like keyboard's [F5] of the IDLE.
>>>
>>> A partial solution for this is the following procedure.
>>>
>>> (defun python-revert-and-send-buffer-to-repl ()
>>>   "Revert current buffer and sends it to the Python REPL."
>>>   (interactive)
>>>   (revert-buffer "ignore-auto-no" "no-confirm")
>>>   (python-shell-send-buffer))
>>>
>>> We can map this to the F5-key and that improves things.  But a restart
>>> of the REPL would be the ideal.  (Sometimes we really want to start
>>> afresh.  Sometimes.  Most often we don't want that.)
>>
>> It's not easy to restart the REPL.  You can send "quit()" to it and
>> invoke run-python again interactively by typing out one command after
>> another, but if you write a procedure such as this one below, it doesn't
>> work: it gives me the impression that there's a timing issue, that is,
>> perhaps the procedure is too fast and something happens before it
>> should.
>>
>> (defun python-save-send-buffer-to-repl ()
>>   (interactive)
>>   (save-buffer)
>>   (python-shell-send-string "quit()")
>>   (run-python)
>>   (python-shell-send-buffer)
>>   (python-shell-switch-to-shell))
>
> It does seem like a timing issue.  This works for me:
>
> (defun python-save-send-buffer-to-repl ()
>   (interactive)
>   (save-buffer)
>   (python-shell-send-string "quit()")
>   (sit-for 0.1)
>   (run-python)
>   (python-shell-send-buffer)
>   (python-shell-switch-to-shell))
>
> But if I decrease the wait to 0.05 it doesn't work.

Interesting.  I can't reproduce this at all.  No matter how long I sit,
I always get a similar ``crash''.  Here's what happens for me with your
procedure, sitting for even 5 seconds.  I'm adding a 5-second delay
after quitting and after running the REPL.

--8<---------------cut here---------------start------------->8---
(defun python-save-send-buffer-to-repl ()
  "Save current buffer and sends it to the Python REPL."
  (interactive)
  (save-buffer)
  (python-shell-send-string "quit()")
  (sit-for 5)
  (run-python)
  (sit-for 5)
  (python-shell-send-buffer)
  (python-shell-switch-to-shell))
--8<---------------cut here---------------end--------------->8---

When the REPL comes up, it looks fine.  When I try to see if a
hello-procedure was defined, I get the following:

--8<---------------cut here---------------start------------->8---
>>> 
Process Python finished
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:/Users/mer/AppData/Local/Temp/pyoUKuUx", line 1
    Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
                                ^
SyntaxError: invalid decimal literal
>>> >>> hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined. Did you mean: 'help'?
>>> 
--8<---------------cut here---------------end--------------->8---

You see?  It seems that the Python's banner is sent to the REPL,
somehow.  I actually see it being sent sometimes --- I see in the
minibuffer that the string ``Python 3.10.6 (tags/v3.10.6:9c7b4bd[...]''
was sent to the REPL.

Somehow my GNU EMACS is reading the banner and sending it back to the
REPL as if it were code in my file.  My file contained just this:

--8<---------------cut here---------------start------------->8---
# -*- mode: python; python-indent-offset: 2 -*-
def hello():
  return 1
--8<---------------cut here---------------end--------------->8---


More information about the Python-list mailing list