bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#37828: 26.3; python-shell-send-defun doesn't find the (whole) defini


From: Per Starbäck
Subject: bug#37828: 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)
======================================================================





reply via email to

[Prev in Thread] Current Thread [Next in Thread]