[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 5a10e6377c 1/2: Make Python evaluation work more reliably
From: |
Lars Ingebrigtsen |
Subject: |
master 5a10e6377c 1/2: Make Python evaluation work more reliably |
Date: |
Sat, 30 Apr 2022 07:38:24 -0400 (EDT) |
branch: master
commit 5a10e6377c2b97420e5617f114bb374d67ec1a58
Author: Jin Choi <jsc@alum.mit.edu>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
Make Python evaluation work more reliably
* lisp/progmodes/python.el (python-shell-buffer-substring): Make
more regions valid Python code (bug#55174).
Copyright-paperwork-exempt: yes
---
lisp/progmodes/python.el | 23 +++++++++++++----------
test/lisp/progmodes/python-tests.el | 18 +++---------------
2 files changed, 16 insertions(+), 25 deletions(-)
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index b7914655aa..11ed732d28 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3292,22 +3292,25 @@ the python shell:
(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 --git a/test/lisp/progmodes/python-tests.el
b/test/lisp/progmodes/python-tests.el
index 1a6a7dc176..01b233cc42 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -3503,10 +3503,7 @@ def foo():
(should (string= (python-shell-buffer-substring
(python-tests-look-at "print ('a')")
(point-max))
- "if True:
-
- print ('a')
-"))))
+ "# -*- coding: utf-8 -*-\nif True:\n print
('a')\n\n"))))
(ert-deftest python-shell-buffer-substring-11 ()
"Check substring from partial block and point within indentation."
@@ -3521,10 +3518,7 @@ def foo():
(backward-char 1)
(point))
(point-max))
- "if True:
-
- print ('a')
-"))))
+ "# -*- coding: utf-8 -*-\nif True:\n print
('a')\n\n"))))
(ert-deftest python-shell-buffer-substring-12 ()
"Check substring from partial block and point in whitespace."
@@ -3539,13 +3533,7 @@ def foo():
(should (string= (python-shell-buffer-substring
(python-tests-look-at "# Whitespace")
(point-max))
- "if True:
-
-
- # Whitespace
-
- print ('a')
-"))))
+ "# -*- coding: utf-8 -*-\n\nif True:\n #
Whitespace\n\n print ('a')\n\n"))))