python-cell.el icon indicating copy to clipboard operation
python-cell.el copied to clipboard

Error occurred when sending an indented cell

Open bamanzi opened this issue 10 years ago • 0 comments

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

bamanzi avatar Mar 07 '14 07:03 bamanzi