XuanEmily / pydelicious

Automatically exported from code.google.com/p/pydelicious
Other
0 stars 0 forks source link

Should not re-encode unicode data #34

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create a unicode string (e.g. for description)
2. Convert the unicode object to str with django's smart_str utility
3. Call posts_add

What is the expected output? What do you see instead?

Expected: successful posting to delicious.

Instead: UnicodeDecodeError as pydelicious tries to encode the, already UTF-8 
string into UTF-8

What version of the product are you using? On what operating system?

0.5.0 on Apple Snow Leopard

Please provide any additional information below.

Recreating with just the django shell command line:

>>> title = u'Carniv\xe0le'
>>> print title.encode('utf-8')
Carnivàle
>>> print smart_str(title)
Carnivàle
>>> print smart_str(title).encode('utf-8') # how pydelicious currently tries to 
handle the data
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal 
not in range(128)

Fix:

Attached, but I just added a new class variable: "encoded" which defaults to 
False. If calling users sets to "True" then the encoding step is skipped. It'd 
probably be more ideal to somehow check the encoding state of the data.

diffed:
def __init__(self, user, passwd, codec='iso-8859-1', 
api_request=dlcs_api_request, xml_parser=dlcs_parse_xml, encoded=False):
…
self.encoded = encoded
…
if not self.encoded:
    for key in params:
        params[key] = params[key].encode(self.codec)

Original issue reported on code.google.com by sdb...@gmail.com on 27 Jun 2010 at 1:21

Attachments:

GoogleCodeExporter commented 8 years ago
I have added the parameter for folks who wish to pass in encoded strings. 
With the disclaimer that I cannot remember how the delicious API service treats 
incoming strings. 
BTW, I don't think auto detection belongs here, API callers need to be aware 
what data goes in. If unsure, see the chardetect project.

Original comment by berend.v...@gmail.com on 21 Nov 2010 at 1:49