Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress bar doesn't work with codecs.open when using encoding #636

Closed
mina-asham opened this issue Aug 19, 2016 · 1 comment
Closed

Progress bar doesn't work with codecs.open when using encoding #636

mina-asham opened this issue Aug 19, 2016 · 1 comment

Comments

@mina-asham
Copy link

Progress bar crashes while trying to get a length hint of a file that is opened using codecs.open function.
Python version: 2.7.11 (64bit) on Windows
Output:

raceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python27\lib\site-packages\click\termui.py", line 306, in progressbar
    width=width, color=color)
  File "C:\Program Files\Python27\lib\site-packages\click\_termui_impl.py", line 73, in __init__
    length = _length_hint(iterable)
  File "C:\Program Files\Python27\lib\site-packages\click\_termui_impl.py", line 33, in _length_hint
    return len(obj)
  File "C:\Program Files\Python27\lib\codecs.py", line 728, in __getattr__
    return getattr(self.stream, name)
AttributeError: 'file' object has no attribute '__len__'

To reproduce:

import codecs
from click import progressbar

file_path = 'my_file.txt'

with progressbar(codecs.open(file_path, encoding='utf-8')) as progress:
    pass

The same code works as expected when not using encoding parameter, mainly because codecs.open return the basic open() method result if no encoding is specified.

The _length_hint function catches the TypeError exception but this one produces an AttributeError exception, should be modified to include the AttributeError type as well.

@untitaker
Copy link
Contributor

Fixed with #637

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants