From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: per@starback.se (Per =?UTF-8?Q?Starb=C3=A4ck?=) Newsgroups: gmane.emacs.bugs Subject: bug#37828: 26.3; python-shell-send-defun doesn't find the (whole) definition Date: Sun, 20 Oct 2019 08:38:04 +0200 Message-ID: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="176943"; mail-complaints-to="usenet@blaine.gmane.org" To: 37828@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Oct 20 08:39:29 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iM4sC-000jvz-Ni for geb-bug-gnu-emacs@m.gmane.org; Sun, 20 Oct 2019 08:39:28 +0200 Original-Received: from localhost ([::1]:52098 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iM4sB-0000uX-KG for geb-bug-gnu-emacs@m.gmane.org; Sun, 20 Oct 2019 02:39:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50222) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iM4rn-0000nl-Eb for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:39:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iM4rm-0006YQ-4U for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:39:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:44892) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iM4rm-0006Y6-0o for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:39:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1iM4rl-0000sj-RZ for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:39:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: per@starback.se (Per =?UTF-8?Q?Starb=C3=A4ck?=) Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 20 Oct 2019 06:39:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 37828 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.15715534973332 (code B ref -1); Sun, 20 Oct 2019 06:39:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 20 Oct 2019 06:38:17 +0000 Original-Received: from localhost ([127.0.0.1]:53713 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1iM4r3-0000rg-3i for submit@debbugs.gnu.org; Sun, 20 Oct 2019 02:38:17 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:44761) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1iM4qy-0000rV-Tr for submit@debbugs.gnu.org; Sun, 20 Oct 2019 02:38:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50097) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iM4qx-0008B2-IH for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:38:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iM4qw-00064L-5t for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:38:11 -0400 Original-Received: from numerus.lingfil.uu.se ([130.238.78.148]:57638) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1iM4qv-000627-Rb for bug-gnu-emacs@gnu.org; Sun, 20 Oct 2019 02:38:10 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by numerus.lingfil.uu.se (Postfix) with ESMTP id 1870BA1E6003; Sun, 20 Oct 2019 08:38:05 +0200 (CEST) X-Virus-Scanned: amavisd-new at lingfil.uu.se Original-Received: from numerus.lingfil.uu.se ([127.0.0.1]) by localhost (numerus.lingfil.uu.se [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id g35vc02FQ7M8; Sun, 20 Oct 2019 08:38:04 +0200 (CEST) Original-Received: from numerus.lingfil.uu.se (localhost [127.0.0.1]) by numerus.lingfil.uu.se (Postfix) with ESMTP id 66A9DA1E6002; Sun, 20 Oct 2019 08:38:04 +0200 (CEST) Original-Received: (from starback@localhost) by numerus.lingfil.uu.se (8.14.7/8.14.7/Submit) id x9K6c48f000430; Sun, 20 Oct 2019 08:38:04 +0200 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 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.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:169767 Archived-At: With emacs 26.3. There are two related bugs which makes python-shell-send-defun sometimes not send the right contents. == reproduce the first one == $ emacs -Q /tmp/newfile.py C-c C-p [start python process] def foo(): RET [define a ...] pass RET [... python function] C-M-x [python-shell-send-defun] In the echo area it says "Sent: pass..." because only that line was sent. Not the whole definition, and the function is not defined in the inferior python. A variant is to have a line "@property" before the "def foo():" line. Then python-shell-send-defun will enter an infinite loop instead. == reason == python-shell-send-defun moves lines backwards one at a time until it is out of the defun, and then (forward-line 1) to go back into it. When the file begins immediately with the first defun (which isn't that common, since normally there'd be a shebang or other comment there) it never goes outside of the defun so then (forward-line 1) is wrong. == fix == ====================================================================== $ diff -u python.el python-fixed.el --- python.el 2019-07-25 21:41:28.000000000 +0200 +++ python-fixed.el 2019-10-20 08:17:46.871142868 +0200 @@ -3151,9 +3151,10 @@ (beginning-of-line 1)) (> (current-indentation) 0))) (when (not arg) - (while (and (forward-line -1) - (looking-at (python-rx decorator)))) - (forward-line 1)) + (let ((remains-to-move 0)) + (while (and (zerop (setq remains-to-move (forward-line -1))) + (looking-at (python-rx decorator)))) + (forward-line (1+ remains-to-move)))) (point-marker)) (progn (or (python-nav-end-of-defun) ====================================================================== === reproduce the second one == $ emacs -Q testfile.py where testfile.py contains ---------------------- def foo(): pass @property def bar(): pass ---------------------- C-c C-p [run-python] M-> [end-of-buffer] C-M-x [python-shell-send-defun] In the echo area it echoes "Sent: ..." and "bar" is not defined in the inferior python which it should be. == reason == When python-shell-send-defun goes to the beginning of the defun it goes to the line with "@property", but from there python-nav-end-of-defun doesn't find the end of that defun. == fix == This could be seen as a bug in python-nav-end-of-defun and be fixed only there instead. I haven't done that. This patches this for python-shell-send-defun (including the patch one above), which might be a good idea anyway, making python-shell-send-defun more robust by going to the end-of-defun from the original position and not from where it ended up looking for the beginning. (The fix is really small except for the indentation changes.) ====================================================================== $ diff -u python.el python-fixed-more.el --- python.el 2019-07-25 21:41:28.000000000 +0200 +++ python-fixed-more.el 2019-10-20 08:16:20.799758867 +0200 @@ -3143,24 +3143,27 @@ user-friendly message if there's no process running; defaults to t when called interactively." (interactive (list current-prefix-arg t)) - (save-excursion - (python-shell-send-region - (progn - (end-of-line 1) - (while (and (or (python-nav-beginning-of-defun) - (beginning-of-line 1)) - (> (current-indentation) 0))) - (when (not arg) - (while (and (forward-line -1) - (looking-at (python-rx decorator)))) - (forward-line 1)) - (point-marker)) - (progn - (or (python-nav-end-of-defun) - (end-of-line 1)) - (point-marker)) - nil ;; noop - msg))) + (let ((starting-pos (point))) + (save-excursion + (python-shell-send-region + (progn + (end-of-line 1) + (while (and (or (python-nav-beginning-of-defun) + (beginning-of-line 1)) + (> (current-indentation) 0))) + (when (not arg) + (let ((remains-to-move 0)) + (while (and (zerop (setq remains-to-move (forward-line -1))) + (looking-at (python-rx decorator)))) + (forward-line (1+ remains-to-move)))) + (point-marker)) + (progn + (goto-char starting-pos) + (or (python-nav-end-of-defun) + (end-of-line 1)) + (point-marker)) + nil ;; noop + msg)))) (defun python-shell-send-file (file-name &optional process temp-file-name delete msg) ======================================================================