From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kyle Meyer Subject: Re: Extraneous output from Python code blocks using :session option Date: Fri, 13 Mar 2015 10:40:21 -0400 Message-ID: <877fult0yy.fsf@kmlap.domain.org> References: <1931A590-8B23-4412-86C7-F3571EC466FF@haas.berkeley.edu> <87r3sv2xra.fsf@kyleam.com> <210D96E7-43E3-4438-8401-7841C6612023@haas.berkeley.edu> <87bnjxq59p.fsf@kyleam.com> <87d24dtmck.fsf@kmlap.domain.org> <87ioe5xq8a.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:58979) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWQh4-0005mv-6Z for emacs-orgmode@gnu.org; Fri, 13 Mar 2015 10:36:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YWQh0-0006RV-35 for emacs-orgmode@gnu.org; Fri, 13 Mar 2015 10:36:06 -0400 Received: from mail-qc0-f177.google.com ([209.85.216.177]:37343) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWQgz-0006RG-UO for emacs-orgmode@gnu.org; Fri, 13 Mar 2015 10:36:02 -0400 Received: by qcxr5 with SMTP id r5so27026143qcx.4 for ; Fri, 13 Mar 2015 07:36:01 -0700 (PDT) In-Reply-To: <87ioe5xq8a.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Fri, 13 Mar 2015 09:21:09 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Richard Stanton Cc: emacs-orgmode@gnu.org, John Kitchin --=-=-= Content-Type: text/plain Nicolas Goaziou wrote: > Hello, > > Kyle Meyer writes: > >> I've attached two patches. The first one is a proposed way to deal with >> the indentation issues in sessions. It is very similar to what >> python.el does for multiline input (use a temporary file and then >> execute that from the shell). The second is an update of my previous >> patch to remove shell prompt characters that are leaking into the >> output. > > Thank you. > > I'll just comment about code, not functionality. Thanks for your comments. I've attached updated patches. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-ob-python.el-Allow-indented-code-in-sessions.patch >From 41aae465038fd789fe92611ecf1fae7e9d08ab5b Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 13 Mar 2015 02:45:04 -0400 Subject: [PATCH 1/2] ob-python.el: Allow indented code in sessions * lisp/ob-python.el (org-babel-python-evaluate-session): Recognize indented code in session and treat it differently to avoid syntax errors. For session blocks with results set to 'output', go through an intermediate file when there is indented code to prevent indentation-related errors when sending code to the Python shell. For session blocks with results set to 'value', issue an user-error. These (usually) would have resulted in a syntax error in the shell anyway, and the '_' variable can't be used to get the last value here (as it is for non-indented code) because it isn't set when executing the code through a file. --- lisp/ob-python.el | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index dd3cc66..0dc74ca 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -238,6 +238,14 @@ (defconst org-babel-python-pp-wrapper-method open('%s', 'w').write( pprint.pformat(main()) )") +(defconst org-babel-python-indented-lines-session-method + (concat "fname= '%s'; fh = open(fname); " + "exec(compile(fh.read(), fname, 'exec')); " + "fh.close()") + "Single line to execute indented Python code in session. +%s will be formatted with the file name of the file containing +the code.") + (defun org-babel-python-evaluate (session body &optional result-type result-params preamble) "Evaluate BODY as Python code." @@ -303,6 +311,13 @@ (defun org-babel-python-evaluate-session (mapc (lambda (line) (insert line) (funcall send-wait)) (split-string body "[\r\n]")) (funcall send-wait))) + (indented-p (org-string-match-p "^[ \t]" body)) + (body (if indented-p + (let ((tmp-file (org-babel-temp-file "python-"))) + (with-temp-file tmp-file (insert body)) + (format org-babel-python-indented-lines-session-method + tmp-file)) + body)) (results (case result-type (output @@ -317,6 +332,8 @@ (defun org-babel-python-evaluate-session (funcall send-wait)) 2) "\n")) (value + (when indented-p + (user-error "Value output limited to unindented lines with session")) (let ((tmp-file (org-babel-temp-file "python-"))) (org-babel-comint-with-output (session org-babel-python-eoe-indicator nil body) -- 2.3.1 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0002-ob-python.el-Strip-leading-session-characters.patch >From 28b6a003e4ae1dbb1474350203444e76df9e8572 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 13 Mar 2015 02:46:38 -0400 Subject: [PATCH 2/2] ob-python.el: Strip leading session characters * lisp/ob-python.el (org-babel-python-evaluate-session): Strip extra leading "..." and ">>>" from session output. --- lisp/ob-python.el | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 0dc74ca..e976d9c 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -321,16 +321,17 @@ (defun org-babel-python-evaluate-session (results (case result-type (output - (mapconcat - #'org-babel-trim - (butlast - (org-babel-comint-with-output - (session org-babel-python-eoe-indicator t body) - (funcall input-body body) - (funcall send-wait) (funcall send-wait) - (insert org-babel-python-eoe-indicator) - (funcall send-wait)) - 2) "\n")) + (org-babel-python--strip-session-chars + (mapconcat + #'org-babel-chomp + (butlast + (org-babel-comint-with-output + (session org-babel-python-eoe-indicator t body) + (funcall input-body body) + (funcall send-wait) (funcall send-wait) + (insert org-babel-python-eoe-indicator) + (funcall send-wait)) + 2) "\n"))) (value (when indented-p (user-error "Value output limited to unindented lines with session")) @@ -356,6 +357,15 @@ (defun org-babel-python-read-string (string) (match-string 1 string) string)) +(defun org-babel-python--strip-session-chars (string) + "Remove leading '>>>' and '...' from STRING. +`org-babel-comint-with-output' splits the Python session output +by `comint-prompt-regexp' (which includes '>>>' and '...'), but, +in some situations, these still make it through at the start of +the output string." + (replace-regexp-in-string "\\`\\s-*\n\\(>>> \\|\\.\\.\\. \\)*" "" + string)) + (provide 'ob-python) -- 2.3.1 --=-=-= Content-Type: text/plain -- Kyle --=-=-=--