emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728


From: Xu Chunyang
Subject: [O] 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
User-agent: Notmuch/0.26.2+26~g9e158fb (https://notmuchmail.org) Emacs/27.0.50 (x86_64-apple-darwin17.5.0)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]