From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jack Kamm Subject: [PATCH] (Tiny) Tweak Python session null return value Date: Mon, 17 Feb 2020 08:24:18 -0800 Message-ID: <87a75hfah9.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:56041) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j3jD6-0008Qd-6f for emacs-orgmode@gnu.org; Mon, 17 Feb 2020 11:25:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j3jD4-0005fB-Uk for emacs-orgmode@gnu.org; Mon, 17 Feb 2020 11:25:28 -0500 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane-mx.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org, Bastien --=-=-= Content-Type: text/plain Hi, Below is a very small patch to Python session blocks, to make them return a blank result (empty string) instead of None when there is no return value. Normally I would push this myself, but since we are so close to 9.4, I thought it prudent to mail a patch and let the maintainers handle it. It would be nice to include in 9.4, but not a big deal if I've missed the window. Now for an explanation of the patch: 9.4 changes Python session blocks to fix several bugs and improve robustness overall [0]. However, there is a cost to these fixes, which is that the session blocks can only return a result when it is a top-level expression on the last line of the block. If the last line is not a top-level expression, the block would previously print "None". However, after some testing, I think this is a little counter-intuitive, and it would be better if it returned a blank (empty) result instead of "None". [0] https://lists.gnu.org/archive/html/emacs-orgmode/2020-01/msg00190.html Best, Jack --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-python-Session-returns-empty-string-if-no-return-.patch >From 0b44c3f1c7454e7948cd34eb02995924046b6976 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Mon, 17 Feb 2020 08:11:49 -0800 Subject: [PATCH] ob-python: Session returns empty string if no return value * lisp/ob-python.el (org-babel-python--eval-ast): Change sessions to return an empty string, instead of None, if there is no return value. --- etc/ORG-NEWS | 12 +++++++----- lisp/ob-python.el | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index b6ee443e4..1b5429870 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -12,13 +12,15 @@ Please send Org bug reports to mailto:emacs-orgmode@gnu.org. * Version 9.4 (not yet released) ** Incompatible changes -*** Python session return values must be top-level expression statements +*** Change how Python sessions handle return values Python blocks with ~:session :results value~ header arguments now only -return a value if the last line is a top-level expression statement, -otherwise the result is None. Also, None will now show up under -"#+RESULTS:", as it already did with ~:results value~ for non-session -blocks. +return a value if the last line is a top-level expression +statement. If the last line is not a top-level expression statement, +the block will return a blank (empty) result. + +Also, a return value of ~None~ will now show up under "#+RESULTS:", as +it already did for non-session blocks. *** In HTML export, change on how outline-container-* is set diff --git a/lisp/ob-python.el b/lisp/ob-python.el index de718b04b..b9e36dba0 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -260,7 +260,7 @@ (defconst org-babel-python--eval-ast "\ __org_babel_python_final.value), '', 'eval')) else: exec(compile(__org_babel_python_ast, '', 'exec')) - __org_babel_python_final = None + __org_babel_python_final = '' except Exception: from traceback import format_exc __org_babel_python_final = format_exc() -- 2.25.0 --=-=-=--