Fix #949.

Fix #949.
Use binary mode to write file if unix_newlines is set.
I don't know if there will be any side effect if using binary mode to write file. At least I tested it on my machine and no side effect up to now.
I closed the #1346 as it seems I need to make changes on master.