From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#49822: 28.0.50; python-shell-send functions show no output Date: Thu, 26 Aug 2021 10:27:43 +0200 Message-ID: <87k0k8wqyo.fsf@gmail.com> References: <855f9eaf1b1d39bb9325d0a88e7f66c0ba0b45d0.camel@gmail.com> <87r1f9fzj4.fsf@gnus.org> <87im0a6789.fsf@gmail.com> <8735rdha6y.fsf@gnus.org> <69e713da-5186-780a-f0b7-8099ce6cde3d@easy-emacs.de> <87h7fsnzmo.fsf@gmail.com> <51fa5c12-53b4-ad92-505f-1896d955b68c@easy-emacs.de> 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="13320"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cc: 49822@debbugs.gnu.org, Lars Ingebrigtsen To: Andreas =?UTF-8?Q?R=C3=B6hler?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Aug 26 10:28:09 2021 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 1mJAk5-0003BU-Ip for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 26 Aug 2021 10:28:09 +0200 Original-Received: from localhost ([::1]:58224 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJAk4-0004if-L2 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 26 Aug 2021 04:28:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45062) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mJAjz-0004iU-4Z for bug-gnu-emacs@gnu.org; Thu, 26 Aug 2021 04:28:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36824) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mJAjy-00073R-TV for bug-gnu-emacs@gnu.org; Thu, 26 Aug 2021 04:28:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mJAjy-0005Nm-5Y for bug-gnu-emacs@gnu.org; Thu, 26 Aug 2021 04:28:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 26 Aug 2021 08:28:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49822 X-GNU-PR-Package: emacs Original-Received: via spool by 49822-submit@debbugs.gnu.org id=B49822.162996647520678 (code B ref 49822); Thu, 26 Aug 2021 08:28:02 +0000 Original-Received: (at 49822) by debbugs.gnu.org; 26 Aug 2021 08:27:55 +0000 Original-Received: from localhost ([127.0.0.1]:48370 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mJAjr-0005NR-3c for submit@debbugs.gnu.org; Thu, 26 Aug 2021 04:27:55 -0400 Original-Received: from mail-wr1-f42.google.com ([209.85.221.42]:37465) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mJAjp-0005ND-M5 for 49822@debbugs.gnu.org; Thu, 26 Aug 2021 04:27:54 -0400 Original-Received: by mail-wr1-f42.google.com with SMTP id v10so3713729wrd.4 for <49822@debbugs.gnu.org>; Thu, 26 Aug 2021 01:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=2KaafmOtQojMSVjMLbHWogy/ViXzLNtjCAuGiPIIvFA=; b=PURk3QlQsiC2NGVZXwsLxuU7SCPx5ACagzeB8U8yclamdk9t+3eRpHrC/vhvlV0fnb 1NH4+gXdB+tYGL6EXxJx7RD+NPyUpIsO0UQvMxnWzFTBCUurOv0hoHGnjDO2LJcrG4nf q/oGO1m0WRP/QDEX8gMWX6tWWIyFFUKRDhdWGWGY+HR7A8Kqi/vLPXvbXIFyFaXsBrXW eRzZymJ/pU2azoJFdnD9AYLroIiRG7zrkmfJdvzugopvlQIfvwgGBpIjTFD/ZSuxIq8h /Pp56wB+mFbSuYq1igUZRwRkWMaWJ2GBaegWB9bWgZBRqwPNHuJg3usmBSH+pRbeKsdq Hy7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=2KaafmOtQojMSVjMLbHWogy/ViXzLNtjCAuGiPIIvFA=; b=LAyAVsjSEEWDuyWidZpVx3jIutz7KY2uicNYTiJXBu3GmQy6iB90XhiuM1oWgLptx7 2X63mzV/4Lh47c8Lp8dd5/xTPnrgHTGs+ClLOZ+PN9pt/cUWj21PNTKFP4mXX1UGAX2b sXQ6T48B1vWy3MagxFD4z0PKSE4VCiIWHeMjM3lkleHYuUXJkfYDuxphkKNePsYOOYMT 9QpivUjuq6t0Ylc2lP3IYzwluTi79dUP7xfSIF+pmZnQGplGRmuyvecE0Are7DzvL3An CgE08EMh6wPAGdlxcWmq1FA7G8GMyHiFDGKTNPSpeeC5QFxBGxrzzjv0n6/7SpGQfz2b TE7g== X-Gm-Message-State: AOAM531fkVg9Tt75JHVVYU4SouUKd7u4/aV9DWDJNz4fDNwblJ+Y7uCG +UgHxIn+5SelEuTA2rRmtMiqpveN1o0= X-Google-Smtp-Source: ABdhPJwf83T0uG0Hg4JDnuQYznUfK/muV6Oxgnqk3FgN3ZjO9QoT0/MZoQBTPxjpqCxU8Lbm8cANyw== X-Received: by 2002:adf:ebc8:: with SMTP id v8mr2414323wrn.153.1629966467016; Thu, 26 Aug 2021 01:27:47 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::ae3f]) by smtp.gmail.com with ESMTPSA id u5sm2281563wrr.94.2021.08.26.01.27.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Aug 2021 01:27:46 -0700 (PDT) In-Reply-To: <51fa5c12-53b4-ad92-505f-1896d955b68c@easy-emacs.de> ("Andreas =?UTF-8?Q?R=C3=B6hler?="'s message of "Sun, 15 Aug 2021 08:41:45 +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" Xref: news.gmane.io gmane.emacs.bugs:212697 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sun, 15 Aug 2021 at 08:41, Andreas R=C3=B6hler wrote: > On 14.08.21 11:29, Augusto Stoffel wrote: >> On Fri, 13 Aug 2021 at 18:44, Andreas R=C3=B6hler wrote: >> >>> Hmm, a buffer is not an interactive shell. Putting a print() around >>> should do it. >> I'm not sure I get your point. Evaluating an expression should print >> the result, no? It's the P in REPL. > > Python's interactive prompt offers a slightly different behavior than > running code through a plain Python interpreter. > > I understand the code in a buffer as something which is executed as a > program. If you run a program by a python interpreter, if it outputs=20 > something, that depends. Well, maybe that depends a bit on the use-case and taste. But I would like "send to the shell" to be more or less equivalent to "copy and paste in the shell buffer". Other people would prefer this as well, cf. https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D29592 Anyway, I played around a bit and it's not hard to recover the Jupyter behavior where evaluating some statements returns the last value. The attached file showing how it works. I think Elpy does something similar, in fact. I'm struggling a bit on how this could be integrated with python.el. Just dropping these new commands there would be easy, but not ideal; one should probably integrate more tightly with the existing python-shell commands, say by introducing a 'python-sell-echo-result' defcustom. But then I feel tempted to change various other things I don't like about the Python shell, in particular the use of temporary files (as opposed to escaped strings) to transfer pieces of code, which is very slow via Tramp. By the way, I also dislike how the 'python-shell-send-*' functions don't even print a newline on the shell buffer, so that you see ">>> >>> >>>" instead of ">>>\n>>>\n>>>" after calling them thrice. This means (1) a result may be printed right next to the input prompt ">>>", and (2) it's impossible to tell if the inferior process is busy by looking at the relative position of the point and prompt. --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=python-shell-eval.el Content-Transfer-Encoding: quoted-printable ;; Evaluate Python code with result output -*- lexical-binding: t -*- (require 'json) (require 'python) ;; From https://bugs.python.org/issue39820#msg387189 ;; ;; How can we hide __PYTHON_EL_eval from stack traces? This is a ;; problem in `python-shell-send-*' as well, see ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D32042 (defvar python-shell-eval-setup-code "\ def __PYTHON_EL_eval(source, filename): print() import ast g, p =3D globals(), ast.parse(source, filename) expr, res =3D None, None if p.body and isinstance(p.body[-1], ast.Expr): expr =3D p.body.pop() code =3D compile(p, filename, 'exec') exec(code, g, g) if expr: code =3D compile(ast.Expression(expr.value), filename, 'eval') res =3D eval(code, g, g) return res" "Code used to evaluate statements in inferior Python processes.") ;;;###autoload (defun python-shell-eval-string (string &optional file-name add-history pro= cess) "Send STRING to inferior Python PROCESS." (interactive (list (read-string "Python statement: ") nil nil nil)) (unless process (setq process (python-shell-get-process-or-error t))) (unless file-name (setq file-name "")) (with-current-buffer (process-buffer process) (compilation-forget-errors) (when add-history (comint-add-to-input-history (if (stringp add-history) add-history string)))) (comint-send-string process (format "exec(%s);__PYTHON_EL_eval(%s, %s)\n" (json-encode python-shell-eval-setup-code) (json-encode string) (json-encode file-name)))) ;;;###autoload (defun python-shell-eval-region (start end &optional add-history) "Send the region delimited by START and END to inferior Python process." (interactive (list (region-beginning) (region-end) (not current-prefix-arg))) (python-shell-eval-string (python-shell-buffer-substring start end) (buffer-file-name) (buffer-substring-no-properties start end))) ;;;###autoload (defun python-shell-eval-buffer (&optional send-main) "Send buffer to inferior Python process." (interactive "P") (let ((string (python-shell-buffer-substring (point-min) (point-max) (not= send-main)))) (python-shell-eval-string string (buffer-file-name)))) (provide 'python-shell-eval) ;;; python-shell-eval.el ends here --=-=-=--