all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: per@starback.se (Per Starbäck)
To: 37828@debbugs.gnu.org
Subject: bug#37828: 26.3; python-shell-send-defun doesn't find the (whole) definition
Date: Sun, 20 Oct 2019 08:38:04 +0200	[thread overview]
Message-ID: <sa57e4zapmb.fsf@numerus.lingfil.uu.se> (raw)

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)
======================================================================





             reply	other threads:[~2019-10-20  6:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-20  6:38 Per Starbäck [this message]
2019-10-24 19:21 ` bug#37828: 26.3; python-shell-send-defun doesn't find the (whole) definition Tomas Nordin
2019-10-31  6:32   ` Per Starbäck
2020-10-02  3:11     ` Lars Ingebrigtsen

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=sa57e4zapmb.fsf@numerus.lingfil.uu.se \
    --to=per@starback.se \
    --cc=37828@debbugs.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 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.