In most cases <code>msg_str</code> is of type <code>str</code> but sometimes it is of type <code>unicode</code>. The latter causes problems so it has to be encoded. But when type of <code>msg_str</code> is <code>str</code> encoding causes an error.