diff options
| author | Jack Kamm | 2020-01-20 17:40:22 -0800 |
|---|---|---|
| committer | Jack Kamm | 2020-02-03 21:17:38 -0800 |
| commit | cc89d5523fcf6786ea202ec765e656fc0c48b346 (patch) | |
| tree | 95bd2b9d940f82158360eb210485ad0f013718d9 /testing/lisp/test-ob-python.el | |
| parent | e076ed6e8508d177a6323638b00e4e1661fc83f5 (diff) | |
| download | emacs-cc89d5523fcf6786ea202ec765e656fc0c48b346.tar.gz emacs-cc89d5523fcf6786ea202ec765e656fc0c48b346.zip | |
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).
Diffstat (limited to 'testing/lisp/test-ob-python.el')
| -rw-r--r-- | testing/lisp/test-ob-python.el | 35 |
1 files changed, 35 insertions, 0 deletions
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: | |||
| 138 | (org-babel-execute-maybe) | 138 | (org-babel-execute-maybe) |
| 139 | (org-babel-execute-src-block))))) | 139 | (org-babel-execute-src-block))))) |
| 140 | 140 | ||
| 141 | (ert-deftest test-ob-python/if-else-block () | ||
| 142 | (should | ||
| 143 | (equal "success" (org-test-with-temp-text "#+begin_src python :session :results value | ||
| 144 | value = 'failure' | ||
| 145 | if False: | ||
| 146 | pass | ||
| 147 | else: | ||
| 148 | value = 'success' | ||
| 149 | value | ||
| 150 | #+end_src" | ||
| 151 | (org-babel-execute-src-block))))) | ||
| 152 | |||
| 153 | (ert-deftest test-ob-python/indent-block-with-blank-lines () | ||
| 154 | (should | ||
| 155 | (equal 20 | ||
| 156 | (org-test-with-temp-text "#+begin_src python :session :results value | ||
| 157 | foo = 0 | ||
| 158 | for i in range(10): | ||
| 159 | foo += 1 | ||
| 160 | |||
| 161 | foo += 1 | ||
| 162 | |||
| 163 | foo | ||
| 164 | #+end_src" | ||
| 165 | (org-babel-execute-src-block))))) | ||
| 166 | |||
| 167 | (ert-deftest test-ob-python/assign-underscore () | ||
| 168 | (should | ||
| 169 | (equal "success" | ||
| 170 | (org-test-with-temp-text "#+begin_src python :session :results value | ||
| 171 | _ = 'failure' | ||
| 172 | 'success' | ||
| 173 | #+end_src" | ||
| 174 | (org-babel-execute-src-block))))) | ||
| 175 | |||
| 141 | (provide 'test-ob-python) | 176 | (provide 'test-ob-python) |
| 142 | 177 | ||
| 143 | ;;; test-ob-python.el ends here | 178 | ;;; test-ob-python.el ends here |