emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Xu Chunyang <mail@xuchunyang.me>
To: emacs-orgmode@gnu.org
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	[thread overview]
Message-ID: <m2muxb4okn.fsf@xuchunyang.me> (raw)

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 "<stdin>", line 1
    else:
    ^
IndentationError: unexpected indent
            result = digits[i % 10] + result
>>>             i = i // 10
  File "<stdin>", line 1
    result = ""
    ^
IndentationError: unexpected indent

>>>         return result
  File "<stdin>", line 1
    while i > 0:
    ^
IndentationError: unexpected indent

>>> 
  File "<stdin>", line 1
    result = digits[i % 10] + result
    ^
IndentationError: unexpected indent
int2str(123)
>>> 
  File "<stdin>", line 1
    i = i // 10
    ^
IndentationError: unexpected indent
open('/var/folders/7f/s191h4q97p90374yw15ssrs00000gn/T/babel-1ZV0Jv/python-Q0QwFi', 'w').write(str(_))
>>> 
>>> 
  File "<stdin>", line 1
    return result
    ^
IndentationError: unexpected indent
'org_babel_python_eoe'
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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/)

             reply	other threads:[~2018-05-07 12:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-07 12:50 Xu Chunyang [this message]
2018-05-07 16:04 ` Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)] Aaron Ecay
2018-05-08  8:47 ` Bastien

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=m2muxb4okn.fsf@xuchunyang.me \
    --to=mail@xuchunyang.me \
    --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).