From cc89d5523fcf6786ea202ec765e656fc0c48b346 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Mon, 20 Jan 2020 17:40:22 -0800 Subject: ob-python: Fix several issues with :session :results value * lisp/ob-python.el (org-babel-python-evaluate-session): Fix a few related issues with :session :results value blocks, including broken if-else statements, indented blocks with blank lines, and returning the wrong value when underscore has been used. (org-babel-python--eval-ast): New constant variable, a string consisting of Python code to execute a source block using ast. Previously, python blocks with parameters ":session :results value" were entered line-by-line into the Python session, which could cause issues around indentation and new lines. Now, such python blocks are written to temp files, then the built-in ast python module is used to parse and execute them, and to extract the last line separately to return as a result. Introduces a change in behavior, requiring that the last line must be a top-level expression statement if its result is to be saved (otherwise, the result is None). --- testing/lisp/test-ob-python.el | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'testing/lisp/test-ob-python.el') diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el index 48ca3d64088..7e2826404f2 100644 --- a/testing/lisp/test-ob-python.el +++ b/testing/lisp/test-ob-python.el @@ -138,6 +138,41 @@ if True: (org-babel-execute-maybe) (org-babel-execute-src-block))))) +(ert-deftest test-ob-python/if-else-block () + (should + (equal "success" (org-test-with-temp-text "#+begin_src python :session :results value +value = 'failure' +if False: + pass +else: + value = 'success' +value +#+end_src" + (org-babel-execute-src-block))))) + +(ert-deftest test-ob-python/indent-block-with-blank-lines () + (should + (equal 20 + (org-test-with-temp-text "#+begin_src python :session :results value + foo = 0 + for i in range(10): + foo += 1 + + foo += 1 + + foo +#+end_src" + (org-babel-execute-src-block))))) + +(ert-deftest test-ob-python/assign-underscore () + (should + (equal "success" + (org-test-with-temp-text "#+begin_src python :session :results value +_ = 'failure' +'success' +#+end_src" + (org-babel-execute-src-block))))) + (provide 'test-ob-python) ;;; test-ob-python.el ends here -- cgit v1.2.1