emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)]
@ 2018-05-07 12:50 Xu Chunyang
  2018-05-07 16:04 ` Aaron Ecay
  2018-05-08  8:47 ` Bastien
  0 siblings, 2 replies; 3+ messages in thread
From: Xu Chunyang @ 2018-05-07 12:50 UTC (permalink / raw)
  To: emacs-orgmode

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/)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)]
  2018-05-07 12:50 Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)] Xu Chunyang
@ 2018-05-07 16:04 ` Aaron Ecay
  2018-05-08  8:47 ` Bastien
  1 sibling, 0 replies; 3+ messages in thread
From: Aaron Ecay @ 2018-05-07 16:04 UTC (permalink / raw)
  To: Xu Chunyang, emacs-orgmode

Hi Chunyang,

This bug was introduced by commit 1966d58b2, which “fixed” another
problematic case of indentation.  Because of issues like this, ob-python
has never really worked reliably (in my experience)

The only sensible way I can see to evaluate python code from emacs
without hitting these kinds of errors is to use jupyter (formerly
ipython).  You might try the ob-ipython or ob-ein libraries and see if
they work for you.  (The latter is part of ein, which provides lots of
integration emacs <-> jupyter, and not only for org-babel).  Disclaimer:
I havenʼt used either of these libraries personally, so I donʼt know how
well they will work for you.

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)]
  2018-05-07 12:50 Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)] Xu Chunyang
  2018-05-07 16:04 ` Aaron Ecay
@ 2018-05-08  8:47 ` Bastien
  1 sibling, 0 replies; 3+ messages in thread
From: Bastien @ 2018-05-08  8:47 UTC (permalink / raw)
  To: Xu Chunyang; +Cc: emacs-orgmode

Hi,

Xu Chunyang <mail@xuchunyang.me> writes:

> By the way, the implementation of ob-python.el is not very robust.

Yes, we need help for this.

-- 
 Bastien

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-05-08  8:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-07 12:50 Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)] Xu Chunyang
2018-05-07 16:04 ` Aaron Ecay
2018-05-08  8:47 ` Bastien

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).