tqdm crashes on Windows console upon accidental mouse/keyboard input

允我心安 提交于 2019-12-24 07:19:17

问题


Running any application on Windows that make use of tqdm progress bars has become a tremendous headache.

It's not clear to me if this is a Windows fault or not, but the bug is easily reproducible. Run the following code on cmd.exe or Powershell:

from tqdm import *
import time

counter = 1000
for i in tqdm(range(counter)):
     time.sleep(.01)

You can do the following while the progress bar is increasing to trigger the crash:

  • Use the left-mouse button to select a few characters on the window (even blank spaces will do, as the screenshot below demonstrates) and then either right-click or press any key to crash the application:

The error message says:

Traceback (most recent call last):
  File "tqdmTest.py", line 5, in <module>
    for i in tqdm(range(counter)):
  File "C:\Users\brcod\AppData\Roaming\Python\Python34\site-packages\tqdm\_tqdm.py", line 979, in __iter__
    sp(self.__repr__())
  File "C:\Users\brcod\AppData\Roaming\Python\Python34\site-packages\tqdm\_tqdm.py", line 241, in print_status
    fp_write('\r' + s + (' ' * max(last_len[0] - len_s, 0)))
  File "C:\Users\brcod\AppData\Roaming\Python\Python34\site-packages\tqdm\_tqdm.py", line 234, in fp_write
    fp.write(_unicode(s))
OSError: raw write() returned invalid length 306 (should have been between 0 and 153)

I'm using Python 3.4.4 on Windows 10 with tqdm 4.19.5.

This is very annoying because the mouse accidentally selects a few characters in the window when I'm just trying to click on it to focus the window.

Can anyone clarify why this happens? Is there a proper workaround for this problem?


回答1:


Solution 1: the most simple solution for this problem is to disable QuickEdit Mode in the properties of the cmd.exe window to prevent mouse clicks from accidentally selecting and pasting text:

Solution 2: this can also be handled programmatically. Just rewrite the loop to catch the exception and pass it when it happens:

from tqdm import *
import time

maxCount = 1000
pbar = tqdm(total = maxCount)

for i in range(maxCount+1):
     try:
          pbar.update(i - pbar.n)
     except OSError as e:
          pass

     time.sleep(.01)

pbar.close()


来源:https://stackoverflow.com/questions/48371993/tqdm-crashes-on-windows-console-upon-accidental-mouse-keyboard-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!