emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [PATCH] ob-python: Insert necessary blank line when sending code to


From: qijian gong
Subject: [O] [PATCH] ob-python: Insert necessary blank line when sending code to interpreter
Date: Mon, 02 Apr 2018 02:21:34 +0000

Hello,

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

In *Python* buffer:

#+BEGIN_QUOTE
if True:
Python 3.6.1 (default, Apr  8 2017, 19:21:02) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
    1
>>> ... ...   File "<stdin>", line 3
    import codecs, os;__pyfile = codecs.open('''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jh0000gn/T/py6GOx5H''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jh0000gn/T/py6GOx5H''');exec(compile(__code, '''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jh0000gn/T/py6GOx5H''', 'exec'));
         ^
SyntaxError: invalid syntax
>>> 2
2

>>> open('/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jh0000gn/T/babel-2PNS5b/python-oXjil1', 'w').write(str(_))
>>> 
1

>>> 'org_babel_python_eoe'
>>> 'org_babel_python_eoe'
#+END_QUOTE

To fix this, need to insert an extra blank line to end the if statement.

More discussion[1][2][3].

Thanks,
Qijian

Footnotes:
[1] https://emacs-china.org/t/topic/5436/
[2] https://stackoverflow.com/questions/8391633/blank-line-rule-at-interactive-prompt/41988906#41988906
[3] https://www.python.org/dev/peps/pep-0008/#blank-line

------------------------------------------------------------------

From 4481a5032e94a85153b708c66858d2965abf6d84 Mon Sep 17 00:00:00 2001
From: Gong Qijian <address@hidden>
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 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 9f1234bac..36d549697 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -308,8 +308,19 @@ 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)
+        (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
-- 
2.16.2

reply via email to

[Prev in Thread] Current Thread [Next in Thread]