emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Schulte <schulte.eric@gmail.com>
To: nicholas.dokos@hp.com
Cc: Herbert Sitz <hsitz@nwlink.com>, emacs-orgmode@gnu.org
Subject: Re: org-babel -- Improper syntax error in session mode?
Date: Mon, 20 Jun 2011 12:23:24 -0700	[thread overview]
Message-ID: <87ei2ouwxk.fsf@gmail.com> (raw)
In-Reply-To: 23747.1308539849@alphaville.dokosmarshall.org

[-- Attachment #1: Type: text/plain, Size: 542 bytes --]

I've changed the python session evaluation so that it explicitly sends a
RET to the inferior Python process after every line of input.  The
attached patch makes this change.  I can confirm that this fixes the
problem in your example (when an empty line is placed between the block
and the subsequent print statement), however, could you please test it
on a variety of other python blocks to confirm it doesn't seem to break
existing behavior (I'm not a python user).

If this looks safe then I'll apply it to the main repo.

Thanks! -- Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-python-Send-RET-after-every-line-w-session-evalua.patch --]
[-- Type: text/x-diff, Size: 1202 bytes --]

From 1edad6a87bb5491061efaf597fa38b9190387232 Mon Sep 17 00:00:00 2001
From: Eric Schulte <schulte.eric@gmail.com>
Date: Mon, 20 Jun 2011 12:18:09 -0700
Subject: [PATCH] ob-python: Send RET after every line w/session evaluation

* lisp/ob-python.el (org-babel-python-evaluate-session): Send
  comint-send-input after every line when interacting with an
  interactive python process.
---
 lisp/ob-python.el |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index f3f4a03..6cb2c44 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -267,9 +267,11 @@ last statement in BODY, as elisp."
 	  (org-babel-comint-with-output
 	      (session org-babel-python-eoe-indicator t body)
 	    (let ((comint-process-echoes nil))
-	      (input-body body)
-	      (insert org-babel-python-eoe-indicator)
-	      (comint-send-input))) 2) "\n"))
+	      (mapc
+	       (lambda (line)
+		 (insert line) (comint-send-input nil t))
+	       (append (split-string body "[\n\r]") (list org-babel-python-eoe-indicator)))))
+	  2) "\n"))
        (value
 	(let ((tmp-file (org-babel-temp-file "python-")))
 	  (org-babel-comint-with-output
-- 
1.7.4.1


[-- Attachment #3: Type: text/plain, Size: 1975 bytes --]


Nick Dokos <nicholas.dokos@hp.com> writes:

> Herbert Sitz <hsitz@nwlink.com> wrote:
>
>> Eric Schulte <schulte.eric <at> gmail.com> writes:
>> > I can confirm that I see the same behavior.  Also, if I manually type
>> > the body of the code block into the session I get the same error output
>> > from Python, so I don't believe this is due to a problem with Babel.
>> > 
>> 
>> It appears the problem is that the python session is interactive and is built to
>> emit output after each Python "block" (e.g., the 'for' block), before another
>> "block" of Python is entered.  If this is the way it's designed then it seems to
>> me that it's Babel's obligation to feed the Python blocks to the Python session
>> as required and then assemble the output pieces as appropriate.  Or am I missing
>> something? -- Herb
>> 
>
> Having babel recognize python blocks in order to feed them to the python
> interpreter as complete blocks seems a bit too much to me. Of course,
> what I think matters little: it's what Eric thinks that matters here.
>
> Having said that, however, I think there *is* a problem:
>
> If you just start the python interpreter and start typing into it:
>
> x = 1
> for i in range(1,5):
>   x = x + i
>   print x
> print "Did it work?"
>
> the problem becomes obvious: the interpreter is still in "indented mode"
> and complains about the last print, because it is not "properly"
> indented.  OTOH, if you exit "indented mode" by pressing another RET
> before the final print, the interpreter is happy. This is a kludge used
> by the interactive interpreter to accommodate python's reliance on
> indentation to delimit block structure.
>
> That however does not work with babel: even if I leave empty lines
> between the print x and the last print, the error persists. Apparently,
> babel does not send the empty lines to the interpreter. If there is a
> bug in babel, it seems to me this is it.
>
> Nick
>
>

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

  parent reply	other threads:[~2011-06-20 19:26 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-19 21:54 org-babel -- Improper syntax error in session mode? Herbert Sitz
2011-06-19 23:21 ` Eric Schulte
2011-06-20  1:59   ` Herbert Sitz
2011-06-20  2:12   ` Herbert Sitz
2011-06-20  3:17     ` Nick Dokos
2011-06-20  3:46       ` Herbert Sitz
2011-06-20 19:23       ` Eric Schulte [this message]
2011-06-20 20:45         ` Herbert Sitz
2011-06-20 21:15           ` Eric Schulte
2011-06-20 23:16             ` Herbert Sitz
2011-06-21  0:08               ` Nick Dokos
2011-06-21  0:27                 ` Herbert Sitz
     [not found]                   ` <hesitz@gmail.com>
2011-06-21  1:17                     ` Nick Dokos
2011-06-21  2:19               ` Eric Schulte
2011-06-21  5:13                 ` Herbert Sitz
2011-06-21  7:15                   ` Thomas S. Dye
2011-06-21 15:35                     ` Herbert Sitz
2011-06-21 16:27                       ` Thomas S. Dye
2011-06-21 17:42                         ` Eric Schulte
2011-06-21 17:51                         ` Herbert Sitz
2011-06-21 17:52                       ` Eric Schulte
2011-06-27 18:09                         ` Herbert Sitz
2011-06-21 17:26                   ` Eric Schulte
2011-06-27 18:22                     ` Herbert Sitz
2011-06-20 21:18     ` Jambunathan K

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=87ei2ouwxk.fsf@gmail.com \
    --to=schulte.eric@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=hsitz@nwlink.com \
    --cc=nicholas.dokos@hp.com \
    /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).