New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
code.interact() does not work right #853
Comments
I've noticed some progress on this one. I get the first
or
Depending on the input. Anything that raises an exception, even on one line, like |
This must win the award of the longest time ever to fix an issue in Brython ;-) |
Cool! I tried using it. Still doesn't work right. My test page: <script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython.js">
</script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython_stdlib.js">
</script>
<body onload="brython()">
<script type="text/python">
from code import interact
interact()
</script>
</body> On Firefox (on Windows, after some load time. The prompts are in pop-up dialogs, and the outputs are in the javascript console.) Python 3.8.0 (default, 2019-12-28 21:59:51.902843)
[Javascript 1.5] on Brython on brython
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 1 # Notice that no value is printed!
>>> print(1) # But this works.
1
>>> def hi(name):
... print('hi', name)
Javascript exception: TypeError: "line_info is undefined"
brython master/www/src/brython.js:7686
brython master/www/src/brython.js:6446
brython master/www/src/brython.js line 9011 > Function:410
brython master/www/src/brython.js line 9011 > Function:99
brython master/www/src/brython.js line 9011 > Function:573
brython master/www/src/brython.js line 9011 > Function:131
brython master/www/src/brython.js line 9011 > Function:870
brython master/www/src/brython.js line 9011 > Function:781
brython master/www/src/brython.js line 9011 > Function:1030
brython master/www/src/brython.js line 5240 > Function:16
brython master/www/src/brython.js:5240
brython master/www/src/brython.js:5171
brython master/www/src/brython.js:5182
This repeats several times, then.
What am I doing wrong? |
…f JS eval, the last evaluated value is returned by the function. Exceptions in exec have attribute $stack set to the part of frames_stack starting with the calling frame. eval(source) prints the result if source is a code object with filename = "<console>" and mode = "single", and the return value is not None. Changes required for issue #853.
You did nothing wrong, it's the issue that was not really fixed... |
Interact does show the repr of evaluated expressions now. But I still can't define functions of more than one line in the interactive session. It's the same traceback as before complaining about I wonder if my test page is using the new version or caching the last one. I tried a force-refresh. I don't know how that interacts with the indexedDB though. >> __BRYTHON__.compiled_date
"2019-12-30 22:24:19.377720" That part looks right. But the stdlib is a separate file. |
I suspect that it's because the debug mode is 0, which happens if you call the function There is nothing I can do for the multi-line input in the prompt box : unfortunately, with Javascript it is impossible to define a function that would have the same effect as It is possible to simulate the Python interactive interpreter in Brython (this is what the console does), but not with |
This seems like an unrelated issue, since So, for example, in CPython's
Then the equivalent I expect from Brython would have five
[This is where it is crashing, currently.]
[empty]
[Prints "hi Bob" to js console.]
[end of example] |
In fact, we can remove the Take this example in CPython: >>> from code import interact
>>> stream = iter("""
... def hi(name):
... print('hi', name)
...
... hi('Bob')
... """.split('\n'))
>>> interact(readfunc=lambda _: next(stream))
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
hi Bob
Traceback (most recent call last):
...
StopIteration
>>> Note that it did print "hi Bob" before crashing when the input iterator ran out. Let's try that in Brython: <script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython.js">
</script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython_stdlib.js">
</script>
<body onload="brython(1)">
<script type="text/python">
from code import interact
stream = iter("""
def hi(name):
print('hi', name)
hi('Bob')
""".split('\n'))
interact(readfunc=lambda _: next(stream))
</script>
</body> It still crashes before the "hi Bob".
[repeated 7 times]
|
@gilch Thanks for the explanation, I think I understand what is going on. I tried your example in Firefox, Edge and Chrome on Windows 10, and always got the expected result: The page <!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython.js">
</script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython_stdlib.js">
</script>
</head>
<body onload="brython(1)">
<script type="text/python">
from code import interact
stream = iter("""
def hi(name):
print('hi', name)
hi('Bob')
""".split('\n'))
interact(readfunc=lambda _: next(stream))
</script>
</body>
</html> But when I disable indexedDB cache and set debug mode to 0: <body onload="brython({indexedDB: false, debug: 0})> I get the same error message as you. So I think that you first ran the script with indexedDB cache enabled (this is the default) and debug mode not set ( In this case, the stdlib modules stored in the indexedDB cache are not reset (caching only tests if it is the same version of |
Yes. |
By using <body onload="brython({debug:1, indexedDB:false})"> I get the expected behavior as well. Both with the input iterator and directly typed in using the |
…g level. Related to issue #853
With the commit referenced above, the example should work now. Can you confirm ? |
I'm still having issues when using <body onload="brython()"> But, >> __BRYTHON__.compiled_date
"2019-12-30 22:24:19.377720" I'm not sure why I can't clear the caches.
Then on reload,
If I try a private window,
|
Maybe it's not my cache. If I go directly to https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython.js and search for |
It seems to be working now. Maybe the CDN caught up or maybe my cache timed out. |
Can we close the issue then ? |
Next test: a try/finally statement <script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython.js">
</script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/gh/brython-dev/brython@master/www/src/brython_stdlib.js">
</script>
<body onload="brython()">
<script type="text/python">
from code import interact
stream = iter("""
try:
print('try')
finally:
print('finally')
print('done')
""".split('\n'))
interact(readfunc=lambda _:next(stream))
</script>
</body> The results:
We expected the The expected result (when run in CPython):
|
I'm also thinking |
With the commit referenced above I think that the issue can be considered closed. If you disagree please reopen it. |
Is this broken again or am I doing it wrong? Test page: <html>
<head>
<script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython.js"
crossorigin="anonymous">
</script>
<script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython_stdlib.js"
crossorigin="anonymous">
</script>
</head>
<body onload="brython({indexedDB: false, debug: 1})">
<script type="text/python">
import code
code.interact()
</script>
<p>Hello, World!</p>
</body>
</html> Test input: Result in Edge console:
|
…operty without setter. Detected in issue #853.
I'm afraid it was broken again, sorry... |
And neither does
import pdb; pdb.set_trace()
, which is what I really wanted, but I get further withinteract()
.I put
in the
<script>
tag. On page load, I get the expected>>>
prompt (in a pop up, but OK, it's a web page). If I give it a simple input, like1
, I get another>>>
prompt, as expected, but the1
doesn't seem to have printed anywhere, not even in the browser developer tools' JS console, which is where you see the output of aprint
call, even though the interact banner printed to the console.However, entering
print(1)
at the pop-up prompt does produce output in the JS console.Also, multi-line inputs completely crash. If I enter
def foo():
I expect the next prompt to be...
so I can do the next line, but this just crashes. I don't get another prompt.If Brython's aim is to be a drop-in replacement for client-side JavaScript, we need some kind of interactive console. Python's just not the same without it. It seems like this should be possible, considering that a bookmarklet like Firebug Lite could inject developer tools in the web page and we already have a demo console. It doesn't seem like much of a stretch to have something like IDLE embedded in a bookmarklet, but
pdb.set_trace()
didn't work right in the demo console either, so I don't know.The text was updated successfully, but these errors were encountered: