datastax/python-driver — pull request #78 by nisanharamati (2014-02-07 22:37:11+00:00)
Title: Fix UTF8Type serialization for already-encoded utf8

If your data is already utf-8 encoded by the time it reaches the UTF8Type.serialize() method, the encode() will fail, raising a UnicodeDecodeError.
Explicitly testing the type for Unicode before the .encode() should guarantee it doesn't fail, and in cases where it isn't Unicode, we simply test whether it's already a valid byte-encoded unicode string by attempting to decode it.
This shouldn't add any significant overhead for strings passed in as Unicode, will prevent unnecessary failures with already-encoded Unicode strings, and raise a UnicodeDecodeError otherwise.