jotes / django-cookies-samesite

This repository contains a middleware which automatically sets SameSite attribute for session and csrf cookies in legacy versions of Django.
BSD 3-Clause "New" or "Revised" License
49 stars 35 forks source link

readme = open('README.rst').read() : UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2752 #27

Closed aniketplivo closed 4 years ago

aniketplivo commented 4 years ago

Description

We are using django-cookies-samesite in our requirements.txt which is failing with below error:

Collecting django-cookies-samesite

[2020-05-14T11:40:18.486Z]   Downloading django-cookies-samesite-0.6.1.tar.gz (7.3 kB)

[2020-05-14T11:40:18.745Z]     ERROR: Command errored out with exit status 1:

[2020-05-14T11:40:18.745Z]      command: /opt/xxx/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-Dps_k2/django-cookies-samesite/setup.py'"'"'; __file__='"'"'/tmp/pip-install-Dps_k2/django-cookies-samesite/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-PbwVp8

[2020-05-14T11:40:18.745Z]          cwd: /tmp/pip-install-Dps_k2/django-cookies-samesite/

[2020-05-14T11:40:18.745Z]     Complete output (7 lines):

[2020-05-14T11:40:18.745Z]     Traceback (most recent call last):

[2020-05-14T11:40:18.745Z]       File "<string>", line 1, in <module>

[2020-05-14T11:40:18.745Z]       File "/tmp/pip-install-Dps_k2/django-cookies-samesite/setup.py", line 70, in <module>

[2020-05-14T11:40:18.745Z]         readme = open('README.rst').read()

[2020-05-14T11:40:18.745Z]       File "/opt/xxxx/lib/python2.7/encodings/ascii.py", line 26, in decode

[2020-05-14T11:40:18.745Z]         return codecs.ascii_decode(input, self.errors)[0]

[2020-05-14T11:40:18.745Z]     UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2752: ordinal not in range(128)

I believe this is happening because of this commit : https://github.com/jotes/django-cookies-samesite/commit/37f68aae1a6c7234058aadb1106e7bf17fb68b73

mainly because of from io import open

As its failing in line 70: readme = open('README.rst').read()

jotes commented 4 years ago

Hey @aniketplivo, I'm trying to reproduce this issue locally by calling python setup.py install (and the version of the interpreter is 2.7.15). Unfortunately, the error doesn't occur (?) Could you provide me more information about your system: what's your version of pip?

mcibique commented 4 years ago

I got the same problem (python 2.7.15, pip 20.0.1), but it can only occur when running pip install inside the docker container. I'm not sure why that happened but I played a little with the code and I see the cause of the problem:

readme = open('README.rst').read() tries to read the file using the current encoding, which fails because of these two names in the README.rst:

Contributors
------------
...
* Jarosław Śmiejczak <poke@jotes.work>
* Jørn Lomax <northlomax@gmail.com>

After changing the names to Jaroslaw Smiejczak and Jorn Lomax, everything works and the package installs. But I think the better solution would be always open the README file using unicode encoding:

readme = open('README.rst', encoding='utf8').read()

This works for me as well.

jotes commented 4 years ago

@mcibique Thanks for help and troubleshooting!

jotes commented 4 years ago

@mcibique can you download the latest release (0.6.5) and verify that the problem was solved?

mcibique commented 4 years ago

Hi @jotes, thanks for a quick reply and hotfix. Unfortunately, it didn't fix it because the problem is on this line:

https://github.com/jotes/django-cookies-samesite/blob/master/setup.py#L70

It should be changed from:

readme = open('README.rst').read()

to:

readme = open('README.rst', encoding='utf8').read()
jotes commented 4 years ago

I'm going to write a test for that :/

jotes commented 4 years ago

@mcibique Hey, I added some tests to the Travis configuration, fixed setup.py, and released v0.6.6. I tested it locally (I had a custom zsh plugin that was messing with my LC_ settings and didn't notice it) and it seems to work (look at the output below):

(testco4) ☁  django-cookies-samesite [master] cd ..
(testco4) ☁  code  export LC_CTYPE=c
(testco4) ☁  code  export LC_LANG=c
(testco4) ☁  code  pip install django-cookies-samesite==0.6.5
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details a
bout Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting django-cookies-samesite==0.6.5
  Downloading django-cookies-samesite-0.6.5.tar.gz (7.5 kB)
    ERROR: Command errored out with exit status 1:
     command: /home/jotes/.pyenv/versions/2.7.15/envs/testco4/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-dPv6Cn/django-cookies-samesite/setup.py'"'"'; __file__='"'"'/tmp/p
ip-install-dPv6Cn/django-cookies-samesite/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'
))' egg_info --egg-base /tmp/pip-pip-egg-info-Ny8f8a
         cwd: /tmp/pip-install-dPv6Cn/django-cookies-samesite/
    Complete output (7 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-dPv6Cn/django-cookies-samesite/setup.py", line 70, in <module>
        readme = open('README.rst').read()
      File "/home/jotes/.pyenv/versions/2.7.15/lib/python2.7/encodings/ascii.py", line 26, in decode
        return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 2844: ordinal not in range(128)
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
(testco4) ☁  code  pip install django-cookies-samesite==0.6.6
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details a
bout Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting django-cookies-samesite==0.6.6
  Downloading django-cookies-samesite-0.6.6.tar.gz (7.5 kB)
Collecting ua-parser==0.10.0
  Using cached ua_parser-0.10.0-py2.py3-none-any.whl (35 kB)
Building wheels for collected packages: django-cookies-samesite
  Building wheel for django-cookies-samesite (setup.py) ... done
  Created wheel for django-cookies-samesite: filename=django_cookies_samesite-0.6.6-py2.py3-none-any.whl size=5836 sha256=ddbdd8b972be8582029db62c5376f640adb7cfefa7249f6a0634f981c8756142
  Stored in directory: /home/jotes/.cache/pip/wheels/7c/4f/15/126d38eded4d9356f097be0f3f7eeae5606b61e55d8a5fbd97
Successfully built django-cookies-samesite
Installing collected packages: ua-parser, django-cookies-samesite
Successfully installed django-cookies-samesite-0.6.6 ua-parser-0.10.0
(testco4) ☁  code
mcibique commented 4 years ago

Hi @jotes, it works like a charm. Thank you very much for fixing it, really appreciate it.