GNU bug report logs - #55174
python-shell-send-statement in python.el sends malformed code

Previous Next

Package: emacs;

Reported by: Jin Choi <jsc <at> alum.mit.edu>

Date: Thu, 28 Apr 2022 20:25:02 UTC

Severity: normal

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 55174 in the body.
You can then email your comments to 55174 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#55174; Package emacs. (Thu, 28 Apr 2022 20:25:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jin Choi <jsc <at> alum.mit.edu>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 28 Apr 2022 20:25:02 GMT) Full text and rfc822 format available.

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

From: Jin Choi <jsc <at> alum.mit.edu>
To: bug-gnu-emacs <at> gnu.org
Subject: python-shell-send-statement in python.el sends malformed code
Date: Thu, 28 Apr 2022 15:37:50 -0400
[Message part 1 (text/plain, inline)]
python-shell-send-statement mangles the sent code if the region is inactive and the current statement is indented. Example: with the indented statement

	a = 1

it will send the string “if True:” by itself. With a multi-line statement such as
	a = (1,
		2)

it will send
if True:
	2)

The first line is always truncated.

I’ve looked a little into what is happening, and it looks like python-shell-buffer-substring has seen a number of edits over time that don’t work together properly.

* python-shell-send-statement works properly if the region is active because it calls python-shell-send-region with no-cookie set to nil, but when it is not, it calls python-shell-send-region with no-cookie set to t.
* python-shell-send-region calls python-shell-buffer-substring, passing along no-cookie.
* python-shell-buffer-substring does the following:
	- the start position is adjusted to the beginning of the line if the statement was indented
	- if no-cookie is false, fillstr is set to be a coding cookie (e.g., “# -*- coding: utf-8 -*-“) and a number of newlines to get the statement to the correct line
	- Then, this code appears:

    (with-temp-buffer
      (python-mode)
      (when fillstr
        (insert fillstr)) ; inserts coding cookie and newlines if not no-cookie
      (insert substring) ; inserts code
      (goto-char (point-min))
      (when (not toplevel-p)
        (insert "if True:")
        (delete-region (point) (line-end-position))) ; inserts “if True:” and deletes the rest of the first line

This works when no-cookie is false, because it *deletes the cookie line*, incidentally negating any benefit the coding cookie was supposed to provide. It fails when an indented statement is sent and no-cookie is true, because the first line IS the line to be sent. Either it gets deleted entirely, or if it is a multiline statement, the first line is replaced with the “if True:”.

Note that setting no-cookie has the added effect of removing the newlines for the line number matching.

I don’t know what use case the no-cookie argument was supposed to address, but the current implementation seems incompatible with how it’s being used with indented statements.
[smime.p7s (application/pkcs7-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#55174; Package emacs. (Fri, 29 Apr 2022 10:22:01 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Jin Choi <jsc <at> alum.mit.edu>
Cc: 55174 <at> debbugs.gnu.org
Subject: Re: bug#55174: python-shell-send-statement in python.el sends
 malformed code
Date: Fri, 29 Apr 2022 12:21:37 +0200
Jin Choi <jsc <at> alum.mit.edu> writes:

> This works when no-cookie is false, because it *deletes the cookie
> line*, incidentally negating any benefit the coding cookie was
> supposed to provide. It fails when an indented statement is sent and
> no-cookie is true, because the first line IS the line to be
> sent. Either it gets deleted entirely, or if it is a multiline
> statement, the first line is replaced with the “if True:”.

You don't say what Emacs version this is about?

Anyway, if it's about a current Emacs, could you propose a patch to fix
these issues?

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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#55174; Package emacs. (Fri, 29 Apr 2022 15:30:01 GMT) Full text and rfc822 format available.

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

From: Jin Choi <jsc <at> alum.mit.edu>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 55174 <at> debbugs.gnu.org
Subject: Re: bug#55174: python-shell-send-statement in python.el sends
 malformed code
Date: Fri, 29 Apr 2022 11:29:26 -0400
[Message part 1 (text/plain, inline)]
On Apr 29, 2022, at 6:21 AM, Lars Ingebrigtsen <larsi <at> gnus.org> wrote:
> 
> Jin Choi <jsc <at> alum.mit.edu> writes:
> 
>> This works when no-cookie is false, because it *deletes the cookie
>> line*, incidentally negating any benefit the coding cookie was
>> supposed to provide. It fails when an indented statement is sent and
>> no-cookie is true, because the first line IS the line to be
>> sent. Either it gets deleted entirely, or if it is a multiline
>> statement, the first line is replaced with the “if True:”.
> 
> You don't say what Emacs version this is about?
> 
> Anyway, if it's about a current Emacs, could you propose a patch to fix
> these issues?
> 
> -- 
> (domestic pets only, the antidote for overdose, milk.)
> bloggy blog: http://lars.ingebrigtsen.no

This is in emacs 28.1.

Here is my take on a proposed fix. I would also remove the ’t’ no-cookie argument to python-shell-send-region in python-shell-send-statement at line 3378, to have it behave identically in both branches of the if statement, but I have left that alone because I don’t understand the rationale.

This patch:
1. Separates the semantics of no-cookie and fillstr; the code to be sent is always placed at the same line number where it appears in the file.
2. If no-cookie is not specified, a coding cookie is added, otherwise only newlines are used. If the code begins on the first line, there is no room for a coding cookie and one will not be inserted.
3. The “if True:” line is placed on the line before the code sent begins, to avoid as much conflict with the first line as possible. It will error if an indented line is the first line of the file, but that is not a valid Python construct. If an indented line is sent as the second line, it will work but the coding cookie will be deleted by the “if True:”. Neither of these cases is likely.


diff -u /Users/jsc/elisp/python.el.orig /Users/jsc/elisp/python.el
--- /Users/jsc/elisp/python.el.orig	2022-04-28 14:30:09.000000000 -0400
+++ /Users/jsc/elisp/python.el	2022-04-29 11:21:17.000000000 -0400
@@ -3284,22 +3284,25 @@
(goto-char start)
(python-util-forward-comment 1)
(current-indentation))))
- (fillstr (and (not no-cookie)
- (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)))))
+ (fillstr (cond (starts-at-point-min-p
+ nil)
+ ((not no-cookie)
+ (concat
+ (format "# -*- coding: %s -*-\n" encoding)
+ (make-string
+ ;; Subtract 2 because of the coding cookie.
+ (- (line-number-at-pos start) 2) ?\n)))
+ (t
+ (make-string (- (line-number-at-pos start) 1) ?\n)))))
(with-temp-buffer
(python-mode)
(when fillstr
(insert fillstr))
- (insert substring)
- (goto-char (point-min))
(when (not toplevel-p)
- (insert "if True:")
+ (forward-line -1)
+ (insert "if True:\n")
(delete-region (point) (line-end-position)))
+ (insert substring)
(when nomain
(let* ((if-name-main-start-end
(and nomain

Diff finished. Fri Apr 29 11:21:22 2022

(Sorry, original not cc’ed to the bugs list)
[smime.p7s (application/pkcs7-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#55174; Package emacs. (Fri, 29 Apr 2022 15:35:01 GMT) Full text and rfc822 format available.

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

From: Jin Choi <jsc <at> alum.mit.edu>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 55174 <at> debbugs.gnu.org
Subject: Re: bug#55174: python-shell-send-statement in python.el sends
 malformed code
Date: Fri, 29 Apr 2022 11:34:16 -0400
[Message part 1 (text/plain, inline)]
Sigh. I’m sorry, that patch got horribly deformed. Here it is as an attachment:

[patch.diff (application/octet-stream, attachment)]
[smime.p7s (application/pkcs7-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#55174; Package emacs. (Sat, 30 Apr 2022 11:36:01 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Jin Choi <jsc <at> alum.mit.edu>
Cc: 55174 <at> debbugs.gnu.org
Subject: Re: bug#55174: python-shell-send-statement in python.el sends
 malformed code
Date: Sat, 30 Apr 2022 13:35:31 +0200
Jin Choi <jsc <at> alum.mit.edu> writes:

> Sigh. I’m sorry, that patch got horribly deformed. Here it is as an
> attachment:

Makes sense to me, and after testing a bit, it seems to work well, so
I've pushed it to Emacs 29.

This change was small enough to apply without assigning copyright to the
FSF, but for future patches you want to submit, it might make sense to
get the paperwork started now, so that subsequent patches can be applied
speedily. Would you be willing to sign such paperwork?

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




bug marked as fixed in version 29.1, send any further explanations to 55174 <at> debbugs.gnu.org and Jin Choi <jsc <at> alum.mit.edu> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sat, 30 Apr 2022 11:36:02 GMT) Full text and rfc822 format available.

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

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

Previous Next


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