Encoding fixes in text._text (pattern code)

- Also, use isinstance(s, basestring) instead of type(s) in string_types