emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: qijian gong <gongqijian@gmail.com>
To: Emacs-orgmode@gnu.org
Subject: [PATCH] ob-python: Insert necessary blank line when sending code to interpreter
Date: Mon, 02 Apr 2018 02:21:34 +0000	[thread overview]
Message-ID: <CAPWNH1Pnx52T2rbRE09ZdaBhGLVQJtyopiPf+2PA-RBBB1+AXw@mail.gmail.com> (raw)

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

             reply	other threads:[~2018-04-02  2:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-02  2:21 qijian gong [this message]
2018-04-02 18:56 ` [PATCH] ob-python: Insert necessary blank line when sending code to interpreter Nicolas Goaziou
2018-04-03 22:29   ` qijian gong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPWNH1Pnx52T2rbRE09ZdaBhGLVQJtyopiPf+2PA-RBBB1+AXw@mail.gmail.com \
    --to=gongqijian@gmail.com \
    --cc=Emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).