From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: kobarity Newsgroups: gmane.emacs.bugs Subject: bug#68559: [PATCH] Improve Python shell completion Date: Sat, 17 Feb 2024 00:24:29 +0900 Message-ID: References: <6BAFD8BF-8F18-4D9F-9DB3-A185AEF2EBCA@gmail.com> <8B4172D0-FC31-4437-99C4-683C892C5F86@gmail.com> Mime-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: multipart/mixed; boundary="Multipart_Sat_Feb_17_00:24:28_2024-1" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30547"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?Q?Goj=C5=8D?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.0.50 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) Cc: Liu Hui , Eli Zaretskii , 68559@debbugs.gnu.org To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Feb 16 16:27:08 2024 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 1rb07I-0007pM-Jh for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 16 Feb 2024 16:27:08 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rb06y-0002Yd-Lz; Fri, 16 Feb 2024 10:26:49 -0500 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 1rb06v-0002YP-9q for bug-gnu-emacs@gnu.org; Fri, 16 Feb 2024 10:26:45 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rb06t-00069d-Hq for bug-gnu-emacs@gnu.org; Fri, 16 Feb 2024 10:26:44 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rb07C-0007ON-EF for bug-gnu-emacs@gnu.org; Fri, 16 Feb 2024 10:27:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: kobarity Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 16 Feb 2024 15:27:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68559 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 68559-submit@debbugs.gnu.org id=B68559.170809716228331 (code B ref 68559); Fri, 16 Feb 2024 15:27:02 +0000 Original-Received: (at 68559) by debbugs.gnu.org; 16 Feb 2024 15:26:02 +0000 Original-Received: from localhost ([127.0.0.1]:59979 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rb06D-0007Mk-HI for submit@debbugs.gnu.org; Fri, 16 Feb 2024 10:26:02 -0500 Original-Received: from mail-pj1-f47.google.com ([209.85.216.47]:42082) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rb067-0007MU-Bw for 68559@debbugs.gnu.org; Fri, 16 Feb 2024 10:25:59 -0500 Original-Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-296db7e258dso1628387a91.0 for <68559@debbugs.gnu.org>; Fri, 16 Feb 2024 07:25:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708097070; x=1708701870; darn=debbugs.gnu.org; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:from:to:cc:subject:date:message-id:reply-to; bh=GygHsR4l8m18UIgxfZPXyuhzcRjg6xewiLzKncgH53c=; b=JyN3soIdqqHaJfghSQXcqKiX/0nao40UqBHnjRTGJVvX/eGE/LCgEQc9PbzYqQt1qx e77y5Dp+Y5b9Np/4XO5yeNTB/eAaUqHHbYy5wl0pxlNZM5VtgcHH5WswET6rt7ILzR2q 0FAMNhI5mau7B+7MWmLvu87w6gNOWv3GhsQubg7ryJwhhgp0BOnVQ96FklvlElz7/tnD beSfsOPKAhQlYhC5di9x3OINKzkZUIal1DAZ/dbuDtzyXkGc+sAiXSle6fGBAsrO0ZKB LQau7gF+AoQP5kSpwJyvF/5g0bswfcA2q07AnRY9EQSHtaAVTPmBtgJq8GcSIhyz4hh7 AP8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708097070; x=1708701870; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GygHsR4l8m18UIgxfZPXyuhzcRjg6xewiLzKncgH53c=; b=IyTIi6zC3yGPNWVemohzDeORAIpn+yuJ+ZDUtSnzA5Dfs+IGtKdTf7IBFLiLWuk7nh 8639FURmMwCFtzyXoKy767eDQznp9GwdMR8r6W1UpkJLHdjQaGNFrWri5HR9qxV1yn07 T8lW6hF6yiWfPLR4cZ/X7KlytPUk8svniusgePSPDo5cfpROg0vH2dqvLmbYkKGuFnCI T4GCxbggfjiJdRZOzW9Ey0etcLAsn730A5i2lYJ6ksQtjKYw/WnDqwEkQvndQF/Eg4iY xPIlFMdFpDkcS4L4b0t5Jsb5VOKjGgy0Yf2H/NL7in8w5GGp6QhR+46etJKYcaGQ5TCQ UTMA== X-Forwarded-Encrypted: i=1; AJvYcCW9AaOIPSkWp+2Ck1N2NJ+OzK8okv66QAbPqS65w9MoLLm7vHm4HdtJl+/dZ2Kh7NaueKzKafgvRjv3OqK50AFkzjL9sMk= X-Gm-Message-State: AOJu0YxFQMjKsKy1iA0YevW+tDIOvQKLHTiQmhWMt/1bY9s5pbMwqD2a dC2JQL7I9ISoq+v2mJ4hQ2n7BMM1cDItrl6epl2HuYubn7Df4wuY X-Google-Smtp-Source: AGHT+IF8D402eXtsgIqKA9SrlSLCEgr19Ev0tP+SrWqtf1joOSu0txbDksnDU3xQTlUCbUduvVPcvQ== X-Received: by 2002:a17:90a:15d7:b0:298:b9e1:ee9c with SMTP id w23-20020a17090a15d700b00298b9e1ee9cmr7323593pjd.13.1708097069706; Fri, 16 Feb 2024 07:24:29 -0800 (PST) Original-Received: from localhost (58x12x133x161.ap58.ftth.ucom.ne.jp. [58.12.133.161]) by smtp.gmail.com with ESMTPSA id pd6-20020a17090b1dc600b0029930881068sm112083pjb.49.2024.02.16.07.24.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 07:24:29 -0800 (PST) In-Reply-To: <8B4172D0-FC31-4437-99C4-683C892C5F86@gmail.com> 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:280103 Archived-At: --Multipart_Sat_Feb_17_00:24:28_2024-1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Mattias Engdeg=E5rd wrote: >=20 > 16 feb. 2024 kl. 10.34 skrev kobarity : >=20 > > This is caused by the fact that the input is echoed back on MacOS > > Python. >=20 > Right, that bug needs to be fixed as well, but the echo problem existed p= rior to the change that broke the tests. >=20 > An alternative might be to disable the tty echo altogether. If I do it ri= ght after process creation then it has no effect; presumably Python or its = readline module turns on echo just a bit later on. Anyway, running >=20 > import tty > tty.setraw(0) >=20 > in the python shell seems to put it right, with working completion and th= e annoying echo gone. >=20 > > So one workaround would be to remove the echoed back string before > > parsing as JSON. >=20 > Yes, either that or turning off echo in the tty. I made prototype patches for each method. I don't use Mac so it would be helpful if you could try these. 0001-Remove-echoed-back-string-in-python-shell-completion.patch extracts only the last line to exclude echoed back strings. 0001-Set-tty-mode-to-raw-when-setting-up-Inferior-Python.patch sets the Inferior Python tty to raw mode. python-ffap-module-path-1 will no longer need to be skipped on Mac. If it is safe to set tty to raw mode on all UNIX based systems, I prefer this method. By the way, is it necessary to send `python-shell-completion-setup-code' for every completion in `python-shell-completion-get-completions'? To me it seems sufficient to send it once at initialization. --Multipart_Sat_Feb_17_00:24:28_2024-1 Content-Type: application/octet-stream; type=patch; name="0001-Remove-echoed-back-string-in-python-shell-completion.patch" Content-Disposition: attachment; filename="0001-Remove-echoed-back-string-in-python-shell-completion.patch" Content-Transfer-Encoding: 7bit >From 601419eef799d2c68e3789222f5ebe2a8c31af97 Mon Sep 17 00:00:00 2001 From: kobarity Date: Fri, 16 Feb 2024 22:48:19 +0900 Subject: [PATCH] Remove echoed back string in python-shell-completion-get-completions * lisp/progmodes/python.el (python-shell-completion-get-completions): Remove echoed back string. (Bug#68559) --- lisp/progmodes/python.el | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index b7e43f3fc68..a842a498113 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -4624,11 +4624,13 @@ python-shell-completion-get-completions "Get completions of INPUT using PROCESS." (with-current-buffer (process-buffer process) (python--parse-json-array - (python-shell-send-string-no-output - (format "%s\nprint(__PYTHON_EL_get_completions(%s))" - python-shell-completion-setup-code - (python-shell--encode-string input)) - process)))) + (car (last (split-string + (python-shell-send-string-no-output + (format "%s\nprint(__PYTHON_EL_get_completions(%s))" + python-shell-completion-setup-code + (python-shell--encode-string input)) + process) + "[\n\r]+" t)))))) (defun python-shell--get-multiline-input () "Return lines at a multi-line input in Python shell." -- 2.34.1 --Multipart_Sat_Feb_17_00:24:28_2024-1 Content-Type: application/octet-stream; type=patch; name="0001-Set-tty-mode-to-raw-when-setting-up-Inferior-Python.patch" Content-Disposition: attachment; filename="0001-Set-tty-mode-to-raw-when-setting-up-Inferior-Python.patch" Content-Transfer-Encoding: 7bit >From 31738d481333bf6c258a694cf57bce944bce7778 Mon Sep 17 00:00:00 2001 From: kobarity Date: Fri, 16 Feb 2024 22:52:06 +0900 Subject: [PATCH] Set tty mode to raw when setting up Inferior Python * lisp/progmodes/python.el (python-shell-setup-code): New constant. (python-shell-comint-watch-for-first-prompt-output-filter): Send `python-shell-setup-code' to the Inferior Python process. * test/lisp/progmodes/python-tests.el (python-ffap-module-path-1): Eliminate skipping on Mac. (Bug#68559) --- lisp/progmodes/python.el | 11 +++++++++++ test/lisp/progmodes/python-tests.el | 5 ----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index b7e43f3fc68..5501926e69d 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3521,6 +3521,16 @@ python-shell-first-prompt-hook :version "25.1" :type 'hook) +(defconst python-shell-setup-code + "\ +try: + import tty +except ImportError: + pass +else: + tty.setraw(0)" + "Code used to setup the inferior Python processes.") + (defconst python-shell-eval-setup-code "\ def __PYTHON_EL_eval(source, filename): @@ -3586,6 +3596,7 @@ python-shell-comint-watch-for-first-prompt-output-filter (format "exec(%s)\n" (python-shell--encode-string string)))))) ;; Bootstrap: the normal definition of `python-shell-send-string' ;; depends on the Python code sent here. + (python-shell-send-string-no-output python-shell-setup-code) (python-shell-send-string-no-output python-shell-eval-setup-code) (python-shell-send-string-no-output python-shell-eval-file-setup-code)) (with-current-buffer (current-buffer) diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index af6c199b5bd..6c6cd9eee2b 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -5037,11 +5037,6 @@ python-completion-at-point-native-with-eldoc-1 (ert-deftest python-ffap-module-path-1 () (skip-unless (executable-find python-tests-shell-interpreter)) - ;; Skip the test on macOS, since the standard Python installation uses - ;; libedit rather than readline which confuses the running of an inferior - ;; interpreter in this case (see bug#59477 and bug#25753). - (skip-when (eq system-type 'darwin)) - (trace-function 'python-shell-output-filter) (python-tests-with-temp-buffer-with-shell " import abc -- 2.34.1 --Multipart_Sat_Feb_17_00:24:28_2024-1--