all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] ob-python: Insert necessary blank line when sending code to interpreter
@ 2018-04-02  2:21 qijian gong
  2018-04-02 18:56 ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: qijian gong @ 2018-04-02  2:21 UTC (permalink / raw)
  To: Emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3029 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 5613 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-04-03 22:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-02  2:21 [PATCH] ob-python: Insert necessary blank line when sending code to interpreter qijian gong
2018-04-02 18:56 ` Nicolas Goaziou
2018-04-03 22:29   ` qijian gong

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.