calculon
calculon copied to clipboard
Variables in bpython
I broke like a kitkat and decided to play with calculon again. I was in the process of familiarising myself with how it worked when I spotted the following.
These blocks were all in the same process, I didn't restart bpython in between. I guess my question is: is this expected behaviour?
First I run bpython
, assign a value to a variable, print the variable:
bpython version 0.15 on top of Python 2.7.11+ /usr/bin/python
>>> test = 10
>>> test
10
Then I import calculon:
>>> import calculon.load
Connected to calculon
>>> test
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'test' is not defined
It looks like importing calculon wipes out the globals? Having looked at the code, I think this is because it instantiates a new repl here: https://github.com/snare/calculon/blob/master/calculon/load.py#L12 Does this sound about right?
So I assign 10 to the variable again:
>>> test = 10
The value shows up as expected in the calculon display.
Then, for some reason, I try printing it again in the bpython REPL:
>>> test
0xa
Traceback (most recent call last):
File "/usr/bin/bpython", line 9, in <module>
load_entry_point('bpython==0.15', 'console_scripts', 'bpython')()
File "/usr/lib/python2.7/dist-packages/bpython/curtsies.py", line 190, in main
exit_value = repl.mainloop()
File "/usr/lib/python2.7/dist-packages/bpython/curtsies.py", line 125, in mainloop
self.process_event_and_paint(e)
File "/usr/lib/python2.7/dist-packages/bpython/curtsies.py", line 99, in process_event_and_paint
array, cursor_pos = self.paint()
File "/usr/lib/python2.7/dist-packages/bpython/curtsiesfrontend/repl.py", line 1356, in paint
self.lines_for_display)
File "/usr/lib/python2.7/dist-packages/bpython/curtsiesfrontend/replpainter.py", line 39, in paint_history
lines.append(fmtstr(line[:columns]))
File "/usr/lib/python2.7/dist-packages/curtsies/formatstring.py", line 617, in fmtstr
string = FmtStr.from_str(string)
File "/usr/lib/python2.7/dist-packages/curtsies/formatstring.py", line 156, in from_str
tokens_and_strings = parse(s)
File "/usr/lib/python2.7/dist-packages/curtsies/escseqparse.py", line 27, in parse
front, token, rest = peel_off_esc_code(rest)
File "/usr/lib/python2.7/dist-packages/curtsies/escseqparse.py", line 79, in peel_off_esc_code
d['numbers'] = [int(x) for x in d['numbers'].split(';')]
ValueError: invalid literal for int() with base 10: ''
First it prints the value in hex rather than decimal, then it raises a value error from somewhere within bpython. None of the traceback appears to apply to calculon, but it's certainly in the REPL parts of bpython.
Feel free to close as EDominicDoesn'tKnowWhatHe'sDoing
Hmm. It's not actually using the new REPL, it's just using it a poke a new bound method into the existing REPL so I'm not sure why it breaks the existing state like that. I'll have to investigate further. TBH the way that the bpython patching works currently is really awful and I'd like to just abandon it entirely. I think a cleaner way would be to use bpython's embed
function after overriding things, and force the user to spawn bpython from calculon. IPython looks like it provides more options for writing extensions (which can provide 'magic' commands like what we do with watch
/unwatch
/etc, and pre-prompt hooks, etc, so I think supporting that will be a lot cleaner.
Oh and the printing in hex rather than decimal - it now grabs the output of each exec
and if a number fell it out formats it per config. I thought I had disabled this for bpython so it didn't break things, but I might not have quite got that right.
I've already done a lot of work re-writing calculon so there's probably not a lot of benefit in looking at the current codebase. Once I have it working again with the embedded REPL and the new display code I'll push it out again and we can look at supporting other REPLs.
Tho if you like I can push what I currently have into another branch if you wanna look at bpython?
No need to push to a branch until you're happy with it.
I spent some time writing code to detect which repl is running and load the appropriate api, but it doesn't work consistently. On 9 May 2016 1:38 a.m., "snare" [email protected] wrote:
Tho if you like I can push what I currently have into another branch if you wanna look at bpython?
— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/snare/calculon/issues/38#issuecomment-217755958