diff options
| author | kobarity | 2022-12-22 23:08:40 +0900 |
|---|---|---|
| committer | Eli Zaretskii | 2022-12-31 10:14:28 +0200 |
| commit | eee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc (patch) | |
| tree | 3643b7b2de10f7ce10e298c65cdc90dbe6bc7e6d | |
| parent | bfdad6c4e5ce95025f1761a404b762589d126505 (diff) | |
| download | emacs-eee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc.tar.gz emacs-eee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc.zip | |
Fix python-shell-buffer-substring when retrieving a single statement
* lisp/progmodes/python.el (python-shell-buffer-substring): Do not add
"if True:" line when retrieving a single statement.
(python-shell-send-region): Add a reference to
`python-shell-buffer-substring' in docstring.
* test/lisp/progmodes/python-tests.el (python-shell-buffer-substring-13)
(python-shell-buffer-substring-14, python-shell-buffer-substring-15)
(python-shell-buffer-substring-16, python-shell-buffer-substring-17):
New tests. (Bug#60142)
| -rw-r--r-- | lisp/progmodes/python.el | 45 | ||||
| -rw-r--r-- | test/lisp/progmodes/python-tests.el | 64 |
2 files changed, 95 insertions, 14 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 07f86d31551..641b4ccff23 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -3736,19 +3736,35 @@ the python shell: | |||
| 3736 | appending extra empty lines so tracebacks are correct. | 3736 | appending extra empty lines so tracebacks are correct. |
| 3737 | 3. When the region sent is a substring of the current buffer, a | 3737 | 3. When the region sent is a substring of the current buffer, a |
| 3738 | coding cookie is added. | 3738 | coding cookie is added. |
| 3739 | 4. Wraps indented regions under an \"if True:\" block so the | 3739 | 4. When the region consists of a single statement, leading |
| 3740 | interpreter evaluates them correctly." | 3740 | whitespaces will be removed. Otherwise, wraps indented |
| 3741 | (let* ((start (save-excursion | 3741 | regions under an \"if True:\" block so the interpreter |
| 3742 | ;; If we're at the start of the expression, and | 3742 | evaluates them correctly." |
| 3743 | ;; there's just blank space ahead of it, then expand | 3743 | (let* ((single-p (save-restriction |
| 3744 | ;; the region to include the start of the line. | 3744 | (narrow-to-region start end) |
| 3745 | ;; This makes things work better with the rest of | 3745 | (= (progn |
| 3746 | ;; the data we're sending over. | 3746 | (goto-char start) |
| 3747 | (python-nav-beginning-of-statement)) | ||
| 3748 | (progn | ||
| 3749 | (goto-char end) | ||
| 3750 | (python-nav-beginning-of-statement))))) | ||
| 3751 | (start (save-excursion | ||
| 3752 | ;; If we're at the start of the expression, and if | ||
| 3753 | ;; the region consists of a single statement, then | ||
| 3754 | ;; remove leading whitespaces, else if there's just | ||
| 3755 | ;; blank space ahead of it, then expand the region | ||
| 3756 | ;; to include the start of the line. This makes | ||
| 3757 | ;; things work better with the rest of the data | ||
| 3758 | ;; we're sending over. | ||
| 3747 | (goto-char start) | 3759 | (goto-char start) |
| 3748 | (if (string-blank-p | 3760 | (if single-p |
| 3749 | (buffer-substring (line-beginning-position) start)) | 3761 | (progn |
| 3750 | (line-beginning-position) | 3762 | (skip-chars-forward "[:space:]" end) |
| 3751 | start))) | 3763 | (point)) |
| 3764 | (if (string-blank-p | ||
| 3765 | (buffer-substring (line-beginning-position) start)) | ||
| 3766 | (line-beginning-position) | ||
| 3767 | start)))) | ||
| 3752 | (substring (buffer-substring-no-properties start end)) | 3768 | (substring (buffer-substring-no-properties start end)) |
| 3753 | (starts-at-point-min-p (save-restriction | 3769 | (starts-at-point-min-p (save-restriction |
| 3754 | (widen) | 3770 | (widen) |
| @@ -3772,7 +3788,7 @@ the python shell: | |||
| 3772 | (python-mode) | 3788 | (python-mode) |
| 3773 | (when fillstr | 3789 | (when fillstr |
| 3774 | (insert fillstr)) | 3790 | (insert fillstr)) |
| 3775 | (when (not toplevel-p) | 3791 | (when (and (not single-p) (not toplevel-p)) |
| 3776 | (forward-line -1) | 3792 | (forward-line -1) |
| 3777 | (insert "if True:\n") | 3793 | (insert "if True:\n") |
| 3778 | (delete-region (point) (line-end-position))) | 3794 | (delete-region (point) (line-end-position))) |
| @@ -3816,7 +3832,8 @@ code inside blocks delimited by \"if __name__== \\='__main__\\=':\". | |||
| 3816 | When called interactively SEND-MAIN defaults to nil, unless it's | 3832 | When called interactively SEND-MAIN defaults to nil, unless it's |
| 3817 | called with prefix argument. When optional argument MSG is | 3833 | called with prefix argument. When optional argument MSG is |
| 3818 | non-nil, forces display of a user-friendly message if there's no | 3834 | non-nil, forces display of a user-friendly message if there's no |
| 3819 | process running; defaults to t when called interactively." | 3835 | process running; defaults to t when called interactively. The |
| 3836 | substring to be sent is retrieved using `python-shell-buffer-substring'." | ||
| 3820 | (interactive | 3837 | (interactive |
| 3821 | (list (region-beginning) (region-end) current-prefix-arg t)) | 3838 | (list (region-beginning) (region-end) current-prefix-arg t)) |
| 3822 | (let* ((string (python-shell-buffer-substring start end (not send-main) | 3839 | (let* ((string (python-shell-buffer-substring start end (not send-main) |
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 17d6d8aa706..930234a12f2 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el | |||
| @@ -4456,6 +4456,70 @@ def foo(): | |||
| 4456 | (point-max)) | 4456 | (point-max)) |
| 4457 | "# -*- coding: utf-8 -*-\n\nif True:\n # Whitespace\n\n print ('a')\n\n")))) | 4457 | "# -*- coding: utf-8 -*-\n\nif True:\n # Whitespace\n\n print ('a')\n\n")))) |
| 4458 | 4458 | ||
| 4459 | (ert-deftest python-shell-buffer-substring-13 () | ||
| 4460 | "Check substring from indented single statement." | ||
| 4461 | (python-tests-with-temp-buffer | ||
| 4462 | " | ||
| 4463 | def foo(): | ||
| 4464 | a = 1 | ||
| 4465 | " | ||
| 4466 | (should (string= (python-shell-buffer-substring | ||
| 4467 | (python-tests-look-at "a = 1") | ||
| 4468 | (pos-eol)) | ||
| 4469 | "# -*- coding: utf-8 -*-\n\na = 1")))) | ||
| 4470 | |||
| 4471 | (ert-deftest python-shell-buffer-substring-14 () | ||
| 4472 | "Check substring from indented single statement spanning multiple lines." | ||
| 4473 | (python-tests-with-temp-buffer | ||
| 4474 | " | ||
| 4475 | def foo(): | ||
| 4476 | a = \"\"\"Some | ||
| 4477 | string\"\"\" | ||
| 4478 | " | ||
| 4479 | (should (string= (python-shell-buffer-substring | ||
| 4480 | (python-tests-look-at "a = \"\"\"Some") | ||
| 4481 | (pos-eol 2)) | ||
| 4482 | "# -*- coding: utf-8 -*-\n\na = \"\"\"Some\n string\"\"\"")))) | ||
| 4483 | |||
| 4484 | (ert-deftest python-shell-buffer-substring-15 () | ||
| 4485 | "Check substring from partial statement." | ||
| 4486 | (python-tests-with-temp-buffer | ||
| 4487 | " | ||
| 4488 | def foo(): | ||
| 4489 | a = 1 | ||
| 4490 | " | ||
| 4491 | (should (string= (python-shell-buffer-substring | ||
| 4492 | (python-tests-look-at " a = 1") | ||
| 4493 | (python-tests-look-at " = 1")) | ||
| 4494 | "# -*- coding: utf-8 -*-\n\na")))) | ||
| 4495 | |||
| 4496 | (ert-deftest python-shell-buffer-substring-16 () | ||
| 4497 | "Check substring from partial statement." | ||
| 4498 | (python-tests-with-temp-buffer | ||
| 4499 | " | ||
| 4500 | def foo(): | ||
| 4501 | a = 1 | ||
| 4502 | " | ||
| 4503 | (should (string= (python-shell-buffer-substring | ||
| 4504 | (python-tests-look-at "1") | ||
| 4505 | (1+ (point))) | ||
| 4506 | "# -*- coding: utf-8 -*-\n\n1")))) | ||
| 4507 | |||
| 4508 | (ert-deftest python-shell-buffer-substring-17 () | ||
| 4509 | "Check substring from multiline string." | ||
| 4510 | (python-tests-with-temp-buffer | ||
| 4511 | " | ||
| 4512 | def foo(): | ||
| 4513 | s = \"\"\" | ||
| 4514 | a = 1 | ||
| 4515 | b = 2 | ||
| 4516 | \"\"\" | ||
| 4517 | " | ||
| 4518 | (should (string= (python-shell-buffer-substring | ||
| 4519 | (python-tests-look-at "a = 1") | ||
| 4520 | (python-tests-look-at "\"\"\"")) | ||
| 4521 | "# -*- coding: utf-8 -*-\n\nif True:\n a = 1\n b = 2\n\n")))) | ||
| 4522 | |||
| 4459 | 4523 | ||
| 4460 | 4524 | ||
| 4461 | ;;; Shell completion | 4525 | ;;; Shell completion |