Mailing List Archive

on GNU EMACS's python-mode, loading entire buffer
Perhaps this isn't the right newsgroup, but I kinda feel I will find
more GNU EMACS users running the native python-mode here than in GNU
EMACS newsgroups. Not every GNU EMACS user cares about Python.
(Right?)

A sort of a complaint is that when I used to run GNU EMACS 24.3.1, I'd
open a file.py and simply say C-c C-c and it would ask me if I wanted a
dedicated process, load a REPL load my entire buffer.

Now in 27.1, things are different. I say C-c C-c and it tells me to
start the process with C-c C-p. I mean --- is that the most polite
thing to do? I feel like it's telling me --- go send this buffer
yourself!

I also don't know how to start a dedicated process. (Sometimes I want
two REPLs. You know, you can get never get enough.)

I would bet all of this and more is available, but I don't even know
which book to read. Would you point me in the right direction?

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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: on GNU EMACS's python-mode, loading entire buffer [ In reply to ]
Paul Rubin <no.email@nospam.invalid> writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>> Now in 27.1, things are different. I say C-c C-c and it tells me to
>> start the process with C-c C-p. I mean --- is that the most polite
>> thing to do? I feel like it's telling me --- go send this buffer
>> yourself!
>
> Hmm, I noticed that too, but I thought it was a change in python-mode
> itself. I just got used to it. I didn't bother trying to chase it down.

These little conveniences are so important. :-)

> To start a new process, try C-u C-c C-p .

Oh, thank you!
--
https://mail.python.org/mailman/listinfo/python-list
Re: on GNU EMACS's python-mode, loading entire buffer [ In reply to ]
Meredith Montgomery <mmontgomery@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.

--8<---------------cut here---------------start------------->8---
(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))
--8<---------------cut here---------------end--------------->8---

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.)
--
https://mail.python.org/mailman/listinfo/python-list
Re: on GNU EMACS's python-mode, loading entire buffer [ In reply to ]
Meredith Montgomery <mmontgomery@levado.to> writes:

> Meredith Montgomery <mmontgomery@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))
--
https://mail.python.org/mailman/listinfo/python-list
Re: on GNU EMACS's python-mode, loading entire buffer [ In reply to ]
On Sun, 04 Sep 2022 16:47:07 -0300 Meredith Montgomery <mmontgomery@levado.to> wrote:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>
>> Meredith Montgomery <mmontgomery@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.

Steve Berman
--
https://mail.python.org/mailman/listinfo/python-list
Re: on GNU EMACS's python-mode, loading entire buffer [ In reply to ]
Stephen Berman <stephen.berman@gmx.net> writes:

> On Sun, 04 Sep 2022 16:47:07 -0300 Meredith Montgomery
> <mmontgomery@levado.to> wrote:
>
>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>
>>> Meredith Montgomery <mmontgomery@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---
--
https://mail.python.org/mailman/listinfo/python-list