martinoidar <martinoi...@gmail.com> writes:

> Hi,
> Any reuse of the variable named =f= in python session
> causes an error.
>
> Minimal example:
>
> #+begin_src python :session *PY* :results output
> f = 2
> print(f)
> #+end_src
>
> #+RESULTS:
> : 2
>
> #+begin_src python :session *PY* :results output
> print(f)
> #+end_src
>
> #+RESULTS:
> : <_io.TextIOWrapper name='/tmp/babel-8s9Pg6/python-P9oMwA' mode='r' 
> encoding='UTF-8'>

Thanks for reporting.  It looks like the bug was introduced in 2023 when
we added support for using ":results value" with pandas, numpy, and
matplotlib objects.  I am attaching a fix.

Ihor -- normally I would just push this to main, but I think it might be
worth pushing this to bugfix branch instead, so I wanted to check with
you first if it's alright for me to do that.

>From a8a0d4ac0ddad342b47660f65a594e3907421826 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackk...@gmail.com>
Date: Fri, 13 Jun 2025 09:23:26 -0700
Subject: [PATCH] ob-python: Fix bug preventing users from using variables
 named "f"

Fixes https://list.orgmode.org/75059a24-4e56-4b9d-ab71-1c4db00db...@gmail.com/T/#u

* lisp/ob-python.el (org-babel-python--def-format-value):
(org-babel-python-evaluate-session): Replace python variable "f" with
"__org_babel_python_tmpfile", following the convention used in
`org-babel-python-format-session-value'
---
 lisp/ob-python.el | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index eb5c1379a..36bc428f5 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -158,14 +158,14 @@ (defconst org-babel-python--output-graphics-wrapper "\
 
 (defconst org-babel-python--def-format-value "\
 def __org_babel_python_format_value(result, result_file, result_params):
-    with open(result_file, 'w') as f:
+    with open(result_file, 'w') as __org_babel_python_tmpfile:
         if 'graphics' in result_params:
             result.savefig(result_file)
         elif 'pp' in result_params:
             import pprint
-            f.write(pprint.pformat(result))
+            __org_babel_python_tmpfile.write(pprint.pformat(result))
         elif 'list' in result_params and isinstance(result, dict):
-            f.write(str(['{} :: {}'.format(k, v) for k, v in result.items()]))
+            __org_babel_python_tmpfile.write(str(['{} :: {}'.format(k, v) for k, v in result.items()]))
         else:
             if not set(result_params).intersection(\
 ['scalar', 'verbatim', 'raw']):
@@ -198,7 +198,7 @@ (defconst org-babel-python--def-format-value "\
                             result = result.tolist()
                         else:
                             result = repr(result)
-            f.write(str(result))"
+            __org_babel_python_tmpfile.write(str(result))"
   "Python function to format value result and save it to file.")
 
 (defun org-babel-variable-assignments:python (params)
@@ -495,8 +495,8 @@ (defun org-babel-python-evaluate-session
             (pcase result-type
 	      (`output
 	       (let ((body (format "\
-with open('%s') as f:
-    exec(compile(f.read(), f.name, 'exec'))"
+with open('%s') as __org_babel_python_tmpfile:
+    exec(compile(__org_babel_python_tmpfile.read(), __org_babel_python_tmpfile.name, 'exec'))"
 				   (org-babel-process-file-name
 				    tmp-src-file 'noquote))))
 		 (org-babel-python-send-string session body)))
-- 
2.49.0

Reply via email to