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

Search can't handle Unicode terms #46

Open
mittonk opened this issue Apr 26, 2016 · 4 comments
Open

Search can't handle Unicode terms #46

mittonk opened this issue Apr 26, 2016 · 4 comments

Comments

@mittonk
Copy link
Contributor

mittonk commented Apr 26, 2016

I'd expect Search to take Unicode objects in Python 2.7:

# -*- coding: utf-8 -*-                                                          
from yelp.client import Client                                                   
from yelp.oauth1_authenticator import Oauth1Authenticator                        

auth = Oauth1Authenticator(...)                                                                                                                                                              
client = Client(auth)                                                           
response = client.search('San Francisco', term=u'aü')                           

It fails:

Traceback (most recent call last):
  File "foo.py", line 19, in <module>
    response = client.search('San Francisco', term=u'aü')
  File "/nail/home/kmitton/pg/other/yelp-python/yelp/endpoint/search.py", line 44, in search
    self.client._make_request(SEARCH_PATH, url_params)
  File "/nail/home/kmitton/pg/other/yelp-python/yelp/client.py", line 47, in _make_request
    signed_url = self.authenticator.sign_request(url, url_params)
  File "/nail/home/kmitton/pg/other/yelp-python/yelp/oauth1_authenticator.py", line 36, in sign_request
    return oauth_request.to_url()
  File "/nail/home/kmitton/pg/kmitton-bash/virtualenv_run/lib/python2.7/site-packages/oauth2/__init__.py", line 363, in to_url
    urllib.urlencode(query, True), fragment)
  File "/usr/lib64/python2.7/urllib.py", line 1354, in urlencode
    l.append(k + '=' + quote_plus(str(elt)))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)

Workaround: Manually encode as UTF-8 bytestring:

response = client.search('San Francisco', term=u'aü'.encode('utf8'))            

Feels like we should be able to handle Unicode.

@mittonk
Copy link
Contributor Author

mittonk commented Apr 26, 2016

(Discovered while investigating Cyrillic & Romanian characters in Yelp/yelp-api#124, but repros on simpler strings like the one above.)

@pathcl
Copy link

pathcl commented Apr 27, 2016

Shouldn't be encouraged Python3 usage? :)

@mittonk
Copy link
Contributor Author

mittonk commented Apr 28, 2016

Encourage, yes!
Force, no.

@pathcl
Copy link

pathcl commented Apr 28, 2016

Well ideally it should work on both worlds I'll try to reproduce it

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