GNU bug report logs - #57885
[PATCH] Add a command to restart the Python shell

Previous Next

Package: emacs;

Reported by: Augusto Stoffel <arstoffel <at> gmail.com>

Date: Sat, 17 Sep 2022 17:10:02 UTC

Severity: normal

Tags: patch

Fixed in version 29.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 57885 in the body.
You can then email your comments to 57885 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sat, 17 Sep 2022 17:10:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Augusto Stoffel <arstoffel <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 17 Sep 2022 17:10:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 19:09:40 +0200
[Message part 1 (text/plain, inline)]
Tags: patch

I find this pretty much essential when running "notebooks" and the like.

I think "C-c C-n" would be a reasonable keybinding, but since I can't
find any precedent, I didn't include a keybinding.

Also, in case the `string-trim' call looks hacky, I should add that the
relationship between the shell buffer and process name is pretty much
hardcoded in python.el, so there doesn't seem to be anything more
reasonable to do that wouldn't require some major refactoring.

[0001-lisp-progmodes-python.el-python-shell-restart-New-co.patch (text/patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sat, 17 Sep 2022 17:17:02 GMT) Full text and rfc822 format available.

Message #8 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 20:15:50 +0300
> From: Augusto Stoffel <arstoffel <at> gmail.com>
> Date: Sat, 17 Sep 2022 19:09:40 +0200
> 
> +(defun python-shell-restart (&optional show)
> +  "Restart the Python shell.
> +With a prefix argument, also SHOW the buffer."

Our style for the second sentence is

  Optional argument SHOW (interactively, the prefix argument), if
  non-nil, means also display the Python shell buffer.

That is, document the argument both for calls from Lisp and for
interactive invocations.

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sat, 17 Sep 2022 17:34:01 GMT) Full text and rfc822 format available.

Message #11 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Augusto Stoffel <arstoffel <at> gmail.com>, 57885 <at> debbugs.gnu.org
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 13:32:53 -0400
Augusto Stoffel <arstoffel <at> gmail.com> writes:

> I think "C-c C-n" would be a reasonable keybinding, but since I can't
> find any precedent, I didn't include a keybinding.

`C-c C-n' is usually used to go to the next thing, so maybe we could try
to find some better key.

Looks good to me (but I didn't test it), with two minor comments:

> +(defun python-shell-restart (&optional show)
> +  "Restart the Python shell.
> +With a prefix argument, also SHOW the buffer."
> +  (interactive "P")
> +  (with-current-buffer
> +      (or (when (derived-mode-p 'inferior-python-mode)
> +            (current-buffer))

As a minor style point, I'd prefer:

    (and (derived-mode-p 'inferior-python-mode)
         (current-buffer))

to make it clear that we want the value.

> +          (seq-some (lambda (dedicated)
> +                      (get-buffer (format "*%s*" (python-shell-get-process-name
> +                                                  dedicated))))
> +                    '(buffer project nil))
> +          (user-error "No Python shell"))
> +    (when-let ((proc (get-buffer-process (current-buffer))))
> +      (kill-process proc)
> +      (while (accept-process-output proc)))

Should there be an error here if there is no running process?

> +    (python-shell-make-comint (python-shell-calculate-command)
> +                              (string-trim (buffer-name) "\\*" "\\*")
> +                              show)))




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sat, 17 Sep 2022 17:37:02 GMT) Full text and rfc822 format available.

Message #14 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 57885 <at> debbugs.gnu.org
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 19:36:15 +0200
On Sat, 17 Sep 2022 at 20:15, Eli Zaretskii wrote:

>> From: Augusto Stoffel <arstoffel <at> gmail.com>
>> Date: Sat, 17 Sep 2022 19:09:40 +0200
>> 
>> +(defun python-shell-restart (&optional show)
>> +  "Restart the Python shell.
>> +With a prefix argument, also SHOW the buffer."
>
> Our style for the second sentence is
>
>   Optional argument SHOW (interactively, the prefix argument), if
>   non-nil, means also display the Python shell buffer.
>
> That is, document the argument both for calls from Lisp and for
> interactive invocations.
>
> Thanks.

Sounds better indeed, will send an update in the other reply.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sat, 17 Sep 2022 17:40:02 GMT) Full text and rfc822 format available.

Message #17 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 19:39:10 +0200
[Message part 1 (text/plain, inline)]
On Sat, 17 Sep 2022 at 13:32, Stefan Kangas wrote:

> Augusto Stoffel <arstoffel <at> gmail.com> writes:
>
>> I think "C-c C-n" would be a reasonable keybinding, but since I can't
>> find any precedent, I didn't include a keybinding.
>
> `C-c C-n' is usually used to go to the next thing, so maybe we could try
> to find some better key.

Okay, then let's decide later (or not decide :-)).

> Looks good to me (but I didn't test it), with two minor comments:
>
>> +(defun python-shell-restart (&optional show)
>> +  "Restart the Python shell.
>> +With a prefix argument, also SHOW the buffer."
>> +  (interactive "P")
>> +  (with-current-buffer
>> +      (or (when (derived-mode-p 'inferior-python-mode)
>> +            (current-buffer))
>
> As a minor style point, I'd prefer:
>
>     (and (derived-mode-p 'inferior-python-mode)
>          (current-buffer))
>
> to make it clear that we want the value.


Okay

>> +          (seq-some (lambda (dedicated)
>> +                      (get-buffer (format "*%s*" (python-shell-get-process-name
>> +                                                  dedicated))))
>> +                    '(buffer project nil))
>> +          (user-error "No Python shell"))
>> +    (when-let ((proc (get-buffer-process (current-buffer))))
>> +      (kill-process proc)
>> +      (while (accept-process-output proc)))
>
> Should there be an error here if there is no running process?
>

No, and if fact, if the process is dead, you want to call this command
to start the shell again (it happens to me and it works).

[0001-lisp-progmodes-python.el-python-shell-restart-New-co.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sat, 17 Sep 2022 18:05:02 GMT) Full text and rfc822 format available.

Message #20 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sat, 17 Sep 2022 14:04:00 -0400
Augusto Stoffel <arstoffel <at> gmail.com> writes:

> Okay, then let's decide later (or not decide :-)).

:-)

> No, and if fact, if the process is dead, you want to call this command
> to start the shell again (it happens to me and it works).

Makes sense.

> Subject: [PATCH] * lisp/progmodes/python.el (python-shell-restart): New
>  command.

BTW, maybe you want to add a first line with text like

    Add new command python-shell-restart




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sun, 18 Sep 2022 06:54:02 GMT) Full text and rfc822 format available.

Message #23 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sun, 18 Sep 2022 08:53:36 +0200
[Message part 1 (text/plain, inline)]
On Sat, 17 Sep 2022 at 14:04, Stefan Kangas wrote:

> Augusto Stoffel <arstoffel <at> gmail.com> writes:
>
>> Okay, then let's decide later (or not decide :-)).
>
> :-)
>
>> No, and if fact, if the process is dead, you want to call this command
>> to start the shell again (it happens to me and it works).
>
> Makes sense.
>
>> Subject: [PATCH] * lisp/progmodes/python.el (python-shell-restart): New
>>  command.
>
> BTW, maybe you want to add a first line with text like
>
>     Add new command python-shell-restart

Find attached a patch with an expanded commit message, and also an
unrelated but trivial refinement for something else.

[0001-Add-new-command-python-shell-restart.patch (text/x-patch, attachment)]
[0002-Remap-up-list-in-python-mode-map.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Sun, 18 Sep 2022 10:54:02 GMT) Full text and rfc822 format available.

Message #26 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org, Stefan Kangas <stefankangas <at> gmail.com>
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sun, 18 Sep 2022 12:53:41 +0200
Augusto Stoffel <arstoffel <at> gmail.com> writes:

> Find attached a patch with an expanded commit message, and also an
> unrelated but trivial refinement for something else.

Thanks; pushed to Emacs 29.




bug marked as fixed in version 29.1, send any further explanations to 57885 <at> debbugs.gnu.org and Augusto Stoffel <arstoffel <at> gmail.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 18 Sep 2022 10:54:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Mon, 19 Sep 2022 03:33:01 GMT) Full text and rfc822 format available.

Message #31 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Richard Stallman <rms <at> gnu.org>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Sun, 18 Sep 2022 23:32:03 -0400
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

In Shell mode, if you terminate the shell, M-x shell will restart it.
That is easy to remember and does not require adding any commands to Emacs.

Can the same solution work here?

Does it perhaps work already?

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Mon, 19 Sep 2022 07:22:02 GMT) Full text and rfc822 format available.

Message #34 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: Richard Stallman <rms <at> gnu.org>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Mon, 19 Sep 2022 09:20:54 +0200
On Sun, 18 Sep 2022 at 23:32, Richard Stallman wrote:

> [[[ To any NSA and FBI agents reading my email: please consider    ]]]
> [[[ whether defending the US Constitution against all enemies,     ]]]
> [[[ foreign or domestic, requires you to follow Snowden's example. ]]]
>
> In Shell mode, if you terminate the shell, M-x shell will restart it.
> That is easy to remember and does not require adding any commands to Emacs.
>
> Can the same solution work here?
>
> Does it perhaps work already?

This already works, but I think the new command is useful for a few
different reasons.

The most important is that you can operate the Python shell entirely
from other buffers using `python-shell-send-region' and related commands
(I use the Python shell to see the outputs, but rarely type input
directly into it).  So the procedure you suggest actually involves a
number of steps that become annoying if you need to repeat them often.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Wed, 28 Sep 2022 02:54:02 GMT) Full text and rfc822 format available.

Message #37 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Richard Stallman <rms <at> gnu.org>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Tue, 27 Sep 2022 22:52:55 -0400
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > The most important is that you can operate the Python shell entirely
  > from other buffers using `python-shell-send-region' and related commands
  > (I use the Python shell to see the outputs, but rarely type input
  > directly into it).

I can see how that command is useful, but why should that shell not
have a buffer to manipulate it by?  You can still have the
commands that don't use the buffer, but you'll also be able
to use the buffer to restart the shell, kill the shell, and
whatever else.

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Wed, 28 Sep 2022 06:37:02 GMT) Full text and rfc822 format available.

Message #40 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: Richard Stallman <rms <at> gnu.org>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Wed, 28 Sep 2022 08:36:44 +0200
On Tue, 27 Sep 2022 at 22:52, Richard Stallman wrote:

> [[[ To any NSA and FBI agents reading my email: please consider    ]]]
> [[[ whether defending the US Constitution against all enemies,     ]]]
> [[[ foreign or domestic, requires you to follow Snowden's example. ]]]
>
>   > The most important is that you can operate the Python shell entirely
>   > from other buffers using `python-shell-send-region' and related commands
>   > (I use the Python shell to see the outputs, but rarely type input
>   > directly into it).
>
> I can see how that command is useful, but why should that shell not
> have a buffer to manipulate it by?  You can still have the
> commands that don't use the buffer, but you'll also be able
> to use the buffer to restart the shell, kill the shell, and
> whatever else.

Yes, you can do all of the above.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Thu, 29 Sep 2022 03:01:02 GMT) Full text and rfc822 format available.

Message #43 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Richard Stallman <rms <at> gnu.org>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Wed, 28 Sep 2022 23:00:22 -0400
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > > I can see how that command is useful, but why should that shell not
  > > have a buffer to manipulate it by?  You can still have the
  > > commands that don't use the buffer, but you'll also be able
  > > to use the buffer to restart the shell, kill the shell, and
  > > whatever else.

  > Yes, you can do all of the above.

I find it hard to understand that concretely.  Are you saying that
these shells do in fact have corresponding buffers?

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Thu, 29 Sep 2022 07:10:02 GMT) Full text and rfc822 format available.

Message #46 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Augusto Stoffel <arstoffel <at> gmail.com>
To: Richard Stallman <rms <at> gnu.org>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Thu, 29 Sep 2022 09:09:45 +0200
On Wed, 28 Sep 2022 at 23:00, Richard Stallman wrote:

> [[[ To any NSA and FBI agents reading my email: please consider    ]]]
> [[[ whether defending the US Constitution against all enemies,     ]]]
> [[[ foreign or domestic, requires you to follow Snowden's example. ]]]
>
>   > > I can see how that command is useful, but why should that shell not
>   > > have a buffer to manipulate it by?  You can still have the
>   > > commands that don't use the buffer, but you'll also be able
>   > > to use the buffer to restart the shell, kill the shell, and
>   > > whatever else.
>
>   > Yes, you can do all of the above.
>
> I find it hard to understand that concretely.  Are you saying that
> these shells do in fact have corresponding buffers?

Yes, it does.  It's a pretty conventional shell in the mold of M-x
shell, that is, a buffer where you can type some expressions and see the
result.

It also has some extra features, such as python-shell-send-region which
effectively copies a region of the current buffer (presumably Python
code) and pastes it in the shell buffer and hits RET for you.  You can
later switch to the Python shell buffer to see the result of that
computation.

I hope this clarifies things :-).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57885; Package emacs. (Fri, 07 Oct 2022 22:48:01 GMT) Full text and rfc822 format available.

Message #49 received at 57885 <at> debbugs.gnu.org (full text, mbox):

From: Richard Stallman <rms <at> gnu.org>
To: Augusto Stoffel <arstoffel <at> gmail.com>
Cc: 57885 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#57885: [PATCH] Add a command to restart the Python shell
Date: Fri, 07 Oct 2022 18:47:01 -0400
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > > I find it hard to understand that concretely.  Are you saying that
  > > these shells do in fact have corresponding buffers?

  > Yes, it does.  It's a pretty conventional shell in the mold of M-x
  > shell, that is, a buffer where you can type some expressions and see the
  > result.

I think that is a good design.  It lets the user do things to this
shell in the usual way, by specifying or selecting its buffer.

Somehow your first message suggested to me that this shell would
not belong to a buffer, but I'm glad that was just my misunderstanding.

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)






bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 05 Nov 2022 11:24:11 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 173 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.