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

body=urllib.urlencode(data)) error in Python 2.6 #1

Open
jdiederichs opened this issue Aug 5, 2009 · 1 comment
Open

body=urllib.urlencode(data)) error in Python 2.6 #1

jdiederichs opened this issue Aug 5, 2009 · 1 comment

Comments

@jdiederichs
Copy link

I got this error for a message that contained an apostrophy in the body (description):
File "/usr/lib64/python2.6/urllib.py", line 1261, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 42: ordinal not in range(128)

A workaround is to first convert the description to Unicode:
data = {
'apikey': self.apikey,
'application': application,
'event': event,
'description': description.encode('utf-8'),
'priority': priority
}

@jacobb
Copy link
Owner

jacobb commented Aug 5, 2009

This fix isn't recommended as it will break if you try and pass it an ascii string with out-of-position characters (done with above fix in place):

p.post(1,2,u'∆˙∂∆˚ƒ˙∆˚ß∂˙∆˚ƒ˙ß˚∂∆')
True
p.post(1,2,'∆˙∂∆˚ƒ˙∆˚ß∂˙∆˚ƒ˙ß˚∂∆')
Traceback (most recent call last):
File "", line 1, in
File "prowlpy.py", line 39, in post
'description': description.encode('utf-8'),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

It will also break if you pass it a non-string type (urlencode does the type casting right now)

A work around for now would be to do the encoding before you pass the variable into function (quick scripts are likely to use non-unicode strings, whereas trying to port this into something like Django would probably use the unicode type).

That said, better UTF8 support that just works should become a priority, and I'll add it to the list and see what similar python libraries behave.

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