Show make Python exceptions visible executing `ob-python' blocks

This commit is contained in:
Gerard Vermeulen 2024-05-15 15:15:29 +02:00
parent 0c1995e7c3
commit 3d55a73be0

View File

@ -3650,6 +3650,43 @@ define the [[https://orgmode.org/][Org mode]] =kbd= macro in listing
(latex (format "@@latex:\\colorbox{PowderBlue}{\\texttt{%s}}@@" keys)))))
#+end_src
*** Handle Python exceptions in ~ob-python~
:PROPERTIES:
:CUSTOM_ID: sec:handle-python-exceptions
:END:
Python exceptions are never visible while executing Python ~org-src-mode~
blocks. Redirecting Python exceptions is a way to make them visible. Listing
[[lst:redirect-python-exceptions]] redirects Python exceptions to the file
~stderr.arm~, listing [[lst:open-python-exceptions]] opens the file ~stderr.arm~ in
~auto-revert-mode~, and listing [[lst:demo-python-exceptions]] demonstrates the
redirection of Python exceptions to the file ~stderr.arm~.
#+caption[Redirect Python exceptions to a file]:
#+caption: Redirect Python exceptions to a file:
#+name: lst:redirect-python-exceptions
#+begin_src python -i -n :results silent :session :tangle no
import sys
file = open("stderr.arm", "w")
sys.stderr = file
#+end_src
#+caption[Open the redirected file in ~auto-revert-mode~]:
#+caption: Open the redirected file in ~auto-revert-mode~.
#+name: lst:open-python-exceptions
#+begin_src emacs-lisp -n :results silent :tangle no
(find-file "stderr.arm")
(auto-revert-mode)
#+end_src
#+caption[Demonstrate the redirection of Python exceptions to a file]:
#+caption: Demonstrate the redirectio of Python exceptions to a file.
#+name: lst:demo-python-exceptions
#+begin_src python -i -n :results silent :session :tangle no
variable
#+end_src
*** [[https://orgmode.org/worg/org-tests/index.html][Testing Org]]
:PROPERTIES:
:CUSTOM_ID: sec:testing-org