Browse Source

ob-python: Rename exec tmpfile handle to prevent conflict

* lisp/ob-python.el (org-babel-python--exec-tmpfile): Rename tmpfile handle
(org-babel-python-format-session-value): Rename tmpfile handle

Opening the exec tmpfile as a `f' variable shadows any such variable
that might by defined by the Python session context. e.g. my Org babel
files commonly pass single letter variables inside a session which is
broken by this behavior.

The new name `__org_babel_python_tmpfile' is in line with other org
mode specific Python variables set by ob-python.  This is unlikely to
conflict with the user's Python code.

Adrian Kummerlaender 3 months ago
1 changed files with 7 additions and 7 deletions
  1. 7 7

+ 7 - 7

@@ -244,8 +244,8 @@ def main():
 open('%s', 'w').write( pprint.pformat(main()) )")
 (defconst org-babel-python--exec-tmpfile "\
-with open('%s') as f:
-    exec(compile(,, 'exec'))"
+with open('%s') as __org_babel_python_tmpfile:
+    exec(compile(,, 'exec'))"
   "Template for Python session command with output results.
 Has a single %s escape, the tempfile containing the source code
@@ -256,20 +256,20 @@ to evaluate.")
   "Return Python code to evaluate SRC-FILE and write result to RESULT-FILE."
   (format "\
 import ast
-with open('%s') as f:
-    __org_babel_python_ast = ast.parse(
+with open('%s') as __org_babel_python_tmpfile:
+    __org_babel_python_ast = ast.parse(
 __org_babel_python_final = __org_babel_python_ast.body[-1]
 if isinstance(__org_babel_python_final, ast.Expr):
     __org_babel_python_ast.body = __org_babel_python_ast.body[:-1]
     exec(compile(__org_babel_python_ast, '<string>', 'exec'))
     __org_babel_python_final = eval(compile(ast.Expression(
         __org_babel_python_final.value), '<string>', 'eval'))
-    with open('%s', 'w') as f:
+    with open('%s', 'w') as __org_babel_python_tmpfile:
         if %s:
             import pprint
-            f.write(pprint.pformat(__org_babel_python_final))
+            __org_babel_python_tmpfile.write(pprint.pformat(__org_babel_python_final))
-            f.write(str(__org_babel_python_final))
+            __org_babel_python_tmpfile.write(str(__org_babel_python_final))
     exec(compile(__org_babel_python_ast, '<string>', 'exec'))
     __org_babel_python_final = None"