Improve support for python 3

Still problems with unicode support and StringIO.

How should we tackle unicode support?  StringIO is fairly straightforward but makes doctests awkward because we have to augment them with conditional import, for example:
```python
try: # python 2
    from StringIO import StringIO
except ImportError: # python 3
    from io import StringIO
```