On Sep 13, 2021, at 11:42 PM, Augusto Stoffel <arstoffel@gmail.com> wrote:

Hi Mark,

After removing the patch to ob-python.el and applying the attached patch
to python.el, could you please test that your notebooks work again?

This will probably not change the independent initialization problem
that you describe (but it might, who knows).  If you want to help me
understanding that part, you can do

 M-x trace-function RET python-shell-send-string-no-output
 M-x trace-function RET python-shell-output-filter-buffer
 M-x trace-function RET python-shell-completion-native-get-completions

and send the result printed to 'trace-buffer'.  Thanks!

<0001-Python-shell-rearrange-printing-of-newline-before-ou.patch>

That patch fixes the multiline result error for me with the unpatched ob-python.el. I noticed yesterday the command in the tmp file was prefixed with \n and now with the patch it is not and org babel processes it fine as an inline function call to add the result within the text of the document.

I was not able to find python-shell-output-filter-buffer to trace so I substituted python-shell-output-filter. Here is the trace-output for my initial C-c C-c on the call inline function. As you suspected the initialization problem is still there for me, but perhaps this will shed some light on that. Below this is the trace for the second and successful call to the inline function.

#+begin_example Trace from initial execution that requires C-g to break
======================================================================
1 -> (python-shell-send-string-no-output "def __PYTHON_EL_eval(source, filename):
    import ast, sys
    if sys.version_info[0] == 2:
        from __builtin__ import compile, eval, globals
    else:
        from builtins import compile, eval, globals
    try:
        p, e = ast.parse(source, filename), None
    except SyntaxError:
        t, v, tb = sys.exc_info()
        sys.excepthook(t, v, tb.tb_next)
        return
    if p.body and isinstance(p.body[-1], ast.Expr):
        e = p.body.pop()
    try:
        g = globals()
        exec(compile(p, filename, 'exec'), g, g)
        if e:
            return eval(compile(ast.Expression(e.value), filename, 'eval'), g, g)
    except Exception:
        t, v, tb = sys.exc_info()
        sys.excepthook(t, v, tb.tb_next)")
| 2 -> (python-shell-output-filter "Traceback (most recent call last):
  File \"<stdin>\", line 1, in <module>
NameError: name '__PYTHON_EL_eval' is not defined
")
| 2 <- python-shell-output-filter: ""
| 2 -> (python-shell-output-filter ">>> ")
| 2 <- python-shell-output-filter: ""
1 <- python-shell-send-string-no-output: "Traceback (most recent call last):
  File \"<stdin>\", line 1, in <module>
NameError: name '__PYTHON_EL_eval' is not defined"
======================================================================
1 -> (python-shell-send-string-no-output "def __PYTHON_EL_eval_file(filename, tempname, delete):
    import codecs, os, re
    pattern = r'^[ ]*#.*?coding[:=][ ]*([-_.a-zA-Z0-9]+)'
    with codecs.open(tempname or filename, encoding='latin-1') as file:
        match = re.match(pattern, file.readline())
        match = match or re.match(pattern, file.readline())
        encoding = match.group(1) if match else 'utf-8'
    with codecs.open(tempname or filename, encoding=encoding) as file:
        source = file.read().encode(encoding)
    if delete and tempname:
        os.remove(tempname)
    return __PYTHON_EL_eval(source, filename)")
| 2 -> (python-shell-output-filter ">>> ")
| 2 <- python-shell-output-filter: ""
1 <- python-shell-send-string-no-output: ""
#+end_example

There was nothing showing up in the trace, until I added the python-shell-send-string to the list of trace functions.
#+begin_example Existing Python session and now works without the ob-python.el patch that modified chomp value
======================================================================
1 -> (python-shell-send-string "try:
    with open('/var/folders/kf/zdpzgs9d30b3jj4bdkdjf1vw0000gn/T/babel-eaPALH/python-ca4zF3') as __org_babel_python_tmpfile:
        exec(compile(__org_babel_python_tmpfile.read(), __org_babel_python_tmpfile.name, 'exec'))
except:
    raise
finally:
    print('org_babel_python_eoe')")
1 <- python-shell-send-string: nil
#+end_example

FYI the contents of that tmp file is:
#+begin_example Tmp file contents
print("testing23", end=“")
#+end_example