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

BaseRequest.url gives UnicodeEncodeError in test run, but not in shell #320

Closed
carn1x opened this issue Jun 8, 2017 · 2 comments
Closed

Comments

@carn1x
Copy link

carn1x commented Jun 8, 2017

Take the following code:

from webob.request import BaseRequest
req = BaseRequest.blank('http://mysite.com/%D0%B4%D1%8D%D0%BB%D1%8C%D1%8D%D0%BD%D0%B9%D1%82')
print req.environ['PATH_INFO']
print req.path

If I run this via a django shell, I get:

>>> req = BaseRequest.blank('http://mysite.com/%D0%B4%D1%8D%D0%BB%D1%8C%D1%8D%D0%BD%D0%B9%D1%82')
>>> print req.environ['PATH_INFO']
/дэльэнйт
>>> print req.path
/%D0%B4%D1%8D%D0%BB%D1%8C%D1%8D%D0%BD%D0%B9%D1%82

Where as running it as part of a Django test case I get the following from the first print:

/дÑ�лÑ�Ñ�нйÑ

Followed by the exception:

File "/code/frontend/tests/test_views.py", line 55, in test_event
    print req.path
File "/virtualenv/frontend/local/lib/python2.7/site-packages/webob/request.py", line 485, in path
    bpath = bytes_(self.path_info, self.url_encoding)
File "/virtualenv/frontend/local/lib/python2.7/site-packages/webob/descriptors.py", line 68, in fget
    return req.encget(key, encattr=encattr)
File "/virtualenv/frontend/local/lib/python2.7/site-packages/webob/request.py", line 175, in encget
    return val.decode(encoding)
File "/virtualenv/frontend/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-16: ordinal not in range(128)

I get that this is probably some weird configuration with my test suite, however both run via the same method in the same environment (Django's manage.py test vs manage.py shell) and so I'm struggling to identify what's different. We're also running a ton of other unicode related tests without issue.

Thanks for any light shed on this.

@carn1x carn1x changed the title BaseRequest.url gives UnicodeDecodeError in test run, but not in shell BaseRequest.url gives UnicodeEncodeError in test run, but not in shell Jun 8, 2017
@digitalresistor
Copy link
Member

Somewhere in your code you have a from __future__ import unicode_literals:

>>> from __future__ import unicode_literals
>>> from webob.request import BaseRequest
>>> req = BaseRequest.blank('http://mysite.com/%D0%B4%D1%8D%D0%BB%D1%8C%D1%8D%D0%BD%D0%B9%D1%82')
>>> print req.environ['PATH_INFO']
/д�л��нй�

@carn1x
Copy link
Author

carn1x commented Jun 9, 2017

Ahh thanks very much. I was actually aiming to fix a larger issue, and mistakenly drilled down to thinking webob was the problem. Eliminating unicode_literals however absolves webob and shifts the issue more towards webtest / django-webtest.

@carn1x carn1x closed this as completed Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants