unicode and hashlib

Bryan Olson fakeaddress at nowhere.org
Tue Dec 2 19:34:40 EST 2008


Scott David Daniels wrote:
> Bryan Olson wrote:
>> ... I think that's good behavior, except that the error message is likely
>> to end beginners to look up the obscure buffer interface before they 
>> find they just need mystring.decode('utf8') or bytes(mystring, 'utf8').

> Oops, careful here (I made this mistake once in this thread as well). 
> You _decode_ from unicode to bytes.  The code you quoted doesn't run.

Doh! I even tested it with .encode(), then wrote it wrong.

Just in case anyone Googles the error message and lands here: If you are 
working with a Python str (string) object and get,

   TypeError: object supporting the buffer API required

Then you probably want to encode the string to a bytes object, and 
UTF-8 is likely the encoding of choice, as in:

     mystring.encode('utf8')

or

     bytes(mystring, 'utf8')


Thanks for the correction.
-- 
--Bryan



More information about the Python-list mailing list