From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kamm Subject: Re: ob-python newline & indentation behavior Date: Sat, 09 Dec 2017 12:12:07 +0000 Message-ID: <878tecqew8.fsf@gmail.com> References: <87h8trtyoa.fsf@kyleam.com> <8760a5tqjo.fsf@kyleam.com> <87r2ssp99s.fsf@kyleam.com> <87609wl6aq.fsf@kyleam.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53503) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNdyM-0006CU-Ae for emacs-orgmode@gnu.org; Sat, 09 Dec 2017 07:11:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNdyJ-0005cY-4x for emacs-orgmode@gnu.org; Sat, 09 Dec 2017 07:11:14 -0500 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:43798) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNdyI-0005ak-QO for emacs-orgmode@gnu.org; Sat, 09 Dec 2017 07:11:11 -0500 Received: by mail-wm0-x22e.google.com with SMTP id n138so7076208wmg.2 for ; Sat, 09 Dec 2017 04:11:10 -0800 (PST) In-reply-to: <87609wl6aq.fsf@kyleam.com> 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" To: Kyle Meyer Cc: emacs-orgmode@gnu.org Kyle Meyer writes: > Hmm, according to the site below, a response should come within 5 > business days. That fits with my personal experience, but I'm not sure > what's typical. Also, the delay might be Thanksgiving-related. FSF sent me the papers which I signed and returned about a week ago, am just waiting for the final confirmation now. >> Given how broken ":session :results value" is, and the difficulty in >> implementing it correctly, I agree with Kyle's suggestion to remove it. >> Should we start another thread to discuss this? > > I think that'd be good since 1) someone with an opinion on that topic > might not notice it within this thread and 2) it's now orthogonal to > this patch. Great, I'll start another thread in a few days, summarizing the various options you laid out before. Another idea I had was to have some special "result" variable that could be assigned to. Anyways, we can discuss more in the new thread. > ... (suggestions to improve patch) I've incorporated all your suggestions/improvements into the patch, please see below. Thanks again for spending time to review it. >From 40e961e349fdb347fbf9d59b3aca58163749f804 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Sat, 2 Dec 2017 09:03:00 +0000 Subject: [PATCH] ob-python: Fix :session :results output multiline behavior * lisp/ob-python.el (orb-babel-python-evaluate-session): When :session :results output, send multiline code blocks to tmpfile and execute in Python with exec(). (org-babel-python--exec-tmpfile): New function. * testing/lisp/test-ob-python.el (test-ob-python/session-multiline): Test for :session with multiple lines and indentation. --- lisp/ob-python.el | 36 ++++++++++++++++++++++++++---------- testing/lisp/test-ob-python.el | 17 +++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 60ec5fa47..b3f50cfe5 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -239,6 +239,15 @@ def main(): open('%s', 'w').write( pprint.pformat(main()) )") +(defconst org-babel-python--exec-tmpfile + (concat + "__org_babel_python_fname = '%s'; " + "__org_babel_python_fh = open(__org_babel_python_fname); " + "exec(compile(" + "__org_babel_python_fh.read(), __org_babel_python_fname, 'exec'" + ")); " + "__org_babel_python_fh.close()")) + (defun org-babel-python-evaluate (session body &optional result-type result-params preamble) "Evaluate BODY as Python code." @@ -306,16 +315,23 @@ last statement in BODY, as elisp." (results (pcase result-type (`output - (mapconcat - #'org-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")) + (let ((body (if (string-match-p ".\n+." body) ; Multiline + (let ((tmp-src-file (org-babel-temp-file + "python-"))) + (with-temp-file tmp-src-file (insert body)) + (format org-babel-python--exec-tmpfile + tmp-src-file)) + body))) + (mapconcat + #'org-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"))) (`value (let ((tmp-file (org-babel-temp-file "python-"))) (org-babel-comint-with-output diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el index d9fca220f..915b1bc77 100644 --- a/testing/lisp/test-ob-python.el +++ b/testing/lisp/test-ob-python.el @@ -101,6 +101,23 @@ return x (should (equal '(("col") ("a") ("b")) (org-babel-execute-src-block))))) +(ert-deftest test-ob-python/session-multiline () + ;; FIXME workaround to prevent starting prompt leaking into output + (run-python) + (sleep-for 0 10) + (org-test-with-temp-text " +#+begin_src python :session :results output + foo = 0 + for _ in range(10): + foo += 1 + + foo += 1 + + print(foo) +#+end_src" + (org-babel-next-src-block) + (should (equal "20" (org-babel-execute-src-block))))) + (provide 'test-ob-python) ;;; test-ob-python.el ends here -- 2.15.1