I've added comment and test (New path file in attachment),the test results
look fine.

#+BEGIN_SRC sh
$ make BTEST_RE="test-ob-python" test-dirty
...
selected tests: test-ob-python
Running 7 tests (2018-04-03 16:51:31+0800)
executing Python code block...
Code block evaluation complete.
   passed  1/7  test-ob-python/colnames-nil-header-argument
executing Python code block...
Code block evaluation complete.
   passed  2/7  test-ob-python/colnames-no-header-argument
executing Python code block...
Code block evaluation complete.
   passed  3/7  test-ob-python/colnames-no-header-argument-again
executing Python code block...
Code block evaluation complete.
   passed  4/7  test-ob-python/colnames-yes-header-argument
executing Python code block...
Code block evaluation complete.
   passed  5/7  test-ob-python/colnames-yes-header-argument-again
executing Python code block...
Code block evaluation complete.
   passed  6/7
test-ob-python/insert-necessary-blank-line-when-sending-code-to-interpreter
executing Python code block...
Code block evaluation complete.
   passed  7/7  test-ob-python/session-multiline

Ran 7 tests, 7 results as expected (2018-04-03 16:51:33+0800)
#+END_SRC

Nicolas Goaziou <m...@nicolasgoaziou.fr>于2018年4月3日 周二上午2:56写道:

> Hello,
>
> qijian gong <gongqij...@gmail.com> writes:
>
> > I've written this patch to fix the syntax error alarm caused by
> evaluating
> > the following code block:
> >
> > #+BEGIN_SRC python :session
> > if True:
> >     1
> > 2
> > #+END_SRC
>
> Thank you.
>
> Could you add a test in "test-ob-python.el"? Could you also add
> a comment explaining what you are doing?
>
> > +        (let ((curr-indent (string-match "[^\s]" line)))
>
>   (string-match "\\S-" line)
>
> Regards,
>
> --
> Nicolas Goaziou
>
From 1ec522ab99daa17c2e7b4e06ace1dccea1a979a3 Mon Sep 17 00:00:00 2001
From: Gong Qijian <gongqijian@gmail.com>
Date: Sun, 1 Apr 2018 16:34:32 +0800
Subject: [PATCH] ob-python: Insert necessary blank line when sending code to
 interpreter * lisp/ob-python.el (org-bable-python-evaluate-session):

Syntax error occurs when evaulating the following code block:

\#+begin_src python :session
if True:
    1
2
\#+end_src

A blank line is required for top level module code to end an indented block, such as a for loop, try/except, or if statement.

https://www.python.org/dev/peps/pep-0008/#blank-line

TINYCHANGE
---
 lisp/ob-python.el              | 14 +++++++++++++-
 testing/lisp/test-ob-python.el | 16 ++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 9f1234bac..13997c1a0 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -308,8 +308,20 @@ 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))
+		       (mapc (lambda (line)
+			       ;; 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))
 			     (split-string body "[\r\n]"))
 		       (funcall send-wait)))
          (results
diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el
index 915b1bc77..8c5ca83be 100644
--- a/testing/lisp/test-ob-python.el
+++ b/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
-- 
2.16.2

Reply via email to