I think there's a possible (if unlikely) failure case here, where `not isinstance(msg, basestring)`, but `str(msg)` returns non-ascii characters. You'll get a unicode error on the line below when you try to interpolate it into the string.