GNU bug report logs - #37828
26.3; python-shell-send-defun doesn't find the (whole) definition

Previous Next

Package: emacs;

Reported by: per <at> starback.se (Per Starbäck)

Date: Sun, 20 Oct 2019 06:39:01 UTC

Severity: normal

Tags: fixed, patch

Found in version 26.3

Fixed in version 28.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 37828 in the body.
You can then email your comments to 37828 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#37828; Package emacs. (Sun, 20 Oct 2019 06:39:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to per <at> starback.se (Per Starbäck):
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 20 Oct 2019 06:39:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: per <at> starback.se (Per Starbäck)
To: bug-gnu-emacs <at> gnu.org
Subject: 26.3; python-shell-send-defun doesn't find the (whole) definition
Date: Sun, 20 Oct 2019 08:38:04 +0200
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)
======================================================================




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37828; Package emacs. (Thu, 24 Oct 2019 19:23:01 GMT) Full text and rfc822 format available.

Message #8 received at 37828 <at> debbugs.gnu.org (full text, mbox):

From: Tomas Nordin <tomasn <at> posteo.net>
To: Per Starbäck <per <at> starback.se>, 37828 <at> debbugs.gnu.org
Subject: Re: bug#37828: 26.3; python-shell-send-defun doesn't find the
 (whole) definition
Date: Thu, 24 Oct 2019 21:21:48 +0200
Hello Per

Per Starbäck <per <at> starback.se> writes:

> 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.

I think this is reported by Bug#30822 and fixed in master.

Best regards
--
Tomas




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37828; Package emacs. (Thu, 31 Oct 2019 06:33:02 GMT) Full text and rfc822 format available.

Message #11 received at 37828 <at> debbugs.gnu.org (full text, mbox):

From: Per Starbäck <per <at> starback.se>
To: Tomas Nordin <tomasn <at> posteo.net>
Cc: 37828 <at> debbugs.gnu.org
Subject: Re: bug#37828: 26.3;
 python-shell-send-defun doesn't find the (whole) definition
Date: Thu, 31 Oct 2019 07:32:06 +0100
[Message part 1 (text/plain, inline)]
> I think this is reported by Bug#30822 and fixed in master.

Thanks! Now I have had time to compare with the trunk. The first part
of the bug is indeed fixed, but not the second part:

Here is an updated patch made against the trunk. (There are only two new lines.)
[patch.txt (text/plain, attachment)]

Added tag(s) patch. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Thu, 27 Aug 2020 11:43:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37828; Package emacs. (Fri, 02 Oct 2020 03:12:02 GMT) Full text and rfc822 format available.

Message #16 received at 37828 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Per Starbäck <per <at> starback.se>
Cc: Tomas Nordin <tomasn <at> posteo.net>, 37828 <at> debbugs.gnu.org
Subject: Re: bug#37828: 26.3; python-shell-send-defun doesn't find the
 (whole) definition
Date: Fri, 02 Oct 2020 05:11:43 +0200
Per Starbäck <per <at> starback.se> writes:

> Thanks! Now I have had time to compare with the trunk. The first part
> of the bug is indeed fixed, but not the second part:
>
> Here is an updated patch made against the trunk. (There are only two
> new lines.)

Thanks; I tried the patch, and it did indeed fix the problem, so I've
now applied it to Emacs 28.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) fixed. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Fri, 02 Oct 2020 03:12:02 GMT) Full text and rfc822 format available.

bug marked as fixed in version 28.1, send any further explanations to 37828 <at> debbugs.gnu.org and per <at> starback.se (Per Starbäck) Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Fri, 02 Oct 2020 03:12:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 30 Oct 2020 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 249 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.