Re: [O] Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)]

2018-05-08 Thread Bastien
Hi,

Xu Chunyang  writes:

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

Yes, we need help for this.

-- 
 Bastien



Re: [O] Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)]

2018-05-07 Thread Aaron Ecay
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



[O] Bug: ob-python: Lots of IndentationError [9.1.12 (release_9.1.12-728-ge8a4f3 @ /Users/xcy/src/org-mode/lisp/)]

2018-05-07 Thread Xu Chunyang
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/s191h4q97p90374yw15ssrs0gn/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/)