Browse Source

Merge branch 'maint'

Nicolas Goaziou 7 năm trước cách đây
mục cha
commit
a1ca78d5b8
2 tập tin đã thay đổi với 30 bổ sung2 xóa
  1. 14 2
      lisp/ob-python.el
  2. 16 0
      testing/lisp/test-ob-python.el

+ 14 - 2
lisp/ob-python.el

@@ -308,9 +308,21 @@ last statement in BODY, as elisp."
 	       (list (format "open('%s', 'w').write(str(_))"
 			     (org-babel-process-file-name tmp-file
                                                           'noquote)))))))
+	 (last-indent 0)
 	 (input-body (lambda (body)
-		       (mapc (lambda (line) (insert line) (funcall send-wait))
-			     (split-string body "[\r\n]"))
+		       (dolist (line (split-string body "[\r\n]"))
+			 ;; Insert a blank line to end an indent
+			 ;; block.
+			 (let ((curr-indent (string-match "\\S-" line)))
+			   (if curr-indent
+			       (progn
+				 (when (< curr-indent last-indent)
+				   (insert "")
+				   (funcall send-wait))
+				 (setq last-indent curr-indent))
+			     (setq last-indent 0)))
+			 (insert line)
+			 (funcall send-wait))
 		       (funcall send-wait)))
          (results
           (pcase result-type

+ 16 - 0
testing/lisp/test-ob-python.el

@@ -118,6 +118,22 @@ return x
    (org-babel-next-src-block)
    (should (equal "20" (org-babel-execute-src-block)))))
 
+(ert-deftest test-ob-python/insert-necessary-blank-line-when-sending-code-to-interpreter ()
+  (org-test-with-temp-text "#+begin_src python :session :results value
+if True:
+    1
+2
+#+end_src"
+    ;; Previously, while adding `:session' to a normal code block, also need to add extra blank lines
+    ;; to end indent block or indicate logical sections. Now, the `org-babel-python-evaluate-session'
+    ;; can do it automatically:
+    ;; >>> if True:
+    ;; >>>     1
+    ;; >>> <insert_blank_line_here>
+    ;; >>> 2
+    (org-babel-execute-maybe)
+    (should (equal 2 (org-babel-execute-src-block)))))
+
 (provide 'test-ob-python)
 
 ;;; test-ob-python.el ends here