unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#21086: 24.5; python.el: python-shell-buffer-substring fails under certain circumstances
@ 2015-07-18 16:13 Yuri D'Elia
  2015-08-23 23:01 ` Fabián Ezequiel Gallina
  0 siblings, 1 reply; 2+ messages in thread
From: Yuri D'Elia @ 2015-07-18 16:13 UTC (permalink / raw)
  To: 21086

[-- Attachment #1: Type: text/plain, Size: 600 bytes --]

Assuming some python buffer like:

#....
if 1:
    print 1

where the selected region is "print \"1\"" (mark before "p"),
`python-shell-send-region' would fail with:

File "test.py", line 3
    print 1
        ^
IndentationError: expected an indented block

The reason lies within `python-shell-buffer-substring', which assumes
that a region always starts with the correct indentation.

The attached patch fixes the above behavior: when the region doesn't
start with indentation, but the current block does (looking at
(current-indentation)), we prefix the fillstr with the correct amount of
indent.

[-- Attachment #2: python-buffer-substring.patch --]
[-- Type: text/x-diff, Size: 1470 bytes --]

--- python.el.Orig	2015-07-18 18:00:49.367517569 +0200
+++ python.el	2015-07-18 18:03:01.203659502 +0200
@@ -2548,18 +2548,22 @@
                      (make-string
                       ;; Subtract 2 because of the coding cookie.
                       (- (line-number-at-pos start) 2) ?\n))))
-         (toplevel-block-p (save-excursion
-                             (goto-char start)
-                             (or (zerop (line-number-at-pos start))
-                                 (progn
-                                   (python-util-forward-comment 1)
-                                   (zerop (current-indentation)))))))
+         (block-param (save-excursion
+                        (goto-char start)
+                        (progn
+                          (python-util-forward-comment 1)
+                          (list (current-indentation)
+                                (/= (point) start)))))
+         (block-indentation (car block-param))
+         (starts-with-indentation-p (cadr block-param)))
     (with-temp-buffer
       (python-mode)
       (if fillstr (insert fillstr))
+      (when (and (> block-indentation 0) (not starts-with-indentation-p))
+        (insert (make-string block-indentation ?\s)))
       (insert substring)
       (goto-char (point-min))
-      (when (not toplevel-block-p)
+      (when (> block-indentation 0)
         (insert "if True:")
         (delete-region (point) (line-end-position)))
       (when nomain

^ permalink raw reply	[flat|nested] 2+ messages in thread

* bug#21086: 24.5; python.el: python-shell-buffer-substring fails under certain circumstances
  2015-07-18 16:13 bug#21086: 24.5; python.el: python-shell-buffer-substring fails under certain circumstances Yuri D'Elia
@ 2015-08-23 23:01 ` Fabián Ezequiel Gallina
  0 siblings, 0 replies; 2+ messages in thread
From: Fabián Ezequiel Gallina @ 2015-08-23 23:01 UTC (permalink / raw)
  To: 21086-done


Fixed in master at af013e0.

I installed a different patch that seems to be more robust on various
conditions (see tests).


Thanks for such detailed report,
Fabián.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-08-23 23:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-18 16:13 bug#21086: 24.5; python.el: python-shell-buffer-substring fails under certain circumstances Yuri D'Elia
2015-08-23 23:01 ` Fabián Ezequiel Gallina

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).