From mboxrd@z Thu Jan 1 00:00:00 1970 From: qijian gong Subject: [PATCH] ob-python: Insert necessary blank line when sending code to interpreter Date: Mon, 02 Apr 2018 02:21:34 +0000 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="00000000000056c52d0568d4400a" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:33342) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f2p6S-0007zO-99 for Emacs-orgmode@gnu.org; Sun, 01 Apr 2018 22:21:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f2p6Q-0007aN-Mm for Emacs-orgmode@gnu.org; Sun, 01 Apr 2018 22:21:48 -0400 Received: from mail-it0-x232.google.com ([2607:f8b0:4001:c0b::232]:39057) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1f2p6Q-0007Zf-EA for Emacs-orgmode@gnu.org; Sun, 01 Apr 2018 22:21:46 -0400 Received: by mail-it0-x232.google.com with SMTP id e98-v6so16805984itd.4 for ; Sun, 01 Apr 2018 19:21:46 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Emacs-orgmode@gnu.org --00000000000056c52d0568d4400a Content-Type: text/plain; charset="UTF-8" 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 "", 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 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 --00000000000056c52d0568d4400a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,

I've written= this patch to fix the syntax error alarm caused by evaluating the followin= g code block:

#+BEGIN_SRC python :session
if True:
=C2=A0 =C2=A0 1
2
#+END_SRC
<= div>
In *Python* buffer:

#+BEGIN_QUO= TE
if True:
Python 3.6.1 (default, Apr=C2=A0 8 2017, 19= :21:02)=C2=A0
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0= .42.1)] on darwin
Type "help", "copyright", &= quot;credits" or "license" for more information.
= =C2=A0 =C2=A0 1
>>> ... ...=C2=A0 =C2=A0File "<s= tdin>", line 3
=C2=A0 =C2=A0 import codecs, os;__pyfile = =3D codecs.open('''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jh0000= gn/T/py6GOx5H''', encoding=3D'''utf-8'''= ;);__code =3D __pyfile.read().encode('''utf-8''');_= _pyfile.close();os.remove('''/var/folders/vq/k0y6n92j6r5dk_hx_j= rl62jh0000gn/T/py6GOx5H''');exec(compile(__code, '''= ;/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jh0000gn/T/py6GOx5H''', = 'exec'));
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^
S= yntaxError: invalid syntax
>>> 2
2
<= br>
>>> open('/var/folders/vq/k0y6n92j6r5dk_hx_jrl62= jh0000gn/T/babel-2PNS5b/python-oXjil1', 'w').write(str(_))
>>>=C2=A0
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,
Q= ijian

Footnotes:
[1] https://emacs-china.org/t/topic/5436/
<= br>
-------------------------------------------------------------= -----

From 4481a5032e94a85153b708c66858d2965abf6d8= 4 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 neces= sary blank line when sending code to
=C2=A0interpreter * lisp/ob-= python.el (org-bable-python-evaluate-session):

Syn= tax error occurs when evaulating the following code block:

\#begin_src python :session
if True:
=C2=A0 = =C2=A0 1
2
\#end_src

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


TINYCHANGE=
---
=C2=A0lisp/ob-python.el | 13 ++++++++++++-
=C2=A01 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 9= f1234bac..36d549697 100644
--- a/lisp/ob-python.el
+++ = b/lisp/ob-python.el
@@ -308,8 +308,19 @@ last statement in BODY, = as elisp."
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0(list (format "open('%s', 'w')= .write(str(_))"
=C2=A0 =C2=A0 =C2=A0 =C2=A0(org-babel-process-file-name tmp-file
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0'noquote))))= )))
+ (last-indent 0)
=C2=A0 (input-body (lambda (= body)
- =C2=A0 =C2=A0 =C2= =A0 =C2=A0(mapc (lambda (line) (insert line) (funcall send-wait))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(mapc= (lambda (line)
+ =C2=A0= =C2=A0 =C2=A0 =C2=A0(let ((curr-indent (string-match "[^\s]" lin= e)))
+ (if curr-indent=
+ =C2=A0 =C2=A0 =C2=A0= (progn
+ =C2=A0 =C2=A0 = =C2=A0 =C2=A0(when (< curr-indent last-indent)
+ (insert "")
+ (funcall send-wait))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(setq last-ind= ent curr-indent))
+ =C2= =A0 =C2=A0(setq last-indent 0)))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(insert line)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(funcall send-wai= t))
=C2=A0 =C2=A0 =C2=A0= =C2=A0(split-string body "[\r\n]"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(funcall send-wait= )))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (results
--=C2= =A0
2.16.2
--00000000000056c52d0568d4400a--