[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Fix several issues with python session value blocks
From: |
Jack Kamm |
Subject: |
[PATCH] Fix several issues with python session value blocks |
Date: |
Mon, 20 Jan 2020 18:26:08 -0800 |
This patch fixes several related issues with python blocks with
parameters ":session :results value", including:
- Broken if-else and try-except statements.
- Correctly parsing blank lines in indented blocks.
- Returning the correct value when the underscore "_" variable
has been assigned.
It works by using the built-in ast python module to parse the source
block, then executes it, evaluating the last line separately to store
the result.
Note this patch also creates a slight change in behavior: the final
result must be a top-level expression, otherwise we return "None" as the
result.
There is some useful background and discussion of the issues here:
https://lists.gnu.org/archive/html/emacs-orgmode/2017-11/msg00274.html
In that thread from 2017, we solved similar problems for the ":results
output" case. With this patch, I'm now circling back to try and fix the
":results value" case.
I've checked this patch works for both Python2 and Python3, as well as
for IPython.
Here are some examples that this patch fixes. I've also added them as
unit tests:
This block should return 20, but previously it raised an
IndentationError and then returned 10:
#+begin_src python :session :results value
foo = 0
for i in range(10):
foo += 1
foo += 1
foo
#+end_src
This block should return "success", but previously it raised a
SyntaxError and then returned "failure":
#+begin_src python :session :results value
value = "failure"
if False:
pass
else:
value = "success"
value
#+end_src
This block should return "success", but previously it returned "failure":
#+begin_src python :session :results value
_ = "failure"
"success"
#+end_src
0001-ob-python-Fix-several-issues-with-session-results-va.patch
Description: Text Data
- [PATCH] Fix several issues with python session value blocks,
Jack Kamm <=