patolin / pylast

Automatically exported from code.google.com/p/pylast
Apache License 2.0
0 stars 0 forks source link

_get_cache_key is concatening non utf-8 encoded objects #17

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please type example code that produces the issue:
def _get_cache_key(self):
        """The cache key is a string of concatenated sorted names and values."""

        keys = self.params.keys()
        keys.sort()

        cache_key = str()

        for key in keys:
            if key != "api_sig" and key != "api_key" and key != "sk":
                cache_key += urllib.quote_plus(key) +
urllib.quote_plus(urllib.quote_plus(self.params[key]))

        return hashlib.sha1(cache_key).hexdigest()

What is the expected output? What do you see instead?
There are 2 chained calls to urllib.quote_plus which is likely a typo, plus
the key/params are not utf-8 encoded while this is assembled into a str object.

Should be:

cache_key += urllib.quote_plus(key.encode('utf-8')) +
urllib.quote_plus(self.params[key].encode('utf-8'))

What version of pyLast is this?
0.3.1 

Please provide any additional information below.

Having some unicode params makes it blow up with:

  File "C:\dev\code\lastfm\pylast.py", line 181, in _get_cache_key
    cache_key += urllib.quote_plus(key) +
urllib.quote_plus(urllib.quote_plus(self.params[key]))
  File "c:\dev\Python25\lib\urllib.py", line 1211, in quote_plus
    s = quote(s, safe + ' ')
  File "c:\dev\Python25\lib\urllib.py", line 1205, in quote
    res = map(safe_map.__getitem__, s)

KeyError: u'\xfc'

Original issue reported on code.google.com by sbaill...@gmail.com on 27 Mar 2009 at 1:03

GoogleCodeExporter commented 9 years ago
this should be fixed by now as well.

Original comment by amr.hassan on 28 Mar 2009 at 8:26

GoogleCodeExporter commented 9 years ago
I still have this issue in 0.3.4

Also the same thing applies to _Request._download_response(self) where 
quote_plus is 
also called without escaping the parameters first.

Original comment by nichte on 13 May 2009 at 12:57

GoogleCodeExporter commented 9 years ago
Can you elaborate on how to reproduce the error?

Original comment by amr.hassan on 13 May 2009 at 1:02

GoogleCodeExporter commented 9 years ago
Sorry :)

It's basically the same as what sabailiez mentioned:

When I try to download data for anything that has unicode characters I get:

File "D:\projects\mainstream_meter\pylast.py" in _download_response
  224.          data.append('='.join((name, urllib.quote_plus(self.params[name]))))
File "C:\Python25\lib\urllib.py" in quote_plus
  1211.         s = quote(s, safe + ' ')
File "C:\Python25\lib\urllib.py" in quote
  1205.     res = map(safe_map.__getitem__, s)

Exception Type: KeyError at /user/Luthya/12month/
Exception Value: u'\xe9'

It works after changing line 224 to:
            data.append('='.join((name, urllib.quote_plus(self.params[name].encode('utf-8')))))

Original comment by nichte on 13 May 2009 at 1:28