prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.23k stars 715 forks source link

pt 2.0, python 3.6 on Windows: object of type 'generator' has no len() #563

Open jskm opened 6 years ago

jskm commented 6 years ago

When trying to run the example programs under examples/full_screen, I get an endless stream of exceptions as follows:

Unhandled exception in event loop:
  File "C:\Code\prompt_testing\lib\site-packages\prompt_toolkit\eventloop\win32.py", line 98, in _run_task
    t()
  File "C:\Code\prompt_testing\lib\site-packages\prompt_toolkit\eventloop\context.py", line 115, in new_func
    return func(*a, **kw)
  File "C:\Code\prompt_testing\lib\site-packages\prompt_toolkit\application\application.py", line 492, in read_from_input
    self.key_processor.feed_multiple(keys)
  File "C:\Code\prompt_testing\lib\site-packages\prompt_toolkit\key_binding\key_processor.py", line 241, in feed_multiple
    self._keys_pressed += len(key_presses)

Exception object of type 'generator' has no len()

It looks like this error only happens on Windows. application.py#L489 calls read_keys() on a Win32Input, which returns a generator.

Is Application.run(set_exception_handler=True) working as intended? According to the docs:

set_exception_handler – When set, in case of an exception, go out of the alternate screen and hide the application, display the exception, and wait for the user to press ENTER.

In this case, there was an infinite loop that did not wait and couldn't be broken without closing the console.

jonathanslenders commented 6 years ago

Hi @jskm,

Thanks a lot for pointing this out! This should fix it: https://github.com/jonathanslenders/python-prompt-toolkit/commit/cf7515e27ce507ea4aa62d2da0397cb9e8595b85 Could you try again?

jonathanslenders commented 6 years ago

Is Application.run(set_exception_handler=True) working as intended? According to the docs:

Yes, this should work and is actually very helpful. Not so much in case of bugs like this, but if there is a bug in an key handler for instance, then this will display the exception, wait for the user to press ENTER, and go on. This is super useful for full screen applications. But of course, if there is a bug in the key input like here, it doesn't help.

jskm commented 6 years ago

Yes, it seems fine now. The examples are working well under Cmder. The Windows 7 console host (cmd/Powershell) seems unusable though, as nearly all of the examples I've tried display incorrectly, with black text on a black background and layout issues. I don't know whether you're intending to support that. I haven't been able to try it on Windows 10 yet.

jonathanslenders commented 6 years ago

Hi @jskm,

Thanks for pointing this out. For Windows 10 (with vt100 support) it used to work fine, but for older versions, there was indeed a bug. This should fix it: https://github.com/jonathanslenders/python-prompt-toolkit/commit/270142de5d9dccfe4902c78a84c975c2e5718b74 Can you try again?

jskm commented 6 years ago

It looks okay. I've tried various examples and apart from color palette and formatting limitations, the prompts are readable.

The only remaining issue I see is related to the code page and font used in the console. The Frame widget in particular breaks under code page 932. I took some screenshots using examples/full-screen/buttons.py.

Running the program with code page 932 creates this mess: code page 932 raster font It looks like the frame is trying to draw double its actual width, and this only happens in the area covered by the Frame widget. Is this related to fullwidth/halfwidth sizes for CJK characters?

The frame is sized appropriately under code page 437, but another problem appears when choosing Consolas or Lucida Console (the only TrueType options available) for the console font: code page 437 consolas This image shows Consolas and was taken after hitting Enter a few times to resize the box. A similar thing happens in other examples, such as with menu dropdowns and block selections in text-editor.py.

I have to choose "Raster Fonts" with code page 437 to avoid glitches. code page 437 raster

I don't know what ConEmu is doing but it has no problems even with Consolas and chcp showing code page 932.

jonathanslenders commented 6 years ago

Hi @jskm, thanks for sharing! I'm not sure what's going on, but I'll see if I can reproduce and fix this on Windows 7.