support subclasses of IO and StringIO

Had a use-case where I needed to use a `StringIO` rather than a `File` object. This patch supports `StringIO` if defined, and all subclasses of `IO`.