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?
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: