unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Jin Choi <jsc@alum.mit.edu>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 55174@debbugs.gnu.org
Subject: bug#55174: python-shell-send-statement in python.el sends malformed code
Date: Fri, 29 Apr 2022 11:29:26 -0400	[thread overview]
Message-ID: <06D722AC-1D78-4520-9927-9C8504D2C72F@alum.mit.edu> (raw)
In-Reply-To: <87h76cgpfy.fsf@gnus.org>

[-- Attachment #1: Type: text/plain, Size: 3149 bytes --]

On Apr 29, 2022, at 6:21 AM, Lars Ingebrigtsen <larsi@gnus.org> wrote:
> 
> Jin Choi <jsc@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)

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 4374 bytes --]

  reply	other threads:[~2022-04-29 15:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28 19:37 bug#55174: python-shell-send-statement in python.el sends malformed code Jin Choi
2022-04-29 10:21 ` Lars Ingebrigtsen
2022-04-29 15:29   ` Jin Choi [this message]
2022-04-29 15:34     ` Jin Choi
2022-04-30 11:35       ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=06D722AC-1D78-4520-9927-9C8504D2C72F@alum.mit.edu \
    --to=jsc@alum.mit.edu \
    --cc=55174@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).