unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Jack Kamm <jackkamm@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: [PATCH] python.el: Returning inferior Python output without inhibiting it
Date: Sun, 01 Oct 2023 13:23:29 -0700	[thread overview]
Message-ID: <87jzs6hz26.fsf@gmail.com> (raw)
In-Reply-To: <83fs2u8ehy.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 550 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

> I think instead of renaming (which then requires us to add an obsolete
> alias), it would be better to leave python-shell-send-string-no-output
> available, but just make it a thin wrapper around
> python-shell-send-string-return-output (which I would suggest to name
> python-shell-send-string-maybe-output instead).  This way, we don't
> annoy Lisp programs that use python-shell-send-string-no-output with
> obsolescence warnings.

Thanks for the feedback -- I've updated the patch accordingly (attached).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-python.el-Function-to-return-output-without-necessar.patch --]
[-- Type: text/x-patch, Size: 4948 bytes --]

From c071d5a531a6f17e229b0f4a9239f0f054321bc9 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
Date: Sat, 30 Sep 2023 21:04:12 -0700
Subject: [PATCH] python.el: Function to return output without necessarily
 inhibiting

*
lisp/progmodes/python.el (python-shell-output-filter-inhibit-output):
Variable to determine whether output currently inhibited.
(python-shell-send-string): Check
`python-shell-output-filter-inhibit-output' instead of
`python-shell-output-filter-in-progress' to determine whether output
is inhibited.
(python-shell-output-filter): Set
`python-shell-output-filter-inhibit-output' to nil after reaching end
of output.
(python-shell-send-string-no-output): Turned into a thin wrapper
around `python-shell-send-string-maybe-output'.
(python-shell-send-string-maybe-output): Adapted from
`python-shell-send-string-no-output'.  Add function argument on
whether output should be inhibited, and set
`python-shell-output-filter-inhibit-output' accordingly.  If not
inhibited, then the original filter function is run in addition to
`python-shell-output-filter', and `comint-last-prompt' is not reset
after evaluation (since the original output filter will set it
approriately).
---
 lisp/progmodes/python.el | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d3cb5a77e22..30d0416d035 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2736,6 +2736,7 @@ python-shell-dedicated
 
 (defvar python-shell-output-filter-in-progress nil)
 (defvar python-shell-output-filter-buffer nil)
+(defvar python-shell-output-filter-inhibit-output nil)
 
 (defmacro python-shell--add-to-path-with-priority (pathvar paths)
   "Modify PATHVAR and ensure PATHS are added only once at beginning."
@@ -3750,7 +3751,7 @@ python-shell-send-string
                       (python-shell--encode-string string)
                       (python-shell--encode-string (or (buffer-file-name)
                                                        "<string>")))))
-    (unless python-shell-output-filter-in-progress
+    (unless python-shell-output-filter-inhibit-output
       (with-current-buffer (process-buffer process)
         (save-excursion
           (goto-char (process-mark process))
@@ -3780,6 +3781,7 @@ python-shell-output-filter
     ;; Output ends when `python-shell-output-filter-buffer' contains
     ;; the prompt attached at the end of it.
     (setq python-shell-output-filter-in-progress nil
+          python-shell-output-filter-inhibit-output nil
           python-shell-output-filter-buffer
           (substring python-shell-output-filter-buffer
                      0 (match-beginning 0)))
@@ -3795,12 +3797,21 @@ python-shell-output-filter
 (defun python-shell-send-string-no-output (string &optional process)
   "Send STRING to PROCESS and inhibit output.
 Return the output."
+  (python-shell-send-string-maybe-output string process))
+
+(defun python-shell-send-string-maybe-output (string &optional process show-output)
+  "Send STRING to PROCESS and return the output.
+Inhibit printing the output unless SHOW-OUTPUT is non-nil."
   (or process (setq process (python-shell-get-process-or-error)))
-  (cl-letf* (((process-filter process)
+  (cl-letf* ((original-filter-fn (process-filter process))
+             ((process-filter process)
               (lambda (_proc str)
                 (with-current-buffer (process-buffer process)
-                  (python-shell-output-filter str))))
+                  (python-shell-output-filter str))
+                (when show-output
+                  (funcall original-filter-fn process str))))
              (python-shell-output-filter-in-progress t)
+             (python-shell-output-filter-inhibit-output (not show-output))
              (inhibit-quit t)
              (buffer (process-buffer process))
              (last-prompt (cond ((boundp 'comint-last-prompt-overlay)
@@ -3812,13 +3823,15 @@ python-shell-send-string-no-output
      (with-local-quit
        (unwind-protect
            (python-shell-send-string string process)
-         (when (not (null last-prompt))
-           (with-current-buffer buffer
-             (set last-prompt last-prompt-value))))
+         (unless show-output
+           (when (not (null last-prompt))
+             (with-current-buffer buffer
+               (set last-prompt last-prompt-value)))))
        (while python-shell-output-filter-in-progress
          ;; `python-shell-output-filter' takes care of setting
-         ;; `python-shell-output-filter-in-progress' to NIL after it
-         ;; detects end of output.
+         ;; `python-shell-output-filter-in-progress' and
+         ;; `python-shell-output-filter-inhibit-output' to NIL after
+         ;; it detects end of output.
          (accept-process-output process))
        (prog1
            python-shell-output-filter-buffer
-- 
2.42.0


  reply	other threads:[~2023-10-01 20:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-01 16:05 [PATCH] python.el: Returning inferior Python output without inhibiting it Jack Kamm
2023-10-01 17:00 ` Eli Zaretskii
2023-10-01 20:23   ` Jack Kamm [this message]
2023-10-02  5:54     ` Eli Zaretskii
2023-10-02 12:42       ` kobarity
2023-10-02 16:10         ` Eli Zaretskii
2023-10-16  0:27           ` Jack Kamm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87jzs6hz26.fsf@gmail.com \
    --to=jackkamm@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).