Safely print data to stdout on both Python 2 and 3

On Python 2, binary data can be printed directly to stdout.

On Python 3, binary data should be written to `sys.stdout.buffer` instead
of `sys.stdout` directly.

This commit adds a ``safe_print`` function to handle both unicode and
bytestrings correctly on both Python versions.