flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
256 stars 25 forks source link

Problems in Terminal Python 3 and 2 #27

Closed ghost closed 5 years ago

ghost commented 5 years ago

When working in the terminal with the following code, an error occurs:

from pscript import py2js
def foo(a, b=2):
   print(a - b)
print(py2js(foo))
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/pscript/functions.py", line 77, in py2js_
    lines, linenr = inspect.getsourcelines(ob)
  File "/usr/lib/python3.6/inspect.py", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.6/inspect.py", line 786, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/pscript/functions.py", line 145, in py2js
    return py2js_(ob)
  File "/usr/local/lib/python3.6/dist-packages/pscript/functions.py", line 80, in py2js_
    (ob, err))
ValueError: Could not get source code for object <function foo at 0x7fc97016aea0>: could not get source code

When studying, it becomes clear that inspect.getsource(foo) gives an error when working in the terminal. But if you use the module dill then everything is ok.

from dill.source import getsource
print(getsource(foo))
almarklein commented 5 years ago

When working in the terminal

Do you mean that you type the lines in right at the terminal? In that cases, indeed, Python is not able to get the source code for these lines. Nothing we can do about that, unfortunately.

ghost commented 5 years ago

Everything works well when using dill

from dill.source import getsource print(getsource(foo))

almarklein commented 5 years ago

Ah, I read over that bit. That's interesting. Seems to be due to this. I'm not very enthusiastic about adding dill (or readline) as a dependency. Though maybe we can add terminal support (using readline as an optional dependency) using a littlebit of custom code that we extract from that dill module...

almarklein commented 5 years ago

Made #28 to describe and track the potential enhancement. Thanks!