#1827·tqdm

`Python -m tqdm --log` 如果缺少或无效的级别,会引发 `IndexError` 或 `AttributeError`

作者: Q7xM4创建于 2026年9月10日更新于 2026年9月10日
  • I have marked all applicable categories:
    • exception-raising bug
    • visual output bug
  • I have visited the [source website], and in particular read the [known issues]
  • I have searched through the [issue tracker] for duplicates
  • I have mentioned version numbers, operating system and environment, where applicable:
    python
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)

When I was using tqdm, I found that when there was a problem with the parameters, it would not report a unified error, but rather a more profound error. This might be because --log is parsed before the try block, and this block converts illegal command-line options into TqdmKeyError / TqdmTypeError, resulting in the following exceptions being unable to be caught:

  • --log is the last parameter: argv[log_idx + 1] exceeds the bounds, throwing IndexError.
  • --log NOTALEVEL: getattr(logging, logLevel) fails, throwing AttributeError.

Reproduction 1.

$ Python -m tqdm --log </dev/nul

Output:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/asfdf/Desktop/issues/tqdm-all/tqdm/tqdm/__main__.py", line 3, in <module>
    main()
  File "/home/asfdf/Desktop/issues/tqdm-all/tqdm/tqdm/cli.py", line 177, in main
    logLevel = argv[log_idx + 1]
               ~~~~^^^^^^^^^^^^^
IndexError: list index out of range
$ Python -m tqdm --log NOTALEVEL </dev/null

Output:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/asfdf/Desktop/issues/tqdm-all/tqdm/tqdm/__main__.py", line 3, in <module>
    main()
  File "/home/asfdf/Desktop/issues/tqdm-all/tqdm/tqdm/cli.py", line 178, in main
    logging.basicConfig(level=getattr(logging, logLevel),
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'logging' has no attribute 'NOTALEVEL'

It is recommended to move the parsing of --log into the try block and add checks for missing parameters and log level validation.