Fix unicode decode/encode error for tests

Before this fix, in python 2, the to_native() call returns bytes that are implicitely decoded from ascii in write() before being encoded again in `charset`. This implicit call to decode('ascii') fails if the string is encoded in 'utf-8' with non-ascii characters for example.

This fix calls encode(`charset`) only once. Moreover, for non-string-like objects, it casts the object into unicode in python 2 too.