Add failing test for issue 2412

Writing history to a file including output fails
Fix writing history with output to a file in Python 2

Closes gh-2412
Merge pull request #5841 from takluyver/i2412

Fix writing history with output to a file in Python 2