python-cell.el
python-cell.el copied to clipboard
Error occurred when sending an indented cell
When sending an indented cell (e.g. a block in a method of a class), Python shell would throw IndentationError: unexpected indent
I figured out a hack to fix this problem( by prefixing a line 'if True:\n'):
(defun python-shell-send-cell ()
"Send the cell the cursor is in to the inferior Python process."
(interactive)
(let* (
(start (save-excursion (python-beginning-of-cell)
(point)))
(end (save-excursion (python-end-of-cell)
(point)))
(content (buffer-substring start end))
(preline "if True:\n")
(line-num (line-number-at-pos start))
;; When sending a region, add blank lines for non sent code so
;; backtraces remain correct.
(postline (make-string (1- line-num) ?\n)))
;;(message "DEBUG: %s" (substring content 0 4))
(python-shell-send-string (concat (if (string= (substring content 0 2) " ")
preline
"")
content
postline))))