prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Non-ascii prints in error handler without tty cause stacktrace
  ```
  23:00:13 style runtests: commands[0] | pre-commit run --all-files
  23:00:13 [INFO] Installing environment for git@git.yelpcorp.com:mirrors/pre-commit/mirrors-jshint.
  23:00:13 [INFO] Once installed this environment will be reused.
  23:00:13 [INFO] This may take a few minutes...
  23:01:33 Traceback (most recent call last):
  23:01:33   File ".tox/style/bin/pre-commit", line 11, in <module>
  23:01:33     sys.exit(main())
  23:01:33   File ".../.tox/style/local/lib/python2.7/site-packages/pre_commit/main.py", line 157, in main
  23:01:33     'Command {0} failed to exit with a returncode'.format(args.command)
  23:01:33   File "/usr/lib64/python2.7/contextlib.py", line 35, in __exit__
  23:01:33     self.gen.throw(type, value, traceback)
  23:01:33   File ".../.tox/style/local/lib/python2.7/site-packages/pre_commit/error_handler.py", line 41, in error_handler
  23:01:33     traceback.format_exc(),
  23:01:33   File ".../.tox/style/local/lib/python2.7/site-packages/pre_commit/error_handler.py", line 21, in _log_and_exit
  23:01:33     print_fn(error_msg)
  23:01:33 UnicodeEncodeError: 'ascii' codec can't encode characters in position 735-737: ordinal not in range(128)
  ```
  
  
  </issue>
  <code>
  [start of pre_commit/error_handler.py]
  1 from __future__ import absolute_import
  2 from __future__ import print_function
  3 from __future__ import unicode_literals
  4 
  5 import contextlib
  6 import io
  7 import os.path
  8 import traceback
  9 
  10 from pre_commit.errors import FatalError
  11 from pre_commit.store import Store
  12 
  13 
  14 # For testing purposes
  15 class PreCommitSystemExit(SystemExit):
  16     pass
  17 
  18 
  19 def _log_and_exit(msg, exc, formatted, print_fn=print):
  20     error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc)
  21     print_fn(error_msg)
  22     print_fn('Check the log at ~/.pre-commit/pre-commit.log')
  23     store = Store()
  24     store.require_created()
  25     with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log:
  26         log.write(error_msg + '\n')
  27         log.write(formatted + '\n')
  28     raise PreCommitSystemExit(1)
  29 
  30 
  31 @contextlib.contextmanager
  32 def error_handler():
  33     try:
  34         yield
  35     except FatalError as e:
  36         _log_and_exit('An error has occurred', e, traceback.format_exc())
  37     except Exception as e:
  38         _log_and_exit(
  39             'An unexpected error has occurred',
  40             e,
  41             traceback.format_exc(),
  42         )
  43
  [end of pre_commit/error_handler.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py
  --- a/pre_commit/error_handler.py
  +++ b/pre_commit/error_handler.py
  @@ -7,7 +7,9 @@
   import os.path
   import traceback
   
  +from pre_commit import five
   from pre_commit.errors import FatalError
  +from pre_commit.output import sys_stdout_write_wrapper
   from pre_commit.store import Store
   
   
  @@ -16,15 +18,15 @@
       pass
   
   
  -def _log_and_exit(msg, exc, formatted, print_fn=print):
  -    error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc)
  -    print_fn(error_msg)
  -    print_fn('Check the log at ~/.pre-commit/pre-commit.log')
  +def _log_and_exit(msg, exc, formatted, write_fn=sys_stdout_write_wrapper):
  +    error_msg = '{0}: {1}: {2}\n'.format(msg, type(exc).__name__, exc)
  +    write_fn(error_msg)
  +    write_fn('Check the log at ~/.pre-commit/pre-commit.log\n')
       store = Store()
       store.require_created()
  -    with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log:
  -        log.write(error_msg + '\n')
  -        log.write(formatted + '\n')
  +    with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
  +        log.write(five.to_bytes(error_msg))
  +        log.write(five.to_bytes(formatted) + b'\n')
       raise PreCommitSystemExit(1)
  
verification_info: {"golden_diff": "diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py\n--- a/pre_commit/error_handler.py\n+++ b/pre_commit/error_handler.py\n@@ -7,7 +7,9 @@\n import os.path\n import traceback\n \n+from pre_commit import five\n from pre_commit.errors import FatalError\n+from pre_commit.output import sys_stdout_write_wrapper\n from pre_commit.store import Store\n \n \n@@ -16,15 +18,15 @@\n     pass\n \n \n-def _log_and_exit(msg, exc, formatted, print_fn=print):\n-    error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc)\n-    print_fn(error_msg)\n-    print_fn('Check the log at ~/.pre-commit/pre-commit.log')\n+def _log_and_exit(msg, exc, formatted, write_fn=sys_stdout_write_wrapper):\n+    error_msg = '{0}: {1}: {2}\\n'.format(msg, type(exc).__name__, exc)\n+    write_fn(error_msg)\n+    write_fn('Check the log at ~/.pre-commit/pre-commit.log\\n')\n     store = Store()\n     store.require_created()\n-    with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log:\n-        log.write(error_msg + '\\n')\n-        log.write(formatted + '\\n')\n+    with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:\n+        log.write(five.to_bytes(error_msg))\n+        log.write(five.to_bytes(formatted) + b'\\n')\n     raise PreCommitSystemExit(1)\n", "issue": "Non-ascii prints in error handler without tty cause stacktrace\n```\n23:00:13 style runtests: commands[0] | pre-commit run --all-files\n23:00:13 [INFO] Installing environment for git@git.yelpcorp.com:mirrors/pre-commit/mirrors-jshint.\n23:00:13 [INFO] Once installed this environment will be reused.\n23:00:13 [INFO] This may take a few minutes...\n23:01:33 Traceback (most recent call last):\n23:01:33   File \".tox/style/bin/pre-commit\", line 11, in <module>\n23:01:33     sys.exit(main())\n23:01:33   File \".../.tox/style/local/lib/python2.7/site-packages/pre_commit/main.py\", line 157, in main\n23:01:33     'Command {0} failed to exit with a returncode'.format(args.command)\n23:01:33   File \"/usr/lib64/python2.7/contextlib.py\", line 35, in __exit__\n23:01:33     self.gen.throw(type, value, traceback)\n23:01:33   File \".../.tox/style/local/lib/python2.7/site-packages/pre_commit/error_handler.py\", line 41, in error_handler\n23:01:33     traceback.format_exc(),\n23:01:33   File \".../.tox/style/local/lib/python2.7/site-packages/pre_commit/error_handler.py\", line 21, in _log_and_exit\n23:01:33     print_fn(error_msg)\n23:01:33 UnicodeEncodeError: 'ascii' codec can't encode characters in position 735-737: ordinal not in range(128)\n```\n\n", "before_files": [{"content": "from __future__ import absolute_import\nfrom __future__ import print_function\nfrom __future__ import unicode_literals\n\nimport contextlib\nimport io\nimport os.path\nimport traceback\n\nfrom pre_commit.errors import FatalError\nfrom pre_commit.store import Store\n\n\n# For testing purposes\nclass PreCommitSystemExit(SystemExit):\n    pass\n\n\ndef _log_and_exit(msg, exc, formatted, print_fn=print):\n    error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc)\n    print_fn(error_msg)\n    print_fn('Check the log at ~/.pre-commit/pre-commit.log')\n    store = Store()\n    store.require_created()\n    with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log:\n        log.write(error_msg + '\\n')\n        log.write(formatted + '\\n')\n    raise PreCommitSystemExit(1)\n\n\n@contextlib.contextmanager\ndef error_handler():\n    try:\n        yield\n    except FatalError as e:\n        _log_and_exit('An error has occurred', e, traceback.format_exc())\n    except Exception as e:\n        _log_and_exit(\n            'An unexpected error has occurred',\n            e,\n            traceback.format_exc(),\n        )\n", "path": "pre_commit/error_handler.py"}]}