From: Jack Kamm <jackkamm@gmail.com>
To: Kyle Meyer <kyle@kyleam.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: ob-python newline & indentation behavior
Date: Mon, 18 Dec 2017 08:22:52 +0000 [thread overview]
Message-ID: <87mv2gxx5v.fsf@gmail.com> (raw)
In-Reply-To: <878tecqew8.fsf@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 141 bytes --]
FSF sent me the signed papers :) I've attached them.
Could Kyle or someone else merge in the patch, which I am reattaching?
Thanks,
Jack
[-- Attachment #2: Kamm.1253922.EMACS.pdf --]
[-- Type: application/pdf, Size: 1434020 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-ob-python-Fix-session-results-output-multiline-behav.patch --]
[-- Type: text/x-patch, Size: 3441 bytes --]
From bab63ad0941f45333208a8b690a38c0f51b568be Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
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
next prev parent reply other threads:[~2017-12-18 8:21 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-18 12:02 ob-python newline & indentation behavior Jack Kamm
2017-11-18 15:05 ` Kyle Meyer
2017-11-18 22:15 ` Jack Kamm
2017-11-19 3:27 ` Martin Alsinet
2017-11-19 3:34 ` Martin Alsinet
2017-11-19 7:41 ` Jack Kamm
2017-11-20 6:25 ` Kyle Meyer
2017-11-20 21:31 ` Jack Kamm
2017-11-21 4:04 ` Kyle Meyer
2017-11-21 8:28 ` Jack Kamm
2017-11-26 2:45 ` Ista Zahn
2017-11-26 8:25 ` Jack Kamm
2017-11-26 8:15 ` Jack Kamm
2017-11-27 4:00 ` Kyle Meyer
2017-12-09 12:12 ` John Kamm
2017-12-18 8:22 ` Jack Kamm [this message]
2017-12-18 11:44 ` Kyle Meyer
2017-12-19 5:11 ` Kyle Meyer
2017-11-19 17:17 ` Kyle Meyer
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=87mv2gxx5v.fsf@gmail.com \
--to=jackkamm@gmail.com \
--cc=emacs-orgmode@gnu.org \
--cc=kyle@kyleam.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).