all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 49822@debbugs.gnu.org
Subject: bug#49822: [RFC PATCH] python-shell-send functions show no output
Date: Sat, 28 Aug 2021 11:28:16 +0200	[thread overview]
Message-ID: <87sfytex5b.fsf_-_@gmail.com> (raw)
In-Reply-To: <87h7fc1enm.fsf@gnus.org> (Lars Ingebrigtsen's message of "Thu, 26 Aug 2021 16:09:17 +0200")

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

On Thu, 26 Aug 2021 at 16:09, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> If you could fix this (i.e., not use temporary files), that would be
> very nice.

All right, I'm attaching a proposal for 'python-shell-send-string'
(affecting also all other 'python-shell-send-' functions except
'python-shell-send-buffer').

The main user-visible change is that now a result is printed in the
shell in all possible cases, i.e.:

1. If the string being evaluated is a one-line expression.
2. If the string being evaluated is a multiline expression such as
   "(1+\n1)".
3. The string being evaluated is contains multiple statements, the last
   of which is an expression.  E.g., "x=1\nx".

Currently, only case 1 prints a result (this is the actual topic of this
bug).

By avoiding to use 'python-shell-send-file', the following additional
issues are fixed:

1. The plumbing code used to execute the user's code could crop up in a
   stack trace (Bug#32042).  This is now almost completely fixed (the
   only exception I see is in the plain python3 interpreter, where a
   not-too-annoying mention to __PYTHON_EL_eval still appears.)
2. Sending temporary files over Tramp has a noticeable delay (and lots
   of echo area messages).  In particular, completion is now faster on
   remote Python shells.
3. Some global symbols (os, codecs, etc.) used to be redefined; some
   others (compile, etc.) used to be assumed to not have been redefined
   by the user.  We now only assume this is the case for 'exec' (because
   it's a keyword in Python 2).

Note, however, that 'python-shell-send-file' remains unchanged.

This is a reasonably sensitive change and it would be good to collect
feedback from others before merging it (a properly formatted patch will
then follow).  In particular, since I'm not an expert in Python's
internals, there might be some possible refinements to the
__PYTHON_EL_eval function.  Is this perhaps a better topic for the
emacs-devel list?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Improve-python-shell-send-functions.patch --]
[-- Type: text/x-patch, Size: 4613 bytes --]

From 10b474c9f82f13b3ee9c451a6aea0d3660041095 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 28 Aug 2021 11:16:40 +0200
Subject: [PATCH] Improve python-shell-send functions

---
 lisp/progmodes/python.el | 64 +++++++++++++++++++++++++++++++---------
 1 file changed, 50 insertions(+), 14 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d5209d8d2f..bed709bb65 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3081,6 +3081,45 @@ python-shell--save-temp-file
       (delete-trailing-whitespace))
     temp-file-name))
 
+(defvar python-shell-eval-setup-code
+  "\
+def __PYTHON_EL_eval(source, filename):
+    import ast, sys
+    if sys.version_info[0] == 2:
+        from __builtin__ import compile, eval, globals
+    else:
+        from builtins import compile, eval, globals
+    sys.stdout.write('\\n')
+    try:
+        p, e = ast.parse(source, filename), None
+    except SyntaxError:
+        t, v, tb = sys.exc_info()
+        sys.excepthook(t, v, tb.tb_next)
+        return
+    if p.body and isinstance(p.body[-1], ast.Expr):
+        e = p.body.pop()
+    try:
+        g = globals()
+        exec(compile(p, filename, 'exec'), g, g)
+        if e:
+            return eval(compile(ast.Expression(e.value), filename, 'eval'), g, g)
+    except Exception:
+        t, v, tb = sys.exc_info()
+        sys.excepthook(t, v, tb.tb_next)"
+  "Code used to evaluate statements in inferior Python processes.")
+
+(defalias 'python-shell--encode-string
+  (let ((fun (if (and (fboundp 'json-serialize)
+                      (>= emacs-major-version 28))
+                 'json-serialize
+               (require 'json)
+               'json-encode-string)))
+    (lambda (text)
+      (if (stringp text)
+          (funcall fun text)
+        (signal 'wrong-type-argument (list 'stringp text)))))
+  "Encode TEXT as a valid Python string.")
+
 (defun python-shell-send-string (string &optional process msg)
   "Send STRING to inferior Python PROCESS.
 When optional argument MSG is non-nil, forces display of a
@@ -3088,16 +3127,12 @@ python-shell-send-string
 t when called interactively."
   (interactive
    (list (read-string "Python command: ") nil t))
-  (let ((process (or process (python-shell-get-process-or-error msg))))
-    (if (string-match ".\n+." string)   ;Multiline.
-        (let* ((temp-file-name (with-current-buffer (process-buffer process)
-                                 (python-shell--save-temp-file string)))
-               (file-name (or (buffer-file-name) temp-file-name)))
-          (python-shell-send-file file-name process temp-file-name t))
-      (comint-send-string process string)
-      (when (or (not (string-match "\n\\'" string))
-                (string-match "\n[ \t].*\n?\\'" string))
-        (comint-send-string process "\n")))))
+  (comint-send-string
+   (or process (python-shell-get-process-or-error msg))
+   (format "exec(%s);__PYTHON_EL_eval(%s, %s)\n"
+           (python-shell--encode-string python-shell-eval-setup-code)
+           (python-shell--encode-string string)
+           (python-shell--encode-string (or (buffer-file-name) "<string>")))))
 
 (defvar python-shell-output-filter-in-progress nil)
 (defvar python-shell-output-filter-buffer nil)
@@ -3139,7 +3174,8 @@ python-shell-send-string-no-output
         (inhibit-quit t))
     (or
      (with-local-quit
-       (python-shell-send-string string process)
+       (comint-send-string process (format "exec(%s)\n"
+                                           (python-shell--encode-string string)))
        (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
@@ -3362,7 +3398,8 @@ python-shell-send-file
          (temp-file-name (when temp-file-name
                            (file-local-name (expand-file-name
                                              temp-file-name)))))
-    (python-shell-send-string
+    (comint-send-string
+     process
      (format
       (concat
        "import codecs, os;"
@@ -3372,8 +3409,7 @@ python-shell-send-file
        (when (and delete temp-file-name)
          (format "os.remove('''%s''');" temp-file-name))
        "exec(compile(__code, '''%s''', 'exec'));")
-      (or temp-file-name file-name) encoding encoding file-name)
-     process)))
+      (or temp-file-name file-name) encoding encoding file-name))))
 
 (defun python-shell-switch-to-shell (&optional msg)
   "Switch to inferior Python process buffer.
-- 
2.31.1


  reply	other threads:[~2021-08-28  9:28 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210903122828.16890.65271@vcs0.savannah.gnu.org>
     [not found] ` <20210903122829.EAAC220B71@vcs0.savannah.gnu.org>
2021-09-03 23:04   ` master e32c7d2: Change Python eval to send directly instead of using temporary files Stefan Monnier
2021-09-04  9:49     ` bug#49822: " Augusto Stoffel
2021-09-04 13:52       ` Stefan Monnier
2021-09-04 13:52       ` bug#49822: " Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-09-05  6:13       ` Barton, Mark
2021-09-05  6:13       ` Barton, Mark
2021-09-05  7:46         ` bug#49822: " Augusto Stoffel
2021-09-05  8:10         ` Augusto Stoffel
2021-09-05 17:18           ` bug#49822: " Mark Barton
2021-09-05 17:33             ` Mark Barton
2021-09-05 17:46             ` Augusto Stoffel
2021-09-05 17:46             ` bug#49822: " Augusto Stoffel
2021-09-05  8:10         ` Augusto Stoffel
2021-09-05  7:41       ` Lars Ingebrigtsen
2021-09-05  7:41       ` bug#32042: " Lars Ingebrigtsen
2021-09-05  7:41       ` Lars Ingebrigtsen
2021-09-05 16:36       ` Andreas Schwab
2021-09-05 18:40         ` Augusto Stoffel
2021-09-06  7:43           ` Michael Albinus
2021-09-06  7:43           ` Michael Albinus
2021-09-06  8:40             ` Andreas Schwab
2021-09-06  8:40             ` Andreas Schwab
2021-09-06 11:23               ` Michael Albinus
2021-09-06 11:53                 ` Andreas Schwab
2021-08-02 14:32                   ` bug#49822: 28.0.50; python-shell-send functions show no output dalanicolai
2021-08-02 15:11                     ` bug#49822: forgot link (and mentioned wrong user) dalanicolai
2021-08-02 15:17                     ` bug#49822: 28.0.50; python-shell-send functions show no output Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-04  7:18                     ` Lars Ingebrigtsen
2021-08-05 10:03                       ` dalanicolai
2021-08-12 14:57                       ` Augusto Stoffel
2021-08-13 11:10                         ` Lars Ingebrigtsen
2021-08-13 16:44                           ` Andreas Röhler
2021-08-14  9:29                             ` Augusto Stoffel
2021-08-15  6:41                               ` Andreas Röhler
2021-08-26  8:27                                 ` Augusto Stoffel
2021-08-26 14:09                                   ` Lars Ingebrigtsen
2021-08-28  9:28                                     ` Augusto Stoffel [this message]
2021-08-28 15:41                                       ` bug#49822: [RFC PATCH] " Lars Ingebrigtsen
2021-09-03  8:02                                         ` bug#49822: [PATCH] " Augusto Stoffel
2021-09-03 12:28                                           ` Lars Ingebrigtsen
2021-09-06 22:15                     ` bug#49822: master e32c7d2: Change Python eval to send directly instead of using temporary files Andy Moreton
2021-09-07  7:18                       ` Andreas Schwab
2021-09-06 12:00                   ` Michael Albinus
2021-09-06 16:08                     ` Augusto Stoffel
2021-09-06 16:08                     ` Augusto Stoffel
2021-09-07 17:37                   ` Augusto Stoffel
2021-09-07 17:48                     ` Eli Zaretskii
2021-09-07 17:59                       ` Augusto Stoffel
2021-09-07 17:59                       ` Augusto Stoffel
2021-09-07 18:19                         ` Eli Zaretskii
2021-09-07 18:13                       ` Augusto Stoffel
2021-09-07 18:31                         ` Eli Zaretskii
2021-09-07 19:00                           ` Augusto Stoffel
2021-09-07 19:16                             ` Eli Zaretskii
2021-09-08  7:02                               ` Michael Albinus
2021-09-08  7:02                               ` Michael Albinus
2021-09-07 19:16                             ` Eli Zaretskii
2021-09-07 18:31                         ` Eli Zaretskii
2021-09-07 18:13                       ` Augusto Stoffel
2021-09-07 17:48                     ` Eli Zaretskii
2021-09-08  3:17                     ` Barton, Mark
2021-09-08  5:09                       ` Augusto Stoffel
2021-09-08  7:50                     ` Lars Ingebrigtsen
2021-09-08  7:50                     ` Lars Ingebrigtsen
2021-09-08 14:05                       ` Augusto Stoffel
2021-09-08 14:05                       ` Augusto Stoffel
2021-09-09 13:48                         ` Lars Ingebrigtsen
2021-09-06 11:23               ` Michael Albinus

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

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

  git send-email \
    --in-reply-to=87sfytex5b.fsf_-_@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=49822@debbugs.gnu.org \
    --cc=larsi@gnus.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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.