Skip to content

Instantly share code, notes, and snippets.

@glittershark
Created February 1, 2024 16:44
Show Gist options
  • Save glittershark/cc8468966b0c9bf79f3f559abed39bd6 to your computer and use it in GitHub Desktop.
Save glittershark/cc8468966b0c9bf79f3f559abed39bd6 to your computer and use it in GitHub Desktop.
Get exceptions in org-babel-python when using sessions
(defun ob-python--eval-python-session-with-exceptions
(orig session body &rest args)
(let* ((exc-file (make-temp-file "session-exception")))
(unwind-protect
(let* ((body (format "\
try:
%s
except:
import traceback
with open(\"%s\", \"w\") as f:
f.write(traceback.format_exc())
raise"
(org-babel-python--shift-right body 4)
exc-file))
(result (apply orig session body args))
(exc-string (with-temp-buffer
(insert-file-contents exc-file)
(buffer-string))))
(when (not (string-empty-p exc-string))
(org-babel-eval-error-notify nil exc-string))
result)
(delete-file exc-file))))
(advice-add
'org-babel-python-evaluate-session
:around
#'ob-python--eval-python-session-with-exceptions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment