From 6295d7abdd43ed6611cc3dd0682d56265cbc4528 Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Sat, 1 Jul 2023 22:12:43 +0200 Subject: [PATCH] Improve Python imports management commands * lisp/progmodes/python.el (python--list-imports): Handle import errors. (python--do-isort): Specialize error message. (Bug#64406) --- lisp/progmodes/python.el | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 50d712ebb0c..4291ab03ca6 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -6446,8 +6446,14 @@ REPORT-FN is Flymake's callback function." ;;; Import management (defconst python--list-imports "\ -from isort import find_imports_in_stream, find_imports_in_paths -from sys import argv, stdin +from sys import argv, exit, stdin + +try: + from isort import find_imports_in_stream, find_imports_in_paths +except ModuleNotFoundError: + exit(1) +except ImportError: + exit(2) query, files, result = argv[1] or None, argv[2:], {} @@ -6501,9 +6507,13 @@ recursively." (or name "") (mapcar #'file-local-name source))))) lines) - (unless (eq 0 status) + (cond + ((eq 1 status) (error "%s exited with status %s (maybe isort is missing?)" python-interpreter status)) + ((eq 2 status) + (error "%s exited with status %s (maybe isort version is <5.7.0?)" + python-interpreter status))) (goto-char (point-min)) (while (not (eobp)) (push (buffer-substring-no-properties (point) (pos-eol)) @@ -6546,9 +6556,13 @@ Return non-nil if the buffer was actually modified." nil (list temp nil) nil "-m" "isort" "-" args)) (tick (buffer-chars-modified-tick))) - (unless (eq 0 status) + (cond + ((eq 1 status) (error "%s exited with status %s (maybe isort is missing?)" python-interpreter status)) + ((eq 2 status) + (error "%s exited with status %s (maybe isort version is <5.7.0?)" + python-interpreter status))) (replace-buffer-contents temp) (not (eq tick (buffer-chars-modified-tick))))))))) -- 2.39.2