From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matthias Meulien Newsgroups: gmane.emacs.bugs Subject: bug#64406: [PATCH] Improve commands to manage Python imports Date: Sun, 09 Jul 2023 08:09:17 +0200 Message-ID: <87bkgl39bm.fsf@gmail.com> References: <874jmnbd5v.fsf@gmail.com> <87pm534puk.fsf@gmail.com> <87lefr399j.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1273"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 64406@debbugs.gnu.org To: Augusto Stoffel Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jul 09 08:10:21 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qINci-0000Af-Sw for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 09 Jul 2023 08:10:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qINcS-00085w-FD; Sun, 09 Jul 2023 02:10:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qINcQ-00085k-Q1 for bug-gnu-emacs@gnu.org; Sun, 09 Jul 2023 02:10:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qINcQ-00044B-GC for bug-gnu-emacs@gnu.org; Sun, 09 Jul 2023 02:10:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qINcQ-0006iE-CV for bug-gnu-emacs@gnu.org; Sun, 09 Jul 2023 02:10:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Matthias Meulien Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 09 Jul 2023 06:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64406 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 64406-submit@debbugs.gnu.org id=B64406.168888296825748 (code B ref 64406); Sun, 09 Jul 2023 06:10:02 +0000 Original-Received: (at 64406) by debbugs.gnu.org; 9 Jul 2023 06:09:28 +0000 Original-Received: from localhost ([127.0.0.1]:45641 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qINbr-0006hD-VL for submit@debbugs.gnu.org; Sun, 09 Jul 2023 02:09:28 -0400 Original-Received: from mail-wm1-f51.google.com ([209.85.128.51]:56816) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qINbp-0006h0-85 for 64406@debbugs.gnu.org; Sun, 09 Jul 2023 02:09:26 -0400 Original-Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-3fbc244d307so39439485e9.1 for <64406@debbugs.gnu.org>; Sat, 08 Jul 2023 23:09:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688882959; x=1691474959; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=AZ1pVYdKIMbtBLar+mQOR5IK7YUHjRnZ22umTGTB7Dg=; b=p7fmzWN4SWo7W1pQzWnSVxDBlL1SErDASe73Y2om5g3r9b186FZOLwnqseWpgE6jYu oD60NkLMmQnArTrT5jAUVBDtL0MQthhlVqzBuHOCQewRsI4GFmW3YbcS4SLE/KQfLufY UYZ9r6dMrjktplP2vdHHU8gxA29jciMfuj6Bf69cXpLl5cqzCokTM0N4ngS5l9kriF+e VrVYRHwrmQhtrl51vQGbjhL+udpocw0bsMQp04L9LaCwfalo26fvRZfShJeZaCP6V1OI 1++Ma0XZsbpT2Ng0CRmhMwA1msilSl4N9GncHeJRNzfluGxb8Fs/PNLveUHj0TK+Xs2a w/gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688882959; x=1691474959; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AZ1pVYdKIMbtBLar+mQOR5IK7YUHjRnZ22umTGTB7Dg=; b=MPZS5BKbkpdV01J1EYIiV5aQC1FSVfrJLCgCnS4ANtkz7viFtwu0/cGpMKL2nKEVSX 7OeewilThWDsW4A0cJLuUOiE9y+kfextbNmXnfzDmmniXqhUhVntL5Ami+BSpE6bZ99o jBBtUPkDN0GVCBigY8rmhPXj+ZvBsvIi8qGFNpxM/vm6iIUu5eI80Qi17XzxHKCtn4CS Xz3KLl3Zxg1OFeP09k8DopqWqehwN0LNj1Gk1BJgbyylm34ZQp5DFDeKTb2pAKS79/69 pkyyQke/pA73VM4zSOzG/EpGtujlbn8j2s3mSZkyn8iIwwweQmk9HwtMSdgG4eUwQUiV sLOw== X-Gm-Message-State: ABy/qLZZoxx2cBfBsmcuDWTJ/O1jRGQXk1GHnXAXngIme8QUtN51AQZe vsM9oO/qLKIU+WffCTboniMqQoB8Ova/Pw== X-Google-Smtp-Source: APBJJlFZOUtNey27VDAN4O85idj5Fxrg/SC8RRKm+bNvfW085XOI1nO/3aN+HCMjOSgMqecIVk8tdA== X-Received: by 2002:adf:ec90:0:b0:314:545f:6e94 with SMTP id z16-20020adfec90000000b00314545f6e94mr8498490wrn.65.1688882958810; Sat, 08 Jul 2023 23:09:18 -0700 (PDT) Original-Received: from carbon.localdomain ([2a01:e0a:245:c850:98f5:429a:aa8e:95bb]) by smtp.gmail.com with ESMTPSA id x4-20020a5d60c4000000b003112ab916cdsm8428209wrt.73.2023.07.08.23.09.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jul 2023 23:09:18 -0700 (PDT) In-Reply-To: <87lefr399j.fsf@gmail.com> (Matthias Meulien's message of "Fri, 07 Jul 2023 19:46:00 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:264814 Archived-At: --=-=-= Content-Type: text/plain Matthias Meulien writes: > Augusto Stoffel writes: >> (...) >> This change implies that the "success" branch may run if the exit code >> is nonzero (without knowing all the isort internals, it can't be >> excluded that an exit code > 2 is used somewhere). I suggest instead a >> (pcase status ...) to construct the " (maybe... ?)" segment of the error >> message. > > Thanks, I'll fix this. >> We could also take this opportunity to distinguish between random >> exceptions happening in the script (which likely leads to exit code 1) >> and the ModuleNotFoundError case. > > Good point, I'll improve this too. Here are two patches. The first one revert part of the original patch where I introduced a useless detailed check of isort exit status itself. The second one relates to your remarks: It shift the custom exit status of the Python script and make sure that an error is reported assoon as exit code >0. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Fix-Improve-Python-imports-management-commands.patch >From 2463a33c5c3b8eb38da8f064459900159330bb47 Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Sun, 9 Jul 2023 07:45:57 +0200 Subject: [PATCH 1/2] Fix "Improve Python imports management commands" * lisp/progmodes/python.el (python--do-isort): The isort library don't check for its version itself! --- lisp/progmodes/python.el | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 4291ab03ca6..8ce10621ed3 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -6556,13 +6556,9 @@ python--do-isort nil (list temp nil) nil "-m" "isort" "-" args)) (tick (buffer-chars-modified-tick))) - (cond - ((eq 1 status) + (unless (eq 0 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 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0002-Improve-Python-imports-management-commands.patch >From 65e5229e62fffc96251e28b90a56c8d79c53703c Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Sun, 9 Jul 2023 07:48:57 +0200 Subject: [PATCH 2/2] Improve Python imports management commands * lisp/progmodes/python.el (python--list-imports): Prefer to use an exit status >1. (python--list-imports-check-status): Check Python script status. --- lisp/progmodes/python.el | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 8ce10621ed3..ea68d7ee59d 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -6451,9 +6451,9 @@ python--list-imports try: from isort import find_imports_in_stream, find_imports_in_paths except ModuleNotFoundError: - exit(1) -except ImportError: exit(2) +except ImportError: + exit(3) query, files, result = argv[1] or None, argv[2:], {} @@ -6484,6 +6484,17 @@ python--import-sources (project-files proj)) (list default-directory))) +(defun python--list-imports-check-status (status) + (unless (eq 0 status) + (let* ((details + (cond + ((eq 2 status) " (maybe isort is missing?)") + ((eq 3 status) " (maybe isort version is less than 5.7.0?)") + (t ""))) + (msg + (concat "%s exited with status %s" details))) + (error msg python-interpreter status)))) + (defun python--list-imports (name source) "List all Python imports matching NAME in SOURCE. If NAME is nil, list all imports. SOURCE can be a buffer or a @@ -6507,13 +6518,7 @@ python--list-imports (or name "") (mapcar #'file-local-name source))))) lines) - (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))) + (python--list-imports-check-status status) (goto-char (point-min)) (while (not (eobp)) (push (buffer-substring-no-properties (point) (pos-eol)) -- 2.39.2 --=-=-=--