gregmuellegger / django-mobile

Detect mobile browsers and serve different template flavours to them.
http://pypi.python.org/pypi/django-mobile
BSD 3-Clause "New" or "Revised" License
560 stars 170 forks source link

Fix encoding issue when installing with Pip3 #58

Closed Zowie closed 9 years ago

Zowie commented 9 years ago

When installing with pip3, the following error is given:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 48: ordinal not in range(128)

It appears that if you don't explicitly set the encoding with locale.setlocale(locale.LC_ALL, 'en_US.utf-8') --- or whatever locale, just the encoding matters AFAIK --- python will try to decode files using ascii (the locale.getpreferredencoding(False) function that open() actually depends on for the type of encoding it uses returns ANSI_X3.4-1968). This generates an issue where Python will say it cannot decode using ascii and the installation will fail.

I'm manually setting the encoding depending on the Python version now, i just copied line 48 (in old setup.py) to check the python version.

bung87 commented 9 years ago

fixed my problem

gregmuellegger commented 9 years ago

@Zowie I adopted your fix, but wrote it myself to encapsulate it into it's own function. See this commit: https://github.com/gregmuellegger/django-mobile/commit/a87c3599f250d34dab0c1b6114490c3e84ea85c1

Can you confirm that this fixes the issue you are having?

Zowie commented 9 years ago

@gregmuellegger Jup, fixes it as well!

gregmuellegger commented 9 years ago

Released 0.5.1 which contains this fix. So installing should be easier now on Python 3. https://pypi.python.org/pypi/django-mobile/

tysonclugg commented 8 years ago

I know this is a bit late, but io.open() behaves consistently across both Python 2 and 3. It returns unicode strings when the mode set to 'rt' (the default), or byte strings when mode is 'rb'. Just use io.open('path/to/file', encoding='utf-8').read() and you'll get Unicode strings with both Python 2 and 3.

gregmuellegger commented 8 years ago

@tysonclugg nevertheless its good input! Thanks, I will use io.open in the future for these cases.