[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#55174: python-shell-send-statement in python.el sends malformed code
From: |
Jin Choi |
Subject: |
bug#55174: python-shell-send-statement in python.el sends malformed code |
Date: |
Fri, 29 Apr 2022 11:29:26 -0400 |
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)
smime.p7s
Description: S/MIME cryptographic signature