prompt-toolkit / ptpython

A better Python REPL
BSD 3-Clause "New" or "Revised" License
5.19k stars 277 forks source link

[bug] conflict with latexify-py #513

Open Freed-Wu opened 1 year ago

Freed-Wu commented 1 year ago

See https://github.com/google/latexify_py/issues/129#issuecomment-1321738215

jonathanslenders commented 1 year ago

Looks like we have to attach the source code to a function when it's evaluated in the REPL. Do you know how we can do that?

odashi commented 1 year ago

Hi, maybe the cause comes from the line cache, because inspect.getsource eventually obtains the source from it. IPython implicitly sets its inputs to the cache and the created objects reflect the information about the underlying cache.

IPython:

In [1]: def f(): pass

In [2]: import linecache

In [3]: linecache.cache
Out[3]: 
{'<ipython-input-1-40d3b0035d30>': (14,
  None,
  ['def f(): pass\n'],
  '<ipython-input-1-40d3b0035d30>'),
 '<ipython-input-2-d25e465def14>': (17,
  None,
  ['import linecache\n'],
  '<ipython-input-2-d25e465def14>'),
 '<ipython-input-3-38e64b9ea04a>': (16,
  None,
  ['linecache.cache\n'],
  '<ipython-input-3-38e64b9ea04a>')}

In [4]: f.__code__
Out[4]: <code object f at 0x7ff79c706600, file "<ipython-input-1-40d3b0035d30>", line 1>

ptpython:

>>> def f(): pass
>>> import linecache
>>> linecache.cache
{}

>>> f.__code__
<code object f at 0x7fb720d0bcb0, file "<stdin>", line 1>