diff options
| author | Matthias Meulien | 2023-07-13 22:47:01 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2023-07-15 11:24:34 +0300 |
| commit | a047fb8494c203bd8f416e7ee9f77ad8dcb03631 (patch) | |
| tree | 1271321b3a5a40b2df9f18e93090321183bee6d2 /lisp/progmodes/python.el | |
| parent | 300f9d23c13232cf20978aa619c3758ad49fa184 (diff) | |
| download | emacs-a047fb8494c203bd8f416e7ee9f77ad8dcb03631.tar.gz emacs-a047fb8494c203bd8f416e7ee9f77ad8dcb03631.zip | |
Fix "Improve Python imports management commands"
* lisp/progmodes/python.el (python--list-imports): Prefer to
use an exit status >1.
(python--list-imports-check-status): New function to check
status of Python script.
(python--do-isort): Fix wrong status check introduced with
6295d7abdd4. (Bug#64406)
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 4291ab03ca6..a23339a2180 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -6451,9 +6451,9 @@ from sys import argv, exit, stdin | |||
| 6451 | try: | 6451 | try: |
| 6452 | from isort import find_imports_in_stream, find_imports_in_paths | 6452 | from isort import find_imports_in_stream, find_imports_in_paths |
| 6453 | except ModuleNotFoundError: | 6453 | except ModuleNotFoundError: |
| 6454 | exit(1) | ||
| 6455 | except ImportError: | ||
| 6456 | exit(2) | 6454 | exit(2) |
| 6455 | except ImportError: | ||
| 6456 | exit(3) | ||
| 6457 | 6457 | ||
| 6458 | query, files, result = argv[1] or None, argv[2:], {} | 6458 | query, files, result = argv[1] or None, argv[2:], {} |
| 6459 | 6459 | ||
| @@ -6484,6 +6484,17 @@ for key in sorted(result): | |||
| 6484 | (project-files proj)) | 6484 | (project-files proj)) |
| 6485 | (list default-directory))) | 6485 | (list default-directory))) |
| 6486 | 6486 | ||
| 6487 | (defun python--list-imports-check-status (status) | ||
| 6488 | (unless (eq 0 status) | ||
| 6489 | (let* ((details | ||
| 6490 | (cond | ||
| 6491 | ((eq 2 status) " (maybe isort is missing?)") | ||
| 6492 | ((eq 3 status) " (maybe isort version is older than 5.7.0?)") | ||
| 6493 | (t ""))) | ||
| 6494 | (msg | ||
| 6495 | (concat "%s exited with status %s" details))) | ||
| 6496 | (error msg python-interpreter status)))) | ||
| 6497 | |||
| 6487 | (defun python--list-imports (name source) | 6498 | (defun python--list-imports (name source) |
| 6488 | "List all Python imports matching NAME in SOURCE. | 6499 | "List all Python imports matching NAME in SOURCE. |
| 6489 | If NAME is nil, list all imports. SOURCE can be a buffer or a | 6500 | If NAME is nil, list all imports. SOURCE can be a buffer or a |
| @@ -6507,13 +6518,7 @@ recursively." | |||
| 6507 | (or name "") | 6518 | (or name "") |
| 6508 | (mapcar #'file-local-name source))))) | 6519 | (mapcar #'file-local-name source))))) |
| 6509 | lines) | 6520 | lines) |
| 6510 | (cond | 6521 | (python--list-imports-check-status status) |
| 6511 | ((eq 1 status) | ||
| 6512 | (error "%s exited with status %s (maybe isort is missing?)" | ||
| 6513 | python-interpreter status)) | ||
| 6514 | ((eq 2 status) | ||
| 6515 | (error "%s exited with status %s (maybe isort version is <5.7.0?)" | ||
| 6516 | python-interpreter status))) | ||
| 6517 | (goto-char (point-min)) | 6522 | (goto-char (point-min)) |
| 6518 | (while (not (eobp)) | 6523 | (while (not (eobp)) |
| 6519 | (push (buffer-substring-no-properties (point) (pos-eol)) | 6524 | (push (buffer-substring-no-properties (point) (pos-eol)) |
| @@ -6556,13 +6561,9 @@ Return non-nil if the buffer was actually modified." | |||
| 6556 | nil (list temp nil) nil | 6561 | nil (list temp nil) nil |
| 6557 | "-m" "isort" "-" args)) | 6562 | "-m" "isort" "-" args)) |
| 6558 | (tick (buffer-chars-modified-tick))) | 6563 | (tick (buffer-chars-modified-tick))) |
| 6559 | (cond | 6564 | (unless (eq 0 status) |
| 6560 | ((eq 1 status) | ||
| 6561 | (error "%s exited with status %s (maybe isort is missing?)" | 6565 | (error "%s exited with status %s (maybe isort is missing?)" |
| 6562 | python-interpreter status)) | 6566 | python-interpreter status)) |
| 6563 | ((eq 2 status) | ||
| 6564 | (error "%s exited with status %s (maybe isort version is <5.7.0?)" | ||
| 6565 | python-interpreter status))) | ||
| 6566 | (replace-buffer-contents temp) | 6567 | (replace-buffer-contents temp) |
| 6567 | (not (eq tick (buffer-chars-modified-tick))))))))) | 6568 | (not (eq tick (buffer-chars-modified-tick))))))))) |
| 6568 | 6569 | ||