summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Kamm <jackkamm@gmail.com>2020-06-06 10:59:23 -0700
committerJack Kamm <jackkamm@gmail.com>2020-06-09 21:01:12 -0700
commit3cec17cde5ad7b26295b5bf919ea3e45cf9bbd40 (patch)
treec3bc6b064d3539f14ea5533a59ba9deb03f7457f
parent99acb17d06e0c7549841ea6cb7c6ce4df927c6c6 (diff)
downloadorg-mode-3cec17cde5ad7b26295b5bf919ea3e45cf9bbd40.tar.gz
ob-python.el: Fix multiline strings in non-session :results value
* lisp/ob-python.el (org-babel-python-evaluate-external-process): Use functions from python.el to indent lines, avoiding multiline strings. * testing/lisp/test-ob-python.el (test-ob-python/multiline-var): Set test as expected to succeed. (test-ob-python/multiline-str): Add test for multiline string in body. (test-ob-python/header-var-assignment): Test that :var is in correct scope and can be assigned to. cf. https://orgmode.org/list/87tv009l9a.fsf@gmail.com/#t
-rw-r--r--lisp/ob-python.el18
-rw-r--r--testing/lisp/test-ob-python.el20
2 files changed, 32 insertions, 6 deletions
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index dbcfac0..69312f2 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -33,6 +33,8 @@
(declare-function py-shell "ext:python-mode" (&rest args))
(declare-function py-toggle-shells "ext:python-mode" (arg))
(declare-function run-python "ext:python" (&optional cmd dedicated show))
+(declare-function python-syntax-context "ext:python" (&rest args))
+(declare-function python-indent-shift-right "ext:python" (&rest args))
(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("python" . "py"))
@@ -296,11 +298,17 @@ last statement in BODY, as elisp."
(if (member "pp" result-params)
org-babel-python-pp-wrapper-method
org-babel-python-wrapper-method)
- (mapconcat
- (lambda (line) (format "\t%s" line))
- (split-string (org-remove-indentation (org-trim body))
- "[\r\n]")
- "\n")
+ (with-temp-buffer
+ (require 'python)
+ (python-mode)
+ (insert body)
+ (goto-char (point-min))
+ (while (not (eobp))
+ (unless (python-syntax-context 'string)
+ (python-indent-shift-right (line-beginning-position)
+ (line-end-position)))
+ (forward-line 1))
+ (buffer-string))
(org-babel-process-file-name tmp-file 'noquote))))
(org-babel-eval-read-file tmp-file))))))
(org-babel-result-cond result-params
diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el
index 763942b..e3f0571 100644
--- a/testing/lisp/test-ob-python.el
+++ b/testing/lisp/test-ob-python.el
@@ -174,7 +174,6 @@ _ = 'failure'
(org-babel-execute-src-block)))))
(ert-deftest test-ob-python/multiline-var ()
- :expected-result :failed
(should
(equal "a\nb\nc"
(org-test-with-temp-text "#+begin_src python :var text=\"a\\nb\\nc\"
@@ -182,6 +181,25 @@ return text
#+end_src"
(org-babel-execute-src-block)))))
+(ert-deftest test-ob-python/multiline-str ()
+ (should
+ (equal "a\nb\nc"
+ (org-test-with-temp-text "#+begin_src python
+text=\"a\\nb\\nc\"
+return text
+#+end_src"
+ (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-python/header-var-assignment ()
+ (should
+ (equal "success"
+ (org-test-with-temp-text "#+begin_src python :var text=\"failure\"
+text
+text=\"success\"
+return text
+#+end_src"
+ (org-babel-execute-src-block)))))
+
(provide 'test-ob-python)
;;; test-ob-python.el ends here