From mboxrd@z Thu Jan 1 00:00:00 1970 From: Xu Chunyang Subject: Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)] Date: Mon, 07 May 2018 20:50:32 +0800 Message-ID: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55225) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFfbO-0005it-4w for emacs-orgmode@gnu.org; Mon, 07 May 2018 08:50:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fFfbJ-0003Oj-O9 for emacs-orgmode@gnu.org; Mon, 07 May 2018 08:50:50 -0400 Received: from forward102p.mail.yandex.net ([77.88.28.102]:57748) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fFfbJ-0003Le-Ch for emacs-orgmode@gnu.org; Mon, 07 May 2018 08:50:45 -0400 Received: from mxback4o.mail.yandex.net (mxback4o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::1e]) by forward102p.mail.yandex.net (Yandex) with ESMTP id D751743037A1 for ; Mon, 7 May 2018 15:50:41 +0300 (MSK) 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 Execute this code block #+BEGIN_SRC python :session :results value def int2str(i): digits = "0123456789" if i == 0: return "0" else: result = "" while i > 0: result = digits[i % 10] + result i = i // 10 return result int2str(123) #+END_SRC I get lots of IndentationError in *Python* #+begin_example def int2str(i): Python 3.6.5 (default, Mar 30 2018, 06:41:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. digits = "0123456789" >>> if i == 0: ... return "0" ... ... else: ... result = "" >>> while i > 0: File "", line 1 else: ^ IndentationError: unexpected indent result = digits[i % 10] + result >>> i = i // 10 File "", line 1 result = "" ^ IndentationError: unexpected indent >>> return result File "", line 1 while i > 0: ^ IndentationError: unexpected indent >>> File "", line 1 result = digits[i % 10] + result ^ IndentationError: unexpected indent int2str(123) >>> File "", line 1 i = i // 10 ^ IndentationError: unexpected indent open('/var/folders/7f/s191h4q97p90374yw15ssrs00000gn/T/babel-1ZV0Jv/python-Q0QwFi', 'w').write(str(_)) >>> >>> File "", line 1 return result ^ IndentationError: unexpected indent 'org_babel_python_eoe' >>> Traceback (most recent call last): File "", line 1, in NameError: name '_' is not defined >>> 'org_babel_python_eoe' >>> #+end_example By the way, the implementation of ob-python.el is not very robust. For example, it contains #+BEGIN_SRC emacs-lisp (dolist (line (split-string body "[\r\n]")) ;; Insert a blank line to end an indent ;; block. ...) #+END_SRC it looks very strange to me, since Emacs Lisp doesn't understand Python code, for example, it might end up inserting a newline within a Python string. And another inconvenience is non-session code block requires 'return' but session code block does not, in my opinion, requiring a 'return' is a very bad idea since by inserting a 'return', the python code will be invalid. I suggest someone who is familiar with Org & Python to review the implementation of ob-python.el, I guess python.el already has exiting solution on how to sending a block of python code to comint-mode, and elpy-shell.el[1] also knows how to capture return value of a python code block. More elegant and reliable solution might already exists. [1]: https://github.com/jorgenschaefer/elpy/blob/master/elpy-shell.el Emacs : GNU Emacs 27.0.50 (build 1, x86_64-apple-darwin17.5.0, NS appkit-1561.40 Version 10.13.4 (Build 17E199)) of 2018-05-05 Package: Org mode version 9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)