Python 3 compat: Only use InstanceType on Python 2
Python 3 compat: fix some import errors
Python 3 compat: Use correct _fileobject
Python 3 compat: Use bytes when reading/writing