Qix-/better-exceptions

Navigation Keys are not working in better-exception Repl(MacOs)

abdulniyaspm opened this issue · 3 comments

I am using Macos and I am unable to use the navigation keys to jump into my previous Repl line, Instead it showing me escape characters.

Abduls-Air:better-exceptions abdulniyas$ python3 -m better_exceptions
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(BetterExceptionsConsole)
>>> import sys
>>> ^[[A # This is what I am getting when I press Up arrow.

I tried the same in my python interpreter but it does shows me the correct result.

Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> import platform # It shows the correct result when I press Up arrow.

I think sys.__interactivehook__ is not getting called(in order register the readline) while initialising better-exception Repl. So if I do the following in better-exception Repl I am getting correct result.

Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(BetterExceptionsConsole)
>>> a = 0     
>>> ^[[A # while clicking up arrow.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/code.py", line 64, in runsource
    code = self.compile(source, filename, symbol)
           │            │       │         └ 'single'
           │            │       └ '@@@REPL@@@1'
           │            └ '\x1b[A'
           └ <better_exceptions.repl.BetterExceptionsConsole object at 0x10249a048>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
           │              │              │       │         └ 'single'
           │              │              │       └ '@@@REPL@@@1'
           │              │              └ '\x1b[A'
           │              └ <codeop.CommandCompiler object at 0x10249a080>
           └ <function _maybe_compile at 0x1024961e0>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codeop.py", line 99, in _maybe_compile
    raise err1
          └ SyntaxError('invalid syntax', ('@@@REPL@@@1', 1, 1, '\x1b[A\n'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + '
'", filename, symbol)
    │       │        │              │         └ 'single'
    │       │        │              └ '@@@REPL@@@1'
    │       │        └ '\x1b[A'
    │       └ <codeop.Compile object at 0x10249a0b8>
    └ None
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
                     │       │         │       └ <codeop.Compile object at 0x10249a0b8>
                     │       │         └ 'single'
                     │       └ '@@@REPL@@@1'
                     └ '\x1b[A\n'
File "@@@REPL@@@1", line 1
    ^
SyntaxError: invalid syntax
>>> import sys
>>> sys.__interactivehook__() # Register the readline
>>> # Now onwards the navigation keys are working
Qix- commented

PR is welcome :)

I also experienced the same on MacOS and Python 3.7.4
I'll look into it some more.

I've added a potential fix for this in https://github.com/Qix-/better-exceptions/pull/91
Please check it out when you get a chance
cc @abdulniyaspm @Qix-